90 lines
2.3 KiB
Rust
90 lines
2.3 KiB
Rust
use std::env;
|
|
use std::fs::create_dir;
|
|
use std::io::Write;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use crate::command_builder::CommandBuilder;
|
|
use crate::{create_file, safe_create_file};
|
|
|
|
fn get_django_settings_module() -> Result<String> {
|
|
let dsm = env::var("DJANGO_SETTINGS_MODULE")?;
|
|
eprintln!("USING: {dsm}");
|
|
|
|
Ok(dsm)
|
|
}
|
|
|
|
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(app: &PathBuf, name: &str) -> Result<()> {
|
|
let app_name = app.to_string_lossy();
|
|
let app_dir = Path::new(&app);
|
|
|
|
let not_app_exists = !app_dir.is_dir();
|
|
if not_app_exists {
|
|
return Err(anyhow!("directory {app_name} does not exist"));
|
|
}
|
|
|
|
eprintln!("found app {app_name}");
|
|
|
|
let management_dir = app_dir.join("management");
|
|
|
|
let not_management_exists = !management_dir.exists();
|
|
if not_management_exists {
|
|
create_dir(&management_dir)?;
|
|
create_file(management_dir.join("__init__.py"))?;
|
|
|
|
eprintln!("created module {app_name}.management")
|
|
};
|
|
|
|
let commands_dir = management_dir.join("commands");
|
|
|
|
let not_commands_exists = !commands_dir.exists();
|
|
if not_commands_exists {
|
|
create_dir(&commands_dir)?;
|
|
create_file(commands_dir.join("__init__.py"))?;
|
|
|
|
eprintln!("created module {app_name}.management.commands")
|
|
};
|
|
|
|
let mut file = safe_create_file(commands_dir.join(format!("{name}.py")))?;
|
|
file.write_all(DEBUG_TEMPLATE.as_bytes())?;
|
|
|
|
eprintln!("created command {app_name}.management.commands.{name}");
|
|
Ok(())
|
|
}
|
|
|
|
pub fn manage(rest: &[String]) -> Result<()> {
|
|
let dsm = get_django_settings_module()?;
|
|
let joined = rest.join(" ");
|
|
let command = format!("run --rm appserver python manage.py {joined} --settings={dsm}");
|
|
CommandBuilder::docker_compose().args(&command).exec()
|
|
}
|
|
|
|
// shortcuts
|
|
pub fn makemigrations() -> Result<()> {
|
|
manage(&["makemigrations".to_string()])
|
|
}
|
|
|
|
pub fn migrate(rest: &[String]) -> Result<()> {
|
|
let mut full_rest = vec!["migrate".to_string()];
|
|
full_rest.extend_from_slice(rest);
|
|
|
|
manage(&full_rest)
|
|
}
|
|
|
|
pub fn shell() -> Result<()> {
|
|
manage(&["shell".to_string()])
|
|
}
|
|
|
|
pub fn test() -> Result<()> {
|
|
manage(&["test".to_string()])
|
|
}
|