Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add autorust.toml with tags_allow to limit tags #747

Merged
merged 6 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions services/autorust/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ once_cell = "1.7"
syn = { version = "1.0", features = ["parsing"] }
camino = "1.0"
askama = "0.11"
toml = "0.5"
12 changes: 7 additions & 5 deletions services/autorust/codegen/examples/gen_mgmt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// cargo run --example gen_mgmt --release
// https://github.com/Azure/azure-rest-api-specs/blob/master/specification/compute/resource-manager
use autorust_codegen::{
self, cargo_toml, get_mgmt_readmes, io, lib_rs,
self, autorust_toml, cargo_toml, get_mgmt_readmes, io, lib_rs,
readme_md::{self, ReadmeMd},
CrateConfig, Error, Result, RunConfig, SpecReadme,
};
Expand Down Expand Up @@ -332,14 +332,16 @@ fn main() -> Result<()> {

fn gen_crate(spec: &SpecReadme, run_config: &RunConfig) -> Result<()> {
let spec_config = spec.config()?;
let tags = &spec_config.tags_filtered(spec.spec(), run_config.skip_service_tags());
let service_name = &spec.service_name();
let crate_name = &format!("{}{}", &run_config.crate_name_prefix, service_name);
let output_folder = &io::join(OUTPUT_FOLDER, service_name)?;
let package_config = autorust_toml::read(&io::join(&output_folder, "autorust.toml")?)?;
let tags = spec_config.tags_filtered(spec.spec(), run_config.skip_service_tags());
let tags = &package_config.tags(tags);
if tags.is_empty() {
println!("not generating {} - no tags", spec.spec());
return Ok(());
}
let service_name = &spec.service_name();
let crate_name = &format!("{}{}", &run_config.crate_name_prefix, service_name);
let output_folder = &io::join(OUTPUT_FOLDER, service_name)?;

let src_folder = io::join(output_folder, "src")?;
if src_folder.exists() {
Expand Down
12 changes: 7 additions & 5 deletions services/autorust/codegen/examples/gen_svc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// cargo run --example gen_svc --release
// https://github.com/Azure/azure-rest-api-specs/blob/master/specification/batch/data-plane
use autorust_codegen::{
self, cargo_toml, get_svc_readmes, io, lib_rs,
self, autorust_toml, cargo_toml, get_svc_readmes, io, lib_rs,
readme_md::{self, ReadmeMd},
CrateConfig, Error, Result, RunConfig, SpecReadme,
};
Expand Down Expand Up @@ -199,14 +199,16 @@ fn main() -> Result<()> {

fn gen_crate(spec: &SpecReadme, run_config: &RunConfig) -> Result<()> {
let spec_config = spec.config()?;
let tags = &spec_config.tags_filtered(spec.spec(), run_config.skip_service_tags());
let service_name = &spec.service_name();
let crate_name = &format!("{}{}", &run_config.crate_name_prefix, service_name);
let output_folder = &io::join(OUTPUT_FOLDER, service_name)?;
let package_config = autorust_toml::read(&io::join(&output_folder, "autorust.toml")?)?;
let tags = spec_config.tags_filtered(spec.spec(), run_config.skip_service_tags());
let tags = &package_config.tags(tags);
if tags.is_empty() {
println!("not generating {} - no tags", spec.spec());
return Ok(());
}
let service_name = &spec.service_name();
let crate_name = &format!("azure_svc_{}", service_name);
let output_folder = &io::join(OUTPUT_FOLDER, service_name)?;

let src_folder = io::join(output_folder, "src")?;
if src_folder.exists() {
Expand Down
57 changes: 57 additions & 0 deletions services/autorust/codegen/src/autorust_toml.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::config_parser::Tag;
use camino::{Utf8Path, Utf8PathBuf};
use serde::Deserialize;
use std::{collections::HashSet, fs};

#[derive(Deserialize, Debug, Default)]
pub struct PackageConfig {
pub tags_allow: Vec<String>,
}
impl<'a> PackageConfig {
pub fn tags(&self, tags: Vec<&'a Tag>) -> Vec<&'a Tag> {
if self.tags_allow.is_empty() {
tags
} else {
let tags_allow: HashSet<&str> = self.tags_allow.iter().map(String::as_str).collect();
tags.into_iter().filter(|tag| tags_allow.contains(tag.name())).collect()
}
}
}

pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Failed to deserialize autorust.toml")]
Deserialize(#[from] toml::de::Error),
#[error(transparent)]
Io(#[from] crate::io::Error),
}

/// Deserializes the autorust.toml into a PackageConfig
/// If the file does not exist, then returns a default instance
pub fn read(path: &Utf8Path) -> Result<PackageConfig> {
if path.exists() {
let bytes = fs::read(path).map_err(|source| crate::io::Error::ReadFile {
source,
file: Utf8PathBuf::from(path),
})?;
Ok(toml::from_slice(&bytes)?)
} else {
Ok(PackageConfig::default())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_tags_allow() -> Result<(), Error> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to cover the case where no autorest.toml exists and the passed tags are returned back. That'll happen far more frequently.

let s = r#"tags_allow = ["package-2021-08", "package-2021-05"]"#;
let config: PackageConfig = toml::from_str(s)?;
assert_eq!(2, config.tags_allow.len());
assert_eq!("package-2021-08", config.tags_allow[0]);
assert_eq!("package-2021-05", config.tags_allow[1]);
Ok(())
}
}
2 changes: 2 additions & 0 deletions services/autorust/codegen/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub enum Error {
CreateOutputDirectory { source: std::io::Error, directory: Utf8PathBuf },
#[error("Could not create file {file}")]
CreateFile { source: std::io::Error, file: Utf8PathBuf },
#[error("Could not read file {file}")]
ReadFile { source: std::io::Error, file: Utf8PathBuf },
#[error("Could not write file {file}")]
WriteFile { source: std::io::Error, file: Utf8PathBuf },
#[error("file name was not utf-8")]
Expand Down
3 changes: 3 additions & 0 deletions services/autorust/codegen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod autorust_toml;
pub mod cargo_toml;
mod codegen;
mod codegen_models;
Expand Down Expand Up @@ -42,6 +43,8 @@ pub enum Error {
LibRs(#[from] lib_rs::Error),
#[error(transparent)]
Spec(#[from] spec::Error),
#[error(transparent)]
AutorustToml(#[from] autorust_toml::Error),
}
impl<T: Into<io::Error>> From<T> for Error {
fn from(error: T) -> Self {
Expand Down
53 changes: 1 addition & 52 deletions services/mgmt/network/Cargo.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 1 addition & 52 deletions services/mgmt/network/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,5 @@ The default tag is `package-2021-08`.

The following [tags](https://github.com/Azure/azure-sdk-for-rust/blob/main/services/tags.md) are available:

- `package-2022-02-preview` has 603 operations from 3 API versions: `2018-10-01`, `2021-05-01`, `2022-02-01-preview`. Use crate feature `package-2022-02-preview` to enable. The operations will be in the `package_2022_02_preview` module.
- `package-2021-08` has 549 operations from 2 API versions: `2018-10-01`, `2021-08-01`. Use crate feature `package-2021-08` to enable. The operations will be in the `package_2021_08` module.
- `package-2021-05-preview` has 603 operations from 3 API versions: `2018-10-01`, `2021-05-01`, `2021-05-01-preview`. Use crate feature `package-2021-05-preview` to enable. The operations will be in the `package_2021_05_preview` module.
- `package-2021-05` has 540 operations from 2 API versions: `2018-10-01`, `2021-05-01`. Use crate feature `package-2021-05` to enable. The operations will be in the `package_2021_05` module.
- `package-2021-03` has 529 operations from 2 API versions: `2018-10-01`, `2021-03-01`. Use crate feature `package-2021-03` to enable. The operations will be in the `package_2021_03` module.
- `package-2021-02` has 527 operations from 2 API versions: `2018-10-01`, `2021-02-01`. Use crate feature `package-2021-02` to enable. The operations will be in the `package_2021_02` module.
- `package-2020-11` has 522 operations from 2 API versions: `2018-10-01`, `2020-11-01`. Use crate feature `package-2020-11` to enable. The operations will be in the `package_2020_11` module.
- `package-2021-02-preview-only` has 65 operations from 1 API versions: `2021-02-01-preview`. Use crate feature `package-2021-02-preview-only` to enable. The operations will be in the `package_2021_02_preview_only` module.
- `package-2021-02-preview` has 594 operations from 3 API versions: `2018-10-01`, `2021-02-01-preview`, `2021-03-01`. Use crate feature `package-2021-02-preview` to enable. The operations will be in the `package_2021_02_preview` module.
- `package-2021-03-preview` has 533 operations from 3 API versions: `2018-10-01`, `2021-02-01`, `2021-03-01-preview`. Use crate feature `package-2021-03-preview` to enable. The operations will be in the `package_2021_03_preview` module.
- `package-2020-08` has 519 operations from 2 API versions: `2018-10-01`, `2020-08-01`. Use crate feature `package-2020-08` to enable. The operations will be in the `package_2020_08` module.
- `package-2020-07` has 513 operations from 2 API versions: `2018-10-01`, `2020-07-01`. Use crate feature `package-2020-07` to enable. The operations will be in the `package_2020_07` module.
- `package-2020-06` has 505 operations from 2 API versions: `2018-10-01`, `2020-06-01`. Use crate feature `package-2020-06` to enable. The operations will be in the `package_2020_06` module.
- `package-2020-05` has 485 operations from 2 API versions: `2018-10-01`, `2020-05-01`. Use crate feature `package-2020-05` to enable. The operations will be in the `package_2020_05` module.
- `package-2020-04` has 462 operations from 2 API versions: `2018-10-01`, `2020-04-01`. Use crate feature `package-2020-04` to enable. The operations will be in the `package_2020_04` module.
- `package-2020-03` has 456 operations from 2 API versions: `2018-10-01`, `2020-03-01`. Use crate feature `package-2020-03` to enable. The operations will be in the `package_2020_03` module.
- `package-2019-12` has 440 operations from 2 API versions: `2018-10-01`, `2019-12-01`. Use crate feature `package-2019-12` to enable. The operations will be in the `package_2019_12` module.
- `package-2019-11` has 434 operations from 2 API versions: `2018-10-01`, `2019-11-01`. Use crate feature `package-2019-11` to enable. The operations will be in the `package_2019_11` module.
- `package-2019-09` has 428 operations from 2 API versions: `2018-10-01`, `2019-09-01`. Use crate feature `package-2019-09` to enable. The operations will be in the `package_2019_09` module.
- `package-2019-08` has 415 operations from 2 API versions: `2018-10-01`, `2019-08-01`. Use crate feature `package-2019-08` to enable. The operations will be in the `package_2019_08` module.
- `package-2019-07` has 408 operations from 2 API versions: `2018-10-01`, `2019-07-01`. Use crate feature `package-2019-07` to enable. The operations will be in the `package_2019_07` module.
- `package-2019-06` has 394 operations from 2 API versions: `2017-03-30`, `2019-06-01`. Use crate feature `package-2019-06` to enable. The operations will be in the `package_2019_06` module.
- `package-2019-04` has 376 operations from 2 API versions: `2017-03-30`, `2019-04-01`. Use crate feature `package-2019-04` to enable. The operations will be in the `package_2019_04` module.
- `package-2019-02` has 354 operations from 2 API versions: `2017-03-30`, `2019-02-01`. Use crate feature `package-2019-02` to enable. The operations will be in the `package_2019_02` module.
- `package-2018-12` has 345 operations from 2 API versions: `2017-03-30`, `2018-12-01`. Use crate feature `package-2018-12` to enable. The operations will be in the `package_2018_12` module.
- `package-2018-12-only` has 332 operations from 1 API versions: `2018-12-01`. Use crate feature `package-2018-12-only` to enable. The operations will be in the `package_2018_12_only` module.
- `package-2018-11` has 337 operations from 2 API versions: `2017-03-30`, `2018-11-01`. Use crate feature `package-2018-11` to enable. The operations will be in the `package_2018_11` module.
- `package-2018-10` has 328 operations from 2 API versions: `2017-03-30`, `2018-10-01`. Use crate feature `package-2018-10` to enable. The operations will be in the `package_2018_10` module.
- `package-2018-08` has 327 operations from 2 API versions: `2017-03-30`, `2018-08-01`. Use crate feature `package-2018-08` to enable. The operations will be in the `package_2018_08` module.
- `package-2018-07` has 269 operations from 2 API versions: `2017-03-30`, `2018-07-01`. Use crate feature `package-2018-07` to enable. The operations will be in the `package_2018_07` module.
- `package-2018-06` has 254 operations from 2 API versions: `2017-03-30`, `2018-06-01`. Use crate feature `package-2018-06` to enable. The operations will be in the `package_2018_06` module.
- `package-2018-04` has 252 operations from 2 API versions: `2017-03-30`, `2018-04-01`. Use crate feature `package-2018-04` to enable. The operations will be in the `package_2018_04` module.
- `package-2018-02` has 216 operations from 2 API versions: `2017-03-30`, `2018-02-01`. Use crate feature `package-2018-02` to enable. The operations will be in the `package_2018_02` module.
- `package-2018-01` has 194 operations from 2 API versions: `2017-03-30`, `2018-01-01`. Use crate feature `package-2018-01` to enable. The operations will be in the `package_2018_01` module.
- `package-2018-01-only` has 186 operations from 1 API versions: `2018-01-01`. Use crate feature `package-2018-01-only` to enable. The operations will be in the `package_2018_01_only` module.
- `package-2017-11` has 194 operations from 2 API versions: `2017-03-30`, `2017-11-01`. Use crate feature `package-2017-11` to enable. The operations will be in the `package_2017_11` module.
- `package-2017-11-only` has 186 operations from 1 API versions: `2017-11-01`. Use crate feature `package-2017-11-only` to enable. The operations will be in the `package_2017_11_only` module.
- `package-2017-10` has 194 operations from 2 API versions: `2017-03-30`, `2017-10-01`. Use crate feature `package-2017-10` to enable. The operations will be in the `package_2017_10` module.
- `package-2017-10-only` has 186 operations from 1 API versions: `2017-10-01`. Use crate feature `package-2017-10-only` to enable. The operations will be in the `package_2017_10_only` module.
- `package-2017-09` has 187 operations from 2 API versions: `2017-03-30`, `2017-09-01`. Use crate feature `package-2017-09` to enable. The operations will be in the `package_2017_09` module.
- `package-2017-09-only` has 179 operations from 1 API versions: `2017-09-01`. Use crate feature `package-2017-09-only` to enable. The operations will be in the `package_2017_09_only` module.
- `package-2017-08` has 163 operations from 2 API versions: `2017-03-30`, `2017-08-01`. Use crate feature `package-2017-08` to enable. The operations will be in the `package_2017_08` module.
- `package-2017-06` has 162 operations from 2 API versions: `2017-03-30`, `2017-06-01`. Use crate feature `package-2017-06` to enable. The operations will be in the `package_2017_06` module.
- `package-2017-03` has 138 operations from 2 API versions: `2017-03-01`, `2017-03-30`. Use crate feature `package-2017-03` to enable. The operations will be in the `package_2017_03` module.
- `package-2017-03-only` has 132 operations from 1 API versions: `2017-03-01`. Use crate feature `package-2017-03-only` to enable. The operations will be in the `package_2017_03_only` module.
- `package-2017-03-30-only` has 8 operations from 2 API versions: `2017-03-30`, `2017-09-01`. Use crate feature `package-2017-03-30-only` to enable. The operations will be in the `package_2017_03_30_only` module.
- `package-2016-12` has 132 operations from 1 API versions: `2016-12-01`. Use crate feature `package-2016-12` to enable. The operations will be in the `package_2016_12` module.
- `package-2016-09` has 120 operations from 1 API versions: `2016-09-01`. Use crate feature `package-2016-09` to enable. The operations will be in the `package_2016_09` module.
- `package-2016-06` has 97 operations from 1 API versions: `2016-06-01`. Use crate feature `package-2016-06` to enable. The operations will be in the `package_2016_06` module.
- `package-2016-03` has 90 operations from 1 API versions: `2016-03-30`. Use crate feature `package-2016-03` to enable. The operations will be in the `package_2016_03` module.
- `package-2015-06split` has 88 operations from 1 API versions: `2015-06-15`. Use crate feature `package-2015-06split` to enable. The operations will be in the `package_2015_06split` module.
- `package-2015-05-preview` has 87 operations from 1 API versions: `2015-05-01-preview`. Use crate feature `package-2015-05-preview` to enable. The operations will be in the `package_2015_05_preview` module.
- `profile-hybrid-2020-09-01` has 112 operations from 1 API versions: `2018-11-01`. Use crate feature `profile-hybrid-2020-09-01` to enable. The operations will be in the `profile_hybrid_2020_09_01` module.
- `package-2021-05` has 540 operations from 2 API versions: `2018-10-01`, `2021-05-01`. Use crate feature `package-2021-05` to enable. The operations will be in the `package_2021_05` module.
1 change: 1 addition & 0 deletions services/mgmt/network/autorust.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tags_allow = ["package-2021-08", "package-2021-05"]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is limiting the tags for azure_mgmt_network to two of the most recent stable tags.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So no root section is necessary? Should we have one to avoid future issues, or likely not a problem?

FWIW, our autorest tooling makes heavy use (required, really) of configuration so I definitely could see adding more in the future here...assuming we don't eventually adopt autorest instead.

Loading