Skip to content

Commit

Permalink
Initial code generator (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejressel authored Mar 30, 2024
1 parent de8d731 commit e7e3982
Show file tree
Hide file tree
Showing 64 changed files with 10,167 additions and 211 deletions.
135 changes: 112 additions & 23 deletions Cargo.lock

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

22 changes: 15 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@ edition.workspace = true

[workspace]
members = [
"pulumi_wasm_runner",
"pulumi_wasm",
"cargo-pulumi",
"cargo-pulumi-gen",
"examples/simple",
"providers/pulumi_wasm_provider_random",
"pulumi_wasm_rust_macro",
"pulumi_wasm",
"pulumi_wasm_generator",
"pulumi_wasm_runner",
"pulumi_wasm_rust",
"pulumi_wasm_rust_macro",
"wasm_common",
"examples/simple",
"cargo-pulumi"
]

[workspace.package]
version = "0.1.0"
edition = "2021"

[workspace.dependencies]
pulumi_wasm_generator = { path = "pulumi_wasm_generator" }
pulumi_wasm_random = { path = "providers/pulumi_wasm_provider_random_rust" }
pulumi_wasm_rust = { path = "pulumi_wasm_rust" }
pulumi_wasm_rust_macro = { path = "pulumi_wasm_rust_macro" }
wasm_common = { path = "wasm_common" }
pulumi_wasm_provider_random_rust = { path = "providers/pulumi_wasm_provider_random_rust" }

anyhow = "1.0.81"
prost = "0.12.3"
Expand Down Expand Up @@ -63,4 +66,9 @@ normpath = "1.2"
simple_logger = "4.3.3"
petgraph = "0.6.4"
cargo_metadata = "0.18.1"
itertools = "0.12.1"
itertools = "0.12.1"
testdir = "0.9.1"
fs_extra = "1.3.0"
handlebars = "5.1.0"
convert_case = "0.6.0"
automod = "1.0.14"
11 changes: 11 additions & 0 deletions cargo-pulumi-gen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "cargo-pulumi-gen"
version.workspace = true
edition.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap.workspace = true
pulumi_wasm_generator.workspace = true
anyhow.workspace = true
109 changes: 109 additions & 0 deletions cargo-pulumi-gen/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};

use std::fs;
use std::path::Path;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct App {
#[clap(subcommand)]
command: Command,
}

#[derive(Debug, Subcommand)]
enum Command {
GenProvider {
#[arg(short, long)]
schema: String,

#[arg(short, long)]
output: String,

#[arg(short, long)]
remove: Option<bool>,
},
GenRust {
#[arg(short, long)]
schema: String,

#[arg(short, long)]
output: String,

#[arg(short, long)]
remove: Option<bool>,
},
}

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)?;
pulumi_wasm_generator::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)?;
pulumi_wasm_generator::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<bool>) -> 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(())
}
}
4 changes: 2 additions & 2 deletions cargo-pulumi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ assert_cmd.workspace = true
predicates.workspace = true
wasmtime.workspace = true
wasmtime-wasi.workspace = true
testdir = "0.9.1"
fs_extra = "1.3.0"
testdir.workspace = true
fs_extra.workspace = true
5 changes: 5 additions & 0 deletions dev-docs/getting-schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Proper way of getting package schema: https://github.com/pulumi/pulumi/blob/2366960ae545d31c52f1e9a6b8b35184511c3b84/pkg/cmd/pulumi/package_extract_schema.go#L28

Basically:
1. Download executable
2. Use GRPC request GetSchema
Loading

0 comments on commit e7e3982

Please sign in to comment.