From a0d5b83a8008c4fec292fbacde61240141554fc3 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Thu, 16 Jun 2016 17:13:43 -0700 Subject: [PATCH] Initial commit. Broken. --- .gitignore | 1 + Cargo.lock | 4 +++ Cargo.toml | 6 ++++ justfile | 2 ++ src/main.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 justfile create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..e55cae7 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "j" +version = "0.1.0" + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e59217b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "j" +version = "0.1.0" +authors = ["Casey Rodarmor "] + +[dependencies] diff --git a/justfile b/justfile new file mode 100644 index 0000000..e13aeaf --- /dev/null +++ b/justfile @@ -0,0 +1,2 @@ +default: + cargo run hello -- -- bar a b c diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..a158939 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,84 @@ +use std::io::prelude::*; + +fn can(command: &str) -> bool { + if let Ok(paths) = std::env::var("PATH") { + for path in paths.split(":") { + let candidate = format!("{}/{}", path, command); + if isfile(&candidate) { + return true; + } + } + } + false +} + +fn isfile(path: &str) -> bool { + if let Ok(metadata) = std::fs::metadata(path) { + metadata.is_file() + } else { + false + } +} + +fn cwd() -> String { + match std::env::current_dir() { + Ok(pathbuf) => pathbuf.to_str().unwrap_or_else(|| panic!("cwd: cwd was not a valid utf8 string")).to_string(), + Err(err) => panic!("cwd: {}", err), + } +} + +fn cd(path: &str) { + if let Err(err) = std::env::set_current_dir(path) { + panic!("cd: {}", err) + } +} + +fn say(s: &str) { + println!("{}", s) +} + +fn warn(s: &str) { + if let Err(err) = std::io::stderr().write(s.as_bytes()) { + panic!("warn: could not write to stderr: {}", err); + } + if let Err(err) = std::io::stderr().write("\n".as_bytes()) { + panic!("warn: could not write to stderr: {}", err); + } +} + +fn die(s: &str) { + warn(s); + std::process::exit(-1); +} + +fn main() { + let can_make = can("make"); + let can_gmake = can("gmake"); + if !(can_make || can_gmake) { + die("cannot find \"make\" or \"gmake\" in $PATH"); + } + + println!("can make: {}", can_make); + println!("can gmake: {}", can_gmake); + + loop { + if isfile("justfile") { + break; + } + if cwd() == "/" { + die("No justfile found.") + } + cd(".."); + } + + + let recipes: Vec = std::env::args().skip(1).take_while(|arg| arg != "--").collect(); + let arguments: Vec = std::env::args().skip(1 + recipes.len() + 1).collect(); + + print!("{:?}", recipes); + print!("{:?}", arguments); + + // export as $ARG0 -> ARGX + // start at 0 or 1? + // exec $MAKE MAKEFLAGS='' --always-make --no-print-directory -f justfile ${RECIPES[*]} +}