From 8ed799fdfda65aa91607dd5ce292eb8d5f9064e5 Mon Sep 17 00:00:00 2001 From: Andrzej Ressel Date: Tue, 24 Dec 2024 02:10:42 +0100 Subject: [PATCH] Remove WASM providers --- .github/workflows/build.yml | 4 +- examples/typesystem/src/lib.rs | 4 +- pulumi_wasm_generator/src/lib.rs | 79 +----- pulumi_wasm_generator_lib/src/lib.rs | 95 +------ .../{combined => }/Cargo.toml.handlebars | 0 .../src/output/{combined => }/cargo.rs | 2 +- .../src/output/combined/mod.rs | 232 ----------------- .../combined/wit/dependencies.handlebars | 59 ----- .../src/output/combined/wit/mod.rs | 114 --------- .../src/output/combined/wit/wit.handlebars | 81 ------ .../function_code.rs.handlebars | 0 .../{combined => }/function_mod.rs.handlebars | 0 .../functions/function_code.rs.handlebars | 0 .../output/{combined => }/functions/mod.rs | 10 +- .../functions/source_code_function_code.rs | 2 +- .../output/{combined => }/lib.rs.handlebars | 0 .../src/output/{combined => }/main.rs | 5 +- pulumi_wasm_generator_lib/src/output/mod.rs | 234 +++++++++++++++++- .../src/output/provider/Cargo.toml.handlebars | 23 -- .../src/output/provider/cargo.rs | 29 --- .../provider/function_code.rs.handlebars | 39 --- .../provider/function_mod.rs.handlebars | 3 - .../src/output/provider/lib.rs.handlebars | 16 -- .../src/output/provider/mod.rs | 6 - .../provider/resource_code.rs.handlebars | 41 --- .../provider/resource_mod.rs.handlebars | 3 - .../provider/source_code_function_code.rs | 75 ------ .../provider/source_code_function_mod.rs | 42 ---- .../src/output/provider/source_code_librs.rs | 33 --- .../provider/source_code_resource_code.rs | 75 ------ .../provider/source_code_resource_mod.rs | 42 ---- .../output/{combined => }/resources/mod.rs | 10 +- .../resources/resource_code.rs.handlebars | 0 .../resources/source_code_resource_code.rs | 2 +- .../src/output/rust/Cargo.toml.handlebars | 12 - .../src/output/rust/cargo.rs | 29 --- .../output/rust/function_code.rs.handlebars | 60 ----- .../output/rust/function_mod.rs.handlebars | 3 - .../functions/function_code.rs.handlebars | 58 ----- .../src/output/rust/functions/mod.rs | 46 ---- .../functions/source_code_function_code.rs | 96 ------- .../functions/source_code_function_mod.rs | 32 --- .../src/output/rust/lib.rs.handlebars | 38 --- .../src/output/rust/mod.rs | 44 ---- .../src/output/rust/resources/mod.rs | 52 ---- .../resources/resource_code.rs.handlebars | 58 ----- .../resources/source_code_resource_code.rs | 94 ------- .../resources/source_code_resource_mod.rs | 32 --- .../src/output/rust/source_code_librs.rs | 76 ------ .../src/output/rust/types/mod.rs | 49 ---- .../rust/types/source_code_types_code.rs | 219 ---------------- .../rust/types/source_code_types_mod.rs | 32 --- .../rust/types/types_code.rs.handlebars | 22 -- .../types_code_integer_enum.rs.handlebars | 42 ---- .../types_code_number_enum.rs.handlebars | 42 ---- .../types_code_string_enum.rs.handlebars | 16 -- .../{combined => }/source_code_librs.rs | 2 +- .../src/output/{combined => }/types/mod.rs | 10 +- .../types/source_code_types_code.rs | 0 .../types/types_code.rs.handlebars | 0 .../types_code_integer_enum.rs.handlebars | 0 .../types_code_number_enum.rs.handlebars | 0 .../types_code_string_enum.rs.handlebars | 0 .../src/output/wit/mod.rs | 102 +------- .../src/output/wit/wit.handlebars | 81 ------ pulumi_wasm_generator_lib/src/utils.rs | 6 +- pulumi_wasm_generator_lib/tests/test.rs | 8 +- 67 files changed, 268 insertions(+), 2453 deletions(-) rename pulumi_wasm_generator_lib/src/output/{combined => }/Cargo.toml.handlebars (100%) rename pulumi_wasm_generator_lib/src/output/{combined => }/cargo.rs (95%) delete mode 100644 pulumi_wasm_generator_lib/src/output/combined/mod.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/combined/wit/dependencies.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/combined/wit/mod.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/combined/wit/wit.handlebars rename pulumi_wasm_generator_lib/src/output/{combined => }/function_code.rs.handlebars (100%) rename pulumi_wasm_generator_lib/src/output/{combined => }/function_mod.rs.handlebars (100%) rename pulumi_wasm_generator_lib/src/output/{combined => }/functions/function_code.rs.handlebars (100%) rename pulumi_wasm_generator_lib/src/output/{combined => }/functions/mod.rs (92%) rename pulumi_wasm_generator_lib/src/output/{combined => }/functions/source_code_function_code.rs (98%) rename pulumi_wasm_generator_lib/src/output/{combined => }/lib.rs.handlebars (100%) rename pulumi_wasm_generator_lib/src/output/{combined => }/main.rs (88%) delete mode 100644 pulumi_wasm_generator_lib/src/output/provider/Cargo.toml.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/provider/cargo.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/provider/function_code.rs.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/provider/function_mod.rs.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/provider/lib.rs.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/provider/mod.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/provider/resource_code.rs.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/provider/resource_mod.rs.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/provider/source_code_function_code.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/provider/source_code_function_mod.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/provider/source_code_librs.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/provider/source_code_resource_code.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/provider/source_code_resource_mod.rs rename pulumi_wasm_generator_lib/src/output/{combined => }/resources/mod.rs (93%) rename pulumi_wasm_generator_lib/src/output/{combined => }/resources/resource_code.rs.handlebars (100%) rename pulumi_wasm_generator_lib/src/output/{combined => }/resources/source_code_resource_code.rs (98%) delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/Cargo.toml.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/cargo.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/function_code.rs.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/function_mod.rs.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/functions/function_code.rs.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/functions/mod.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/functions/source_code_function_code.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/functions/source_code_function_mod.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/lib.rs.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/mod.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/resources/mod.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/resources/resource_code.rs.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/resources/source_code_resource_code.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/resources/source_code_resource_mod.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/source_code_librs.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/types/mod.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/types/source_code_types_code.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/types/source_code_types_mod.rs delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/types/types_code.rs.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/types/types_code_integer_enum.rs.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/types/types_code_number_enum.rs.handlebars delete mode 100644 pulumi_wasm_generator_lib/src/output/rust/types/types_code_string_enum.rs.handlebars rename pulumi_wasm_generator_lib/src/output/{combined => }/source_code_librs.rs (98%) rename pulumi_wasm_generator_lib/src/output/{combined => }/types/mod.rs (92%) rename pulumi_wasm_generator_lib/src/output/{combined => }/types/source_code_types_code.rs (100%) rename pulumi_wasm_generator_lib/src/output/{combined => }/types/types_code.rs.handlebars (100%) rename pulumi_wasm_generator_lib/src/output/{combined => }/types/types_code_integer_enum.rs.handlebars (100%) rename pulumi_wasm_generator_lib/src/output/{combined => }/types/types_code_number_enum.rs.handlebars (100%) rename pulumi_wasm_generator_lib/src/output/{combined => }/types/types_code_string_enum.rs.handlebars (100%) delete mode 100644 pulumi_wasm_generator_lib/src/output/wit/wit.handlebars diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 33181a276..97e000ebd 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -107,8 +107,8 @@ jobs: pulumi_wasm_generator_lib/tests/output/nested-module/ pulumi_wasm_generator_lib/tests/output/nested-module-thirdparty/ # DO NOT EDIT - END 1 - - name: Regenerate provider list - run: just regenerate-provider-list + - name: Regenerator + run: just regenerator - name: Check run: just check - name: Build diff --git a/examples/typesystem/src/lib.rs b/examples/typesystem/src/lib.rs index ad7f73eab..3fefdb306 100644 --- a/examples/typesystem/src/lib.rs +++ b/examples/typesystem/src/lib.rs @@ -186,14 +186,14 @@ mod tests { } fn resource_compilation_test() { - let _ = pulumi_wasm_providers_typesystem::deep::nested::module::some_resource::create( + pulumi_wasm_providers_typesystem::deep::nested::module::some_resource::create( "test", SomeResourceArgs::builder().build_struct(), ); } fn function_compilation_test() { - let _ = pulumi_wasm_providers_typesystem::functions::deep::nested::module::some_function::invoke(); + pulumi_wasm_providers_typesystem::functions::deep::nested::module::some_function::invoke(); } fn types_compilation_test() { diff --git a/pulumi_wasm_generator/src/lib.rs b/pulumi_wasm_generator/src/lib.rs index f4f8fcb18..0442dc971 100644 --- a/pulumi_wasm_generator/src/lib.rs +++ b/pulumi_wasm_generator/src/lib.rs @@ -1,10 +1,6 @@ -use anyhow::{Context, Result}; use clap::{Parser, Subcommand}; -use tempfile; -use pulumi_wasm_generator_lib::{ - extract_micro_package, generate_combined, generate_rust_library, generate_wasm_provider, -}; +use pulumi_wasm_generator_lib::{extract_micro_package, generate_combined}; use std::path::Path; use std::{env, fs}; @@ -72,76 +68,3 @@ pub fn generate_from_schema(schema_file: &Path) { generate_combined(schema_file, &location).unwrap(); println!("cargo::rerun-if-changed=build.rs"); } - -fn main() -> Result<()> { - let args = App::parse(); - - match args.command { - Command::GenProvider { - schema, - output: destination, - remove, - } => { - check_if_schema_exists(schema.as_ref())?; - check_if_not_empty(destination.as_ref(), remove)?; - generate_wasm_provider(schema.as_ref(), destination.as_ref())?; - } - Command::GenRust { - schema, - output: destination, - remove, - } => { - check_if_schema_exists(schema.as_ref())?; - check_if_not_empty(destination.as_ref(), remove)?; - generate_rust_library(schema.as_ref(), destination.as_ref())?; - } - }; - - Ok(()) -} - -fn check_if_schema_exists(schema: &Path) -> Result<()> { - if !schema.exists() { - Err(anyhow::anyhow!( - "Schema file [{}] does not exist", - schema.display() - )) - } else if !schema.is_file() { - Err(anyhow::anyhow!( - "Schema [{}] is not a file", - schema.display() - )) - } else { - Ok(()) - } -} - -fn check_if_not_empty(output_directory: &Path, remove: Option) -> Result<()> { - let remove = remove.unwrap_or(false); - if output_directory.exists() && remove { - fs::remove_dir_all(output_directory).context(format!( - "Cannot remove directory [{}]", - output_directory.display() - ))?; - } - fs::create_dir_all(output_directory).context(format!( - "Cannot create directory [{}]", - output_directory.display() - ))?; - let is_empty = output_directory - .read_dir() - .context(format!( - "Cannot read directory [{}]", - output_directory.display() - ))? - .next() - .is_none(); - if !is_empty { - Err(anyhow::anyhow!( - "Directory \"{}\" is not empty", - output_directory.display() - )) - } else { - Ok(()) - } -} diff --git a/pulumi_wasm_generator_lib/src/lib.rs b/pulumi_wasm_generator_lib/src/lib.rs index 754c6d6a4..43a803baa 100644 --- a/pulumi_wasm_generator_lib/src/lib.rs +++ b/pulumi_wasm_generator_lib/src/lib.rs @@ -1,14 +1,10 @@ +use crate::schema::Package; +use anyhow::{Context, Result}; use std::fs; use std::fs::File; -use std::io::{BufReader, Write}; +use std::io::BufReader; use std::path::Path; -use crate::output::rust::functions::generate_function_code; -use crate::output::rust::resources::generate_resources_code; -use crate::schema::Package; -use anyhow::{Context, Result}; -use output::rust::types::generate_types_code; - mod code_generation; mod description; mod model; @@ -16,96 +12,13 @@ mod output; mod schema; mod utils; -pub fn generate_rust_library(schema_json: &Path, result_path: &Path) -> Result<()> { - let schema_package: schema::Package = extract_schema_from_file(schema_json)?; - let package = schema::to_model(&schema_package)?; - - fs::create_dir_all(result_path.join("wit").join("deps"))?; - fs::create_dir_all(result_path.join("src"))?; - fs::create_dir_all(result_path.join("src").join("resource"))?; - fs::create_dir_all(result_path.join("src").join("types"))?; - fs::create_dir_all(result_path.join("src").join("function"))?; - - let mut wit_file = File::create(result_path.join("wit").join("world.wit"))?; - wit_file.write_all(output::wit::generate_wit(&package)?.as_ref())?; - - let mut deps_wit_file = - File::create(result_path.join("wit").join("deps").join("pulumi-wasm.wit"))?; - deps_wit_file.write_all(output::wit::get_dependencies()?.as_ref())?; - - let mut cargo_file = File::create(result_path.join("Cargo.toml"))?; - cargo_file.write_all(output::rust::cargo::generate_cargo(&package).as_bytes())?; - - let mut lib_file = File::create(result_path.join("src").join("lib.rs"))?; - lib_file - .write_all(output::rust::source_code_librs::generate_source_code(&package).as_bytes())?; - - generate_types_code(&package, result_path); - generate_resources_code(&package, result_path); - generate_function_code(&package, result_path); - - Ok(()) -} - -pub fn generate_wasm_provider(schema_json: &Path, result_path: &Path) -> Result<()> { - let schema_package: schema::Package = extract_schema_from_file(schema_json)?; - let package = schema::to_model(&schema_package)?; - - fs::create_dir_all(result_path.join("wit").join("deps"))?; - fs::create_dir_all(result_path.join("src").join("resource"))?; - fs::create_dir_all(result_path.join("src").join("function"))?; - - let mut wit_file = File::create(result_path.join("wit").join("world.wit"))?; - wit_file.write_all(output::wit::generate_wit(&package)?.as_ref())?; - - let mut deps_wit_file = - File::create(result_path.join("wit").join("deps").join("pulumi-wasm.wit"))?; - deps_wit_file.write_all(output::wit::get_dependencies()?.as_ref())?; - - let mut cargo_file = File::create(result_path.join("Cargo.toml"))?; - cargo_file.write_all(output::provider::cargo::generate_cargo(&package).as_bytes())?; - - let mut lib_file = File::create(result_path.join("src").join("lib.rs"))?; - lib_file.write_all( - output::provider::source_code_librs::generate_source_code(&package).as_bytes(), - )?; - - let mut source_file = File::create(result_path.join("src").join("resource").join("mod.rs"))?; - source_file.write_all( - output::provider::source_code_resource_mod::generate_source_code(&package).as_bytes(), - )?; - - output::provider::source_code_resource_code::generate_source_code(&package) - .iter() - .for_each(|(path, content)| { - let mut lib_file = - File::create(result_path.join("src").join("resource").join(path)).unwrap(); - lib_file.write_all(content.as_bytes()).unwrap(); - }); - - let mut source_file = File::create(result_path.join("src").join("function").join("mod.rs"))?; - source_file.write_all( - output::provider::source_code_function_mod::generate_source_code(&package).as_bytes(), - )?; - - output::provider::source_code_function_code::generate_source_code(&package) - .iter() - .for_each(|(path, content)| { - let mut lib_file = - File::create(result_path.join("src").join("function").join(path)).unwrap(); - lib_file.write_all(content.as_bytes()).unwrap(); - }); - - Ok(()) -} - pub fn generate_combined(schema_json: &Path, result_path: &Path) -> Result<()> { let schema_package: schema::Package = extract_schema_from_file(schema_json)?; let package = schema::to_model(&schema_package)?; fs::create_dir_all(result_path)?; - output::combined::generate_combined_code(&package, result_path); + output::generate_combined_code(&package, result_path); Ok(()) } diff --git a/pulumi_wasm_generator_lib/src/output/combined/Cargo.toml.handlebars b/pulumi_wasm_generator_lib/src/output/Cargo.toml.handlebars similarity index 100% rename from pulumi_wasm_generator_lib/src/output/combined/Cargo.toml.handlebars rename to pulumi_wasm_generator_lib/src/output/Cargo.toml.handlebars diff --git a/pulumi_wasm_generator_lib/src/output/combined/cargo.rs b/pulumi_wasm_generator_lib/src/output/cargo.rs similarity index 95% rename from pulumi_wasm_generator_lib/src/output/combined/cargo.rs rename to pulumi_wasm_generator_lib/src/output/cargo.rs index 61a3fb4ec..e0940f655 100644 --- a/pulumi_wasm_generator_lib/src/output/combined/cargo.rs +++ b/pulumi_wasm_generator_lib/src/output/cargo.rs @@ -1,4 +1,4 @@ -use crate::output::get_main_version; +use crate::utils::get_main_version; use handlebars::Handlebars; use serde::Serialize; use serde_json::json; diff --git a/pulumi_wasm_generator_lib/src/output/combined/mod.rs b/pulumi_wasm_generator_lib/src/output/combined/mod.rs deleted file mode 100644 index da6ed0cd3..000000000 --- a/pulumi_wasm_generator_lib/src/output/combined/mod.rs +++ /dev/null @@ -1,232 +0,0 @@ -use crate::model::{ElementId, GlobalType, Package}; -use crate::output::combined::types::generate_types_code; -use convert_case::Case::UpperCamel; -use convert_case::{Case, Casing}; -use itertools::Itertools; -use std::collections::{BTreeMap, BTreeSet}; -use std::fs::File; -use std::io::Write; - -pub(crate) mod cargo; -pub(crate) mod functions; -mod main; -pub(crate) mod resources; -pub(crate) mod source_code_librs; -pub(crate) mod types; -pub(crate) mod wit; - -#[derive(Debug)] -enum TreeNode { - Namespace(BTreeMap, BTreeSet), -} - -impl TreeNode { - // Create an empty tree - pub(crate) fn new() -> Self { - TreeNode::Namespace(BTreeMap::new(), BTreeSet::new()) - } - - fn insert(&mut self, element: ElementId) { - self.insert_priv(element, 0); - } - - fn insert_priv(&mut self, element: ElementId, index: usize) { - match self { - TreeNode::Namespace(children, children_elements) => { - if element.namespace.len() == index { - children_elements.insert(element.clone()); - } else { - let next_node = children - .entry(element.namespace[index].clone()) - .or_insert_with(TreeNode::new); - next_node.insert_priv(element, index + 1); - } - } - } - } -} - -pub(crate) fn generate_combined_code( - package: &crate::model::Package, - result_path: &std::path::Path, -) { - generate_files( - package, - &result_path.join("functions"), - &package.functions, - &functions::generate_single_file, - ); - generate_files( - package, - &result_path.join("resources"), - &package.resources, - &resources::generate_single_file, - ); - generate_types_code(package, &result_path.join("types")); - - let main = main::generate( - generate_includes( - package, - "functions", - &package.functions, - &functions::generate_single_file_docs, - ), - generate_includes( - package, - "resources", - &package.resources, - &resources::generate_single_file_docs, - ), - types::generate_module_imports(package), - find_consts(package), - ) - .unwrap(); - - std::fs::write(result_path.join("main.rs"), main).unwrap(); -} - -fn generate_files( - package: &Package, - result_path: &std::path::Path, - objects: &BTreeMap, - generator: &(impl Fn(&Package, &ElementId) -> String + 'static), -) { - if objects.is_empty() { - return; - } - let mut tree = TreeNode::new(); - - for element_id in objects.keys() { - tree.insert(element_id.clone()); - } - - generate_files_looper(package, &tree, &result_path, &Box::new(&generator)); -} - -fn generate_files_looper( - package: &Package, - tree_node: &TreeNode, - current_path: &std::path::Path, - generator: &dyn Fn(&Package, &ElementId) -> String, -) { - match tree_node { - TreeNode::Namespace(ns, types) => { - std::fs::create_dir_all(current_path).unwrap(); - for (name, node) in ns { - generate_files_looper(package, node, ¤t_path.join(name), generator); - } - - for (type_) in types { - let file_name = format!("{}.rs", type_.get_rust_struct_name().to_case(Case::Snake)); - let content = generator(package, type_); - let mut file = File::create(current_path.join(file_name)).unwrap(); - file.write_all(content.as_bytes()).unwrap(); - } - } - } -} - -fn generate_includes( - package: &Package, - name: &str, - objects: &BTreeMap, - generator: &(impl Fn(&Package, &ElementId) -> Vec + 'static), -) -> String { - if objects.is_empty() { - return "".to_string(); - } - let mut tree = TreeNode::new(); - - for element_id in objects.keys() { - tree.insert(element_id.clone()); - } - - generate_includes_looper(package, &tree, std::path::Path::new(name), generator) -} - -fn generate_includes_looper( - package: &Package, - tree_node: &TreeNode, - current_path: &std::path::Path, - generator: &dyn Fn(&Package, &ElementId) -> Vec, -) -> 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_includes_looper( - package, - node, - ¤t_path.join(name), - generator, - )); - s.push_str("}\n"); - } - - for (function) in functions { - for (line) in generator(package, function) { - s.push_str(&format!("/// {}\n", line)); - } - 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 - } - } -} - -fn find_consts(package: &crate::model::Package) -> Vec { - let mut consts = BTreeSet::new(); - for resource in package.resources.values() { - for input in &resource.input_properties { - consts.extend(input.r#type.get_consts().clone()); - } - for output in &resource.output_properties { - consts.extend(output.r#type.get_consts().clone()); - } - } - for function in package.functions.values() { - for input in &function.input_properties { - consts.extend(input.r#type.get_consts().clone()); - } - for output in &function.output_properties { - consts.extend(output.r#type.get_consts().clone()); - } - } - for type_ in package.types.values() { - if let GlobalType::Object(_, obj) = type_ { - for gtp in obj { - consts.extend(gtp.r#type.get_consts().clone()); - } - } - } - consts - .into_iter() - .map(|s| s.to_case(UpperCamel)) - .sorted() - .collect() -} - -pub(crate) fn get_register_interface(element_id: &ElementId) -> String { - let depth = element_id.namespace.len() + 2; - let prefix = if depth > 0 { - "super::".repeat(depth) - } else { - "self::".to_string() - }; - format!( - "{}bindings::component::pulumi_wasm::register_interface", - prefix - ) -} diff --git a/pulumi_wasm_generator_lib/src/output/combined/wit/dependencies.handlebars b/pulumi_wasm_generator_lib/src/output/combined/wit/dependencies.handlebars deleted file mode 100644 index c6059f707..000000000 --- a/pulumi_wasm_generator_lib/src/output/combined/wit/dependencies.handlebars +++ /dev/null @@ -1,59 +0,0 @@ -package component:pulumi-wasm@{{pulumi_wasm_version}}; - -interface output-interface { - - resource output { - constructor(value: string); - map: func(function-name: string) -> output; - } - combine: func(outputs: list>) -> output; -} - - -interface register-interface { - use output-interface.{output}; - - record object-field { - name: string, - value: borrow - } - - record result-field { - name: string - } - - record register-resource-result-field { - name: string, - output: output - } - - record register-resource-request { - %type: string, - name: string, - object: list, - results: list - } - - record register-resource-result { - fields: list - } - - register: func(request: register-resource-request) -> register-resource-result; - - record resource-invoke-result-field { - name: string, - output: output - } - - record resource-invoke-request { - token: string, - object: list, - results: list - } - - record resource-invoke-result { - fields: list - } - - invoke: func(request: resource-invoke-request) -> resource-invoke-result; -} \ No newline at end of file diff --git a/pulumi_wasm_generator_lib/src/output/combined/wit/mod.rs b/pulumi_wasm_generator_lib/src/output/combined/wit/mod.rs deleted file mode 100644 index 27c6b254b..000000000 --- a/pulumi_wasm_generator_lib/src/output/combined/wit/mod.rs +++ /dev/null @@ -1,114 +0,0 @@ -use crate::output::get_main_version; -use handlebars::Handlebars; - -use serde::Serialize; - -static TEMPLATE: &str = include_str!("wit.handlebars"); -static DEPENDENCIES: &str = include_str!("dependencies.handlebars"); - -#[derive(Serialize)] -struct Argument { - name: String, -} - -#[derive(Serialize)] -struct Result { - name: String, -} - -#[derive(Serialize)] -struct Resources { - name: String, - arguments: Vec, - results: Vec, -} - -#[derive(Serialize)] -struct Function { - name: String, - arguments: Vec, - results: Vec, -} - -#[derive(Serialize)] -struct Package { - name: String, - version: String, - pulumi_wasm_version: String, - resources: Vec, - functions: Vec, -} - -fn convert_model(package: &crate::model::Package) -> Package { - let resources: Vec<_> = package - .resources - .iter() - .map(|(element_id, resource)| Resources { - name: element_id.get_wit_interface_name(), - arguments: resource - .input_properties - .iter() - .map(|input_property| Argument { - name: input_property.get_wit_argument_name(), - }) - .collect(), - results: resource - .output_properties - .iter() - .map(|output_property| Result { - name: output_property.get_wit_argument_name(), - }) - .collect(), - }) - .collect(); - - let functions: Vec<_> = package - .functions - .iter() - .map(|(element_id, resource)| Function { - name: element_id.get_wit_interface_name(), - arguments: resource - .input_properties - .iter() - .map(|input_property| Argument { - name: input_property.get_wit_argument_name(), - }) - .collect(), - results: resource - .output_properties - .iter() - .map(|output_property| Result { - name: output_property.get_wit_argument_name(), - }) - .collect(), - }) - .collect(); - - Package { - name: package.get_wit_name(), - version: package.version.clone(), - pulumi_wasm_version: get_main_version().to_string(), - resources, - functions, - } -} - -pub(crate) fn generate_wit(package: &crate::model::Package) -> anyhow::Result { - let mut data = std::collections::BTreeMap::new(); - data.insert("package", convert_model(package)); - - let reg = Handlebars::new(); - let output = reg.render_template(TEMPLATE, &data)?; - - Ok(output) -} - -pub(crate) fn get_dependencies() -> anyhow::Result { - let mut data = std::collections::BTreeMap::new(); - data.insert("pulumi_wasm_version", get_main_version()); - - let reg = Handlebars::new(); - let output = reg.render_template(DEPENDENCIES, &data)?; - - Ok(output) -} diff --git a/pulumi_wasm_generator_lib/src/output/combined/wit/wit.handlebars b/pulumi_wasm_generator_lib/src/output/combined/wit/wit.handlebars deleted file mode 100644 index f577bd84b..000000000 --- a/pulumi_wasm_generator_lib/src/output/combined/wit/wit.handlebars +++ /dev/null @@ -1,81 +0,0 @@ -package pulumi:{{package.name}}@{{package.version}}--{{package.pulumi_wasm_version}}; - -world {{package.name}}-pulumi { - import component:pulumi-wasm/register-interface@{{package.pulumi_wasm_version}}; -{{#each package.resources}} - export {{name}}; -{{/each}} -{{#each package.functions}} - export {{name}}; -{{/each}} -} - -world {{package.name}}-pulumi-client { -{{#each package.resources}} - import {{name}}; -{{/each}} -{{#each package.functions}} - import {{name}}; -{{/each}} -} - -{{#each package.resources}} -interface {{name}} { - - use component:pulumi-wasm/output-interface@{{@root.package.pulumi_wasm_version}}.{output}; - - {{#if arguments}} - record args { - {{#each arguments}} - {{name}}: borrow, - {{/each}} - } - {{/if}} - - {{#if results}} - record res { - {{#each results}} - {{name}}: output, - {{/each}} - } - {{/if}} - - invoke: func( - name: string, -{{#if arguments}} - args: args -{{/if}} - ){{#if results}} -> res{{/if}}; - -} -{{/each}} - -{{#each package.functions}} -interface {{name}} { - - use component:pulumi-wasm/output-interface@{{@root.package.pulumi_wasm_version}}.{output}; - - {{#if arguments}} - record args { - {{#each arguments}} - {{name}}: borrow, - {{/each}} - } - {{/if}} - - {{#if results}} - record res { - {{#each results}} - {{name}}: output, - {{/each}} - } - {{/if}} - - invoke: func( - {{#if arguments}} - args: args - {{/if}} - ){{#if results}} -> res{{/if}}; - -} -{{/each}} \ No newline at end of file diff --git a/pulumi_wasm_generator_lib/src/output/combined/function_code.rs.handlebars b/pulumi_wasm_generator_lib/src/output/function_code.rs.handlebars similarity index 100% rename from pulumi_wasm_generator_lib/src/output/combined/function_code.rs.handlebars rename to pulumi_wasm_generator_lib/src/output/function_code.rs.handlebars diff --git a/pulumi_wasm_generator_lib/src/output/combined/function_mod.rs.handlebars b/pulumi_wasm_generator_lib/src/output/function_mod.rs.handlebars similarity index 100% rename from pulumi_wasm_generator_lib/src/output/combined/function_mod.rs.handlebars rename to pulumi_wasm_generator_lib/src/output/function_mod.rs.handlebars diff --git a/pulumi_wasm_generator_lib/src/output/combined/functions/function_code.rs.handlebars b/pulumi_wasm_generator_lib/src/output/functions/function_code.rs.handlebars similarity index 100% rename from pulumi_wasm_generator_lib/src/output/combined/functions/function_code.rs.handlebars rename to pulumi_wasm_generator_lib/src/output/functions/function_code.rs.handlebars diff --git a/pulumi_wasm_generator_lib/src/output/combined/functions/mod.rs b/pulumi_wasm_generator_lib/src/output/functions/mod.rs similarity index 92% rename from pulumi_wasm_generator_lib/src/output/combined/functions/mod.rs rename to pulumi_wasm_generator_lib/src/output/functions/mod.rs index 2c3f352e6..e55dde9e3 100644 --- a/pulumi_wasm_generator_lib/src/output/combined/functions/mod.rs +++ b/pulumi_wasm_generator_lib/src/output/functions/mod.rs @@ -1,8 +1,8 @@ use crate::model::{ElementId, Package}; -use crate::output::combined::functions::source_code_function_code::{ +use crate::output::functions::source_code_function_code::{ generate_docs, generate_single_function_source_code, }; -use crate::output::combined::TreeNode; +use crate::output::TreeNode; use crate::utils::reformat_code; use anyhow::Context; use convert_case::{Case, Casing}; @@ -31,7 +31,7 @@ pub(crate) fn generate_function_code(package: &Package, result_path: &std::path: tree.insert(element_id.clone()); } - generate_files(package, &tree, &result_path); + generate_files(package, &tree, result_path); } fn generate_files(package: &Package, tree_node: &TreeNode, current_path: &std::path::Path) { @@ -42,7 +42,7 @@ fn generate_files(package: &Package, tree_node: &TreeNode, current_path: &std::p generate_files(package, node, ¤t_path.join(name)); } - for (function) in functions { + for function in functions { let file_name = format!( "{}.rs", function.get_rust_struct_name().to_case(Case::Snake) @@ -87,7 +87,7 @@ fn generate_module_imports_1( s.push_str("}\n"); } - for (function) in functions { + for function in functions { s.push_str(&format!( "pub mod {}{{\n", function.get_rust_struct_name().to_case(Case::Snake) diff --git a/pulumi_wasm_generator_lib/src/output/combined/functions/source_code_function_code.rs b/pulumi_wasm_generator_lib/src/output/functions/source_code_function_code.rs similarity index 98% rename from pulumi_wasm_generator_lib/src/output/combined/functions/source_code_function_code.rs rename to pulumi_wasm_generator_lib/src/output/functions/source_code_function_code.rs index 05da4957a..8c9a80f5f 100644 --- a/pulumi_wasm_generator_lib/src/output/combined/functions/source_code_function_code.rs +++ b/pulumi_wasm_generator_lib/src/output/functions/source_code_function_code.rs @@ -1,5 +1,5 @@ use crate::model::{ElementId, Type}; -use crate::output::combined::get_register_interface; +use crate::output::get_register_interface; use convert_case::{Case, Casing}; use handlebars::Handlebars; use serde::Serialize; diff --git a/pulumi_wasm_generator_lib/src/output/combined/lib.rs.handlebars b/pulumi_wasm_generator_lib/src/output/lib.rs.handlebars similarity index 100% rename from pulumi_wasm_generator_lib/src/output/combined/lib.rs.handlebars rename to pulumi_wasm_generator_lib/src/output/lib.rs.handlebars diff --git a/pulumi_wasm_generator_lib/src/output/combined/main.rs b/pulumi_wasm_generator_lib/src/output/main.rs similarity index 88% rename from pulumi_wasm_generator_lib/src/output/combined/main.rs rename to pulumi_wasm_generator_lib/src/output/main.rs index b5419cfa6..58dc3ac58 100644 --- a/pulumi_wasm_generator_lib/src/output/combined/main.rs +++ b/pulumi_wasm_generator_lib/src/output/main.rs @@ -1,4 +1,5 @@ -use crate::output::get_main_version; +use crate::output::wit; +use crate::utils::get_main_version; use rinja::Template; #[derive(Template)] @@ -18,7 +19,7 @@ pub(crate) fn generate( types: String, constants: Vec, ) -> anyhow::Result { - let wit = crate::output::wit::get_dependencies()?; + let wit = wit::get_dependencies()?; let file = TemplateModel { functions, diff --git a/pulumi_wasm_generator_lib/src/output/mod.rs b/pulumi_wasm_generator_lib/src/output/mod.rs index 5e628ceb8..e163ad1d6 100644 --- a/pulumi_wasm_generator_lib/src/output/mod.rs +++ b/pulumi_wasm_generator_lib/src/output/mod.rs @@ -1,8 +1,232 @@ -pub(crate) mod combined; -pub(crate) mod provider; -pub(crate) mod rust; +use crate::model::{ElementId, GlobalType, Package}; +use crate::output::types::generate_types_code; +use convert_case::Case::UpperCamel; +use convert_case::{Case, Casing}; +use itertools::Itertools; +use std::collections::{BTreeMap, BTreeSet}; +use std::fs::File; +use std::io::Write; + +pub(crate) mod cargo; +pub(crate) mod functions; +mod main; +pub(crate) mod resources; +pub(crate) mod source_code_librs; +pub(crate) mod types; pub(crate) mod wit; -fn get_main_version() -> String { - env!("CARGO_PKG_VERSION").to_string() +#[derive(Debug)] +enum TreeNode { + Namespace(BTreeMap, BTreeSet), +} + +impl TreeNode { + // Create an empty tree + pub(crate) fn new() -> Self { + TreeNode::Namespace(BTreeMap::new(), BTreeSet::new()) + } + + fn insert(&mut self, element: ElementId) { + self.insert_priv(element, 0); + } + + fn insert_priv(&mut self, element: ElementId, index: usize) { + match self { + TreeNode::Namespace(children, children_elements) => { + if element.namespace.len() == index { + children_elements.insert(element.clone()); + } else { + let next_node = children + .entry(element.namespace[index].clone()) + .or_insert_with(TreeNode::new); + next_node.insert_priv(element, index + 1); + } + } + } + } +} + +pub(crate) fn generate_combined_code( + package: &crate::model::Package, + result_path: &std::path::Path, +) { + generate_files( + package, + &result_path.join("functions"), + &package.functions, + &functions::generate_single_file, + ); + generate_files( + package, + &result_path.join("resources"), + &package.resources, + &resources::generate_single_file, + ); + generate_types_code(package, &result_path.join("types")); + + let main = main::generate( + generate_includes( + package, + "functions", + &package.functions, + &functions::generate_single_file_docs, + ), + generate_includes( + package, + "resources", + &package.resources, + &resources::generate_single_file_docs, + ), + types::generate_module_imports(package), + find_consts(package), + ) + .unwrap(); + + std::fs::write(result_path.join("main.rs"), main).unwrap(); +} + +fn generate_files( + package: &Package, + result_path: &std::path::Path, + objects: &BTreeMap, + generator: &(impl Fn(&Package, &ElementId) -> String + 'static), +) { + if objects.is_empty() { + return; + } + let mut tree = TreeNode::new(); + + for element_id in objects.keys() { + tree.insert(element_id.clone()); + } + + generate_files_looper(package, &tree, result_path, &Box::new(&generator)); +} + +fn generate_files_looper( + package: &Package, + tree_node: &TreeNode, + current_path: &std::path::Path, + generator: &dyn Fn(&Package, &ElementId) -> String, +) { + match tree_node { + TreeNode::Namespace(ns, types) => { + std::fs::create_dir_all(current_path).unwrap(); + for (name, node) in ns { + generate_files_looper(package, node, ¤t_path.join(name), generator); + } + + for type_ in types { + let file_name = format!("{}.rs", type_.get_rust_struct_name().to_case(Case::Snake)); + let content = generator(package, type_); + let mut file = File::create(current_path.join(file_name)).unwrap(); + file.write_all(content.as_bytes()).unwrap(); + } + } + } +} + +fn generate_includes( + package: &Package, + name: &str, + objects: &BTreeMap, + generator: &(impl Fn(&Package, &ElementId) -> Vec + 'static), +) -> String { + if objects.is_empty() { + return "".to_string(); + } + let mut tree = TreeNode::new(); + + for element_id in objects.keys() { + tree.insert(element_id.clone()); + } + + generate_includes_looper(package, &tree, std::path::Path::new(name), generator) +} + +fn generate_includes_looper( + package: &Package, + tree_node: &TreeNode, + current_path: &std::path::Path, + generator: &dyn Fn(&Package, &ElementId) -> Vec, +) -> 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_includes_looper( + package, + node, + ¤t_path.join(name), + generator, + )); + s.push_str("}\n"); + } + + for function in functions { + for line in generator(package, function) { + s.push_str(&format!("/// {}\n", line)); + } + 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 + } + } +} + +fn find_consts(package: &crate::model::Package) -> Vec { + let mut consts = BTreeSet::new(); + for resource in package.resources.values() { + for input in &resource.input_properties { + consts.extend(input.r#type.get_consts().clone()); + } + for output in &resource.output_properties { + consts.extend(output.r#type.get_consts().clone()); + } + } + for function in package.functions.values() { + for input in &function.input_properties { + consts.extend(input.r#type.get_consts().clone()); + } + for output in &function.output_properties { + consts.extend(output.r#type.get_consts().clone()); + } + } + for type_ in package.types.values() { + if let GlobalType::Object(_, obj) = type_ { + for gtp in obj { + consts.extend(gtp.r#type.get_consts().clone()); + } + } + } + consts + .into_iter() + .map(|s| s.to_case(UpperCamel)) + .sorted() + .collect() +} + +pub(crate) fn get_register_interface(element_id: &ElementId) -> String { + let depth = element_id.namespace.len() + 2; + let prefix = if depth > 0 { + "super::".repeat(depth) + } else { + "self::".to_string() + }; + format!( + "{}bindings::component::pulumi_wasm::register_interface", + prefix + ) } diff --git a/pulumi_wasm_generator_lib/src/output/provider/Cargo.toml.handlebars b/pulumi_wasm_generator_lib/src/output/provider/Cargo.toml.handlebars deleted file mode 100644 index 6bb575bf8..000000000 --- a/pulumi_wasm_generator_lib/src/output/provider/Cargo.toml.handlebars +++ /dev/null @@ -1,23 +0,0 @@ -[package] -name = "pulumi_wasm_{{package.name}}_provider" -version = "{{package.version}}-{{package.pulumi_wasm_version}}" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[lib] -crate-type = ["cdylib"] - -[dependencies] -wit-bindgen-rt.workspace = true -pulumi_wasm_common.workspace = true - -[package.metadata.component] -package = "pulumi:{{package.name}}" - -[package.metadata.component.target] -path = "wit" -world = "{{package.name}}-pulumi" - -[package.metadata.component.target.dependencies] -"component:pulumi-wasm" = { path = "wit/deps/pulumi-wasm.wit" } diff --git a/pulumi_wasm_generator_lib/src/output/provider/cargo.rs b/pulumi_wasm_generator_lib/src/output/provider/cargo.rs deleted file mode 100644 index 61a3fb4ec..000000000 --- a/pulumi_wasm_generator_lib/src/output/provider/cargo.rs +++ /dev/null @@ -1,29 +0,0 @@ -use crate::output::get_main_version; -use handlebars::Handlebars; -use serde::Serialize; -use serde_json::json; - -static TEMPLATE: &str = include_str!("Cargo.toml.handlebars"); - -#[derive(Serialize)] -struct Package { - name: String, - version: String, - pulumi_wasm_version: String, -} - -fn convert_model(package: &crate::model::Package) -> Package { - Package { - name: package.name.clone(), - version: package.version.clone(), - pulumi_wasm_version: get_main_version().to_string(), - } -} - -pub(crate) fn generate_cargo(package: &crate::model::Package) -> String { - let package = convert_model(package); - let handlebars = Handlebars::new(); - handlebars - .render_template(TEMPLATE, &json!({"package": &package})) - .unwrap() -} diff --git a/pulumi_wasm_generator_lib/src/output/provider/function_code.rs.handlebars b/pulumi_wasm_generator_lib/src/output/provider/function_code.rs.handlebars deleted file mode 100644 index 800146d65..000000000 --- a/pulumi_wasm_generator_lib/src/output/provider/function_code.rs.handlebars +++ /dev/null @@ -1,39 +0,0 @@ -use std::collections::HashMap; -use crate::bindings::exports::pulumi::{{interface.package_name}}::{{interface.name}}; -use crate::bindings::component::pulumi_wasm::register_interface::{ObjectField, invoke, ResourceInvokeRequest, ResultField}; -use crate::Component; - -impl {{interface.name}}::Guest for Component { - fn invoke( -{{#if interface.input_properties}} - args: {{interface.name}}::Args -{{/if}} - ){{#if interface.output_properties}} -> {{interface.name}}::Res{{/if}} { - pulumi_wasm_common::setup_logger(); - let request = ResourceInvokeRequest { - token: "{{interface.type}}".into(), - object: vec![ - {{#each interface.input_properties}} - ObjectField { name: "{{name}}".into(), value: args.{{arg_name}} }, - {{/each}} - ], - results: vec![ - {{#each interface.output_properties}} - ResultField { name: "{{name}}".into() }, - {{/each}} - ], - }; - - {{#if interface.output_properties}} - let o = invoke(&request); - let mut hashmap: HashMap = o.fields.into_iter().map(|f| (f.name, f.output)).collect(); - {{interface.name}}::Res { - {{#each interface.output_properties}} - {{arg_name}}: hashmap.remove("{{name}}").unwrap(), - {{/each}} - } - {{else}} - invoke(&request); - {{/if}} - } -} diff --git a/pulumi_wasm_generator_lib/src/output/provider/function_mod.rs.handlebars b/pulumi_wasm_generator_lib/src/output/provider/function_mod.rs.handlebars deleted file mode 100644 index aac055a22..000000000 --- a/pulumi_wasm_generator_lib/src/output/provider/function_mod.rs.handlebars +++ /dev/null @@ -1,3 +0,0 @@ -{{#each package.interfaces as |interface|}} -pub mod {{interface.name}}; -{{/each}} \ No newline at end of file diff --git a/pulumi_wasm_generator_lib/src/output/provider/lib.rs.handlebars b/pulumi_wasm_generator_lib/src/output/provider/lib.rs.handlebars deleted file mode 100644 index 3bcf99a9e..000000000 --- a/pulumi_wasm_generator_lib/src/output/provider/lib.rs.handlebars +++ /dev/null @@ -1,16 +0,0 @@ -{{#if package.contains_resources}} -mod resource; -{{/if}} -{{#if package.contains_functions}} -mod function; -{{/if}} - -{{#if package.contains_resources_or_functions}} -#[allow(unused_braces)] -#[allow(unused_imports)] -#[allow(static_mut_refs)] -mod bindings; -bindings::export!(Component with_types_in bindings); -{{/if}} - -struct Component {} diff --git a/pulumi_wasm_generator_lib/src/output/provider/mod.rs b/pulumi_wasm_generator_lib/src/output/provider/mod.rs deleted file mode 100644 index 811c91be3..000000000 --- a/pulumi_wasm_generator_lib/src/output/provider/mod.rs +++ /dev/null @@ -1,6 +0,0 @@ -pub(crate) mod cargo; -pub(crate) mod source_code_function_code; -pub(crate) mod source_code_function_mod; -pub(crate) mod source_code_librs; -pub(crate) mod source_code_resource_code; -pub(crate) mod source_code_resource_mod; diff --git a/pulumi_wasm_generator_lib/src/output/provider/resource_code.rs.handlebars b/pulumi_wasm_generator_lib/src/output/provider/resource_code.rs.handlebars deleted file mode 100644 index 9d618e7c8..000000000 --- a/pulumi_wasm_generator_lib/src/output/provider/resource_code.rs.handlebars +++ /dev/null @@ -1,41 +0,0 @@ -use std::collections::HashMap; -use crate::bindings::exports::pulumi::{{interface.package_name}}::{{interface.name}}; -use crate::bindings::component::pulumi_wasm::register_interface::{ObjectField, register, RegisterResourceRequest, ResultField}; -use crate::Component; - -impl {{interface.name}}::Guest for Component { - fn invoke( - name: String, -{{#if interface.input_properties}} - args: {{interface.name}}::Args -{{/if}} - ){{#if interface.output_properties}} -> {{interface.name}}::Res{{/if}} { - pulumi_wasm_common::setup_logger(); - let request = RegisterResourceRequest { - type_: "{{interface.type}}".into(), - name, - object: vec![ - {{#each interface.input_properties}} - ObjectField { name: "{{name}}".into(), value: args.{{arg_name}} }, - {{/each}} - ], - results: vec![ - {{#each interface.output_properties}} - ResultField { name: "{{name}}".into() }, - {{/each}} - ], - }; - - {{#if interface.output_properties}} - let o = register(&request); - let mut hashmap: HashMap = o.fields.into_iter().map(|f| (f.name, f.output)).collect(); - {{interface.name}}::Res { - {{#each interface.output_properties}} - {{arg_name}}: hashmap.remove("{{name}}").unwrap(), - {{/each}} - } - {{else}} - register(&request); - {{/if}} - } -} diff --git a/pulumi_wasm_generator_lib/src/output/provider/resource_mod.rs.handlebars b/pulumi_wasm_generator_lib/src/output/provider/resource_mod.rs.handlebars deleted file mode 100644 index aac055a22..000000000 --- a/pulumi_wasm_generator_lib/src/output/provider/resource_mod.rs.handlebars +++ /dev/null @@ -1,3 +0,0 @@ -{{#each package.interfaces as |interface|}} -pub mod {{interface.name}}; -{{/each}} \ No newline at end of file diff --git a/pulumi_wasm_generator_lib/src/output/provider/source_code_function_code.rs b/pulumi_wasm_generator_lib/src/output/provider/source_code_function_code.rs deleted file mode 100644 index f2d0df990..000000000 --- a/pulumi_wasm_generator_lib/src/output/provider/source_code_function_code.rs +++ /dev/null @@ -1,75 +0,0 @@ -use handlebars::Handlebars; -use std::collections::HashMap; -use std::path::PathBuf; - -use serde::Serialize; -use serde_json::json; - -static TEMPLATE: &str = include_str!("function_code.rs.handlebars"); - -#[derive(Serialize)] -struct InputProperty { - name: String, - arg_name: String, -} - -#[derive(Serialize)] -struct OutputProperty { - name: String, - arg_name: String, -} - -#[derive(Serialize)] -struct Interface { - package_name: String, - name: String, - r#type: String, - input_properties: Vec, - output_properties: Vec, -} - -fn convert_model(package: &crate::model::Package) -> Vec { - package - .functions - .iter() - .map(|(element_id, function)| Interface { - package_name: package.name.clone().replace('-', "_"), - name: element_id.get_rust_namespace_name(), - r#type: element_id.raw.clone(), - input_properties: function - .input_properties - .iter() - .map(|input_property| InputProperty { - name: input_property.name.clone(), - arg_name: input_property.get_rust_argument_name(), - }) - .collect(), - output_properties: function - .output_properties - .iter() - .map(|output_property| OutputProperty { - name: output_property.name.clone(), - arg_name: output_property.get_rust_argument_name(), - }) - .collect(), - }) - .collect() -} - -pub(crate) fn generate_source_code(package: &crate::model::Package) -> HashMap { - let handlebars = Handlebars::new(); - let interfaces = convert_model(package); - - interfaces - .iter() - .map(|interface| { - let rendered_file = handlebars - .render_template(TEMPLATE, &json!({"interface": interface})) - .unwrap(); - ( - PathBuf::from(format!("{}.rs", interface.name)), - rendered_file, - ) - }) - .collect() -} diff --git a/pulumi_wasm_generator_lib/src/output/provider/source_code_function_mod.rs b/pulumi_wasm_generator_lib/src/output/provider/source_code_function_mod.rs deleted file mode 100644 index b602b6ba1..000000000 --- a/pulumi_wasm_generator_lib/src/output/provider/source_code_function_mod.rs +++ /dev/null @@ -1,42 +0,0 @@ -use handlebars::Handlebars; -use serde::Serialize; -use serde_json::json; - -static TEMPLATE: &str = include_str!("resource_mod.rs.handlebars"); - -#[derive(Serialize)] -struct Interface { - name: String, -} - -#[derive(Serialize)] -struct Package { - name: String, - interfaces: Vec, -} - -fn convert_model(package: &crate::model::Package) -> Package { - Package { - name: package.name.clone(), - interfaces: package - .functions - .keys() - .map(|element_id| Interface { - name: element_id.get_rust_namespace_name(), - }) - .collect(), - } -} - -pub(crate) fn generate_source_code(package: &crate::model::Package) -> String { - let package = convert_model(package); - - let content = { - let handlebars = Handlebars::new(); - handlebars - .render_template(TEMPLATE, &json!({"package": &package})) - .unwrap() - }; - - content -} diff --git a/pulumi_wasm_generator_lib/src/output/provider/source_code_librs.rs b/pulumi_wasm_generator_lib/src/output/provider/source_code_librs.rs deleted file mode 100644 index 137308e35..000000000 --- a/pulumi_wasm_generator_lib/src/output/provider/source_code_librs.rs +++ /dev/null @@ -1,33 +0,0 @@ -use handlebars::Handlebars; - -use serde::Serialize; -use serde_json::json; - -static TEMPLATE: &str = include_str!("lib.rs.handlebars"); - -#[derive(Serialize)] -struct Package { - name: String, - name_escaped: String, - contains_resources: bool, - contains_functions: bool, - contains_resources_or_functions: bool, -} - -fn convert_model(package: &crate::model::Package) -> Package { - Package { - name: package.name.clone(), - name_escaped: package.name.clone().replace('-', "_"), - contains_resources: !package.resources.is_empty() || !package.functions.is_empty(), - contains_functions: !package.functions.is_empty(), - contains_resources_or_functions: !package.resources.is_empty() - || !package.functions.is_empty(), - } -} - -pub(crate) fn generate_source_code(package: &crate::model::Package) -> String { - let handlebars = Handlebars::new(); - handlebars - .render_template(TEMPLATE, &json!({"package": &convert_model(package)})) - .unwrap() -} diff --git a/pulumi_wasm_generator_lib/src/output/provider/source_code_resource_code.rs b/pulumi_wasm_generator_lib/src/output/provider/source_code_resource_code.rs deleted file mode 100644 index ad6f5c954..000000000 --- a/pulumi_wasm_generator_lib/src/output/provider/source_code_resource_code.rs +++ /dev/null @@ -1,75 +0,0 @@ -use handlebars::Handlebars; -use std::collections::HashMap; -use std::path::PathBuf; - -use serde::Serialize; -use serde_json::json; - -static TEMPLATE: &str = include_str!("resource_code.rs.handlebars"); - -#[derive(Serialize)] -struct InputProperty { - name: String, - arg_name: String, -} - -#[derive(Serialize)] -struct OutputProperty { - name: String, - arg_name: String, -} - -#[derive(Serialize)] -struct Interface { - package_name: String, - name: String, - r#type: String, - input_properties: Vec, - output_properties: Vec, -} - -fn convert_model(package: &crate::model::Package) -> Vec { - package - .resources - .iter() - .map(|(element_id, resource)| Interface { - package_name: package.name.clone().replace('-', "_"), - name: element_id.get_rust_namespace_name(), - r#type: element_id.raw.clone(), - input_properties: resource - .input_properties - .iter() - .map(|input_property| InputProperty { - name: input_property.name.clone(), - arg_name: input_property.get_rust_argument_name(), - }) - .collect(), - output_properties: resource - .output_properties - .iter() - .map(|output_property| OutputProperty { - name: output_property.name.clone(), - arg_name: output_property.get_rust_argument_name(), - }) - .collect(), - }) - .collect() -} - -pub(crate) fn generate_source_code(package: &crate::model::Package) -> HashMap { - let handlebars = Handlebars::new(); - let interfaces = convert_model(package); - - interfaces - .iter() - .map(|interface| { - let rendered_file = handlebars - .render_template(TEMPLATE, &json!({"interface": interface})) - .unwrap(); - ( - PathBuf::from(format!("{}.rs", interface.name)), - rendered_file, - ) - }) - .collect() -} diff --git a/pulumi_wasm_generator_lib/src/output/provider/source_code_resource_mod.rs b/pulumi_wasm_generator_lib/src/output/provider/source_code_resource_mod.rs deleted file mode 100644 index ac5629f45..000000000 --- a/pulumi_wasm_generator_lib/src/output/provider/source_code_resource_mod.rs +++ /dev/null @@ -1,42 +0,0 @@ -use handlebars::Handlebars; -use serde::Serialize; -use serde_json::json; - -static TEMPLATE: &str = include_str!("resource_mod.rs.handlebars"); - -#[derive(Serialize)] -struct Interface { - name: String, -} - -#[derive(Serialize)] -struct Package { - name: String, - interfaces: Vec, -} - -fn convert_model(package: &crate::model::Package) -> Package { - Package { - name: package.name.clone(), - interfaces: package - .resources - .keys() - .map(|element_id| Interface { - name: element_id.get_rust_namespace_name(), - }) - .collect(), - } -} - -pub(crate) fn generate_source_code(package: &crate::model::Package) -> String { - let package = convert_model(package); - - let content = { - let handlebars = Handlebars::new(); - handlebars - .render_template(TEMPLATE, &json!({"package": &package})) - .unwrap() - }; - - content -} diff --git a/pulumi_wasm_generator_lib/src/output/combined/resources/mod.rs b/pulumi_wasm_generator_lib/src/output/resources/mod.rs similarity index 93% rename from pulumi_wasm_generator_lib/src/output/combined/resources/mod.rs rename to pulumi_wasm_generator_lib/src/output/resources/mod.rs index cbe3b0a55..7f3ef3a98 100644 --- a/pulumi_wasm_generator_lib/src/output/combined/resources/mod.rs +++ b/pulumi_wasm_generator_lib/src/output/resources/mod.rs @@ -1,10 +1,10 @@ mod source_code_resource_code; use crate::model::{ElementId, Package}; -use crate::output::combined::resources::source_code_resource_code::{ +use crate::output::resources::source_code_resource_code::{ generate_docs, generate_single_resource_source_code, }; -use crate::output::combined::TreeNode; +use crate::output::TreeNode; use crate::utils::reformat_code; use anyhow::Context; use convert_case::{Case, Casing}; @@ -36,7 +36,7 @@ pub(crate) fn generate_resources_code( println!("Tree {:?}", tree); - generate_files(package, &tree, &result_path); + generate_files(package, &tree, result_path); } fn generate_files( @@ -51,7 +51,7 @@ fn generate_files( generate_files(package, node, ¤t_path.join(name)); } - for (resource) in resources { + for resource in resources { let file_name = format!( "{}.rs", resource.get_rust_struct_name().to_case(Case::Snake) @@ -96,7 +96,7 @@ fn generate_module_imports_1( s.push_str("}\n"); } - for (function) in functions { + for function in functions { s.push_str(&format!( "pub mod {}{{\n", function.get_rust_struct_name().to_case(Case::Snake) diff --git a/pulumi_wasm_generator_lib/src/output/combined/resources/resource_code.rs.handlebars b/pulumi_wasm_generator_lib/src/output/resources/resource_code.rs.handlebars similarity index 100% rename from pulumi_wasm_generator_lib/src/output/combined/resources/resource_code.rs.handlebars rename to pulumi_wasm_generator_lib/src/output/resources/resource_code.rs.handlebars diff --git a/pulumi_wasm_generator_lib/src/output/combined/resources/source_code_resource_code.rs b/pulumi_wasm_generator_lib/src/output/resources/source_code_resource_code.rs similarity index 98% rename from pulumi_wasm_generator_lib/src/output/combined/resources/source_code_resource_code.rs rename to pulumi_wasm_generator_lib/src/output/resources/source_code_resource_code.rs index 2ff48147d..a70b389e3 100644 --- a/pulumi_wasm_generator_lib/src/output/combined/resources/source_code_resource_code.rs +++ b/pulumi_wasm_generator_lib/src/output/resources/source_code_resource_code.rs @@ -1,5 +1,5 @@ use crate::model::{ElementId, Type}; -use crate::output::combined::get_register_interface; +use crate::output::get_register_interface; use handlebars::Handlebars; use serde::Serialize; use serde_json::json; diff --git a/pulumi_wasm_generator_lib/src/output/rust/Cargo.toml.handlebars b/pulumi_wasm_generator_lib/src/output/rust/Cargo.toml.handlebars deleted file mode 100644 index 0fd438121..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/Cargo.toml.handlebars +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "pulumi_wasm_{{package.name}}" -version = "{{package.version}}-{{package.pulumi_wasm_version}}" -edition = "2021" - -[dependencies] -wit-bindgen.workspace = true -pulumi_wasm_rust.workspace = true -serde.workspace = true -pulumi_wasm_wit = { workspace = true, features = ["client"] } -bon.workspace = true -pulumi_wasm_provider_common.workspace = true diff --git a/pulumi_wasm_generator_lib/src/output/rust/cargo.rs b/pulumi_wasm_generator_lib/src/output/rust/cargo.rs deleted file mode 100644 index 61a3fb4ec..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/cargo.rs +++ /dev/null @@ -1,29 +0,0 @@ -use crate::output::get_main_version; -use handlebars::Handlebars; -use serde::Serialize; -use serde_json::json; - -static TEMPLATE: &str = include_str!("Cargo.toml.handlebars"); - -#[derive(Serialize)] -struct Package { - name: String, - version: String, - pulumi_wasm_version: String, -} - -fn convert_model(package: &crate::model::Package) -> Package { - Package { - name: package.name.clone(), - version: package.version.clone(), - pulumi_wasm_version: get_main_version().to_string(), - } -} - -pub(crate) fn generate_cargo(package: &crate::model::Package) -> String { - let package = convert_model(package); - let handlebars = Handlebars::new(); - handlebars - .render_template(TEMPLATE, &json!({"package": &package})) - .unwrap() -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/function_code.rs.handlebars b/pulumi_wasm_generator_lib/src/output/rust/function_code.rs.handlebars deleted file mode 100644 index cddca49df..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/function_code.rs.handlebars +++ /dev/null @@ -1,60 +0,0 @@ -{{#each interface.description_lines}} -//! {{&this}} -{{/each}} - -{{#if interface.input_properties}} -#[derive(bon::Builder, Clone)] -#[builder(finish_fn = build_struct)] -pub struct {{interface.struct_name}}Args { -{{#each interface.input_properties}} - {{#each description_lines}} - /// {{&this}} - {{/each}} - {{#if default}} - #[builder(into, default)] - {{else if skip}} - #[builder(skip)] - {{else}} - #[builder(into)] - {{/if}} - {{#unless private}}pub {{/unless}}{{arg_name}}: pulumi_wasm_rust::Output<{{&type_}}>, -{{/each}} -} -{{/if}} - -pub struct {{interface.struct_name}}Result { -{{#each interface.output_properties}} - {{#each description_lines}} - /// {{&this}} - {{/each}} - pub {{arg_name}}: pulumi_wasm_rust::Output<{{&type_}}>, -{{/each}} -} - -/// -/// Registers a new resource with the given unique name and arguments -/// -pub fn invoke( -{{#if interface.input_properties}} - args: {{interface.struct_name}}Args -{{/if}} -) -> {{interface.struct_name}}Result { - - let result = crate::bindings::pulumi::{{package_name}}::{{interface.name}}::invoke( -{{#if interface.input_properties}} - &crate::bindings::pulumi::{{package_name}}::{{interface.name}}::Args { - {{#each interface.input_properties}} - {{arg_name}}: &args.{{arg_name}}.get_inner(), - {{/each}} - } -{{/if}} - ); - - let _ = &result; - - {{interface.struct_name}}Result { - {{#each interface.output_properties}} - {{arg_name}}: crate::into_domain(result.{{arg_name}}), - {{/each}} - } -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/function_mod.rs.handlebars b/pulumi_wasm_generator_lib/src/output/rust/function_mod.rs.handlebars deleted file mode 100644 index f81cb278e..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/function_mod.rs.handlebars +++ /dev/null @@ -1,3 +0,0 @@ -{{#each package.functions as |function|}} -pub mod {{function.name}}; -{{/each}} \ No newline at end of file diff --git a/pulumi_wasm_generator_lib/src/output/rust/functions/function_code.rs.handlebars b/pulumi_wasm_generator_lib/src/output/rust/functions/function_code.rs.handlebars deleted file mode 100644 index ecca1b5be..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/functions/function_code.rs.handlebars +++ /dev/null @@ -1,58 +0,0 @@ -{{#each function.description_lines}} -//! {{&this}} -{{/each}} - -{{#if function.input_properties}} -#[derive(bon::Builder, Clone)] -#[builder(finish_fn = build_struct)] -pub struct {{function.struct_name}}Args { -{{#each function.input_properties}} - {{#each description_lines}} - /// {{&this}} - {{/each}} - {{#if default}} - #[builder(into, default)] - {{else if skip}} - #[builder(skip)] - {{else}} - #[builder(into)] - {{/if}} - {{#unless private}}pub {{/unless}}{{arg_name}}: pulumi_wasm_rust::Output<{{&type_}}>, -{{/each}} -} -{{/if}} - -pub struct {{function.struct_name}}Result { -{{#each function.output_properties}} - {{#each description_lines}} - /// {{&this}} - {{/each}} - pub {{arg_name}}: pulumi_wasm_rust::Output<{{&type_}}>, -{{/each}} -} - -/// -/// Registers a new resource with the given unique name and arguments -/// -pub fn invoke( -{{#if function.input_properties}} - args: {{function.struct_name}}Args -{{/if}} -) -> {{function.struct_name}}Result { - - let result = crate::bindings::pulumi::{{function.package_name}}::{{function.name}}::invoke( -{{#if function.input_properties}} - &crate::bindings::pulumi::{{function.package_name}}::{{function.name}}::Args { - {{#each function.input_properties}} - {{arg_name}}: &args.{{arg_name}}.get_inner(), - {{/each}} - } -{{/if}} - ); - - {{function.struct_name}}Result { - {{#each function.output_properties}} - {{arg_name}}: crate::into_domain(result.{{arg_name}}), - {{/each}} - } -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/functions/mod.rs b/pulumi_wasm_generator_lib/src/output/rust/functions/mod.rs deleted file mode 100644 index dd49ef1e1..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/functions/mod.rs +++ /dev/null @@ -1,46 +0,0 @@ -use crate::model::Package; -use crate::output::rust::functions::source_code_function_code::generate_single_function_source_code; -use crate::output::rust::functions::source_code_function_mod::generate_mod; -use crate::output::rust::TreeNode; -use convert_case::{Case, Casing}; -use std::fs::File; -use std::io::Write; - -mod source_code_function_code; -mod source_code_function_mod; - -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()); - } - - let root = result_path.join("src").join("function"); - - generate_files(package, &tree, &root); -} - -fn generate_files(package: &Package, tree_node: &TreeNode, current_path: &std::path::Path) { - match tree_node { - TreeNode::Namespace(ns) => { - for (name, node) in ns { - generate_files(package, node, ¤t_path.join(name)); - } - let content = generate_mod(ns); - let mut file = File::create(current_path.join("mod.rs")).unwrap(); - file.write_all(content.as_bytes()).unwrap(); - } - TreeNode::Leaf(ref leaf) => { - let file_name = format!("{}.rs", leaf.get_rust_struct_name().to_case(Case::Snake)); - let content = generate_single_function_source_code(package, leaf); - let dir = current_path.parent().unwrap(); - std::fs::create_dir_all(dir).unwrap(); - let mut file = File::create(current_path.parent().unwrap().join(file_name)).unwrap(); - file.write_all(content.as_bytes()).unwrap(); - } - } -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/functions/source_code_function_code.rs b/pulumi_wasm_generator_lib/src/output/rust/functions/source_code_function_code.rs deleted file mode 100644 index da3e344b9..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/functions/source_code_function_code.rs +++ /dev/null @@ -1,96 +0,0 @@ -use crate::model::{ElementId, Type}; -use convert_case::{Case, Casing}; -use handlebars::Handlebars; -use serde::Serialize; -use serde_json::json; - -static TEMPLATE: &str = include_str!("function_code.rs.handlebars"); - -#[derive(Serialize)] -struct InputProperty { - name: String, - arg_name: String, - type_: String, - description_lines: Vec, - default: bool, - skip: bool, - private: bool, -} - -#[derive(Serialize)] -struct OutputProperty { - name: String, - arg_name: String, - type_: String, - description_lines: Vec, -} - -#[derive(Serialize)] -struct Function { - name: String, - package_name: String, - input_properties: Vec, - output_properties: Vec, - struct_name: String, - function_name: String, - description_lines: Vec, -} - -fn convert_function(package: &crate::model::Package, element_id: &ElementId) -> Function { - let function = package.functions.get(element_id).unwrap(); - let depth = element_id.namespace.len(); - Function { - name: element_id.get_rust_namespace_name(), - package_name: package.name.clone().replace("-", "_"), - struct_name: element_id.name.clone().to_case(Case::Pascal), - function_name: element_id.get_rust_function_name(), - description_lines: crate::utils::to_lines( - function.description.clone(), - package, - Some(element_id.clone()), - ), - input_properties: function - .input_properties - .iter() - .map(|input_property| InputProperty { - name: input_property.name.clone(), - arg_name: input_property.get_rust_argument_name(), - default: matches!(input_property.r#type, Type::Option(_)), - skip: matches!(input_property.r#type, Type::ConstString(_)), - private: matches!(input_property.r#type, Type::ConstString(_)), - type_: input_property.r#type.get_rust_type(depth), - description_lines: crate::utils::to_lines( - input_property.description.clone(), - package, - None, - ), - }) - .collect(), - output_properties: function - .output_properties - .iter() - .map(|output_property| OutputProperty { - name: output_property.name.clone(), - arg_name: output_property.get_rust_argument_name(), - type_: output_property.r#type.get_rust_type(depth), - description_lines: crate::utils::to_lines( - output_property.description.clone(), - package, - None, - ), - }) - .collect(), - } -} - -pub(crate) fn generate_single_function_source_code( - package: &crate::model::Package, - element_id: &ElementId, -) -> String { - let handlebars = Handlebars::new(); - let function = convert_function(package, element_id); - - handlebars - .render_template(TEMPLATE, &json!({"function": function})) - .unwrap() -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/functions/source_code_function_mod.rs b/pulumi_wasm_generator_lib/src/output/rust/functions/source_code_function_mod.rs deleted file mode 100644 index 9c6c33e77..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/functions/source_code_function_mod.rs +++ /dev/null @@ -1,32 +0,0 @@ -use crate::output::rust::TreeNode; -use convert_case::{Case, Casing}; -use rinja::Template; -use std::collections::BTreeMap; - -#[derive(Template)] -#[template(path = "function_mod.rs.jinja")] -struct TemplateModel { - files: Vec, - directories: Vec, -} - -pub(crate) fn generate_mod(tree_nodes: &BTreeMap) -> String { - let mut files = Vec::new(); - let mut directories = Vec::new(); - - for (name, tree_node) in tree_nodes { - match tree_node { - TreeNode::Namespace(_) => directories.push(name.clone()), - TreeNode::Leaf(l) => { - files.push(l.get_rust_struct_name().to_case(Case::Snake)); - } - } - } - - TemplateModel { files, directories } - .render() - .unwrap() - .trim() - .to_string() - + "\n" -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/lib.rs.handlebars b/pulumi_wasm_generator_lib/src/output/rust/lib.rs.handlebars deleted file mode 100644 index bef062cc0..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/lib.rs.handlebars +++ /dev/null @@ -1,38 +0,0 @@ -use pulumi_wasm_rust::Output; -{{#if package.contains_elements}} -use pulumi_wasm_wit::client_bindings::component::pulumi_wasm::output_interface::Output as WitOutput; -{{/if}} -{{#if package.contains_resources}} -#[allow(clippy::doc_lazy_continuation, clippy::tabs_in_doc_comments)] -mod resource; -pub use resource::*; -{{/if}} -{{#if package.contains_functions}} -#[allow(clippy::doc_lazy_continuation, clippy::tabs_in_doc_comments)] -pub mod function; -{{/if}} -{{#if package.contains_types}} -#[allow(clippy::doc_lazy_continuation, clippy::tabs_in_doc_comments)] -pub mod types; -{{/if}} - -mod bindings { - wit_bindgen::generate!({ - // the name of the world in the `*.wit` input file - world: "{{package.name}}-pulumi-client", -{{#if package.contains_elements}} - with: { - "component:pulumi-wasm/output-interface@{{package.pulumi_wasm_version}}": pulumi_wasm_wit::client_bindings::component::pulumi_wasm::output_interface - } -{{/if}} - }); -} - -{{#if package.contains_elements}} -fn into_domain(output: WitOutput) -> Output { - unsafe { Output::::new_from_handle(output) } -} -{{/if}} -{{#each package.const_strings}} -pulumi_wasm_provider_common::generate_string_const!(ConstString{{&this}}, "{{&this}}"); -{{/each}} \ No newline at end of file diff --git a/pulumi_wasm_generator_lib/src/output/rust/mod.rs b/pulumi_wasm_generator_lib/src/output/rust/mod.rs deleted file mode 100644 index 7fa28396d..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/mod.rs +++ /dev/null @@ -1,44 +0,0 @@ -use crate::model::ElementId; -use std::collections::BTreeMap; - -pub(crate) mod cargo; -pub(crate) mod functions; -pub(crate) mod resources; -pub(crate) mod source_code_librs; -pub(crate) mod types; - -#[derive(Debug)] -enum TreeNode { - Namespace(BTreeMap), - Leaf(ElementId), -} - -impl TreeNode { - // Create an empty tree - pub(crate) fn new() -> Self { - TreeNode::Namespace(BTreeMap::new()) - } - - fn insert(&mut self, element: ElementId) { - self.insert_priv(element, 0); - } - - fn insert_priv(&mut self, element: ElementId, index: usize) { - match self { - TreeNode::Namespace(children) => { - if element.namespace.len() == index { - if children.contains_key(&element.name) { - panic!("Duplicate element: {:?}", element); // Replace with error handling code - } - children.insert(element.name.clone(), TreeNode::Leaf(element.clone())); - } else { - let next_node = children - .entry(element.namespace[index].clone()) - .or_insert_with(TreeNode::new); - next_node.insert_priv(element, index + 1); - } - } - TreeNode::Leaf(_) => panic!("Cannot insert into a leaf node!"), - } - } -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/resources/mod.rs b/pulumi_wasm_generator_lib/src/output/rust/resources/mod.rs deleted file mode 100644 index 69b214643..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/resources/mod.rs +++ /dev/null @@ -1,52 +0,0 @@ -mod source_code_resource_code; -mod source_code_resource_mod; - -use crate::output::rust::resources::source_code_resource_code::generate_single_resource_source_code; -use crate::output::rust::resources::source_code_resource_mod::generate_mod; -use crate::output::rust::TreeNode; -use convert_case::{Case, Casing}; -use std::fs::File; -use std::io::Write; - -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()); - } - - let root = result_path.join("src").join("resource"); - - generate_files(package, &tree, &root); -} - -fn generate_files( - package: &crate::model::Package, - tree_node: &TreeNode, - current_path: &std::path::Path, -) { - match tree_node { - TreeNode::Namespace(ns) => { - for (name, node) in ns { - generate_files(package, node, ¤t_path.join(name)); - } - let content = generate_mod(ns); - let mut file = File::create(current_path.join("mod.rs")).unwrap(); - file.write_all(content.as_bytes()).unwrap(); - } - TreeNode::Leaf(ref leaf) => { - let file_name = format!("{}.rs", leaf.get_rust_struct_name().to_case(Case::Snake)); - let content = generate_single_resource_source_code(package, leaf); - let dir = current_path.parent().unwrap(); - std::fs::create_dir_all(dir).unwrap(); - let mut file = File::create(current_path.parent().unwrap().join(file_name)).unwrap(); - file.write_all(content.as_bytes()).unwrap(); - } - } -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/resources/resource_code.rs.handlebars b/pulumi_wasm_generator_lib/src/output/rust/resources/resource_code.rs.handlebars deleted file mode 100644 index b58f43fa3..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/resources/resource_code.rs.handlebars +++ /dev/null @@ -1,58 +0,0 @@ -{{#each resource.description_lines}} -//! {{&this}} -{{/each}} - -#[derive(bon::Builder, Clone)] -#[builder(finish_fn = build_struct)] -pub struct {{resource.struct_name}}Args { -{{#each resource.input_properties}} - {{#each description_lines}} - /// {{&this}} - {{/each}} - {{#if default}} - #[builder(into, default)] - {{else if skip}} - #[builder(skip)] - {{else}} - #[builder(into)] - {{/if}} - {{#unless private}}pub {{/unless}}{{arg_name}}: pulumi_wasm_rust::Output<{{&type_}}>, -{{/each}} -} - -pub struct {{resource.struct_name}}Result { -{{#each resource.output_properties}} - {{#each description_lines}} - /// {{&this}} - {{/each}} - pub {{arg_name}}: pulumi_wasm_rust::Output<{{&type_}}>, -{{/each}} -} - -/// -/// Registers a new resource with the given unique name and arguments -/// -pub fn create( - name: &str, -{{#if resource.input_properties}} - args: {{resource.struct_name}}Args -{{/if}} -) -> {{resource.struct_name}}Result { - - let result = crate::bindings::pulumi::{{resource.package_name}}::{{resource.name}}::invoke( - name, - {{#if resource.input_properties}} - &crate::bindings::pulumi::{{resource.package_name}}::{{resource.name}}::Args { - {{#each resource.input_properties}} - {{arg_name}}: &args.{{arg_name}}.get_inner(), - {{/each}} - } - {{/if}} - ); - - {{resource.struct_name}}Result { - {{#each resource.output_properties}} - {{arg_name}}: crate::into_domain(result.{{arg_name}}), - {{/each}} - } -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/resources/source_code_resource_code.rs b/pulumi_wasm_generator_lib/src/output/rust/resources/source_code_resource_code.rs deleted file mode 100644 index b3d7a05b5..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/resources/source_code_resource_code.rs +++ /dev/null @@ -1,94 +0,0 @@ -use crate::model::{ElementId, Type}; -use handlebars::Handlebars; -use serde::Serialize; -use serde_json::json; - -static TEMPLATE: &str = include_str!("resource_code.rs.handlebars"); - -#[derive(Serialize)] -struct InputProperty { - name: String, - arg_name: String, - type_: String, - description_lines: Vec, - default: bool, - skip: bool, - private: bool, -} - -#[derive(Serialize)] -struct OutputProperty { - name: String, - arg_name: String, - type_: String, - description_lines: Vec, -} - -#[derive(Serialize)] -struct Resource { - name: String, - package_name: String, - input_properties: Vec, - output_properties: Vec, - struct_name: String, - function_name: String, - description_lines: Vec, -} - -fn convert_resource(package: &crate::model::Package, element_id: &ElementId) -> Resource { - let resource = package.resources.get(element_id).unwrap(); - let depth = element_id.namespace.len(); - Resource { - name: element_id.get_rust_namespace_name(), - package_name: package.name.clone().replace("-", "_"), - struct_name: element_id.name.clone(), - function_name: element_id.get_rust_function_name(), - description_lines: crate::utils::to_lines( - resource.description.clone(), - package, - Some(element_id.clone()), - ), - input_properties: resource - .input_properties - .iter() - .map(|input_property| InputProperty { - name: input_property.name.clone(), - arg_name: input_property.get_rust_argument_name(), - default: matches!(input_property.r#type, Type::Option(_)), - skip: matches!(input_property.r#type, Type::ConstString(_)), - private: matches!(input_property.r#type, Type::ConstString(_)), - type_: input_property.r#type.get_rust_type(depth), - description_lines: crate::utils::to_lines( - input_property.description.clone(), - package, - None, - ), - }) - .collect(), - output_properties: resource - .output_properties - .iter() - .map(|output_property| OutputProperty { - name: output_property.name.clone(), - arg_name: output_property.get_rust_argument_name(), - type_: output_property.r#type.get_rust_type(depth), - description_lines: crate::utils::to_lines( - output_property.description.clone(), - package, - None, - ), - }) - .collect(), - } -} - -pub(crate) fn generate_single_resource_source_code( - package: &crate::model::Package, - element_id: &ElementId, -) -> String { - let handlebars = Handlebars::new(); - let resource = convert_resource(package, element_id); - handlebars - .render_template(TEMPLATE, &json!({"resource": resource})) - .unwrap() -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/resources/source_code_resource_mod.rs b/pulumi_wasm_generator_lib/src/output/rust/resources/source_code_resource_mod.rs deleted file mode 100644 index 25728b1c0..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/resources/source_code_resource_mod.rs +++ /dev/null @@ -1,32 +0,0 @@ -use crate::output::rust::TreeNode; -use convert_case::{Case, Casing}; -use rinja::Template; -use std::collections::BTreeMap; - -#[derive(Template)] -#[template(path = "resource_mod.rs.jinja")] -struct TemplateModel { - files: Vec, - directories: Vec, -} - -pub(crate) fn generate_mod(tree_nodes: &BTreeMap) -> String { - let mut files = Vec::new(); - let mut directories = Vec::new(); - - for (name, tree_node) in tree_nodes { - match tree_node { - TreeNode::Namespace(_) => directories.push(name.clone()), - TreeNode::Leaf(l) => { - files.push(l.get_rust_struct_name().to_case(Case::Snake)); - } - } - } - - TemplateModel { files, directories } - .render() - .unwrap() - .trim() - .to_string() - + "\n" -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/source_code_librs.rs b/pulumi_wasm_generator_lib/src/output/rust/source_code_librs.rs deleted file mode 100644 index 67b6e97a9..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/source_code_librs.rs +++ /dev/null @@ -1,76 +0,0 @@ -use crate::model::GlobalType; -use crate::output::get_main_version; -use convert_case::Case::UpperCamel; -use convert_case::Casing; -use handlebars::Handlebars; -use itertools::Itertools; -use serde::Serialize; -use serde_json::json; -use std::collections::BTreeSet; - -static TEMPLATE: &str = include_str!("lib.rs.handlebars"); - -#[derive(Serialize)] -struct Interface {} - -#[derive(Serialize)] -struct Package { - name: String, - contains_elements: bool, - contains_types: bool, - pulumi_wasm_version: String, - contains_resources: bool, - contains_functions: bool, - const_strings: Vec, -} - -fn convert_model(package: &crate::model::Package) -> Package { - Package { - name: package.name.clone(), - pulumi_wasm_version: get_main_version().to_string(), - contains_types: !package.types.is_empty(), - contains_elements: !package.resources.is_empty() || !package.functions.is_empty(), - contains_resources: !package.resources.is_empty(), - contains_functions: !package.functions.is_empty(), - const_strings: find_consts(package), - } -} - -fn find_consts(package: &crate::model::Package) -> Vec { - let mut consts = BTreeSet::new(); - for resource in package.resources.values() { - for input in &resource.input_properties { - consts.extend(input.r#type.get_consts().clone()); - } - for output in &resource.output_properties { - consts.extend(output.r#type.get_consts().clone()); - } - } - for function in package.functions.values() { - for input in &function.input_properties { - consts.extend(input.r#type.get_consts().clone()); - } - for output in &function.output_properties { - consts.extend(output.r#type.get_consts().clone()); - } - } - for type_ in package.types.values() { - if let GlobalType::Object(_, obj) = type_ { - for gtp in obj { - consts.extend(gtp.r#type.get_consts().clone()); - } - } - } - consts - .into_iter() - .map(|s| s.to_case(UpperCamel)) - .sorted() - .collect() -} - -pub(crate) fn generate_source_code(package: &crate::model::Package) -> String { - let handlebars = Handlebars::new(); - handlebars - .render_template(TEMPLATE, &json!({"package": &convert_model(package)})) - .unwrap() -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/types/mod.rs b/pulumi_wasm_generator_lib/src/output/rust/types/mod.rs deleted file mode 100644 index 78d53a608..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/types/mod.rs +++ /dev/null @@ -1,49 +0,0 @@ -mod source_code_types_code; -mod source_code_types_mod; - -use crate::output::rust::types::source_code_types_code::generate_single_type_source_file; -use crate::output::rust::types::source_code_types_mod::generate_mod; -use crate::output::rust::TreeNode; -use convert_case::{Case, Casing}; -use std::fs::File; -use std::io::Write; - -pub(crate) fn generate_types_code(package: &crate::model::Package, result_path: &std::path::Path) { - if package.types.is_empty() { - return; - } - let mut tree = TreeNode::new(); - - for element_id in package.types.keys() { - tree.insert(element_id.clone()); - } - - let root = result_path.join("src").join("types"); - - generate_files(package, &tree, &root); -} - -fn generate_files( - package: &crate::model::Package, - tree_node: &TreeNode, - current_path: &std::path::Path, -) { - match tree_node { - TreeNode::Namespace(ns) => { - for (name, node) in ns { - generate_files(package, node, ¤t_path.join(name)); - } - let content = generate_mod(ns); - let mut file = File::create(current_path.join("mod.rs")).unwrap(); - file.write_all(content.as_bytes()).unwrap(); - } - TreeNode::Leaf(ref leaf) => { - let file_name = format!("{}.rs", leaf.get_rust_struct_name().to_case(Case::Snake)); - let content = generate_single_type_source_file(package, leaf); - let dir = current_path.parent().unwrap(); - std::fs::create_dir_all(dir).unwrap(); - let mut file = File::create(current_path.parent().unwrap().join(file_name)).unwrap(); - file.write_all(content.as_bytes()).unwrap(); - } - } -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/types/source_code_types_code.rs b/pulumi_wasm_generator_lib/src/output/rust/types/source_code_types_code.rs deleted file mode 100644 index 97e720c9e..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/types/source_code_types_code.rs +++ /dev/null @@ -1,219 +0,0 @@ -use crate::model::{ElementId, GlobalType, Type}; -use convert_case::{Case, Casing}; -use handlebars::Handlebars; -use serde::Serialize; -use serde_json::json; -use std::collections::BTreeSet; - -static TEMPLATE: &str = include_str!("types_code.rs.handlebars"); -static STRING_ENUM_TEMPLATE: &str = include_str!("types_code_string_enum.rs.handlebars"); -static NUMBER_ENUM_TEMPLATE: &str = include_str!("types_code_number_enum.rs.handlebars"); -static INTEGER_ENUM_TEMPLATE: &str = include_str!("types_code_integer_enum.rs.handlebars"); - -#[derive(Serialize)] -struct Property { - name: String, - original_name: String, - type_: String, - description_lines: Vec, - default: bool, - skip: bool, - private: bool, -} - -#[derive(Serialize)] -struct RefType { - // name: String, - fields: Vec, - description_lines: Vec, - struct_name: String, - file_name: String, - const_strings: BTreeSet, -} - -#[derive(Serialize)] -struct StringEnum { - struct_name: String, - file_name: String, - description_lines: Vec, - values: Vec, -} - -#[derive(Serialize)] -struct StringEnumValue { - name: String, - description_lines: Vec, - value: Option, -} - -#[derive(Serialize)] -struct IntegerEnum { - struct_name: String, - file_name: String, - description_lines: Vec, - values: Vec, -} - -#[derive(Serialize)] -struct IntegerEnumValue { - name: String, - description_lines: Vec, - value: i64, -} - -#[derive(Serialize)] -struct NumberEnum { - struct_name: String, - file_name: String, - description_lines: Vec, - values: Vec, -} - -#[derive(Serialize)] -struct NumberEnumValue { - name: String, - description_lines: Vec, - value: f64, -} - -#[derive(Serialize)] -struct AliasType { - name: String, - type_: String, -} - -enum GenerateResource { - RealType(RefType), - StringEnum(StringEnum), - NumberEnum(NumberEnum), - IntegerEnum(IntegerEnum), -} - -fn convert_resource(package: &crate::model::Package, element_id: &ElementId) -> GenerateResource { - let resource = package.types.get(element_id).unwrap(); - let depth = element_id.namespace.len(); - match resource { - GlobalType::Object(description, properties) => { - let ref_type = RefType { - struct_name: element_id.get_rust_struct_name(), - file_name: element_id.get_rust_struct_name().to_case(Case::Snake), - description_lines: crate::utils::to_lines(description.clone(), package, None), - fields: properties - .iter() - .map(|global_type_property| Property { - name: global_type_property - .name - .clone() - .from_case(Case::Camel) - .to_case(Case::Snake), - original_name: global_type_property.name.clone(), - type_: global_type_property.r#type.get_rust_type(depth), - default: matches!(global_type_property.r#type, Type::Option(_)), - skip: matches!(global_type_property.r#type, Type::ConstString(_)), - private: matches!(global_type_property.r#type, Type::ConstString(_)), - description_lines: crate::utils::to_lines( - global_type_property.description.clone(), - package, - None, - ), - }) - .collect(), - const_strings: properties - .iter() - .flat_map(|global_type_property| global_type_property.r#type.get_consts()) - .collect(), - }; - GenerateResource::RealType(ref_type) - } - GlobalType::StringEnum(description, enum_values) => { - let enum_type = StringEnum { - struct_name: element_id.get_rust_struct_name(), - file_name: element_id.get_rust_struct_name().to_case(Case::Snake), - description_lines: crate::utils::to_lines(description.clone(), package, None), - values: enum_values - .iter() - .map(|enum_value| StringEnumValue { - name: enum_value.name.clone(), - value: enum_value.value.clone().map(|s| format!("\"{}\"", s)), - description_lines: crate::utils::to_lines( - enum_value.description.clone(), - package, - None, - ), - }) - .collect(), - }; - GenerateResource::StringEnum(enum_type) - } - GlobalType::NumberEnum(description, enum_values) => { - let enum_type = NumberEnum { - struct_name: element_id.get_rust_struct_name(), - file_name: element_id.get_rust_struct_name().to_case(Case::Snake), - description_lines: crate::utils::to_lines(description.clone(), package, None), - values: enum_values - .iter() - .map(|enum_value| NumberEnumValue { - name: enum_value.name.clone(), - value: enum_value.value, - description_lines: crate::utils::to_lines( - enum_value.description.clone(), - package, - None, - ), - }) - .collect(), - }; - GenerateResource::NumberEnum(enum_type) - } - GlobalType::IntegerEnum(description, enum_values) => { - let enum_type = IntegerEnum { - struct_name: element_id.get_rust_struct_name(), - file_name: element_id.get_rust_struct_name().to_case(Case::Snake), - description_lines: crate::utils::to_lines(description.clone(), package, None), - values: enum_values - .iter() - .map(|enum_value| IntegerEnumValue { - name: enum_value.name.clone(), - value: enum_value.value, - description_lines: crate::utils::to_lines( - enum_value.description.clone(), - package, - None, - ), - }) - .collect(), - }; - GenerateResource::IntegerEnum(enum_type) - } - } -} - -pub(crate) fn generate_single_type_source_file( - package: &crate::model::Package, - element_id: &ElementId, -) -> String { - let handlebars = Handlebars::new(); - - match convert_resource(package, element_id) { - GenerateResource::RealType(type_) => handlebars - .render_template(TEMPLATE, &json!({"type": type_})) - .unwrap() - .trim_start() - .to_string(), - GenerateResource::StringEnum(enum_) => handlebars - .render_template(STRING_ENUM_TEMPLATE, &json!({"enum": enum_})) - .unwrap() - .trim_start() - .to_string(), - GenerateResource::NumberEnum(enum_) => handlebars - .render_template(NUMBER_ENUM_TEMPLATE, &json!({"enum": enum_})) - .unwrap() - .trim_start() - .to_string(), - GenerateResource::IntegerEnum(enum_) => handlebars - .render_template(INTEGER_ENUM_TEMPLATE, &json!({"enum": enum_})) - .unwrap() - .trim_start() - .to_string(), - } -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/types/source_code_types_mod.rs b/pulumi_wasm_generator_lib/src/output/rust/types/source_code_types_mod.rs deleted file mode 100644 index 4a72fd98e..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/types/source_code_types_mod.rs +++ /dev/null @@ -1,32 +0,0 @@ -use crate::output::rust::TreeNode; -use convert_case::{Case, Casing}; -use rinja::Template; -use std::collections::BTreeMap; - -#[derive(Template)] -#[template(path = "types_mod.rs.jinja")] -struct TemplateModel { - files: Vec, - directories: Vec, -} - -pub(crate) fn generate_mod(tree_nodes: &BTreeMap) -> String { - let mut files = Vec::new(); - let mut directories = Vec::new(); - - for (name, tree_node) in tree_nodes { - match tree_node { - TreeNode::Namespace(_) => directories.push(name.clone()), - TreeNode::Leaf(l) => { - files.push(l.get_rust_struct_name().to_case(Case::Snake)); - } - } - } - - TemplateModel { files, directories } - .render() - .unwrap() - .trim() - .to_string() - + "\n" -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/types/types_code.rs.handlebars b/pulumi_wasm_generator_lib/src/output/rust/types/types_code.rs.handlebars deleted file mode 100644 index 474b1134c..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/types/types_code.rs.handlebars +++ /dev/null @@ -1,22 +0,0 @@ -{{#each type.description_lines}} - //! {{&this}} -{{/each}} - -#[derive(serde::Deserialize, serde::Serialize, bon::Builder, Debug, PartialEq, Clone)] -#[builder(finish_fn = build_struct)] -pub struct {{type.struct_name}} { - {{#each type.fields as |field|}} - {{#each field.description_lines}} - /// {{&this}} - {{/each}} - {{#if default}} - #[builder(into, default)] - {{else if skip}} - #[builder(skip)] - {{else}} - #[builder(into)] - {{/if}} - #[serde(rename = "{{field.original_name}}")] - {{#unless private}}pub {{/unless}}r#{{field.name}}: Box<{{&field.type_}}>, - {{/each}} -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/types/types_code_integer_enum.rs.handlebars b/pulumi_wasm_generator_lib/src/output/rust/types/types_code_integer_enum.rs.handlebars deleted file mode 100644 index cca674028..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/types/types_code_integer_enum.rs.handlebars +++ /dev/null @@ -1,42 +0,0 @@ -{{#each enum.description_lines}} - //! {{&this}} -{{/each}} - -#[derive(Debug, PartialEq, Clone)] -pub enum {{enum.struct_name}} { - {{#each enum.values as |value|}} - {{#each value.description_lines}} - /// {{&this}} - {{/each}} - {{value.name}}, - {{/each}} -} - -impl serde::Serialize for {{enum.struct_name}} { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - let value = match self { - {{#each enum.values as |value|}} - {{@root.enum.struct_name}}::{{value.name}} => {{value.value}}, - {{/each}} - }; - serializer.serialize_i64(value) - } -} - -impl<'de> serde::Deserialize<'de> for {{enum.struct_name}} { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - let f = i64::deserialize(deserializer)?; - match f { - {{#each enum.values as |value|}} - {{value.value}} => Ok({{@root.enum.struct_name}}::{{value.name}}), - {{/each}} - _ => Err(serde::de::Error::custom(format!("Invalid enum value: {}", f))), - } - } -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/types/types_code_number_enum.rs.handlebars b/pulumi_wasm_generator_lib/src/output/rust/types/types_code_number_enum.rs.handlebars deleted file mode 100644 index 2134a8556..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/types/types_code_number_enum.rs.handlebars +++ /dev/null @@ -1,42 +0,0 @@ -{{#each enum.description_lines}} - //! {{&this}} -{{/each}} - -#[derive(Debug, PartialEq, Clone)] -pub enum {{enum.struct_name}} { - {{#each enum.values as |value|}} - {{#each value.description_lines}} - /// {{&this}} - {{/each}} - {{value.name}}, - {{/each}} -} - -impl serde::Serialize for {{enum.struct_name}} { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - let value = match self { - {{#each enum.values as |value|}} - {{@root.enum.struct_name}}::{{value.name}} => {{value.value}}, - {{/each}} - }; - serializer.serialize_f64(value) - } -} - -impl<'de> serde::Deserialize<'de> for {{enum.struct_name}} { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - let f = f64::deserialize(deserializer)?; - match f { - {{#each enum.values as |value|}} - {{value.value}} => Ok({{@root.enum.struct_name}}::{{value.name}}), - {{/each}} - _ => Err(serde::de::Error::custom(format!("Invalid enum value: {}", f))), - } - } -} diff --git a/pulumi_wasm_generator_lib/src/output/rust/types/types_code_string_enum.rs.handlebars b/pulumi_wasm_generator_lib/src/output/rust/types/types_code_string_enum.rs.handlebars deleted file mode 100644 index d940e49fb..000000000 --- a/pulumi_wasm_generator_lib/src/output/rust/types/types_code_string_enum.rs.handlebars +++ /dev/null @@ -1,16 +0,0 @@ -{{#each enum.description_lines}} - //! {{&this}} -{{/each}} - -#[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq, Clone)] -pub enum {{enum.struct_name}} { - {{#each enum.values as |value|}} - {{#each value.description_lines}} - /// {{&this}} - {{/each}} - {{#if value.value}} - #[serde(rename = {{{value.value}}})] - {{/if}} - {{value.name}}, - {{/each}} -} diff --git a/pulumi_wasm_generator_lib/src/output/combined/source_code_librs.rs b/pulumi_wasm_generator_lib/src/output/source_code_librs.rs similarity index 98% rename from pulumi_wasm_generator_lib/src/output/combined/source_code_librs.rs rename to pulumi_wasm_generator_lib/src/output/source_code_librs.rs index 67b6e97a9..03a059735 100644 --- a/pulumi_wasm_generator_lib/src/output/combined/source_code_librs.rs +++ b/pulumi_wasm_generator_lib/src/output/source_code_librs.rs @@ -1,5 +1,5 @@ use crate::model::GlobalType; -use crate::output::get_main_version; +use crate::utils::get_main_version; use convert_case::Case::UpperCamel; use convert_case::Casing; use handlebars::Handlebars; diff --git a/pulumi_wasm_generator_lib/src/output/combined/types/mod.rs b/pulumi_wasm_generator_lib/src/output/types/mod.rs similarity index 92% rename from pulumi_wasm_generator_lib/src/output/combined/types/mod.rs rename to pulumi_wasm_generator_lib/src/output/types/mod.rs index d719e65c5..eb4e6eba4 100644 --- a/pulumi_wasm_generator_lib/src/output/combined/types/mod.rs +++ b/pulumi_wasm_generator_lib/src/output/types/mod.rs @@ -1,10 +1,10 @@ mod source_code_types_code; use crate::model::{ElementId, Package}; -use crate::output::combined::types::source_code_types_code::{ +use crate::output::types::source_code_types_code::{ generate_docs, generate_single_type_source_file, }; -use crate::output::combined::TreeNode; +use crate::output::TreeNode; use crate::utils::reformat_code; use anyhow::Context; use convert_case::{Case, Casing}; @@ -31,7 +31,7 @@ pub(crate) fn generate_types_code(package: &Package, result_path: &std::path::Pa tree.insert(element_id.clone()); } - generate_files(package, &tree, &result_path); + generate_files(package, &tree, result_path); } fn generate_files(package: &Package, tree_node: &TreeNode, current_path: &std::path::Path) { @@ -42,7 +42,7 @@ fn generate_files(package: &Package, tree_node: &TreeNode, current_path: &std::p generate_files(package, node, ¤t_path.join(name)); } - for (type_) in types { + for type_ in types { let file_name = format!("{}.rs", type_.get_rust_struct_name().to_case(Case::Snake)); let content = generate_single_type_source_file(package, type_); let mut file = File::create(current_path.join(file_name)).unwrap(); @@ -84,7 +84,7 @@ fn generate_module_imports_1( s.push_str("}\n"); } - for (function) in functions { + for function in functions { s.push_str(&format!( "include!(\"{}/{}.rs\");\n", current_path.to_str().unwrap().replace("\\", "/"), diff --git a/pulumi_wasm_generator_lib/src/output/combined/types/source_code_types_code.rs b/pulumi_wasm_generator_lib/src/output/types/source_code_types_code.rs similarity index 100% rename from pulumi_wasm_generator_lib/src/output/combined/types/source_code_types_code.rs rename to pulumi_wasm_generator_lib/src/output/types/source_code_types_code.rs diff --git a/pulumi_wasm_generator_lib/src/output/combined/types/types_code.rs.handlebars b/pulumi_wasm_generator_lib/src/output/types/types_code.rs.handlebars similarity index 100% rename from pulumi_wasm_generator_lib/src/output/combined/types/types_code.rs.handlebars rename to pulumi_wasm_generator_lib/src/output/types/types_code.rs.handlebars diff --git a/pulumi_wasm_generator_lib/src/output/combined/types/types_code_integer_enum.rs.handlebars b/pulumi_wasm_generator_lib/src/output/types/types_code_integer_enum.rs.handlebars similarity index 100% rename from pulumi_wasm_generator_lib/src/output/combined/types/types_code_integer_enum.rs.handlebars rename to pulumi_wasm_generator_lib/src/output/types/types_code_integer_enum.rs.handlebars diff --git a/pulumi_wasm_generator_lib/src/output/combined/types/types_code_number_enum.rs.handlebars b/pulumi_wasm_generator_lib/src/output/types/types_code_number_enum.rs.handlebars similarity index 100% rename from pulumi_wasm_generator_lib/src/output/combined/types/types_code_number_enum.rs.handlebars rename to pulumi_wasm_generator_lib/src/output/types/types_code_number_enum.rs.handlebars diff --git a/pulumi_wasm_generator_lib/src/output/combined/types/types_code_string_enum.rs.handlebars b/pulumi_wasm_generator_lib/src/output/types/types_code_string_enum.rs.handlebars similarity index 100% rename from pulumi_wasm_generator_lib/src/output/combined/types/types_code_string_enum.rs.handlebars rename to pulumi_wasm_generator_lib/src/output/types/types_code_string_enum.rs.handlebars diff --git a/pulumi_wasm_generator_lib/src/output/wit/mod.rs b/pulumi_wasm_generator_lib/src/output/wit/mod.rs index 27c6b254b..3383e94e7 100644 --- a/pulumi_wasm_generator_lib/src/output/wit/mod.rs +++ b/pulumi_wasm_generator_lib/src/output/wit/mod.rs @@ -1,108 +1,8 @@ -use crate::output::get_main_version; +use crate::utils::get_main_version; use handlebars::Handlebars; -use serde::Serialize; - -static TEMPLATE: &str = include_str!("wit.handlebars"); static DEPENDENCIES: &str = include_str!("dependencies.handlebars"); -#[derive(Serialize)] -struct Argument { - name: String, -} - -#[derive(Serialize)] -struct Result { - name: String, -} - -#[derive(Serialize)] -struct Resources { - name: String, - arguments: Vec, - results: Vec, -} - -#[derive(Serialize)] -struct Function { - name: String, - arguments: Vec, - results: Vec, -} - -#[derive(Serialize)] -struct Package { - name: String, - version: String, - pulumi_wasm_version: String, - resources: Vec, - functions: Vec, -} - -fn convert_model(package: &crate::model::Package) -> Package { - let resources: Vec<_> = package - .resources - .iter() - .map(|(element_id, resource)| Resources { - name: element_id.get_wit_interface_name(), - arguments: resource - .input_properties - .iter() - .map(|input_property| Argument { - name: input_property.get_wit_argument_name(), - }) - .collect(), - results: resource - .output_properties - .iter() - .map(|output_property| Result { - name: output_property.get_wit_argument_name(), - }) - .collect(), - }) - .collect(); - - let functions: Vec<_> = package - .functions - .iter() - .map(|(element_id, resource)| Function { - name: element_id.get_wit_interface_name(), - arguments: resource - .input_properties - .iter() - .map(|input_property| Argument { - name: input_property.get_wit_argument_name(), - }) - .collect(), - results: resource - .output_properties - .iter() - .map(|output_property| Result { - name: output_property.get_wit_argument_name(), - }) - .collect(), - }) - .collect(); - - Package { - name: package.get_wit_name(), - version: package.version.clone(), - pulumi_wasm_version: get_main_version().to_string(), - resources, - functions, - } -} - -pub(crate) fn generate_wit(package: &crate::model::Package) -> anyhow::Result { - let mut data = std::collections::BTreeMap::new(); - data.insert("package", convert_model(package)); - - let reg = Handlebars::new(); - let output = reg.render_template(TEMPLATE, &data)?; - - Ok(output) -} - pub(crate) fn get_dependencies() -> anyhow::Result { let mut data = std::collections::BTreeMap::new(); data.insert("pulumi_wasm_version", get_main_version()); diff --git a/pulumi_wasm_generator_lib/src/output/wit/wit.handlebars b/pulumi_wasm_generator_lib/src/output/wit/wit.handlebars deleted file mode 100644 index f577bd84b..000000000 --- a/pulumi_wasm_generator_lib/src/output/wit/wit.handlebars +++ /dev/null @@ -1,81 +0,0 @@ -package pulumi:{{package.name}}@{{package.version}}--{{package.pulumi_wasm_version}}; - -world {{package.name}}-pulumi { - import component:pulumi-wasm/register-interface@{{package.pulumi_wasm_version}}; -{{#each package.resources}} - export {{name}}; -{{/each}} -{{#each package.functions}} - export {{name}}; -{{/each}} -} - -world {{package.name}}-pulumi-client { -{{#each package.resources}} - import {{name}}; -{{/each}} -{{#each package.functions}} - import {{name}}; -{{/each}} -} - -{{#each package.resources}} -interface {{name}} { - - use component:pulumi-wasm/output-interface@{{@root.package.pulumi_wasm_version}}.{output}; - - {{#if arguments}} - record args { - {{#each arguments}} - {{name}}: borrow, - {{/each}} - } - {{/if}} - - {{#if results}} - record res { - {{#each results}} - {{name}}: output, - {{/each}} - } - {{/if}} - - invoke: func( - name: string, -{{#if arguments}} - args: args -{{/if}} - ){{#if results}} -> res{{/if}}; - -} -{{/each}} - -{{#each package.functions}} -interface {{name}} { - - use component:pulumi-wasm/output-interface@{{@root.package.pulumi_wasm_version}}.{output}; - - {{#if arguments}} - record args { - {{#each arguments}} - {{name}}: borrow, - {{/each}} - } - {{/if}} - - {{#if results}} - record res { - {{#each results}} - {{name}}: output, - {{/each}} - } - {{/if}} - - invoke: func( - {{#if arguments}} - args: args - {{/if}} - ){{#if results}} -> res{{/if}}; - -} -{{/each}} \ No newline at end of file diff --git a/pulumi_wasm_generator_lib/src/utils.rs b/pulumi_wasm_generator_lib/src/utils.rs index 078f2c76a..d1be67fea 100644 --- a/pulumi_wasm_generator_lib/src/utils.rs +++ b/pulumi_wasm_generator_lib/src/utils.rs @@ -174,7 +174,11 @@ fn fix_pulumi_docker_docs(s: String, element_id: Option) -> String { s } -pub fn reformat_code(code: &str) -> anyhow::Result { +pub(crate) fn reformat_code(code: &str) -> anyhow::Result { let syntax_tree = syn::parse_file(code)?; Ok(prettyplease::unparse(&syntax_tree)) } + +pub(crate) fn get_main_version() -> String { + env!("CARGO_PKG_VERSION").to_string() +} diff --git a/pulumi_wasm_generator_lib/tests/test.rs b/pulumi_wasm_generator_lib/tests/test.rs index 46f69883f..4455e7656 100644 --- a/pulumi_wasm_generator_lib/tests/test.rs +++ b/pulumi_wasm_generator_lib/tests/test.rs @@ -1,11 +1,7 @@ -use anyhow::{Context, Result}; +use anyhow::Result; use assert_cmd::assert::OutputAssertExt; -use pulumi_wasm_generator_lib::{ - extract_micro_package, generate_combined, generate_rust_library, generate_wasm_provider, -}; +use pulumi_wasm_generator_lib::generate_combined; use std::fs; -use std::fs::File; -use std::io::BufReader; use std::path::{Path, PathBuf}; use std::process::Command;