92 lines
2.2 KiB
Rust
92 lines
2.2 KiB
Rust
use std::path::{Path, PathBuf};
|
|
|
|
use anyhow::{Result, anyhow};
|
|
|
|
use crate::cmd::{Bash, Cmd, Manage, Words};
|
|
use crate::ctx::Ctx;
|
|
use crate::fsops::{create_dir, touch, write_new};
|
|
use crate::output::note;
|
|
use crate::project::Project;
|
|
|
|
const DEBUG_TEMPLATE: &str = r#"from django.core.management.base import BaseCommand
|
|
|
|
class Command(BaseCommand):
|
|
def handle(self, *args, **options):
|
|
pass
|
|
|
|
"#;
|
|
|
|
pub fn make_command(ctx: &Ctx, app: &PathBuf, name: &str) -> Result<()> {
|
|
let app_name = app.to_string_lossy();
|
|
let app_dir = Path::new(&app);
|
|
|
|
if !app_dir.is_dir() {
|
|
return Err(anyhow!("directory {app_name} does not exist"));
|
|
}
|
|
|
|
note!("found app {app_name}");
|
|
|
|
let management_dir = app_dir.join("management");
|
|
|
|
if !management_dir.exists() {
|
|
create_dir(ctx, &management_dir)?;
|
|
touch(ctx, &management_dir.join("__init__.py"))?;
|
|
|
|
note!("created module {app_name}.management")
|
|
};
|
|
|
|
let commands_dir = management_dir.join("commands");
|
|
|
|
if !commands_dir.exists() {
|
|
create_dir(ctx, &commands_dir)?;
|
|
touch(ctx, &commands_dir.join("__init__.py"))?;
|
|
|
|
note!("created module {app_name}.management.commands")
|
|
};
|
|
|
|
write_new(
|
|
ctx,
|
|
&commands_dir.join(format!("{name}.py")),
|
|
DEBUG_TEMPLATE.as_bytes(),
|
|
)?;
|
|
|
|
note!("created command {app_name}.management.commands.{name}");
|
|
Ok(())
|
|
}
|
|
|
|
pub fn bash(ctx: &Ctx) -> Result<()> {
|
|
Bash.in_service(&service(ctx)?).replace(ctx)
|
|
}
|
|
|
|
pub fn run(ctx: &Ctx, rest: &[String]) -> Result<()> {
|
|
Words::new(rest).in_service(&service(ctx)?).replace(ctx)
|
|
}
|
|
|
|
pub fn manage(ctx: &Ctx, rest: &[String]) -> Result<()> {
|
|
Manage::new(rest).in_service(&service(ctx)?).replace(ctx)
|
|
}
|
|
|
|
// shortcuts
|
|
pub fn makemigrations(ctx: &Ctx) -> Result<()> {
|
|
manage(ctx, &["makemigrations".to_string()])
|
|
}
|
|
|
|
pub fn migrate(ctx: &Ctx, rest: &[String]) -> Result<()> {
|
|
let mut args = vec!["migrate".to_string()];
|
|
args.extend_from_slice(rest);
|
|
|
|
manage(ctx, &args)
|
|
}
|
|
|
|
pub fn shell(ctx: &Ctx) -> Result<()> {
|
|
manage(ctx, &["shell".to_string()])
|
|
}
|
|
|
|
pub fn test(ctx: &Ctx) -> Result<()> {
|
|
manage(ctx, &["test".to_string()])
|
|
}
|
|
|
|
fn service(ctx: &Ctx) -> Result<String> {
|
|
Project::resolve(ctx)?.django()
|
|
}
|