Skip to content

Commit 38123d5

Browse files
m4txseqre
andauthored
feat(cli): handle imports in the migration generator (#66)
* feat(cli): handle imports in the migration generator 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). * Update flareon-cli/src/migration_generator.rs Co-authored-by: Marek Grzelak <[email protected]> * Update arg name after refactoring --------- Co-authored-by: Marek Grzelak <[email protected]>
1 parent d6b2eb6 commit 38123d5

File tree

9 files changed

+733
-86
lines changed

9 files changed

+733
-86
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ sqlx = { version = "0.8", default-features = false, features = ["macros", "json"
7171
subtle = "2"
7272
syn = { version = "2", features = ["full", "extra-traits"] }
7373
sync_wrapper = "1"
74+
tempfile = "3"
7475
thiserror = "2"
7576
time = "0.3.35"
7677
tokio = { version = "1.40", features = ["macros", "rt-multi-thread"] }

flareon-cli/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ prettyplease.workspace = true
2121
proc-macro2 = { workspace = true, features = ["span-locations"] }
2222
quote.workspace = true
2323
syn.workspace = true
24+
25+
[dev-dependencies]
26+
tempfile.workspace = true
27+
trybuild.workspace = true

flareon-cli/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod migration_generator;
2+
mod utils;

flareon-cli/src/main.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use anyhow::Context;
77
use clap::{Parser, Subcommand};
88
use clap_verbosity_flag::Verbosity;
99

10-
use crate::migration_generator::make_migrations;
10+
use crate::migration_generator::{make_migrations, MigrationGeneratorOptions};
1111

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

2121
#[derive(Debug, Subcommand)]
2222
enum Commands {
23-
MakeMigrations { path: Option<PathBuf> },
23+
MakeMigrations {
24+
/// Path to the crate directory to generate migrations for (default:
25+
/// current directory)
26+
path: Option<PathBuf>,
27+
/// Name of the app to use in the migration (default: crate name)
28+
#[arg(long)]
29+
app_name: Option<String>,
30+
/// Directory to write the migrations to (default: migrations/ directory
31+
/// in the crate's src/ directory)
32+
#[arg(long)]
33+
output_dir: Option<PathBuf>,
34+
},
2435
}
2536

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

3344
match cli.command {
34-
Commands::MakeMigrations { path } => {
45+
Commands::MakeMigrations {
46+
path,
47+
app_name,
48+
output_dir,
49+
} => {
3550
let path = path.unwrap_or_else(|| PathBuf::from("."));
36-
make_migrations(&path).with_context(|| "unable to create migrations")?;
51+
let options = MigrationGeneratorOptions {
52+
app_name,
53+
output_dir,
54+
};
55+
make_migrations(&path, options).with_context(|| "unable to create migrations")?;
3756
}
3857
}
3958

0 commit comments

Comments
 (0)