WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit bdc3394

Browse files
committed
Add update command and relative env file paths
The changes include: - Adding new update command to download/install latest version - Supporting relative env file paths based on taskfile location - Modifying env parser to handle base paths for env files
1 parent 4502c61 commit bdc3394

File tree

5 files changed

+88
-10
lines changed

5 files changed

+88
-10
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ members = [".", "crates/env-parser", "crates/taskfile-runner"]
33

44
[package]
55
name = "taskfile"
6-
version = "0.2.0"
6+
version = "0.3.0"
77
edition = "2024"
88

99
[[bin]]

crates/env-parser/src/lib.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,28 @@ impl EnvParser {
2626
}
2727

2828
pub fn load_env_files(&self) -> Result<(), Box<dyn std::error::Error>> {
29+
self.load_env_files_with_base_path(None)
30+
}
31+
32+
pub fn load_env_files_with_base_path(
33+
&self,
34+
base_path: Option<&std::path::Path>,
35+
) -> Result<(), Box<dyn std::error::Error>> {
2936
if let Some(env_config) = &self.config {
3037
for file_path in &env_config.files {
31-
if std::path::Path::new(file_path).exists() {
32-
match self.load_env_file(file_path) {
38+
let full_path = if let Some(base) = base_path {
39+
base.join(file_path)
40+
} else {
41+
std::path::PathBuf::from(file_path)
42+
};
43+
44+
if full_path.exists() {
45+
let path_str = full_path.to_string_lossy();
46+
match self.load_env_file(&path_str) {
3347
Ok(count) => {
34-
println!("Loaded {} environment variables from: {}", count, file_path)
48+
println!("Loaded {} environment variables from: {}", count, path_str)
3549
}
36-
Err(e) => eprintln!("Warning: Failed to load {}: {}", file_path, e),
50+
Err(e) => eprintln!("Warning: Failed to load {}: {}", path_str, e),
3751
}
3852
}
3953
}

crates/taskfile-runner/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ impl TaskRunner {
3131

3232
let env_parser = if let Some(env_config) = &taskfile.env {
3333
let parser = EnvParser::with_config(env_config.clone());
34-
parser.load_env_files()?;
34+
let taskfile_dir = std::path::Path::new(taskfile_path).parent();
35+
parser.load_env_files_with_base_path(taskfile_dir)?;
3536
parser
3637
} else {
3738
EnvParser::new()
@@ -44,9 +45,13 @@ impl TaskRunner {
4445
}
4546

4647
pub fn new(taskfile: TaskFile) -> Self {
48+
Self::new_with_base_path(taskfile, None)
49+
}
50+
51+
pub fn new_with_base_path(taskfile: TaskFile, base_path: Option<&std::path::Path>) -> Self {
4752
let env_parser = if let Some(env_config) = &taskfile.env {
4853
let parser = EnvParser::with_config(env_config.clone());
49-
if let Err(e) = parser.load_env_files() {
54+
if let Err(e) = parser.load_env_files_with_base_path(base_path) {
5055
eprintln!("{} Error loading environment files: {}", "✗".red(), e);
5156
}
5257
parser

src/main.rs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async fn main() {
99
.about("A simple task runner")
1010
.arg(
1111
Arg::new("command")
12-
.help("The command to run (list, version, or task name)")
12+
.help("The command to run (list, version, update, or task name)")
1313
.value_name("COMMAND")
1414
.index(1),
1515
)
@@ -27,6 +27,16 @@ async fn main() {
2727
println!("taskfile-runner v{}", env!("CARGO_PKG_VERSION"));
2828
println!("A simple task runner written in Rust");
2929
}
30+
Some(cmd) if cmd == "update" => {
31+
println!("Updating task runner...");
32+
match update_task_runner().await {
33+
Ok(_) => println!("✓ Update completed successfully!"),
34+
Err(e) => {
35+
eprintln!("{} Update failed: {}", "✗".red(), e);
36+
std::process::exit(1);
37+
}
38+
}
39+
}
3040
Some(task_name) => {
3141
if let Err(e) = runner.run_task(task_name).await {
3242
eprintln!("{} Error running task '{}': {}", "✗".red(), task_name, e);
@@ -35,7 +45,7 @@ async fn main() {
3545
}
3646
None => {
3747
println!("Please specify a task to run or use 'list' to see available tasks.");
38-
println!("Usage: task <task_name> | list | version");
48+
println!("Usage: task <task_name> | list | version | update");
3949
std::process::exit(1);
4050
}
4151
},
@@ -54,3 +64,52 @@ async fn main() {
5464
}
5565
}
5666
}
67+
68+
async fn update_task_runner() -> Result<(), Box<dyn std::error::Error>> {
69+
use std::env;
70+
use std::process::Stdio;
71+
use tokio::process::Command;
72+
73+
let current_exe = env::current_exe()?;
74+
let install_dir = current_exe.parent().unwrap();
75+
76+
println!("Downloading latest version...");
77+
78+
let output = Command::new("curl")
79+
.args(&[
80+
"-sSL",
81+
"https://raw.githubusercontent.com/lassejlv/taskfile/main/install.sh",
82+
])
83+
.stdout(Stdio::piped())
84+
.stderr(Stdio::piped())
85+
.output()
86+
.await?;
87+
88+
if !output.status.success() {
89+
return Err("Failed to download install script".into());
90+
}
91+
92+
let install_script = String::from_utf8(output.stdout)?;
93+
94+
let output = Command::new("bash")
95+
.arg("-c")
96+
.arg(&format!(
97+
"echo '{}' | bash -s -- --install-dir '{}'",
98+
install_script.replace("'", "'\"'\"'"),
99+
install_dir.display()
100+
))
101+
.stdout(Stdio::piped())
102+
.stderr(Stdio::piped())
103+
.output()
104+
.await?;
105+
106+
if output.status.success() {
107+
println!("Installation output:");
108+
print!("{}", String::from_utf8_lossy(&output.stdout));
109+
Ok(())
110+
} else {
111+
eprintln!("Error output:");
112+
eprint!("{}", String::from_utf8_lossy(&output.stderr));
113+
Err("Update installation failed".into())
114+
}
115+
}

0 commit comments

Comments
 (0)