From 123537be1f4990dd0d762d2d1b54745805e3225e Mon Sep 17 00:00:00 2001 From: Tetsuharu OHZEKI Date: Thu, 11 May 2017 05:59:55 +0900 Subject: [PATCH 1/4] Dump pkg version & build date & commit hash if run with `--version` flag. - The output format is `rls ( )` - This embed approach is based on [rustup.rs][rustup]'s code. - rustup.rs is licensed under same licenses (Apache 2.0/MIT), so I think there is no license problem. [rustup]: https://github.com/rust-lang-nursery/rustup.rs/tree/a1c6be19add9c99f8d8868ec384aa76057fe7f70 --- Cargo.toml | 2 ++ build.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 13 ++++++++-- src/version.rs | 6 +++++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 build.rs create mode 100644 src/version.rs diff --git a/Cargo.toml b/Cargo.toml index 829ad34f5c1..09351483c6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,8 @@ name = "rls" version = "0.1.0" authors = ["Jonathan Turner "] +build = "build.rs" + [dependencies] cargo = { git = "https://github.com/rust-lang/cargo" } derive-new = "0.3" diff --git a/build.rs b/build.rs new file mode 100644 index 00000000000..de1f5a90194 --- /dev/null +++ b/build.rs @@ -0,0 +1,64 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// The original code is [rustup.rs][rustup] +// [rustup]: https://github.com/rust-lang-nursery/rustup.rs/tree/a1c6be19add9c99f8d8868ec384aa76057fe7f70 + +use std::env; +use std::error::Error; +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; +use std::process::Command; + +struct Ignore; + +impl From for Ignore + where E: Error +{ + fn from(_: E) -> Ignore { + Ignore + } +} + +fn main() { + let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); + + File::create(out_dir.join("commit-info.txt")) + .unwrap() + .write_all(commit_info().as_bytes()) + .unwrap(); +} + +// Try to get hash and date of the last commit on a best effort basis. If anything goes wrong +// (git not installed or if this is not a git repository) just return an empty string. +fn commit_info() -> String { + match (commit_hash(), commit_date()) { + (Ok(hash), Ok(date)) => format!(" ({} {})", hash.trim_right(), date), + _ => String::new(), + } +} + +fn commit_hash() -> Result { + Ok(try!(String::from_utf8(try!(Command::new("git") + .args(&["rev-parse", "--short", "HEAD"]) + .output()) + .stdout))) +} + +fn commit_date() -> Result { + Ok(try!(String::from_utf8(try!(Command::new("git") + .args(&["log", + "-1", + "--date=short", + "--pretty=format:%cd"]) + .output()) + .stdout))) +} diff --git a/src/main.rs b/src/main.rs index 502d992015a..df0e91cf2d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,8 +51,13 @@ type Span = span::Span; pub fn main() { env_logger::init().unwrap(); - let arg_count = ::std::env::args().count(); - if arg_count > 1 { + let args: Vec = ::std::env::args().collect(); + if args[1] == "--version" || args[1] == "-V" { + println!("rls {}", version()); + return; + } + + if args.len() > 1 { cmd::run(); } else { let analysis = Arc::new(analysis::AnalysisHost::new(analysis::Target::Debug)); @@ -62,3 +67,7 @@ pub fn main() { server::run_server(analysis, vfs, build_queue); } } + +fn version() -> &'static str { + concat!(env!("CARGO_PKG_VERSION"), include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt"))) +} diff --git a/src/version.rs b/src/version.rs new file mode 100644 index 00000000000..3449b49b472 --- /dev/null +++ b/src/version.rs @@ -0,0 +1,6 @@ +use getopts::{Options, Matches}; + +pub fn create_cmd_option() -> Options { + let mut opt = Options::new(); + opt +} From 4fde1ff0ef1f9631c7e4929c08bec78b5321a017 Mon Sep 17 00:00:00 2001 From: Tetsuharu OHZEKI Date: Thu, 11 May 2017 08:23:08 +0900 Subject: [PATCH 2/4] fixup! Dump pkg version & build date & commit hash if run with `--version` flag. --- src/version.rs | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 src/version.rs diff --git a/src/version.rs b/src/version.rs deleted file mode 100644 index 3449b49b472..00000000000 --- a/src/version.rs +++ /dev/null @@ -1,6 +0,0 @@ -use getopts::{Options, Matches}; - -pub fn create_cmd_option() -> Options { - let mut opt = Options::new(); - opt -} From c3d99adb4d4a38155c8a4af3c0999f9f63dd54ae Mon Sep 17 00:00:00 2001 From: Tetsuharu OHZEKI Date: Thu, 11 May 2017 08:25:07 +0900 Subject: [PATCH 3/4] Remove Ignore in /build.rs --- build.rs | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/build.rs b/build.rs index de1f5a90194..f14117aa8f0 100644 --- a/build.rs +++ b/build.rs @@ -12,22 +12,11 @@ // [rustup]: https://github.com/rust-lang-nursery/rustup.rs/tree/a1c6be19add9c99f8d8868ec384aa76057fe7f70 use std::env; -use std::error::Error; use std::fs::File; use std::io::Write; use std::path::PathBuf; use std::process::Command; -struct Ignore; - -impl From for Ignore - where E: Error -{ - fn from(_: E) -> Ignore { - Ignore - } -} - fn main() { let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); @@ -41,24 +30,26 @@ fn main() { // (git not installed or if this is not a git repository) just return an empty string. fn commit_info() -> String { match (commit_hash(), commit_date()) { - (Ok(hash), Ok(date)) => format!(" ({} {})", hash.trim_right(), date), + (Some(hash), Some(date)) => format!(" ({} {})", hash.trim_right(), date), _ => String::new(), } } -fn commit_hash() -> Result { - Ok(try!(String::from_utf8(try!(Command::new("git") - .args(&["rev-parse", "--short", "HEAD"]) - .output()) - .stdout))) +fn commit_hash() -> Option { + Command::new("git") + .args(&["rev-parse", "--short", "HEAD"]) + .output() + .ok() + .and_then(|r| String::from_utf8(r.stdout).ok()) } -fn commit_date() -> Result { - Ok(try!(String::from_utf8(try!(Command::new("git") - .args(&["log", - "-1", - "--date=short", - "--pretty=format:%cd"]) - .output()) - .stdout))) +fn commit_date() -> Option { + Command::new("git") + .args(&["log", + "-1", + "--date=short", + "--pretty=format:%cd"]) + .output() + .ok() + .and_then(|r| String::from_utf8(r.stdout).ok()) } From 0384629b7bcab7be32b0cb00ab13b32c9dc8aba4 Mon Sep 17 00:00:00 2001 From: Tetsuharu OHZEKI Date: Thu, 11 May 2017 09:25:38 +0900 Subject: [PATCH 4/4] fixup! Dump pkg version & build date & commit hash if run with `--version` flag. --- src/main.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index df0e91cf2d1..7054286eb02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,21 +51,19 @@ type Span = span::Span; pub fn main() { env_logger::init().unwrap(); - let args: Vec = ::std::env::args().collect(); - if args[1] == "--version" || args[1] == "-V" { - println!("rls {}", version()); + if let Some(first_arg) = ::std::env::args().skip(1).next() { + match first_arg.as_str() { + "--version" | "-V" => println!("rls {}", version()), + _ => cmd::run(), + } return; } - if args.len() > 1 { - cmd::run(); - } else { - let analysis = Arc::new(analysis::AnalysisHost::new(analysis::Target::Debug)); - let vfs = Arc::new(vfs::Vfs::new()); - let build_queue = Arc::new(build::BuildQueue::new(vfs.clone())); + let analysis = Arc::new(analysis::AnalysisHost::new(analysis::Target::Debug)); + let vfs = Arc::new(vfs::Vfs::new()); + let build_queue = Arc::new(build::BuildQueue::new(vfs.clone())); - server::run_server(analysis, vfs, build_queue); - } + server::run_server(analysis, vfs, build_queue); } fn version() -> &'static str {