Skip to content

Commit

Permalink
feat(cli): rework generator api (#4984)
Browse files Browse the repository at this point in the history
  • Loading branch information
tknickman committed May 16, 2023
1 parent e3549be commit 721bfcf
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 38 deletions.
78 changes: 54 additions & 24 deletions crates/turborepo-lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,27 @@ pub enum Command {
target: LinkTarget,
},
/// Generate a new app / package
#[clap(aliases = ["g", "gen"])]
Generate {
#[serde(skip)]
#[clap(long, default_value_t = String::from("latest"), hide = true)]
tag: String,
/// The name of the generator to run
generator_name: Option<String>,
/// Generator configuration file
#[clap(short = 'c', long)]
config: Option<String>,
/// The root of your repository (default: directory with root
/// turbo.json)
#[clap(short = 'r', long)]
root: Option<String>,
/// Answers passed directly to generator
#[clap(short = 'a', long, value_delimiter = ' ', num_args = 1..)]
args: Vec<String>,

#[clap(subcommand)]
#[serde(flatten)]
command: GenerateCommand,
#[serde(skip)]
command: Option<GenerateCommand>,
},
/// Login to your Vercel account
Login {
Expand Down Expand Up @@ -309,22 +324,7 @@ pub enum Command {
}

#[derive(Parser, Clone, Debug, Default, Serialize, PartialEq)]
pub struct GenerateCustomArgs {
/// The name of the generator to run
pub generator_name: Option<String>,
/// Generator configuration file
#[clap(short = 'c', long)]
pub config: Option<String>,
/// The root of your repository (default: directory with root turbo.json)
#[clap(short = 'r', long)]
pub root: Option<String>,
/// Answers passed directly to generator
#[clap(short = 'a', long, value_delimiter = ' ', num_args = 1..)]
pub args: Vec<String>,
}

#[derive(Parser, Clone, Debug, Default, Serialize, PartialEq)]
pub struct GenerateAddArgs {
pub struct GenerateWorkspaceArgs {
/// Name for the new workspace
#[clap(short = 'n', long)]
pub name: Option<String>,
Expand Down Expand Up @@ -358,14 +358,29 @@ pub struct GenerateAddArgs {
pub show_all_dependencies: bool,
}

#[derive(Parser, Clone, Debug, Default, Serialize, PartialEq)]
pub struct GeneratorCustomArgs {
/// The name of the generator to run
generator_name: Option<String>,
/// Generator configuration file
#[clap(short = 'c', long)]
config: Option<String>,
/// The root of your repository (default: directory with root
/// turbo.json)
#[clap(short = 'r', long)]
root: Option<String>,
/// Answers passed directly to generator
#[clap(short = 'a', long, value_delimiter = ' ', num_args = 1..)]
args: Vec<String>,
}

#[derive(Subcommand, Clone, Debug, Serialize, PartialEq)]
pub enum GenerateCommand {
/// Add a new package or app to your project
#[clap(name = "add", alias = "a")]
Add(GenerateAddArgs),
/// Run custom generators
#[clap(name = "workspace", alias = "w")]
Workspace(GenerateWorkspaceArgs),
#[clap(name = "run", alias = "r")]
Custom(GenerateCustomArgs),
Run(GeneratorCustomArgs),
}

#[derive(Parser, Clone, Debug, Default, Serialize, PartialEq)]
Expand Down Expand Up @@ -635,8 +650,23 @@ pub async fn run(

Ok(Payload::Rust(Ok(0)))
}
Command::Generate { command, tag } => {
generate::run(command, tag)?;
Command::Generate {
tag,
generator_name,
config,
root,
args,
command,
} => {
// build GeneratorCustomArgs struct
let args = GeneratorCustomArgs {
generator_name: generator_name.clone(),
config: config.clone(),
root: root.clone(),
args: args.clone(),
};

generate::run(tag, command, &args)?;
Ok(Payload::Rust(Ok(0)))
}
Command::Daemon { command, idle_time } => {
Expand Down
34 changes: 20 additions & 14 deletions crates/turborepo-lib/src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::process::{Command, Stdio};

use anyhow::Result;

use crate::{child::spawn_child, cli::GenerateCommand};
use crate::{
child::spawn_child,
cli::{GenerateCommand, GeneratorCustomArgs},
};

fn verify_requirements() -> Result<()> {
let output = Command::new("npx")
Expand Down Expand Up @@ -34,25 +37,28 @@ fn call_turbo_gen(command: &str, tag: &String, raw_args: &str) -> Result<i32> {
Ok(exit_code)
}

pub fn run(command: &GenerateCommand, tag: &String) -> Result<()> {
pub fn run(
tag: &String,
command: &Option<GenerateCommand>,
args: &GeneratorCustomArgs,
) -> Result<()> {
// ensure npx is available
verify_requirements()?;

match command {
GenerateCommand::Add(args) => {
let mut add_args = args.clone();
// example implies copy
if add_args.example.is_some() {
add_args.copy = true;
add_args.empty = false;
// check if a subcommand was passed
Some(command) => {
if let GenerateCommand::Workspace(workspace_args) = command {
let raw_args = serde_json::to_string(&workspace_args)?;
call_turbo_gen("add", tag, &raw_args)?;
} else {
let raw_args = serde_json::to_string(&args)?;
call_turbo_gen("generate", tag, &raw_args)?;
}

// convert args to json
let raw_args = serde_json::to_string(&add_args)?;
call_turbo_gen("add", tag, &raw_args)?;
}
GenerateCommand::Custom(args) => {
let raw_args = serde_json::to_string(args)?;
// if no subcommand was passed, run the generate command as default
None => {
let raw_args = serde_json::to_string(&args)?;
call_turbo_gen("generate", tag, &raw_args)?;
}
};
Expand Down

0 comments on commit 721bfcf

Please sign in to comment.