Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions cli/Cargo.lock

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

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
17 changes: 10 additions & 7 deletions cli/src/command/codegen.rs
Original file line number Diff line number Diff line change
@@ -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::<String>("blueprint").expect("required");
let template_reference = sub_matches.get_one::<String>("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");
}
Expand Down
13 changes: 0 additions & 13 deletions cli/src/command/codegen_local.rs

This file was deleted.

32 changes: 32 additions & 0 deletions cli/src/command/install.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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::<String>("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 mut url = blueprint_url;
if url.contains("github.com") {
url = 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");
}
}
8 changes: 4 additions & 4 deletions cli/src/command/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
27 changes: 9 additions & 18 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ mod schema {}
pub struct CodegenQueryVariables {
pub name: String,
pub scope: String,
pub template: String,
}

#[derive(cynic::QueryFragment, Debug)]
Expand All @@ -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<String> {
let query = CodegenQuery::build(CodegenQueryVariables { name, scope, template });
async fn run_blueprint_query(graphql_url: String, scope: String, name: String) -> Option<String> {
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
}
}
Expand All @@ -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!(<blueprint> "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!(<blueprint> "The blueprint reference for the code generation"))
.arg(arg!(<template> "The template reference for the code generation"))
.arg_required_else_help(true))
.subcommand(
Command::new("codegen-local")
.about("Generates the boilerplate code for the selected local blueprint file")
.arg(arg!(<blueprint_path> "The blueprint file path reference for the code generation"))
.arg(arg!(<template> "The template reference for the code generation"))
.arg_required_else_help(true))
.subcommand(
Command::new("publish")
.about("Publish a blueprint to the registry"));
Expand Down
Loading
Loading