Skip to content

Commit

Permalink
feat(cli): handle imports in the migration generator
Browse files Browse the repository at this point in the history
Previously, `use` statements had to be added manually after generating a
migration. This commit adds a simple solution that scans the file for
top-level `use`s, `struct`s and `const`s so that they are imported
automatically.

In addition to that, this adds some tests to the migration generator and
some useful options to the CLI (--app-name and --output-dir to override
the migration's app name and generator's output directory).
  • Loading branch information
m4tx committed Nov 12, 2024
1 parent c7bbf69 commit 070ed84
Show file tree
Hide file tree
Showing 9 changed files with 741 additions and 80 deletions.
2 changes: 2 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ sqlx = { version = "0.8", default-features = false, features = ["macros", "json"
subtle = "2"
syn = { version = "2", features = ["full", "extra-traits"] }
sync_wrapper = "1"
tempfile = "3"
thiserror = "2"
time = "0.3.35"
tokio = { version = "1.40", features = ["macros", "rt-multi-thread"] }
Expand Down
4 changes: 4 additions & 0 deletions flareon-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ prettyplease.workspace = true
proc-macro2 = { workspace = true, features = ["span-locations"] }
quote.workspace = true
syn.workspace = true

[dev-dependencies]
tempfile.workspace = true
trybuild.workspace = true
2 changes: 2 additions & 0 deletions flareon-cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod migration_generator;
mod utils;
27 changes: 23 additions & 4 deletions flareon-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use anyhow::Context;
use clap::{Parser, Subcommand};
use clap_verbosity_flag::Verbosity;

use crate::migration_generator::make_migrations;
use crate::migration_generator::{make_migrations, MigrationGeneratorOptions};

#[derive(Debug, Parser)]
#[command(version, about, long_about = None)]
Expand All @@ -20,7 +20,18 @@ struct Cli {

#[derive(Debug, Subcommand)]
enum Commands {
MakeMigrations { path: Option<PathBuf> },
MakeMigrations {
/// Path to the crate directory to generate migrations for (default:
/// current directory)
path: Option<PathBuf>,
/// Name of the app to use in the migration (default: crate name)
#[arg(long)]
app_name: Option<String>,
/// Directory to write the migrations to (default: migrations/ directory
/// in the crate's src/ directory)
#[arg(long)]
output_dir: Option<PathBuf>,
},
}

fn main() -> anyhow::Result<()> {
Expand All @@ -31,9 +42,17 @@ fn main() -> anyhow::Result<()> {
.init();

match cli.command {
Commands::MakeMigrations { path } => {
Commands::MakeMigrations {
path,
app_name,
output_dir,
} => {
let path = path.unwrap_or_else(|| PathBuf::from("."));
make_migrations(&path).with_context(|| "unable to create migrations")?;
let options = MigrationGeneratorOptions {
app_name,
output_dir,
};
make_migrations(&path, options).with_context(|| "unable to create migrations")?;
}
}

Expand Down
Loading

0 comments on commit 070ed84

Please sign in to comment.