/
version.rs
254 lines (232 loc) · 8.58 KB
/
version.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2013 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/// A version is either an exact revision,
/// or a semantic version
extern mod std;
use extra::semver;
use std::{char, os, result, run, str};
use extra::tempfile::mkdtemp;
use path_util::rust_path;
#[deriving(Clone)]
pub enum Version {
ExactRevision(~str), // Should look like a m.n.(...).x
SemanticVersion(semver::Version),
Tagged(~str), // String that can't be parsed as a version.
// Requirements get interpreted exactly
NoVersion // user didn't specify a version -- prints as 0.1
}
// Equality on versions is non-symmetric: if self is NoVersion, it's equal to
// anything; but if self is a precise version, it's not equal to NoVersion.
// We should probably make equality symmetric, and use less-than and greater-than
// where we currently use eq
impl Eq for Version {
fn eq(&self, other: &Version) -> bool {
match (self, other) {
(&ExactRevision(ref s1), &ExactRevision(ref s2)) => *s1 == *s2,
(&SemanticVersion(ref v1), &SemanticVersion(ref v2)) => *v1 == *v2,
(&NoVersion, _) => true,
_ => false
}
}
}
impl Ord for Version {
fn lt(&self, other: &Version) -> bool {
match (self, other) {
(&NoVersion, _) => true,
(&ExactRevision(ref f1), &ExactRevision(ref f2)) => f1 < f2,
(&SemanticVersion(ref v1), &SemanticVersion(ref v2)) => v1 < v2,
_ => false // incomparable, really
}
}
fn le(&self, other: &Version) -> bool {
match (self, other) {
(&NoVersion, _) => true,
(&ExactRevision(ref f1), &ExactRevision(ref f2)) => f1 <= f2,
(&SemanticVersion(ref v1), &SemanticVersion(ref v2)) => v1 <= v2,
_ => false // incomparable, really
}
}
fn ge(&self, other: &Version) -> bool {
match (self, other) {
(&ExactRevision(ref f1), &ExactRevision(ref f2)) => f1 > f2,
(&SemanticVersion(ref v1), &SemanticVersion(ref v2)) => v1 > v2,
_ => false // incomparable, really
}
}
fn gt(&self, other: &Version) -> bool {
match (self, other) {
(&ExactRevision(ref f1), &ExactRevision(ref f2)) => f1 >= f2,
(&SemanticVersion(ref v1), &SemanticVersion(ref v2)) => v1 >= v2,
_ => false // incomparable, really
}
}
}
impl ToStr for Version {
fn to_str(&self) -> ~str {
match *self {
ExactRevision(ref n) | Tagged(ref n) => fmt!("%s", n.to_str()),
SemanticVersion(ref v) => fmt!("%s", v.to_str()),
NoVersion => ~"0.1"
}
}
}
pub fn parse_vers(vers: ~str) -> result::Result<semver::Version, ~str> {
match semver::parse(vers) {
Some(vers) => result::Ok(vers),
None => result::Err(~"could not parse version: invalid")
}
}
/// If `local_path` is a git repo in the RUST_PATH, and the most recent tag
/// in that repo denotes a version, return it; otherwise, `None`
pub fn try_getting_local_version(local_path: &Path) -> Option<Version> {
let rustpath = rust_path();
for rp in rustpath.iter() {
let local_path = rp.push_rel(local_path);
debug!("in try_getting_local_version");
let outp = run::process_output("git",
[fmt!("--git-dir=%s", local_path.push(".git").to_str()),
~"tag", ~"-l"]);
debug!("git --git-dir=%s tag -l ~~~> %?", local_path.push(".git").to_str(), outp.status);
if outp.status != 0 {
loop;
}
let mut output = None;
let output_text = str::from_bytes(outp.output);
for l in output_text.line_iter() {
if !l.is_whitespace() {
output = Some(l);
}
match output.chain(try_parsing_version) {
Some(v) => return Some(v),
None => ()
}
}
}
None
}
/// If `remote_path` refers to a git repo that can be downloaded,
/// and the most recent tag in that repo denotes a version, return it;
/// otherwise, `None`
pub fn try_getting_version(remote_path: &Path) -> Option<Version> {
debug!("try_getting_version: %s", remote_path.to_str());
if is_url_like(remote_path) {
debug!("Trying to fetch its sources..");
let tmp_dir = mkdtemp(&os::tmpdir(),
"test").expect("try_getting_version: couldn't create temp dir");
debug!("(to get version) executing {git clone https://%s %s}",
remote_path.to_str(),
tmp_dir.to_str());
let outp = run::process_output("git", [~"clone", fmt!("https://%s", remote_path.to_str()),
tmp_dir.to_str()]);
if outp.status == 0 {
debug!("Cloned it... ( %s, %s )",
str::from_bytes(outp.output),
str::from_bytes(outp.error));
let mut output = None;
debug!("(getting version, now getting tags) executing {git --git-dir=%s tag -l}",
tmp_dir.push(".git").to_str());
let outp = run::process_output("git",
[fmt!("--git-dir=%s", tmp_dir.push(".git").to_str()),
~"tag", ~"-l"]);
let output_text = str::from_bytes(outp.output);
debug!("Full output: ( %s ) [%?]", output_text, outp.status);
for l in output_text.line_iter() {
debug!("A line of output: %s", l);
if !l.is_whitespace() {
output = Some(l);
}
}
output.chain(try_parsing_version)
}
else {
None
}
}
else {
None
}
}
// Being lazy since we don't have a regexp library now
#[deriving(Eq)]
enum ParseState {
Start,
SawDigit,
SawDot
}
pub fn try_parsing_version(s: &str) -> Option<Version> {
let s = s.trim();
debug!("Attempting to parse: %s", s);
let mut parse_state = Start;
for c in s.iter() {
if char::is_digit(c) {
parse_state = SawDigit;
}
else if c == '.' && parse_state == SawDigit {
parse_state = SawDot;
}
else {
return None;
}
}
match parse_state {
SawDigit => Some(ExactRevision(s.to_owned())),
_ => None
}
}
/// Just an approximation
fn is_url_like(p: &Path) -> bool {
let str = p.to_str();
str.split_iter('/').len() > 2
}
/// If s is of the form foo#bar, where bar is a valid version
/// number, return the prefix before the # and the version.
/// Otherwise, return None.
pub fn split_version<'a>(s: &'a str) -> Option<(&'a str, Version)> {
// Check for extra '#' characters separately
if s.split_iter('#').len() > 2 {
return None;
}
split_version_general(s, '#')
}
pub fn split_version_general<'a>(s: &'a str, sep: char) -> Option<(&'a str, Version)> {
match s.rfind(sep) {
Some(i) => {
debug!("in %s, i = %?", s, i);
let path = s.slice(0, i);
debug!("path = %s", path);
// n.b. for now, assuming an exact revision is intended, not a SemVer
Some((path, ExactRevision(s.slice(i + 1, s.len()).to_owned())))
}
None => {
debug!("%s doesn't look like an explicit-version thing", s);
None
}
}
}
#[test]
fn test_parse_version() {
assert!(try_parsing_version("1.2") == Some(ExactRevision(~"1.2")));
assert!(try_parsing_version("1.0.17") == Some(ExactRevision(~"1.0.17")));
assert!(try_parsing_version("you're_a_kitty") == None);
assert!(try_parsing_version("42..1") == None);
assert!(try_parsing_version("17") == Some(ExactRevision(~"17")));
assert!(try_parsing_version(".1.2.3") == None);
assert!(try_parsing_version("2.3.") == None);
}
#[test]
fn test_split_version() {
let s = "a/b/c#0.1";
debug!("== %? ==", split_version(s));
assert!(split_version(s) == Some((s.slice(0, 5), ExactRevision(~"0.1"))));
assert!(split_version("a/b/c") == None);
let s = "a#1.2";
assert!(split_version(s) == Some((s.slice(0, 1), ExactRevision(~"1.2"))));
assert!(split_version("a#a#3.4") == None);
}