From c204b5e72837fd5a0fb7aeb09e3c1af5080a4604 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 17 Jan 2019 15:15:40 -0800 Subject: [PATCH] Print help/version info when run w/ no subcommand --- cli/build.rs | 26 ++++++++++++++++++++++++++ cli/src/main.rs | 6 ++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/cli/build.rs b/cli/build.rs index e0ebd1c4..f8e62274 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -1,6 +1,32 @@ +use std::{io, env, fs}; + fn main() { + let git_sha = read_git_sha().unwrap(); + println!("cargo:rustc-env={}={}", "BUILD_SHA", git_sha); + println!( "cargo:rustc-env=BUILD_TARGET={}", std::env::var("TARGET").unwrap() ); } + +fn read_git_sha() -> io::Result { + let git_path = env::current_dir().unwrap().parent().unwrap().join(".git"); + let git_head_path = git_path.join("HEAD"); + println!("cargo:rerun-if-changed={}", git_head_path.to_str().unwrap()); + let mut head_content = fs::read_to_string(&git_head_path)?; + assert!(head_content.ends_with("\n")); + head_content.pop(); + + if head_content.starts_with("ref: ") { + // We're on a branch. Read the SHA from the ref file. + head_content.replace_range(0.."ref: ".len(), ""); + let ref_filename = git_path.join(&head_content); + println!("cargo:rerun-if-changed={}", ref_filename.to_str().unwrap()); + fs::read_to_string(&ref_filename) + } else { + // We're not on a branch. The `HEAD` file itself contains the sha. + assert_eq!(head_content.len(), 40); + Ok(head_content) + } +} diff --git a/cli/src/main.rs b/cli/src/main.rs index 80a40758..1860ecc2 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -21,7 +21,7 @@ mod util; mod tests; use self::loader::Loader; -use clap::{App, Arg, SubCommand}; +use clap::{App, AppSettings, Arg, SubCommand}; use std::env; use std::fs; use std::path::Path; @@ -37,7 +37,8 @@ fn main() { fn run() -> error::Result<()> { let matches = App::new("tree-sitter") - .version("0.1") + .version(concat!(env!("CARGO_PKG_VERSION"), " (", env!("BUILD_SHA"), ")")) + .setting(AppSettings::SubcommandRequiredElseHelp) .author("Max Brunsfeld ") .about("Generates and tests parsers") .subcommand( @@ -77,6 +78,7 @@ fn run() -> error::Result<()> { let home_dir = dirs::home_dir().unwrap(); let current_dir = env::current_dir().unwrap(); let config_dir = home_dir.join(".tree-sitter"); + fs::create_dir_all(&config_dir).unwrap(); let mut loader = Loader::new(config_dir);