Skip to content

Commit 0804e69

Browse files
authored
Merge pull request #402 from sanders41/just
Add justfile for poetry and setuptools projects
2 parents 81f25f6 + 9d0fe0e commit 0804e69

4 files changed

+108
-19
lines changed

src/project_generator.rs

Lines changed: 98 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,44 @@ fn save_dev_requirements(project_info: &ProjectInfo) -> Result<()> {
480480
Ok(())
481481
}
482482

483+
fn create_justfile(module: &str) -> String {
484+
format!(
485+
r#"@lint:
486+
echo mypy
487+
just --justfile {{{{justfile()}}}} mypy
488+
echo ruff
489+
just --justfile {{{{justfile()}}}} ruff
490+
echo ruff-format
491+
just --justfile {{{{justfile()}}}} ruff-format
492+
493+
@mypy:
494+
poetry run mypy {module} tests
495+
496+
@ruff:
497+
poetry run ruff check {module} tests
498+
499+
@ruff-format:
500+
poetry run ruff format {module} tests
501+
502+
@test:
503+
-poetry run pytest -x
504+
505+
@install:
506+
poetry install
507+
"#
508+
)
509+
}
510+
511+
fn save_justfile(project_info: &ProjectInfo) -> Result<()> {
512+
let module = project_info.source_dir.replace([' ', '-'], "_");
513+
let file_path = project_info.base_dir().join("justfile");
514+
let content = create_justfile(&module);
515+
516+
save_file_with_content(&file_path, &content)?;
517+
518+
Ok(())
519+
}
520+
483521
fn create_pyo3_justfile(module: &str) -> String {
484522
format!(
485523
r#"@develop:
@@ -518,12 +556,11 @@ fn create_pyo3_justfile(module: &str) -> String {
518556
ruff check . --fix
519557
520558
@ruff-format:
521-
ruff format {} tests
559+
ruff format {module} tests
522560
523561
@test:
524562
pytest
525-
"#,
526-
module
563+
"#
527564
)
528565
}
529566

@@ -586,27 +623,37 @@ pub fn generate_project(project_info: &ProjectInfo) -> Result<()> {
586623
bail!("Error creating pyproject.toml file");
587624
}
588625

589-
if let ProjectManager::Maturin = &project_info.project_manager {
590-
if save_dev_requirements(project_info).is_err() {
591-
bail!("Error creating requirements-dev.txt file");
626+
match &project_info.project_manager {
627+
ProjectManager::Poetry => {
628+
if save_justfile(project_info).is_err() {
629+
bail!("Error creating justfile");
630+
}
592631
}
632+
ProjectManager::Maturin => {
633+
if save_dev_requirements(project_info).is_err() {
634+
bail!("Error creating requirements-dev.txt file");
635+
}
593636

594-
if save_pyo3_justfile(project_info).is_err() {
595-
bail!("Error creating justfile");
596-
}
637+
if save_pyo3_justfile(project_info).is_err() {
638+
bail!("Error creating justfile");
639+
}
597640

598-
if save_lib_file(project_info).is_err() {
599-
bail!("Error creating Rust lib.rs file");
600-
}
641+
if save_lib_file(project_info).is_err() {
642+
bail!("Error creating Rust lib.rs file");
643+
}
601644

602-
if save_cargo_toml_file(project_info).is_err() {
603-
bail!("Error creating Rust lib.rs file");
645+
if save_cargo_toml_file(project_info).is_err() {
646+
bail!("Error creating Rust lib.rs file");
647+
}
604648
}
605-
}
649+
ProjectManager::Setuptools => {
650+
if save_dev_requirements(project_info).is_err() {
651+
bail!("Error creating requirements-dev.txt file");
652+
}
606653

607-
if let ProjectManager::Setuptools = &project_info.project_manager {
608-
if save_dev_requirements(project_info).is_err() {
609-
bail!("Error creating requirements-dev.txt file");
654+
if save_justfile(project_info).is_err() {
655+
bail!("Error creating justfile");
656+
}
610657
}
611658
}
612659

@@ -987,7 +1034,39 @@ mod tests {
9871034
}
9881035

9891036
#[test]
990-
fn test_save_pyo3_justfile() {
1037+
fn test_save_justfile_poetry() {
1038+
let mut project_info = project_info_dummy();
1039+
project_info.project_manager = ProjectManager::Poetry;
1040+
let base = project_info.base_dir();
1041+
create_dir_all(&base).unwrap();
1042+
let expected_file = base.join("justfile");
1043+
save_justfile(&project_info).unwrap();
1044+
1045+
assert!(expected_file.is_file());
1046+
1047+
let content = std::fs::read_to_string(expected_file).unwrap();
1048+
1049+
assert_yaml_snapshot!(content);
1050+
}
1051+
1052+
#[test]
1053+
fn test_save_justfile_setuptools() {
1054+
let mut project_info = project_info_dummy();
1055+
project_info.project_manager = ProjectManager::Setuptools;
1056+
let base = project_info.base_dir();
1057+
create_dir_all(&base).unwrap();
1058+
let expected_file = base.join("justfile");
1059+
save_justfile(&project_info).unwrap();
1060+
1061+
assert!(expected_file.is_file());
1062+
1063+
let content = std::fs::read_to_string(expected_file).unwrap();
1064+
1065+
assert_yaml_snapshot!(content);
1066+
}
1067+
1068+
#[test]
1069+
fn test_save_justfile_maturin() {
9911070
let mut project_info = project_info_dummy();
9921071
project_info.project_manager = ProjectManager::Maturin;
9931072
project_info.is_application = false;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: src/project_generator.rs
3+
expression: content
4+
---
5+
"@lint:\n echo mypy\n just --justfile {{justfile()}} mypy\n echo ruff\n just --justfile {{justfile()}} ruff\n echo ruff-format\n just --justfile {{justfile()}} ruff-format\n\n@mypy:\n poetry run mypy my_project tests\n\n@ruff:\n poetry run ruff check my_project tests\n\n@ruff-format:\n poetry run ruff format my_project tests\n\n@test:\n -poetry run pytest -x\n\n@install:\n poetry install\n"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: src/project_generator.rs
3+
expression: content
4+
---
5+
"@lint:\n echo mypy\n just --justfile {{justfile()}} mypy\n echo ruff\n just --justfile {{justfile()}} ruff\n echo ruff-format\n just --justfile {{justfile()}} ruff-format\n\n@mypy:\n poetry run mypy my_project tests\n\n@ruff:\n poetry run ruff check my_project tests\n\n@ruff-format:\n poetry run ruff format my_project tests\n\n@test:\n -poetry run pytest -x\n\n@install:\n poetry install\n"

0 commit comments

Comments
 (0)