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

improve scaffold generator #749

Merged
merged 3 commits into from
Sep 16, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/ci-generators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
run: cargo install sea-orm-cli

- name: scaffold
run: cargo run -- generate scaffold car name:string year:int && cargo build
run: cargo run -- generate scaffold --htmx car name:string year:int && cargo build
working-directory: ./examples/demo
env:
REDIS_URL: redis://localhost:${{job.services.redis.ports[6379]}}
Expand Down Expand Up @@ -215,7 +215,7 @@ jobs:

- name: test ${{ matrix.field_type }} scaffold field type
run: |
cargo run -- generate scaffold room \
cargo run -- generate scaffold --api room \
uuid_uniq:uuid \
uuid_null:uuid_col \
uuid:uuid_col! \
Expand Down
5 changes: 4 additions & 1 deletion docs-site/content/docs/the-app/your-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ Arguments:
[FIELDS]... Model fields, eg. title:string hits:int

Options:
-k, --kind <KIND> The kind of scaffold to generate [default: api] [possible values: api, html, htmx]
-k, --kind <KIND> The kind of scaffold to generate [possible values: api, html, htmx]
--htmx Use HTMX scaffold
--html Use HTML scaffold
--api Use API scaffold
-e, --environment <ENVIRONMENT> Specify the environment [default: development]
-h, --help Print help
-V, --version Print version
Expand Down
68 changes: 52 additions & 16 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::{
ServeParams, StartMode,
},
environment::{resolve_from_env, Environment, DEFAULT_ENVIRONMENT},
gen::{self, Component},
gen::{self, Component, ScaffoldKind},
logger, task,
};
#[derive(Parser)]
Expand Down Expand Up @@ -160,8 +160,20 @@ enum ComponentArg {
fields: Vec<(String, String)>,

/// The kind of scaffold to generate
#[clap(short, long, value_enum, default_value_t = gen::ScaffoldKind::Api)]
kind: gen::ScaffoldKind,
#[clap(short, long, value_enum, group = "scaffold_kind_group")]
kind: Option<gen::ScaffoldKind>,

/// Use HTMX scaffold
#[clap(long, group = "scaffold_kind_group")]
htmx: bool,

/// Use HTML scaffold
#[clap(long, group = "scaffold_kind_group")]
html: bool,

/// Use API scaffold
#[clap(long, group = "scaffold_kind_group")]
api: bool,
},
/// Generate a new controller with the given controller name, and test file.
Controller {
Expand Down Expand Up @@ -189,31 +201,55 @@ enum ComponentArg {
Deployment {},
}

impl From<ComponentArg> for Component {
fn from(value: ComponentArg) -> Self {
impl TryFrom<ComponentArg> for Component {
type Error = crate::Error;
fn try_from(value: ComponentArg) -> Result<Self, Self::Error> {
match value {
#[cfg(feature = "with-db")]
ComponentArg::Model {
name,
link,
migration_only,
fields,
} => Self::Model {
} => Ok(Self::Model {
name,
link,
migration_only,
fields,
},
}),
#[cfg(feature = "with-db")]
ComponentArg::Migration { name } => Self::Migration { name },
ComponentArg::Migration { name } => Ok(Self::Migration { name }),
#[cfg(feature = "with-db")]
ComponentArg::Scaffold { name, fields, kind } => Self::Scaffold { name, fields, kind },
ComponentArg::Controller { name } => Self::Controller { name },
ComponentArg::Task { name } => Self::Task { name },
ComponentArg::Scheduler {} => Self::Scheduler {},
ComponentArg::Worker { name } => Self::Worker { name },
ComponentArg::Mailer { name } => Self::Mailer { name },
ComponentArg::Deployment {} => Self::Deployment {},
ComponentArg::Scaffold {
name,
fields,
kind,
htmx,
html,
api,
} => {
let kind = if let Some(kind) = kind {
kind
} else if htmx {
ScaffoldKind::Htmx
} else if html {
ScaffoldKind::Html
} else if api {
ScaffoldKind::Api
} else {
return Err(crate::Error::string(
"Error: One of `kind`, `htmx`, `html`, or `api` must be specified.",
));
};

Ok(Self::Scaffold { name, fields, kind })
}
ComponentArg::Controller { name } => Ok(Self::Controller { name }),
ComponentArg::Task { name } => Ok(Self::Task { name }),
ComponentArg::Scheduler {} => Ok(Self::Scheduler {}),
ComponentArg::Worker { name } => Ok(Self::Worker { name }),
ComponentArg::Mailer { name } => Ok(Self::Mailer { name }),
ComponentArg::Deployment {} => Ok(Self::Deployment {}),
}
}
}
Expand Down Expand Up @@ -375,7 +411,7 @@ pub async fn main<H: Hooks, M: MigratorTrait>() -> crate::Result<()> {
run_scheduler::<H>(&app_context, config.as_ref(), name, tag, list).await?;
}
Commands::Generate { component } => {
gen::generate::<H>(component.into(), &config)?;
gen::generate::<H>(component.try_into()?, &config)?;
}
Commands::Doctor {} => {
let mut should_exit = false;
Expand Down
Loading