|
15 | 15 | //! `package_vers`, and otherwise indicating to the compiler what it should
|
16 | 16 | //! print out as part of its version information.
|
17 | 17 |
|
| 18 | +use std::path::Path; |
18 | 19 | use std::process::Command;
|
19 | 20 |
|
20 | 21 | use build_helper::output;
|
21 | 22 |
|
22 | 23 | use Build;
|
23 | 24 |
|
24 | 25 | // The version number
|
25 |
| -const CFG_RELEASE_NUM: &'static str = "1.17.0"; |
| 26 | +pub const CFG_RELEASE_NUM: &'static str = "1.17.0"; |
26 | 27 |
|
27 | 28 | // An optional number to put after the label, e.g. '.2' -> '-beta.2'
|
28 | 29 | // Be sure to make this starts with a dot to conform to semver pre-release
|
29 | 30 | // versions (section 9)
|
30 |
| -const CFG_PRERELEASE_VERSION: &'static str = ".1"; |
| 31 | +pub const CFG_PRERELEASE_VERSION: &'static str = ".1"; |
31 | 32 |
|
32 |
| -pub fn collect(build: &mut Build) { |
33 |
| - build.release_num = CFG_RELEASE_NUM.to_string(); |
34 |
| - build.prerelease_version = CFG_RELEASE_NUM.to_string(); |
| 33 | +pub struct GitInfo { |
| 34 | + inner: Option<Info>, |
| 35 | +} |
35 | 36 |
|
36 |
| - // Depending on the channel, passed in `./configure --release-channel`, |
37 |
| - // determine various properties of the build. |
38 |
| - match &build.config.channel[..] { |
39 |
| - "stable" => { |
40 |
| - build.release = CFG_RELEASE_NUM.to_string(); |
41 |
| - build.package_vers = build.release.clone(); |
42 |
| - build.unstable_features = false; |
43 |
| - } |
44 |
| - "beta" => { |
45 |
| - build.release = format!("{}-beta{}", CFG_RELEASE_NUM, |
46 |
| - CFG_PRERELEASE_VERSION); |
47 |
| - build.package_vers = "beta".to_string(); |
48 |
| - build.unstable_features = false; |
49 |
| - } |
50 |
| - "nightly" => { |
51 |
| - build.release = format!("{}-nightly", CFG_RELEASE_NUM); |
52 |
| - build.package_vers = "nightly".to_string(); |
53 |
| - build.unstable_features = true; |
54 |
| - } |
55 |
| - _ => { |
56 |
| - build.release = format!("{}-dev", CFG_RELEASE_NUM); |
57 |
| - build.package_vers = build.release.clone(); |
58 |
| - build.unstable_features = true; |
59 |
| - } |
60 |
| - } |
61 |
| - build.version = build.release.clone(); |
| 37 | +struct Info { |
| 38 | + commit_date: String, |
| 39 | + sha: String, |
| 40 | + short_sha: String, |
| 41 | +} |
62 | 42 |
|
63 |
| - // If we have a git directory, add in some various SHA information of what |
64 |
| - // commit this compiler was compiled from. |
65 |
| - if build.src.join(".git").is_dir() { |
66 |
| - let ver_date = output(Command::new("git").current_dir(&build.src) |
| 43 | +impl GitInfo { |
| 44 | + pub fn new(dir: &Path) -> GitInfo { |
| 45 | + if !dir.join(".git").is_dir() { |
| 46 | + return GitInfo { inner: None } |
| 47 | + } |
| 48 | + let ver_date = output(Command::new("git").current_dir(dir) |
67 | 49 | .arg("log").arg("-1")
|
68 | 50 | .arg("--date=short")
|
69 | 51 | .arg("--pretty=format:%cd"));
|
70 |
| - let ver_hash = output(Command::new("git").current_dir(&build.src) |
| 52 | + let ver_hash = output(Command::new("git").current_dir(dir) |
71 | 53 | .arg("rev-parse").arg("HEAD"));
|
72 | 54 | let short_ver_hash = output(Command::new("git")
|
73 |
| - .current_dir(&build.src) |
| 55 | + .current_dir(dir) |
74 | 56 | .arg("rev-parse")
|
75 | 57 | .arg("--short=9")
|
76 | 58 | .arg("HEAD"));
|
77 |
| - let ver_date = ver_date.trim().to_string(); |
78 |
| - let ver_hash = ver_hash.trim().to_string(); |
79 |
| - let short_ver_hash = short_ver_hash.trim().to_string(); |
80 |
| - build.version.push_str(&format!(" ({} {})", short_ver_hash, |
81 |
| - ver_date)); |
82 |
| - build.ver_date = Some(ver_date.to_string()); |
83 |
| - build.ver_hash = Some(ver_hash); |
84 |
| - build.short_ver_hash = Some(short_ver_hash); |
| 59 | + GitInfo { |
| 60 | + inner: Some(Info { |
| 61 | + commit_date: ver_date.trim().to_string(), |
| 62 | + sha: ver_hash.trim().to_string(), |
| 63 | + short_sha: short_ver_hash.trim().to_string(), |
| 64 | + }), |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + pub fn sha(&self) -> Option<&str> { |
| 69 | + self.inner.as_ref().map(|s| &s.sha[..]) |
| 70 | + } |
| 71 | + |
| 72 | + pub fn sha_short(&self) -> Option<&str> { |
| 73 | + self.inner.as_ref().map(|s| &s.short_sha[..]) |
| 74 | + } |
| 75 | + |
| 76 | + pub fn commit_date(&self) -> Option<&str> { |
| 77 | + self.inner.as_ref().map(|s| &s.commit_date[..]) |
| 78 | + } |
| 79 | + |
| 80 | + pub fn version(&self, build: &Build, num: &str) -> String { |
| 81 | + let mut version = build.release(num); |
| 82 | + if let Some(ref inner) = self.inner { |
| 83 | + version.push_str(" ("); |
| 84 | + version.push_str(&inner.short_sha); |
| 85 | + version.push_str(" "); |
| 86 | + version.push_str(&inner.commit_date); |
| 87 | + version.push_str(")"); |
| 88 | + } |
| 89 | + return version |
85 | 90 | }
|
86 | 91 | }
|
0 commit comments