Skip to content

Commit 3b04584

Browse files
Merge pull request #19 from code0-tech/16-add-action
16 add action
2 parents a4d2109 + 9e5104b commit 3b04584

File tree

8 files changed

+393
-140
lines changed

8 files changed

+393
-140
lines changed

.github/workflows/ci.yml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
name: Rust CI/CD Pipeline
2+
3+
on:
4+
push:
5+
pull_request:
6+
types: [opened, reopened]
7+
8+
env:
9+
RUST_BACKTRACE: 1
10+
11+
jobs:
12+
# Job 1: Format Check
13+
format:
14+
name: Format Check
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Install Rust toolchain
21+
uses: dtolnay/rust-toolchain@stable
22+
with:
23+
components: rustfmt
24+
25+
- name: Check formatting
26+
run: cargo fmt --all -- --check
27+
28+
# Job 2: Lint Check
29+
lint:
30+
name: Lint Check
31+
runs-on: ubuntu-latest
32+
steps:
33+
- name: Checkout code
34+
uses: actions/checkout@v4
35+
36+
- name: Install Rust toolchain
37+
uses: dtolnay/rust-toolchain@stable
38+
with:
39+
components: clippy
40+
41+
- name: Cache cargo registry
42+
uses: actions/cache@v4
43+
with:
44+
path: |
45+
~/.cargo/registry
46+
~/.cargo/git
47+
target
48+
key: ${{ runner.os }}-cargo-lint-${{ hashFiles('**/Cargo.lock') }}
49+
restore-keys: |
50+
${{ runner.os }}-cargo-
51+
52+
- name: Run clippy
53+
run: cargo clippy --all-targets --all-features -- -D warnings
54+
55+
# Job 3: Build
56+
build:
57+
name: Build
58+
needs: [lint, format]
59+
runs-on: ubuntu-latest
60+
steps:
61+
- name: Checkout code
62+
uses: actions/checkout@v4
63+
64+
- name: Install Rust toolchain
65+
uses: dtolnay/rust-toolchain@stable
66+
67+
- name: Cache cargo registry
68+
uses: actions/cache@v4
69+
with:
70+
path: |
71+
~/.cargo/registry
72+
~/.cargo/git
73+
target
74+
key: ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }}
75+
restore-keys: |
76+
${{ runner.os }}-cargo-
77+
78+
- name: Build project
79+
run: cargo build --verbose --all-features
80+
81+
82+
report:
83+
name: Report
84+
needs: [build]
85+
runs-on: ubuntu-latest
86+
steps:
87+
- name: Checkout code
88+
uses: actions/checkout@v4
89+
90+
- name: Install Rust toolchain
91+
uses: dtolnay/rust-toolchain@stable
92+
93+
- name: Cache cargo registry
94+
uses: actions/cache@v4
95+
with:
96+
path: |
97+
~/.cargo/registry
98+
~/.cargo/git
99+
target
100+
key: ${{ runner.os }}-cargo-report-${{ hashFiles('**/Cargo.lock') }}
101+
restore-keys: |
102+
${{ runner.os }}-cargo-
103+
104+
- name: Generate report
105+
run: cargo run report

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[workspace]
22
members = ["cli", "reader/rust"]
3+
resolver = "3"
34

45
[workspace.package]
56
version = "0.0.1"

cli/src/main.rs

Lines changed: 84 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
use crate::table::*;
12
use clap::{Parser as ClapParser, Subcommand};
3+
use colored::*;
4+
use notify::{Event, EventKind, RecursiveMode, Watcher, recommended_watcher};
25
use reader::parser::Parser;
3-
use notify::{Watcher, RecursiveMode, Event, EventKind, recommended_watcher};
46
use std::sync::mpsc::channel;
5-
use colored::*;
6-
use crate::table::*;
7-
use serde_json;
87

98
mod table;
109

@@ -71,28 +70,28 @@ fn main() {
7170
Commands::Feature { name, path } => {
7271
let dir_path = path.unwrap_or_else(|| "./definitions".to_string());
7372

74-
let parser = match Parser::from_path(dir_path.as_str()) {
75-
Some(reader) => reader,
76-
None => {
77-
panic!("Error reading definitions");
78-
}
79-
};
80-
81-
if let Some(feature_name) = name {
82-
let mut features_to_report = Vec::new();
83-
for feature in &parser.features {
84-
if feature.name == feature_name {
85-
feature_table(&feature);
86-
features_to_report.push(feature.clone());
87-
}
88-
}
89-
summary_table(&features_to_report);
90-
} else {
91-
for feature in &parser.features {
92-
feature_table(&feature);
93-
}
94-
summary_table(&parser.features);
95-
}
73+
let parser = match Parser::from_path(dir_path.as_str()) {
74+
Some(reader) => reader,
75+
None => {
76+
panic!("Error reading definitions");
77+
}
78+
};
79+
80+
if let Some(feature_name) = name {
81+
let mut features_to_report = Vec::new();
82+
for feature in &parser.features {
83+
if feature.name == feature_name {
84+
feature_table(feature);
85+
features_to_report.push(feature.clone());
86+
}
87+
}
88+
summary_table(&features_to_report);
89+
} else {
90+
for feature in &parser.features {
91+
feature_table(feature);
92+
}
93+
summary_table(&parser.features);
94+
}
9695
}
9796
Commands::Definition { name, path } => {
9897
let dir_path = path.unwrap_or_else(|| "./definitions".to_string());
@@ -109,7 +108,12 @@ fn main() {
109108
Commands::Watch { path } => {
110109
let dir_path = path.unwrap_or_else(|| "./definitions".to_string());
111110

112-
println!("{}", format!("Watching directory: {}", dir_path).bright_yellow().bold());
111+
println!(
112+
"{}",
113+
format!("Watching directory: {dir_path}")
114+
.bright_yellow()
115+
.bold()
116+
);
113117
println!("{}", "Press Ctrl+C to stop watching...".dimmed());
114118

115119
{
@@ -126,30 +130,42 @@ fn main() {
126130
// Set up file watcher
127131
let (tx, rx) = channel();
128132
let mut watcher = recommended_watcher(tx).unwrap();
129-
watcher.watch(std::path::Path::new(&dir_path), RecursiveMode::Recursive).unwrap();
133+
watcher
134+
.watch(std::path::Path::new(&dir_path), RecursiveMode::Recursive)
135+
.unwrap();
130136

131137
loop {
132138
match rx.recv() {
133-
Ok(event) => {
134-
match event {
135-
Ok(Event { kind: EventKind::Create(_), .. }) |
136-
Ok(Event { kind: EventKind::Modify(_), .. }) |
137-
Ok(Event { kind: EventKind::Remove(_), .. }) => {
138-
println!("\n{}", "Change detected! Regenerating report...".bright_yellow());
139-
140-
let parser = match Parser::from_path(dir_path.as_str()) {
141-
Some(reader) => reader,
142-
None => {
143-
panic!("Error reading definitions");
144-
}
145-
};
146-
147-
error_table(&parser.features);
148-
}
149-
_ => {}
139+
Ok(event) => match event {
140+
Ok(Event {
141+
kind: EventKind::Create(_),
142+
..
143+
})
144+
| Ok(Event {
145+
kind: EventKind::Modify(_),
146+
..
147+
})
148+
| Ok(Event {
149+
kind: EventKind::Remove(_),
150+
..
151+
}) => {
152+
println!(
153+
"\n{}",
154+
"Change detected! Regenerating report...".bright_yellow()
155+
);
156+
157+
let parser = match Parser::from_path(dir_path.as_str()) {
158+
Some(reader) => reader,
159+
None => {
160+
panic!("Error reading definitions");
161+
}
162+
};
163+
164+
error_table(&parser.features);
150165
}
151-
}
152-
Err(e) => println!("Watch error: {:?}", e),
166+
_ => {}
167+
},
168+
Err(e) => println!("Watch error: {e:?}"),
153169
}
154170
}
155171
}
@@ -159,7 +175,12 @@ fn main() {
159175
fn search_and_display_definitions(search_name: &str, parser: &Parser) {
160176
let mut found_any = false;
161177
let mut total_matches = 0;
162-
println!("{}", format!("Searching for definitions matching: '{}'", search_name).bright_yellow().bold());
178+
println!(
179+
"{}",
180+
format!("Searching for definitions matching: '{search_name}'")
181+
.bright_yellow()
182+
.bold()
183+
);
163184
println!("{}", "─".repeat(60).dimmed());
164185

165186
for feature in &parser.features {
@@ -217,7 +238,11 @@ fn search_and_display_definitions(search_name: &str, parser: &Parser) {
217238
let mut index = 0;
218239
for line in json.lines() {
219240
index += 1;
220-
println!("{} {}", format!("{}:", index).bright_blue(), line.bright_green());
241+
println!(
242+
"{} {}",
243+
format!("{index}:").bright_blue(),
244+
line.bright_green()
245+
);
221246
}
222247
}
223248
Err(_) => println!("{}", "Error serializing RuntimeFunction".red()),
@@ -227,9 +252,17 @@ fn search_and_display_definitions(search_name: &str, parser: &Parser) {
227252
}
228253

229254
if !found_any {
230-
println!("\n{}", format!("No definitions found matching '{}'", search_name).red().bold());
255+
println!(
256+
"\n{}",
257+
format!("No definitions found matching '{search_name}'")
258+
.red()
259+
.bold()
260+
);
231261
} else {
232262
println!("\n{}", "─".repeat(60).dimmed());
233-
println!("{}", format!("Found {} matching definition(s)", total_matches).bright_yellow());
263+
println!(
264+
"{}",
265+
format!("Found {total_matches} matching definition(s)").bright_yellow()
266+
);
234267
}
235268
}

0 commit comments

Comments
 (0)