Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): rework generator api #4984

Merged
merged 1 commit into from
May 16, 2023
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
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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default commands in clap are a little tricky, so Generate becomes the default command (run) and then the subcommand is optional.

#[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