diff --git a/Cargo.lock b/Cargo.lock index cbfd2883..0e966950 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2345,7 +2345,6 @@ name = "pulumi_wasm_generator" version = "0.0.0-DEV" dependencies = [ "anyhow", - "clap", "pulumi_wasm_generator_lib", "tempfile", ] diff --git a/justfile b/justfile index 5f895b22..613c352e 100644 --- a/justfile +++ b/justfile @@ -53,8 +53,6 @@ build-all-wasm-projects-release: cargo component build -p pulumi_wasm_example_dependencies --release cargo component build -p pulumi_wasm_example_multiple_providers --release -build-wasm-components-release: - check: cargo fmt {{FORMATTABLE_PROJECTS}} -- --check diff --git a/providers/cloudflare/build.rs b/providers/cloudflare/build.rs index 4a60c67a..5a9fe142 100644 --- a/providers/cloudflare/build.rs +++ b/providers/cloudflare/build.rs @@ -1,4 +1,5 @@ use pulumi_wasm_generator; -fn main() { - pulumi_wasm_generator::generate("cloudflare", "5.43.1"); +fn main() -> Result<(), Box> { + pulumi_wasm_generator::generate("cloudflare", "5.43.1")?; + Ok(()) } \ No newline at end of file diff --git a/providers/docker/build.rs b/providers/docker/build.rs index 7c2141b9..275fe794 100644 --- a/providers/docker/build.rs +++ b/providers/docker/build.rs @@ -1,4 +1,5 @@ use pulumi_wasm_generator; -fn main() { - pulumi_wasm_generator::generate("docker", "4.5.3"); +fn main() -> Result<(), Box> { + pulumi_wasm_generator::generate("docker", "4.5.3")?; + Ok(()) } \ No newline at end of file diff --git a/providers/random/build.rs b/providers/random/build.rs index b376d9b7..46d83338 100644 --- a/providers/random/build.rs +++ b/providers/random/build.rs @@ -1,4 +1,5 @@ use pulumi_wasm_generator; -fn main() { - pulumi_wasm_generator::generate("random", "4.15.0"); +fn main() -> Result<(), Box> { + pulumi_wasm_generator::generate("random", "4.15.0")?; + Ok(()) } \ No newline at end of file diff --git a/providers/typesystem/build.rs b/providers/typesystem/build.rs index 1c5a9038..b351b2a6 100644 --- a/providers/typesystem/build.rs +++ b/providers/typesystem/build.rs @@ -1,4 +1,5 @@ use pulumi_wasm_generator; -fn main() { - pulumi_wasm_generator::generate_from_schema("typesystem.json".as_ref()); +fn main() -> Result<(), Box> { + pulumi_wasm_generator::generate_from_schema("typesystem.json".as_ref())?; + Ok(()) } \ No newline at end of file diff --git a/pulumi_wasm_generator/Cargo.toml b/pulumi_wasm_generator/Cargo.toml index 381a7a46..331550a9 100644 --- a/pulumi_wasm_generator/Cargo.toml +++ b/pulumi_wasm_generator/Cargo.toml @@ -6,7 +6,6 @@ edition.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -clap.workspace = true pulumi_wasm_generator_lib.workspace = true anyhow.workspace = true tempfile = "3.14.0" \ No newline at end of file diff --git a/pulumi_wasm_generator/src/lib.rs b/pulumi_wasm_generator/src/lib.rs index 0442dc97..70954f4b 100644 --- a/pulumi_wasm_generator/src/lib.rs +++ b/pulumi_wasm_generator/src/lib.rs @@ -1,70 +1,47 @@ -use clap::{Parser, Subcommand}; - +use anyhow::{Context, Result}; use pulumi_wasm_generator_lib::{extract_micro_package, generate_combined}; use std::path::Path; use std::{env, fs}; -#[derive(Parser, Debug)] -#[command(version, about, long_about = None)] -struct App { - #[clap(subcommand)] - command: Command, -} - -#[derive(Debug, Subcommand)] -enum Command { - GenProvider { - #[arg(short, long)] - schema: String, - - #[arg(short, long)] - output: String, - - #[arg(short, long)] - remove: Option, - }, - GenRust { - #[arg(short, long)] - schema: String, - - #[arg(short, long)] - output: String, - - #[arg(short, long)] - remove: Option, - }, -} - -pub fn generate(provider_name: &str, provider_version: &str) { +pub fn generate(provider_name: &str, provider_version: &str) -> Result<()> { let schema_output = std::process::Command::new("pulumi") .arg("package") .arg("get-schema") .arg(format!("{}@{}", provider_name, provider_version)) .output() - .expect("Failed to execute pulumi command"); + .context("Failed to execute pulumi command")?; let schema = String::from_utf8(schema_output.stdout).expect("Invalid UTF-8 in pulumi output"); - let out_dir = env::var_os("OUT_DIR").unwrap(); - let out_dir = out_dir.to_str().unwrap(); + let out_dir = env::var_os("OUT_DIR").context("Failed to get OUT_DIR environment variable")?; + let out_dir = out_dir + .to_str() + .context(format!("Failed to convert [{:?}] to string", out_dir))?; + let location = Path::new(out_dir).join("pulumi").join(provider_name); - let temp_dir = tempfile::tempdir().unwrap(); + let temp_dir = tempfile::tempdir().context("Failed to create temporary directory")?; let file = temp_dir.path().join("schema.json"); - fs::write(&file, &schema).unwrap(); + fs::write(&file, &schema).context("Failed to write schema")?; - generate_combined(file.as_path(), &location).unwrap(); + generate_combined(file.as_path(), &location).context("Failed to generate glue files")?; println!("cargo::rerun-if-changed=build.rs"); + + Ok(()) } -pub fn generate_from_schema(schema_file: &Path) { - let package = extract_micro_package(schema_file).unwrap(); +pub fn generate_from_schema(schema_file: &Path) -> Result<()> { + let package = extract_micro_package(schema_file).context("Failed to deserialize package")?; let provider_name = package.name; - let out_dir = env::var_os("OUT_DIR").unwrap(); - let out_dir = out_dir.to_str().unwrap(); + let out_dir = env::var_os("OUT_DIR").context("Failed to get OUT_DIR environment variable")?; + let out_dir = out_dir + .to_str() + .context(format!("Failed to convert [{:?}] to string", out_dir))?; let location = Path::new(out_dir).join("pulumi").join(provider_name); - generate_combined(schema_file, &location).unwrap(); + generate_combined(schema_file, &location).context("Failed to generate glue files")?; println!("cargo::rerun-if-changed=build.rs"); + println!("cargo::rerun-if-changed={}", schema_file.display()); + Ok(()) } diff --git a/pulumi_wasm_generator_lib/src/output/functions/mod.rs b/pulumi_wasm_generator_lib/src/output/functions/mod.rs index e55dde9e..ca029a31 100644 --- a/pulumi_wasm_generator_lib/src/output/functions/mod.rs +++ b/pulumi_wasm_generator_lib/src/output/functions/mod.rs @@ -2,12 +2,8 @@ use crate::model::{ElementId, Package}; use crate::output::functions::source_code_function_code::{ generate_docs, generate_single_function_source_code, }; -use crate::output::TreeNode; use crate::utils::reformat_code; use anyhow::Context; -use convert_case::{Case, Casing}; -use std::fs::File; -use std::io::Write; mod source_code_function_code; @@ -20,87 +16,3 @@ pub(crate) fn generate_single_file(package: &Package, element_id: &ElementId) -> pub(crate) fn generate_single_file_docs(package: &Package, element_id: &ElementId) -> Vec { generate_docs(package, element_id) } - -pub(crate) fn generate_function_code(package: &Package, result_path: &std::path::Path) { - if package.functions.is_empty() { - return; - } - let mut tree = TreeNode::new(); - - for element_id in package.functions.keys() { - tree.insert(element_id.clone()); - } - - generate_files(package, &tree, result_path); -} - -fn generate_files(package: &Package, tree_node: &TreeNode, current_path: &std::path::Path) { - match tree_node { - TreeNode::Namespace(ns, functions) => { - std::fs::create_dir_all(current_path).unwrap(); - for (name, node) in ns { - generate_files(package, node, ¤t_path.join(name)); - } - - for function in functions { - let file_name = format!( - "{}.rs", - function.get_rust_struct_name().to_case(Case::Snake) - ); - let content = generate_single_function_source_code(package, function); - let mut file = File::create(current_path.join(file_name)).unwrap(); - file.write_all(content.as_bytes()).unwrap(); - } - } - } -} - -pub(crate) fn generate_module_imports(package: &Package) -> String { - if package.functions.is_empty() { - return "".to_string(); - } - let mut tree = TreeNode::new(); - - for element_id in package.functions.keys() { - tree.insert(element_id.clone()); - } - - generate_module_imports_1(package, &tree, std::path::Path::new("functions")) -} - -fn generate_module_imports_1( - package: &Package, - tree_node: &TreeNode, - current_path: &std::path::Path, -) -> String { - match tree_node { - TreeNode::Namespace(ns, functions) => { - let mut s = String::new(); - - for (name, node) in ns { - s.push_str(&format!("pub mod {}{{\n", name)); - s.push_str(&generate_module_imports_1( - package, - node, - ¤t_path.join(name), - )); - s.push_str("}\n"); - } - - for function in functions { - s.push_str(&format!( - "pub mod {}{{\n", - function.get_rust_struct_name().to_case(Case::Snake) - )); - s.push_str(&format!( - "include!(\"{}/{}.rs\");\n", - current_path.to_str().unwrap().replace("\\", "/"), - function.get_rust_struct_name().to_case(Case::Snake) - )); - s.push_str("}\n"); - } - - s - } - } -} diff --git a/pulumi_wasm_generator_lib/src/output/mod.rs b/pulumi_wasm_generator_lib/src/output/mod.rs index 73979dee..72384200 100644 --- a/pulumi_wasm_generator_lib/src/output/mod.rs +++ b/pulumi_wasm_generator_lib/src/output/mod.rs @@ -44,10 +44,7 @@ impl TreeNode { } } -pub(crate) fn generate_combined_code( - package: &crate::model::Package, - result_path: &std::path::Path, -) { +pub(crate) fn generate_combined_code(package: &Package, result_path: &std::path::Path) { generate_files( package, &result_path.join("functions"), diff --git a/pulumi_wasm_generator_lib/src/output/resources/mod.rs b/pulumi_wasm_generator_lib/src/output/resources/mod.rs index 7f3ef3a9..c5322992 100644 --- a/pulumi_wasm_generator_lib/src/output/resources/mod.rs +++ b/pulumi_wasm_generator_lib/src/output/resources/mod.rs @@ -4,12 +4,8 @@ use crate::model::{ElementId, Package}; use crate::output::resources::source_code_resource_code::{ generate_docs, generate_single_resource_source_code, }; -use crate::output::TreeNode; use crate::utils::reformat_code; use anyhow::Context; -use convert_case::{Case, Casing}; -use std::fs::File; -use std::io::Write; pub(crate) fn generate_single_file(package: &Package, element_id: &ElementId) -> String { reformat_code(&generate_single_resource_source_code(package, element_id)) @@ -20,96 +16,3 @@ pub(crate) fn generate_single_file(package: &Package, element_id: &ElementId) -> pub(crate) fn generate_single_file_docs(package: &Package, element_id: &ElementId) -> Vec { generate_docs(package, element_id) } - -pub(crate) fn generate_resources_code( - package: &crate::model::Package, - result_path: &std::path::Path, -) { - if package.resources.is_empty() { - return; - } - let mut tree = TreeNode::new(); - - for element_id in package.resources.keys() { - tree.insert(element_id.clone()); - } - - println!("Tree {:?}", tree); - - generate_files(package, &tree, result_path); -} - -fn generate_files( - package: &crate::model::Package, - tree_node: &TreeNode, - current_path: &std::path::Path, -) { - match tree_node { - TreeNode::Namespace(ns, resources) => { - std::fs::create_dir_all(current_path).unwrap(); - for (name, node) in ns { - generate_files(package, node, ¤t_path.join(name)); - } - - for resource in resources { - let file_name = format!( - "{}.rs", - resource.get_rust_struct_name().to_case(Case::Snake) - ); - let content = generate_single_resource_source_code(package, resource); - let mut file = File::create(current_path.join(file_name)).unwrap(); - file.write_all(content.as_bytes()).unwrap(); - } - } - } -} - -pub(crate) fn generate_module_imports(package: &Package) -> String { - if package.resources.is_empty() { - return "".to_string(); - } - let mut tree = TreeNode::new(); - - for element_id in package.resources.keys() { - tree.insert(element_id.clone()); - } - - generate_module_imports_1(package, &tree, std::path::Path::new("resources")) -} - -fn generate_module_imports_1( - package: &Package, - tree_node: &TreeNode, - current_path: &std::path::Path, -) -> String { - match tree_node { - TreeNode::Namespace(ns, functions) => { - let mut s = String::new(); - - for (name, node) in ns { - s.push_str(&format!("pub mod {}{{\n", name)); - s.push_str(&generate_module_imports_1( - package, - node, - ¤t_path.join(name), - )); - s.push_str("}\n"); - } - - for function in functions { - s.push_str(&format!( - "pub mod {}{{\n", - function.get_rust_struct_name().to_case(Case::Snake) - )); - s.push_str(&format!( - "include!(\"{}/{}.rs\");\n", - current_path.to_str().unwrap().replace("\\", "/"), - function.get_rust_struct_name().to_case(Case::Snake) - )); - s.push_str("}\n"); - } - - s - } - } -} diff --git a/regenerator/src/main.rs b/regenerator/src/main.rs index dd442f1f..4d1159ea 100644 --- a/regenerator/src/main.rs +++ b/regenerator/src/main.rs @@ -1,5 +1,4 @@ use std::fs; -use std::process::Command; #[derive(Debug)] struct Provider<'a> {