Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 5551c4e

Browse files
authored
Merge pull request #308 from saneyuki/version-flag
Dump pkg version & build date & commit hash if run with `--version` flag
2 parents e66caa4 + 0384629 commit 5551c4e

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ name = "rls"
33
version = "0.1.0"
44
authors = ["Jonathan Turner <[email protected]>"]
55

6+
build = "build.rs"
7+
68
[dependencies]
79
cargo = { git = "https://github.com/rust-lang/cargo" }
810
derive-new = "0.3"

build.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// The original code is [rustup.rs][rustup]
12+
// [rustup]: https://github.com/rust-lang-nursery/rustup.rs/tree/a1c6be19add9c99f8d8868ec384aa76057fe7f70
13+
14+
use std::env;
15+
use std::fs::File;
16+
use std::io::Write;
17+
use std::path::PathBuf;
18+
use std::process::Command;
19+
20+
fn main() {
21+
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
22+
23+
File::create(out_dir.join("commit-info.txt"))
24+
.unwrap()
25+
.write_all(commit_info().as_bytes())
26+
.unwrap();
27+
}
28+
29+
// Try to get hash and date of the last commit on a best effort basis. If anything goes wrong
30+
// (git not installed or if this is not a git repository) just return an empty string.
31+
fn commit_info() -> String {
32+
match (commit_hash(), commit_date()) {
33+
(Some(hash), Some(date)) => format!(" ({} {})", hash.trim_right(), date),
34+
_ => String::new(),
35+
}
36+
}
37+
38+
fn commit_hash() -> Option<String> {
39+
Command::new("git")
40+
.args(&["rev-parse", "--short", "HEAD"])
41+
.output()
42+
.ok()
43+
.and_then(|r| String::from_utf8(r.stdout).ok())
44+
}
45+
46+
fn commit_date() -> Option<String> {
47+
Command::new("git")
48+
.args(&["log",
49+
"-1",
50+
"--date=short",
51+
"--pretty=format:%cd"])
52+
.output()
53+
.ok()
54+
.and_then(|r| String::from_utf8(r.stdout).ok())
55+
}

src/main.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,21 @@ type Span = span::Span<span::ZeroIndexed>;
5757
pub fn main() {
5858
env_logger::init().unwrap();
5959

60-
let arg_count = ::std::env::args().count();
61-
if arg_count > 1 {
62-
cmd::run();
63-
} else {
64-
let analysis = Arc::new(analysis::AnalysisHost::new(analysis::Target::Debug));
65-
let vfs = Arc::new(vfs::Vfs::new());
66-
let build_queue = Arc::new(build::BuildQueue::new(vfs.clone()));
67-
68-
server::run_server(analysis, vfs, build_queue);
60+
if let Some(first_arg) = ::std::env::args().skip(1).next() {
61+
match first_arg.as_str() {
62+
"--version" | "-V" => println!("rls {}", version()),
63+
_ => cmd::run(),
64+
}
65+
return;
6966
}
67+
68+
let analysis = Arc::new(analysis::AnalysisHost::new(analysis::Target::Debug));
69+
let vfs = Arc::new(vfs::Vfs::new());
70+
let build_queue = Arc::new(build::BuildQueue::new(vfs.clone()));
71+
72+
server::run_server(analysis, vfs, build_queue);
73+
}
74+
75+
fn version() -> &'static str {
76+
concat!(env!("CARGO_PKG_VERSION"), include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt")))
7077
}

0 commit comments

Comments
 (0)