From e37a4c08b94d5f0a00a213b2ebe8d7886257a12d Mon Sep 17 00:00:00 2001 From: EehMauro Date: Tue, 11 Mar 2025 19:45:24 -0300 Subject: [PATCH 1/3] Added CLI install command --- cli/Cargo.lock | 49 + cli/Cargo.toml | 1 + cli/src/command/codegen.rs | 17 +- cli/src/command/codegen_local.rs | 13 - cli/src/command/install.rs | 29 + cli/src/command/mod.rs | 8 +- cli/src/main.rs | 27 +- codegen/examples/package-lock.json | 5527 +++++++++++++++-- codegen/examples/package.json | 3 +- codegen/examples/types/mesh_txpipe_asteria.ts | 23 + 10 files changed, 5085 insertions(+), 612 deletions(-) delete mode 100644 cli/src/command/codegen_local.rs create mode 100644 cli/src/command/install.rs create mode 100644 codegen/examples/types/mesh_txpipe_asteria.ts diff --git a/cli/Cargo.lock b/cli/Cargo.lock index 9a65908..f34d65e 100644 --- a/cli/Cargo.lock +++ b/cli/Cargo.lock @@ -798,6 +798,27 @@ dependencies = [ "subtle", ] +[[package]] +name = "dirs" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + [[package]] name = "discard" version = "1.0.4" @@ -1696,6 +1717,16 @@ dependencies = [ "libc", ] +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags 2.8.0", + "libc", +] + [[package]] name = "libz-sys" version = "1.1.21" @@ -2022,6 +2053,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "ouroboros" version = "0.18.5" @@ -2387,6 +2424,17 @@ dependencies = [ "bitflags 2.8.0", ] +[[package]] +name = "redox_users" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" +dependencies = [ + "getrandom 0.2.15", + "libredox", + "thiserror 1.0.69", +] + [[package]] name = "ref-cast" version = "1.0.23" @@ -2948,6 +2996,7 @@ dependencies = [ "clearscreen", "cynic", "cynic-codegen", + "dirs", "dotenv_codegen", "inquire", "oci-client", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index fc2d069..5f7d629 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -19,6 +19,7 @@ chrono = "0.4.39" dotenv_codegen = "0.15.0" inquire = "0.7.5" clearscreen = "4.0.1" +dirs = "5.0" telchar-codegen = { version = "0.1.1", path = "../codegen" } [build-dependencies] diff --git a/cli/src/command/codegen.rs b/cli/src/command/codegen.rs index df43ea9..2c5238e 100644 --- a/cli/src/command/codegen.rs +++ b/cli/src/command/codegen.rs @@ -1,16 +1,19 @@ -use crate::run_codegen_query; +use std::str::FromStr; +use dirs::home_dir; + +use telchar_codegen::Codegen; +use telchar_codegen::template::Template; pub async fn run(sub_matches: &clap::ArgMatches) { - let graphql_url = dotenv!("GRAPHQL_URL"); + let codegen = Codegen::new(); let blueprint_reference = sub_matches.get_one::("blueprint").expect("required"); let template_reference = sub_matches.get_one::("template").expect("required"); if let Some((scope, name)) = blueprint_reference.split_once('/') { - let codegen = run_codegen_query(graphql_url.to_string(), scope.to_string(), name.to_string(), template_reference.to_string()).await; - match codegen { - Some(codegen) => println!("{}", codegen), - None => println!("Blueprint not found") - } + let blueprint_path = home_dir().unwrap().join(".telchar").join("protocol").join(format!("{}_{}.json", scope, name)); + let blueprint = codegen.get_blueprint_from_path(blueprint_path.to_str().unwrap().to_string()); + let template = Template::from_str(&template_reference).unwrap(); + println!("{}", codegen.get_template_from_blueprint(blueprint, template)); } else { println!("Invalid blueprint reference provided"); } diff --git a/cli/src/command/codegen_local.rs b/cli/src/command/codegen_local.rs deleted file mode 100644 index c85df16..0000000 --- a/cli/src/command/codegen_local.rs +++ /dev/null @@ -1,13 +0,0 @@ -use std::str::FromStr; - -use telchar_codegen::Codegen; -use telchar_codegen::template::Template; - -pub async fn run(sub_matches: &clap::ArgMatches) { - let codegen = Codegen::new(); - let blueprint_reference = sub_matches.get_one::("blueprint_path").expect("required"); - let template_reference = sub_matches.get_one::("template").expect("required"); - let blueprint = codegen.get_blueprint_from_path(blueprint_reference.to_string()); - let template = Template::from_str(&template_reference).unwrap(); - println!("{}", codegen.get_template_from_blueprint(blueprint, template)); -} \ No newline at end of file diff --git a/cli/src/command/install.rs b/cli/src/command/install.rs new file mode 100644 index 0000000..a0d2b24 --- /dev/null +++ b/cli/src/command/install.rs @@ -0,0 +1,29 @@ +use std::fs; +use dirs::home_dir; +use crate::run_blueprint_query; + +pub async fn run(sub_matches: &clap::ArgMatches) { + let graphql_url = dotenv!("GRAPHQL_URL"); + + let blueprint_reference = sub_matches.get_one::("blueprint").expect("required"); + if let Some((scope, name)) = blueprint_reference.split_once('/') { + let blueprint_url = run_blueprint_query(graphql_url.to_string(), scope.to_string(), name.to_string()).await; + if let Some(blueprint_url) = blueprint_url { + let telchar_path = home_dir().unwrap().join(".telchar").join("protocol"); + fs::create_dir_all(&telchar_path).unwrap(); + + let blueprint_path = telchar_path.join(format!("{}_{}.json", scope, name)); + + let url = blueprint_url.replace("github.com", "raw.githubusercontent.com").replace("/blob", ""); + let response = surf::get(url).await.unwrap().body_string().await.unwrap(); + + fs::write(blueprint_path, response).unwrap(); + + println!("Blueprint installed"); + } else { + println!("Blueprint not found"); + } + } else { + println!("Invalid blueprint reference provided"); + } +} \ No newline at end of file diff --git a/cli/src/command/mod.rs b/cli/src/command/mod.rs index 2444fe2..164d42d 100644 --- a/cli/src/command/mod.rs +++ b/cli/src/command/mod.rs @@ -1,15 +1,15 @@ +mod install; mod codegen; -mod codegen_local; mod publish; pub async fn execute(matches: clap::ArgMatches) { match matches.subcommand() { + Some(("install", sub_matches)) => { + install::run(sub_matches).await; + } Some(("codegen", sub_matches)) => { codegen::run(sub_matches).await; } - Some(("codegen-local", sub_matches)) => { - codegen_local::run(sub_matches).await; - } Some(("publish", _)) => { publish::run().await; } diff --git a/cli/src/main.rs b/cli/src/main.rs index 830241d..9db7530 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -15,7 +15,6 @@ mod schema {} pub struct CodegenQueryVariables { pub name: String, pub scope: String, - pub template: String, } #[derive(cynic::QueryFragment, Debug)] @@ -28,21 +27,14 @@ pub struct CodegenQuery { #[derive(cynic::QueryFragment, Debug)] #[cynic(variables = "CodegenQueryVariables")] pub struct Dapp { - pub blueprint: DappBlueprint, + pub blueprint_url: String, } -#[derive(cynic::QueryFragment, Debug)] -#[cynic(variables = "CodegenQueryVariables")] -pub struct DappBlueprint { - #[arguments(template: $template)] - pub codegen: String, -} - -async fn run_codegen_query(graphql_url: String, scope: String, name: String, template: String) -> Option { - let query = CodegenQuery::build(CodegenQueryVariables { name, scope, template }); +async fn run_blueprint_query(graphql_url: String, scope: String, name: String) -> Option { + let query = CodegenQuery::build(CodegenQueryVariables { name, scope }); let response = surf::post(graphql_url).run_graphql(query).await.unwrap().data; match response { - Some(CodegenQuery { dapp: Some(dapp) }) => Some(dapp.blueprint.codegen), + Some(CodegenQuery { dapp: Some(dapp) }) => Some(dapp.blueprint_url), _ => None } } @@ -53,18 +45,17 @@ async fn main() { .about("Telchar CLI") .subcommand_required(true) .arg_required_else_help(true) + .subcommand( + Command::new("install") + .about("Installs a blueprint from the registry") + .arg(arg!( "The blueprint reference for installation")) + .arg_required_else_help(true)) .subcommand( Command::new("codegen") .about("Generates the boilerplate code for the selected blueprint") .arg(arg!( "The blueprint reference for the code generation")) .arg(arg!(