Skip to content

Commit d5f1bf2

Browse files
committed
refactor: move tasks to scripts directory and update config format to match README
1 parent b9d7468 commit d5f1bf2

File tree

13 files changed

+194
-199
lines changed

13 files changed

+194
-199
lines changed

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ fn main() {
44
println!("cargo:rustc-cfg=feature=\"petgraph\"");
55
println!("cargo:rustc-cfg=feature=\"dialoguer\"");
66
println!("cargo:rustc-cfg=feature=\"serde_json\"");
7-
}
7+
}

scripts/build/script.yaml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
1-
name: build
1+
name: Build Script
22
description: Build tasks for the project
33

4+
# Add paths to the script's execution context
5+
exec_paths:
6+
- target/debug
7+
- target/release
8+
9+
# Set environment variables for this script
10+
env:
11+
RUST_BACKTRACE: "1"
12+
13+
# The default task is invoked by simply running `bodo build`
414
defaultTask:
515
command: cargo build
6-
env:
7-
RUST_BACKTRACE: "1"
816

17+
# Subtasks
918
subtasks:
1019
release:
1120
command: cargo build --release
12-
env:
13-
RUST_BACKTRACE: "1"
1421

1522
check:
1623
command: cargo check
1724

18-
clean:
19-
command: cargo clean
25+
clippy:
26+
command: cargo clippy
27+
28+
fmt:
29+
command: cargo fmt

scripts/check/script.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: check
2+
description: Run all checks for the project, including clippy
3+
4+
defaultTask:
5+
command: echo "All checks completed"
6+
pre_deps:
7+
- clean
8+
- task: build # from scripts/build/script.yaml
9+
- release
10+
- check
11+
- clippy
12+
- test
13+
- fmt
14+
env:
15+
RUST_BACKTRACE: "1"
16+
17+
subtasks:
18+
check:
19+
command: cargo check
20+
21+
clean:
22+
command: cargo clean
23+
24+
clippy:
25+
command: cargo clippy
26+
27+
test:
28+
command: cargo test
29+
30+
fmt:
31+
command: cargo fmt --check

src/cli.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clap::{Parser, Subcommand};
1+
use clap::Parser;
22

33
#[derive(Parser, Debug)]
44
#[command(name = "bodo")]
@@ -19,12 +19,12 @@ pub struct BodoCli {
1919
pub target: Option<String>,
2020

2121
/// Subtask arguments
22-
#[arg(last = true)]
22+
#[arg(index = 2, num_args = 0..)]
2323
pub args: Vec<String>,
2424
}
2525

2626
impl BodoCli {
2727
pub fn new() -> Self {
2828
Self::parse()
2929
}
30-
}
30+
}

src/config.rs

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use serde::{Deserialize, Serialize};
22
use serde_json;
33
use serde_yaml;
4+
use std::error::Error;
45
use std::fs;
6+
use std::path::Path;
7+
use std::collections::HashMap;
58

69
#[derive(Debug, Deserialize, Serialize, Clone)]
710
pub struct BodoConfig {
8-
pub tasks: Option<Vec<TaskConfig>>,
911
pub env_files: Option<Vec<String>>,
1012
pub executable_map: Option<Vec<ExecutableMap>>,
1113
pub max_concurrency: Option<usize>,
@@ -14,7 +16,6 @@ pub struct BodoConfig {
1416

1517
#[derive(Debug, Deserialize, Serialize, Clone)]
1618
pub struct TaskConfig {
17-
pub name: String,
1819
pub command: String,
1920
pub cwd: Option<String>,
2021
pub env: Option<Vec<String>>,
@@ -28,10 +29,20 @@ pub struct ExecutableMap {
2829
pub path: Option<String>,
2930
}
3031

32+
#[derive(Debug, Deserialize, Serialize, Clone)]
33+
pub struct ScriptConfig {
34+
pub name: String,
35+
pub description: Option<String>,
36+
pub exec_paths: Option<Vec<String>>,
37+
pub env: Option<HashMap<String, String>>,
38+
#[serde(rename = "defaultTask")]
39+
pub default_task: TaskConfig,
40+
pub subtasks: Option<HashMap<String, TaskConfig>>,
41+
}
42+
3143
impl Default for BodoConfig {
3244
fn default() -> Self {
3345
Self {
34-
tasks: None,
3546
env_files: None,
3647
executable_map: None,
3748
max_concurrency: None,
@@ -40,7 +51,7 @@ impl Default for BodoConfig {
4051
}
4152
}
4253

43-
pub fn load_bodo_config() -> BodoConfig {
54+
pub fn load_bodo_config() -> Result<BodoConfig, Box<dyn Error>> {
4455
let config_paths = [
4556
"bodo.json",
4657
"bodo.yaml",
@@ -59,12 +70,25 @@ pub fn load_bodo_config() -> BodoConfig {
5970
};
6071

6172
if let Some(config) = config {
62-
return config;
73+
return Ok(config);
6374
}
6475
}
6576
}
6677

67-
BodoConfig::default()
78+
Ok(BodoConfig::default())
79+
}
80+
81+
pub fn load_script_config(task_name: &str) -> Result<ScriptConfig, Box<dyn Error>> {
82+
let script_path = format!("scripts/{}/script.yaml", task_name);
83+
let path = Path::new(&script_path);
84+
85+
if !path.exists() {
86+
return Err(format!("Script file not found: {}", script_path).into());
87+
}
88+
89+
let contents = fs::read_to_string(path)?;
90+
let config: ScriptConfig = serde_yaml::from_str(&contents)?;
91+
Ok(config)
6892
}
6993

7094
#[cfg(test)]
@@ -77,10 +101,10 @@ mod tests {
77101
fn create_temp_config_file(content: &str, extension: &str) -> PathBuf {
78102
let mut temp_path = std::env::temp_dir();
79103
temp_path.push(format!("test_config.{}", extension));
80-
104+
81105
let mut file = File::create(&temp_path).unwrap();
82106
file.write_all(content.as_bytes()).unwrap();
83-
107+
84108
temp_path
85109
}
86110

@@ -91,7 +115,6 @@ mod tests {
91115
#[test]
92116
fn test_default_config() {
93117
let config = BodoConfig::default();
94-
assert!(config.tasks.is_none());
95118
assert!(config.env_files.is_none());
96119
assert!(config.executable_map.is_none());
97120
assert!(config.max_concurrency.is_none());
@@ -101,16 +124,6 @@ mod tests {
101124
#[test]
102125
fn test_load_json_config() {
103126
let content = r#"{
104-
"tasks": [
105-
{
106-
"name": "test",
107-
"command": "echo hello",
108-
"cwd": ".",
109-
"env": ["TEST=true"],
110-
"dependencies": ["prep"],
111-
"plugins": ["test-plugin"]
112-
}
113-
],
114127
"env_files": [".env"],
115128
"executable_map": [
116129
{
@@ -121,35 +134,21 @@ mod tests {
121134
"max_concurrency": 4,
122135
"plugins": ["plugin1"]
123136
}"#;
124-
137+
125138
let temp_path = create_temp_config_file(content, "json");
126139
std::env::set_current_dir(temp_path.parent().unwrap()).unwrap();
127-
140+
128141
let config: BodoConfig = serde_json::from_str(content).unwrap();
129-
assert!(config.tasks.is_some());
130-
assert_eq!(config.tasks.as_ref().unwrap().len(), 1);
131-
132-
let task = &config.tasks.as_ref().unwrap()[0];
133-
assert_eq!(task.name, "test");
134-
assert_eq!(task.command, "echo hello");
135-
assert_eq!(task.cwd, Some(".".to_string()));
136-
142+
assert!(config.env_files.is_some());
143+
assert!(config.executable_map.is_some());
144+
assert_eq!(config.max_concurrency, Some(4));
145+
137146
cleanup_temp_file(temp_path);
138147
}
139148

140149
#[test]
141150
fn test_load_yaml_config() {
142151
let content = r#"
143-
tasks:
144-
- name: test
145-
command: echo hello
146-
cwd: .
147-
env:
148-
- TEST=true
149-
dependencies:
150-
- prep
151-
plugins:
152-
- test-plugin
153152
env_files:
154153
- .env
155154
executable_map:
@@ -159,19 +158,15 @@ max_concurrency: 4
159158
plugins:
160159
- plugin1
161160
"#;
162-
161+
163162
let temp_path = create_temp_config_file(content, "yaml");
164163
std::env::set_current_dir(temp_path.parent().unwrap()).unwrap();
165-
164+
166165
let config: BodoConfig = serde_yaml::from_str(content).unwrap();
167-
assert!(config.tasks.is_some());
168-
assert_eq!(config.tasks.as_ref().unwrap().len(), 1);
169-
170-
let task = &config.tasks.as_ref().unwrap()[0];
171-
assert_eq!(task.name, "test");
172-
assert_eq!(task.command, "echo hello");
173-
assert_eq!(task.cwd, Some(".".to_string()));
174-
166+
assert!(config.env_files.is_some());
167+
assert!(config.executable_map.is_some());
168+
assert_eq!(config.max_concurrency, Some(4));
169+
175170
cleanup_temp_file(temp_path);
176171
}
177172

@@ -181,8 +176,8 @@ plugins:
181176
executable: Some("node".to_string()),
182177
path: Some("/usr/local/bin/node".to_string()),
183178
};
184-
179+
185180
assert_eq!(map.executable.as_ref().unwrap(), "node");
186181
assert_eq!(map.path.as_ref().unwrap(), "/usr/local/bin/node");
187182
}
188-
}
183+
}

0 commit comments

Comments
 (0)