Skip to content
This repository has been archived by the owner on Apr 10, 2021. It is now read-only.

create identifier module #74

Merged
merged 8 commits into from
Oct 30, 2020
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
11 changes: 6 additions & 5 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ edition = "2018"
[lib]

[dependencies]
autorust_openapi = { git = "https://github.com/ctaggart/autorust_openapi" }
autorust_openapi = { rev = "181baf2b89d32c013258e40b59d3e00bb501c9c4", git = "https://github.com/ctaggart/autorust_openapi" }
# autorust_openapi = { path = "../autorust_openapi" }
quote = "*"
proc-macro2 = { version = "*", default-features = false }
serde_json = "*"
serde_yaml = "*"
heck = "*"
regex = "*"
indexmap = {version = "*", features = ["serde-1"]}
indexmap = { version = "*", features = ["serde-1"] }
path_abs = "*"
comrak = "0.8"
serde = "1.0"
snafu = "0.6"
http = "0.2"
lazy_static = "1.4"
lazy_static = "1.4"
syn = { version = "1.0", features = ["parsing"] }
82 changes: 53 additions & 29 deletions codegen/examples/gen_mgmt.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
// cargo run --example gen_mgmt
// https://github.com/Azure/azure-rest-api-specs/blob/master/specification/compute/resource-manager

use autorust_codegen::{
cargo_toml,
self, cargo_toml,
config_parser::{self, to_api_version, to_mod_name},
lib_rs, path, run, Config,
lib_rs, path, Config,
};
use heck::SnakeCase;
use snafu::{OptionExt, ResultExt, Snafu};
use std::{
collections::{HashMap, HashSet},
fs,
};

pub type Error = Box<dyn std::error::Error + Send + Sync>;
pub type Result<T> = std::result::Result<T, Error>;

const SPEC_FOLDER: &str = "../azure-rest-api-specs/specification";
const OUTPUT_FOLDER: &str = "../azure-sdk-for-rust/services/mgmt";

Expand All @@ -24,15 +21,16 @@ const SERVICE_NAMES: &[(&str, &str)] = &[
];

const ONLY_SERVICES: &[&str] = &[
// "datafactory",
];
// "network",
// "redis",
];

const SKIP_SERVICES: &[&str] = &[
"apimanagement", // missing properties, all preview apis
"automation", // Error: Error("data did not match any variant of untagged enum ReferenceOr", line: 90, column: 5)
"cosmos-db", // get_gremlin_graph_throughput defined twice
"cost-management", // use of undeclared crate or module `definition`
"databox", // recursive type has infinite size
"databox", // TODO #73 recursive types
"databoxedge", // duplicate model pub struct SkuCost {
"datamigration", // Error: "schema not found ../azure-rest-api-specs/specification/datamigration/resource-manager/Microsoft.DataMigration/preview/2018-07-15-preview/definitions/MigrateSqlServerSqlDbTask.json ValidationStatus"
"deploymentmanager", // missing params
Expand All @@ -41,14 +39,14 @@ const SKIP_SERVICES: &[&str] = &[
"hardwaresecuritymodules", // recursive without indirection on Error
"healthcareapis", // Error: "schema not found ../azure-rest-api-specs/specification/common-types/resource-management/v1/types.json Resource"
"hybridcompute", // use of undeclared crate or module `status`
"logic", // recursive type has infinite size
"logic", // TODO #73 recursive types
"machinelearning", // missing params
"managedservices", // registration_definition
"mediaservices", // Error: Error("invalid unicode code point", line: 1380, column: 289)
"migrateprojects", // recursive type has infinite size
"mixedreality", // &AccountKeyRegenerateRequest not found in scope
"netapp", // codegen wrong, missing operation params in function
"network", // thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', codegen/src/codegen.rs:419:42
"network", // TODO #73 recursive types
"powerplatform", // Error: "parameter not found ../azure-rest-api-specs/specification/powerplatform/resource-manager/Microsoft.PowerPlatform/common/v1/definitions.json ResourceGroupNameParameter"
"recoveryservicessiterecovery", // duplicate package-2016-08 https://github.com/Azure/azure-rest-api-specs/pull/11287
"redis", // map_type
Expand All @@ -63,16 +61,40 @@ const SKIP_SERVICE_TAGS: &[(&str, &str)] = &[
("azureactivedirectory", "package-preview-2020-07"),
("resources", "package-policy-2020-03"),
("recoveryservicesbackup", "package-2020-07"), // duplicate fn get_operation_status
("network", "package-2017-03-30-only"), // SchemaNotFound 2017-09-01/network.json SubResource
];

pub type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("file name was not utf-8"))]
FileNameNotUtf8Error {},
IoError {
source: std::io::Error,
},
PathError {
source: path::Error,
},
CodegenError {
source: autorust_codegen::Error,
},
CargoTomlError {
source: cargo_toml::Error,
},
LibRsError {
source: lib_rs::Error,
},
}

fn main() -> Result<()> {
let paths = fs::read_dir(SPEC_FOLDER)?;
let paths = fs::read_dir(SPEC_FOLDER).context(IoError)?;
let mut spec_folders = Vec::new();
for path in paths {
let path = path?;
if path.file_type()?.is_dir() {
let path = path.context(IoError)?;
if path.file_type().context(IoError)?.is_dir() {
let file_name = path.file_name();
let spec_folder = file_name.to_str().ok_or("file name was not utf-8")?;
let spec_folder = file_name.to_str().context(FileNameNotUtf8Error)?;
spec_folders.push(spec_folder.to_owned());
}
}
Expand All @@ -95,8 +117,8 @@ fn main() -> Result<()> {
}

fn gen_crate(spec_folder: &str) -> Result<()> {
let spec_folder_full = path::join(SPEC_FOLDER, spec_folder)?;
let readme = &path::join(spec_folder_full, "resource-manager/readme.md")?;
let spec_folder_full = path::join(SPEC_FOLDER, spec_folder).context(PathError)?;
let readme = &path::join(spec_folder_full, "resource-manager/readme.md").context(PathError)?;
if !readme.exists() {
println!("readme not found at {:?}", readme);
return Ok(());
Expand All @@ -105,14 +127,13 @@ fn gen_crate(spec_folder: &str) -> Result<()> {
let service_name = &get_service_name(spec_folder);
// println!("{} -> {}", spec_folder, service_name);
let crate_name = &format!("azure_mgmt_{}", service_name);
let output_folder = &path::join(OUTPUT_FOLDER, service_name)?;
let output_folder = &path::join(OUTPUT_FOLDER, service_name).context(PathError)?;

let src_folder = path::join(output_folder, "src")?;
let src_folder = path::join(output_folder, "src").context(PathError)?;
if src_folder.exists() {
fs::remove_dir_all(&src_folder)?;
fs::remove_dir_all(&src_folder).context(IoError)?;
}

// fs::create_dir_all(&src_folder)?;
let packages = config_parser::parse_configurations_from_autorest_config_file(&readme);
let mut feature_mod_names = Vec::new();
let skip_service_tags: HashSet<&(&str, &str)> = SKIP_SERVICE_TAGS.iter().collect();
Expand All @@ -128,24 +149,26 @@ fn gen_crate(spec_folder: &str) -> Result<()> {
let mod_name = &to_mod_name(tag);
feature_mod_names.push((tag.to_string(), mod_name.clone()));
// println!(" {}", mod_name);
let mod_output_folder = path::join(&src_folder, mod_name)?;
let mod_output_folder = path::join(&src_folder, mod_name).context(PathError)?;
// println!(" {:?}", mod_output_folder);
// for input_file in &package.input_files {
// println!(" {}", input_file);
// }
let input_files: Vec<_> = package
let input_files: Result<Vec<_>> = package
.input_files
.iter()
.map(|input_file| path::join(readme, input_file).unwrap())
.map(|input_file| Ok(path::join(readme, input_file).context(PathError)?))
.collect();
let input_files = input_files?;
// for input_file in &input_files {
// println!(" {:?}", input_file);
// }
run(Config {
autorust_codegen::run(Config {
api_version: Some(api_version),
output_folder: mod_output_folder.into(),
input_files,
})?;
})
.context(CodegenError)?;
}
}
if feature_mod_names.len() == 0 {
Expand All @@ -154,9 +177,10 @@ fn gen_crate(spec_folder: &str) -> Result<()> {
cargo_toml::create(
crate_name,
&feature_mod_names,
&path::join(output_folder, "Cargo.toml").map_err(|_| "Cargo.toml")?,
)?;
lib_rs::create(&feature_mod_names, &path::join(src_folder, "lib.rs").map_err(|_| "lib.rs")?)?;
&path::join(output_folder, "Cargo.toml").context(PathError)?,
)
.context(CargoTomlError)?;
lib_rs::create(&feature_mod_names, &path::join(src_folder, "lib.rs").context(PathError)?).context(LibRsError)?;

Ok(())
}
Expand Down
Loading