From 114f6b7bdc016f581fd83092d93ac56a5f6758cf Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 27 Sep 2016 22:49:17 -0700 Subject: [PATCH] Working --- Cargo.lock | 38 ++++++++++++++ Cargo.toml | 2 + README.md | 31 ++++++++++++ justfile | 35 ++++++++++++- src/main.rs | 141 ++++++++++++++++++++++++++++++---------------------- 5 files changed, 186 insertions(+), 61 deletions(-) create mode 100644 README.md diff --git a/Cargo.lock b/Cargo.lock index e55cae7..cec2092 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,4 +1,42 @@ [root] name = "j" version = "0.1.0" +dependencies = [ + "brev 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "brev" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "glob" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rand" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "tempdir" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", +] diff --git a/Cargo.toml b/Cargo.toml index e59217b..fd641ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,5 @@ version = "0.1.0" authors = ["Casey Rodarmor "] [dependencies] + +brev = "0.1.6" diff --git a/README.md b/README.md new file mode 100644 index 0000000..f3464da --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +j += + +`j` is a handy way to run project-specific commands. + +`j` looks upward from the current directory for a file called `justfile` and then runs make with that file as the makefile. `j` also sets the current working directory to where it found the justfile, so your commands are executed from the root of your project and not from whatever subdirectory you happen to be in. + +With no arguments it runs the default recipe: + +`j` + +Adding one argument specifies the recipe: + +`j compile` + +Arguments after `--` are exported as `ARG0, ARG1, ..., ARGN`, which can be used in the justfile. To run recipe `compile` and export `ARG0=bar` and `ARG1=baz`: + +`just compile -- bar baz` + +By way of example, the included justfile has a pinch of fanciful fluff. + +getting j +--------- + +J is distributed via `cargo`, rust's package manager. + +1. Get cargo at [rustup.rs](https://www.rustup.rs) +2. Run `cargo install j` +3. Add `~/.cargo/bin` to your PATH + +`j` depends on make to actually run commands, but hopefully if you're on a unix, make is already installed. If not, you can get it from friendly local package manager. diff --git a/justfile b/justfile index e13aeaf..2ecaf6b 100644 --- a/justfile +++ b/justfile @@ -1,2 +1,33 @@ -default: - cargo run hello -- -- bar a b c +# just a makefile with no special magic + +test: + cargo build + ./target/debug/j args -- a b c + +# list all recipies +list: + @$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | xargs + +args: + @echo "I got some arguments: ARG0=${ARG0} ARG1=${ARG1} ARG2=${ARG2}" + +# make a quine and compile it +quine: create compile + +# create our quine +create: + mkdir -p tmp + echo 'int printf(const char*, ...); int main() { char *s = "int printf(const char*, ...); int main() { char *s = %c%s%c; printf(s, 34, s, 34); return 0; }"; printf(s, 34, s, 34); return 0; }' > tmp/gen0.c + +# make sure it's really a quine +compile: + cc tmp/gen0.c -o tmp/gen0 + ./tmp/gen0 > tmp/gen1.c + cc tmp/gen1.c -o tmp/gen1 + ./tmp/gen1 > tmp/gen2.c + diff tmp/gen1.c tmp/gen2.c + @echo 'It was a quine!' + +# clean up +clean: + rm -r tmp diff --git a/src/main.rs b/src/main.rs index a158939..499378e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,84 +1,107 @@ -use std::io::prelude::*; +#[macro_use] +extern crate brev; -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; - } +#[derive(PartialEq, Clone, Copy)] +enum Make { + GNU, // GNU Make installed as `gmake` + GNUStealthy, // GNU Make installed as `make` + Other, // Another make installed as `make` +} + +impl Make { + fn command(self) -> &'static str { + if self == Make::GNU { + "gmake" + } else { + "make" } } - false -} -fn isfile(path: &str) -> bool { - if let Ok(metadata) = std::fs::metadata(path) { - metadata.is_file() - } else { - false + fn gnu(self) -> bool { + self != Make::Other } } -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 status(command: &mut std::process::Command) -> std::io::Result { + command + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .status() } -fn cd(path: &str) { - if let Err(err) = std::env::set_current_dir(path) { - panic!("cd: {}", err) - } -} +fn which_make() -> Option { + // check `gmake` + let result = status(std::process::Command::new("gmake").arg("-v")); -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 Ok(exit_status) = result { + if exit_status.success() { + return Some(Make::GNU); + } } - 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); + // check `make`. pass gmake specific flags to see if it's actually gmake + let result = status(std::process::Command::new("make").arg("-v").arg("--always-make")); + + if let Ok(exit_status) = result { + return if exit_status.success() { + Some(Make::GNUStealthy) + } else { + Some(Make::Other) + }; + } + + return None; } 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); + let make = match which_make() { + None => die!("Could not execute `make` or `gmake`."), + Some(make) => make, + }; loop { - if isfile("justfile") { - break; + match std::fs::metadata("justfile") { + Ok(metadata) => if metadata.is_file() { break; }, + Err(error) => die!("Error fetching justfile metadata: {}", error), } - if cwd() == "/" { - die("No justfile found.") - } - cd(".."); - } + match std::env::current_dir() { + Ok(pathbuf) => if pathbuf.as_os_str() == "/" { die!("No justfile found"); }, + Err(error) => die!("Error getting current dir: {}", error), + } + + if let Err(error) = std::env::set_current_dir("..") { + die!("Error changing directory: {}", error); + } + } 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); + for (i, argument) in arguments.into_iter().enumerate() { + std::env::set_var(format!("ARG{}", i), argument); + } - // export as $ARG0 -> ARGX - // start at 0 or 1? - // exec $MAKE MAKEFLAGS='' --always-make --no-print-directory -f justfile ${RECIPES[*]} + let mut command = std::process::Command::new(make.command()); + + command.arg("MAKEFLAGS="); + + if make.gnu() { + command.arg("--always-make").arg("--no-print-directory"); + } + + command.arg("-f").arg("justfile"); + + for recipe in recipes { + command.arg(recipe); + } + + match command.status() { + Err(error) => die!("Failed to execute `{:?}`: {}", command, error), + Ok(exit_status) => match exit_status.code() { + Some(code) => std::process::exit(code), + None => std::process::exit(-1), + } + } }