Print help/version info when run w/ no subcommand

This commit is contained in:
Max Brunsfeld 2019-01-17 15:15:40 -08:00
parent 53c8eaa4c2
commit c204b5e728
2 changed files with 30 additions and 2 deletions

View file

@ -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<String> {
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)
}
}

View file

@ -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 <maxbrunsfeld@gmail.com>")
.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);