Skip to content

Commit 537cd50

Browse files
committed
change project to rack
Signed-off-by: scepter914 <scepter914@gmail.com>
1 parent 56e8840 commit 537cd50

8 files changed

Lines changed: 125 additions & 115 deletions

File tree

src/bin/ml-cellar.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
use clap::{Parser, Subcommand};
2-
use ml_cellar::repository_config::RepositoryConfig;
2+
use ml_cellar::file::{GITATTRIBUTES, TEMPLATE_MD};
3+
use ml_cellar::rack::RackConfig;
4+
use ml_cellar::repository::RepositoryConfig;
35
use std::path::PathBuf;
46
use std::process::{Command, Stdio};
57
use toml::to_string_pretty;
68

7-
use ml_cellar::file::{GITATTRIBUTES, TEMPLATE_MD};
8-
use ml_cellar::project_config::ProjectConfig;
9-
109
#[derive(Parser, Debug)]
1110
#[command(name = "ml-cellar")]
1211
#[command(about = "ML Cellar CLI", version)]
1312
struct Cli {
13+
/// Subcommands
1414
#[command(subcommand)]
1515
command: Commands,
1616
}
1717

1818
#[derive(Subcommand, Debug)]
1919
enum Commands {
20-
/// Create a project at {path}
21-
Project {
22-
/// Project path
20+
/// Create a new rack at the specified path
21+
/// "Rack" is a collection of ML models and related files.
22+
Rack {
23+
/// Path to the new rack
2324
path: PathBuf,
2425
},
26+
/// Initialize a new repository
2527
Init {},
2628
}
2729
fn main() {
@@ -30,8 +32,8 @@ fn main() {
3032
Commands::Init {} => {
3133
init();
3234
}
33-
Commands::Project { path } => {
34-
project(path);
35+
Commands::Rack { path } => {
36+
rack(path);
3537
}
3638
}
3739
}
@@ -88,29 +90,32 @@ fn init() {
8890

8991
// Make .mlcellar.toml
9092
let config = RepositoryConfig::default();
91-
let toml_str = to_string_pretty(&config).expect("failed to serialize project config to TOML");
93+
let toml_str =
94+
to_string_pretty(&config).expect("failed to serialize repository config to TOML");
9295
std::fs::write(".mlcellar.toml", toml_str).expect("failed to write .mlcellar.toml");
9396
}
9497

95-
fn project(path: PathBuf) {
98+
fn rack(path: PathBuf) {
9699
if path.exists() {
97100
eprintln!("{:?} already exists", path);
98101
std::process::exit(1);
99102
} else {
100-
std::fs::create_dir_all(&path).expect("failed to create project dir");
101-
println!("Create project at {}", path.display());
103+
let rack_name = path.file_name().unwrap().to_str().unwrap();
104+
105+
std::fs::create_dir_all(&path).expect("failed to create the directory for the rack");
106+
println!("Create rack at {}", path.display());
102107

103108
// Make README.md
104109
let readme_path = path.join("README.md");
105-
let readme_content = format!("# {}\n\n## Summary\n\n## Release\n", &path.display());
110+
let readme_content = format!("# {}\n\n## Summary\n\n## Release\n", &rack_name);
106111
std::fs::write(&readme_path, readme_content).expect("failed to write README.md");
107112

108-
// Make project.toml
109-
let config_path = path.join("project.toml");
110-
let config = ProjectConfig::default();
111-
let toml_str =
112-
to_string_pretty(&config).expect("failed to serialize project config to TOML");
113-
std::fs::write(&config_path, toml_str).expect("failed to write project.toml");
113+
// Make config.toml
114+
let config_path = path.join("config.toml");
115+
let mut config = RackConfig::default();
116+
config.rack.name = rack_name.to_owned();
117+
let toml_str = to_string_pretty(&config).expect("failed to serialize rack config to TOML");
118+
std::fs::write(&config_path, toml_str).expect("failed to write config.toml");
114119

115120
// Make template.md
116121
let template_path = path.join("template.md");

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
pub mod file;
2-
pub mod project_config;
3-
pub mod repository_config;
2+
pub mod rack;
3+
pub mod repository;

src/project_config.rs

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/rack.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Debug, Deserialize, Serialize, Default)]
4+
#[serde(default)]
5+
pub struct RackConfig {
6+
pub rack: RackInfoConfig,
7+
pub artifact: ArtifactConfig,
8+
pub project: ProjectConfig,
9+
pub document: DocumentConfig,
10+
}
11+
12+
#[derive(Debug, Deserialize, Serialize, Default)]
13+
#[serde(default)]
14+
pub struct RackInfoConfig {
15+
pub name: String,
16+
}
17+
18+
#[derive(Debug, Deserialize, Serialize, Default)]
19+
#[serde(default)]
20+
pub struct ArtifactConfig {
21+
pub required_files: Vec<String>,
22+
pub optional_files: Vec<String>,
23+
}
24+
25+
#[derive(Debug, Deserialize, Serialize, Default)]
26+
#[serde(default)]
27+
pub struct ProjectConfig {
28+
pub project: Option<Vec<String>>,
29+
pub version_yymmdd: Option<Vec<String>>,
30+
pub version_x: Option<Vec<String>>,
31+
pub version_x_y: Option<Vec<String>>,
32+
}
33+
34+
#[derive(Debug, Deserialize, Serialize, Default)]
35+
#[serde(default)]
36+
pub struct DocumentConfig {
37+
pub template_file: Option<String>,
38+
pub result_file: Option<String>,
39+
}

tests/test_init.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn test_init() {
2020
assert!(gitattributes_path.is_file(), ".gitattributes should exist");
2121

2222
let gitattributes_text = fs::read_to_string(&gitattributes_path).unwrap();
23-
let target_gitattributes = fs::read_to_string("tests/fixtures/.gitattributes").unwrap();
23+
let target_gitattributes = fs::read_to_string("tests/fixtures/init_test/.gitattributes").unwrap();
2424
assert_eq!(
2525
gitattributes_text, target_gitattributes,
2626
".gitattributes is \n***\n{} \n*** \n and different from the target file.",
@@ -35,7 +35,7 @@ fn test_init() {
3535
);
3636

3737
let mlcellar_text = fs::read_to_string(&repository_config_path).unwrap();
38-
let target_mlcellar = fs::read_to_string("tests/fixtures/.mlcellar.toml").unwrap();
38+
let target_mlcellar = fs::read_to_string("tests/fixtures/init_test/.mlcellar.toml").unwrap();
3939
assert_eq!(
4040
mlcellar_text, target_mlcellar,
4141
".mlcellar.toml is \n***\n{} \n*** \n and different from the target file.",

tests/test_project.rs

Lines changed: 0 additions & 55 deletions
This file was deleted.

tests/test_rack.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use assert_cmd::cargo::cargo_bin_cmd;
2+
use predicates::prelude::*;
3+
use std::fs;
4+
use tempfile::tempdir;
5+
6+
/// Test `ml-cellar rack {path}`
7+
/// This test uses `tests/fixtures/test_registry/algorithm_1/` as test directory
8+
/// The directory `tests/fixtures/test_registry/algorithm_1/` is the expected output of `ml-cellar rack tests/fixtures/test_registry/algorithm_1/`.
9+
#[test]
10+
fn test_rack() {
11+
let temp_dir = tempdir().unwrap();
12+
let rack_path = temp_dir.path().join("my_rack");
13+
14+
let mut cmd = cargo_bin_cmd!("ml-cellar");
15+
cmd.arg("rack").arg(&rack_path);
16+
cmd.assert()
17+
.success()
18+
.stdout(predicate::str::contains("Create rack at"));
19+
20+
// Check that the rack directory was created
21+
assert!(rack_path.is_dir());
22+
23+
// Check README.md, project.toml, and template.md
24+
let readme = rack_path.join("README.md");
25+
let config = rack_path.join("config.toml");
26+
let template = rack_path.join("template.md");
27+
28+
assert!(readme.is_file(), "README.md should exist");
29+
assert!(config.is_file(), "config.toml should exist");
30+
assert!(template.is_file(), "template.md should exist");
31+
32+
// Check contents of README.md
33+
let readme_text = fs::read_to_string(&readme).unwrap();
34+
assert!(readme_text.contains("my_rack"), "README content is wrong");
35+
36+
// Check contents of config.toml
37+
let config_text = fs::read_to_string(&config).unwrap();
38+
let target_config =
39+
fs::read_to_string("tests/fixtures/test_registry/algorithm_1/config.toml").unwrap();
40+
41+
assert_eq!(
42+
config_text, target_config,
43+
"config.toml is \n***\n{} \n*** \n and different from the config file.",
44+
config_text
45+
);
46+
47+
// Check contents of template.md
48+
let template_text = fs::read_to_string(&template).unwrap();
49+
let target_template =
50+
fs::read_to_string("tests/fixtures/test_registry/algorithm_1/template.md").unwrap();
51+
52+
assert_eq!(
53+
template_text, target_template,
54+
"template.md is \n***\n{} \n*** \n and different from the template file.",
55+
template_text
56+
);
57+
}

0 commit comments

Comments
 (0)