Skip to content

Commit 70bff79

Browse files
committed
added a lot new features to samfileparser
1 parent 4111e61 commit 70bff79

5 files changed

Lines changed: 1055 additions & 5 deletions

File tree

crates/samfileparser/Cargo.lock

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/samfileparser/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[package]
44
name = "samfileparser"
5-
version = "0.1.3"
5+
version = "0.1.4"
66
edition = "2024"
77
authors = ["Shadowdara"]
88
readme = "README.md"
@@ -14,3 +14,4 @@ repository = "https://github.com/shadowdara/samengine"
1414

1515
[dependencies]
1616
fluaterm = "0.2.2"
17+
fs_extra = "1"

crates/samfileparser/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Empty lines are ignored. Comments can start with `#`, `//`, or `--`.
4444

4545
## Commands
4646

47+
There a lot for more commands in this version! Go to [docs.rs](https://docs.rs/samfileparser/0.1.4/samfileparser/) to see more infos!
48+
4749
### `run`
4850

4951
Runs a program in the current runtime directory.

crates/samfileparser/src/init.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use std::{collections::HashMap, fs, path::Path};
2+
3+
use fluaterm::{YELLOW, END};
4+
5+
use crate::run_task;
6+
use crate::validate_all;
7+
use crate::parse;
8+
use crate::RuntimeState;
9+
10+
// Run sth from the samfile
11+
pub fn run_sam_file(command: &str) {
12+
let mut state = RuntimeState {
13+
cwd: std::env::current_dir().unwrap(),
14+
env: HashMap::new(),
15+
};
16+
17+
let content = match std::fs::read_to_string(".samengine/samfile") {
18+
Ok(c) => c,
19+
Err(e) => {
20+
eprintln!("Error while reading samfile: {}", e);
21+
return;
22+
}
23+
};
24+
25+
let tasks = parse(&content);
26+
27+
// Check for cycled dependencies
28+
validate_all(&tasks);
29+
30+
// Map which one was already visited
31+
let mut visited = std::collections::HashSet::new();
32+
33+
// Execute the Task
34+
run_task(&tasks, command, &mut visited, &mut state);
35+
}
36+
37+
fn has_gitignore(dir: &str) -> bool {
38+
Path::new(dir).join(".gitignore").exists()
39+
}
40+
41+
fn read_gitignore(dir: &str) -> Option<String> {
42+
let path = std::path::Path::new(dir).join(".gitignore");
43+
44+
fs::read_to_string(path).ok()
45+
}
46+
47+
fn is_samfile_ignored(gitignore_content: &str) -> bool {
48+
gitignore_content
49+
.lines()
50+
.any(|line| line.trim() == "samfile")
51+
}
52+
53+
// Create new samfile
54+
pub fn init() {
55+
let dir = std::path::Path::new(".samengine");
56+
let file = dir.join("samfile");
57+
58+
// check first if exists
59+
if dir.exists() && file.exists() {
60+
println!("samefile already exists — aborting init");
61+
return;
62+
}
63+
64+
println!("Creating a new samfile!");
65+
66+
// Create .samengine Directory
67+
std::fs::create_dir_all(dir)
68+
.expect("failed to create directory");
69+
70+
std::fs::write(
71+
&file,
72+
"# A new samfile, write your scripts here"
73+
)
74+
.expect("failed to create file");
75+
76+
let dir2 = std::env::current_dir()
77+
.unwrap()
78+
.to_str()
79+
.unwrap()
80+
.to_string();
81+
82+
// Check if there is a gitignore
83+
if has_gitignore(&dir2) {
84+
if let Some(content) = read_gitignore(&dir2) {
85+
if is_samfile_ignored(&content) {
86+
println!("samfile is ignored by git");
87+
} else {
88+
println!("{}WARN: samfile is NOT ignored{}", YELLOW, END);
89+
}
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)