forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrust.rc
235 lines (208 loc) · 6.51 KB
/
rust.rc
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
// 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.
// rust - central access to other rust tools
// FIXME #2238 Make commands run and test emit proper file endings on windows
// FIXME #2238 Make run only accept source that emits an executable
#[link(name = "rust",
vers = "0.7-pre",
uuid = "4a24da33-5cc8-4037-9352-2cbe9bd9d27c",
url = "https://github.com/mozilla/rust/tree/master/src/rust")];
#[license = "MIT/ASL2"];
#[crate_type = "lib"];
extern mod rustpkg(vers = "0.7-pre");
extern mod rustdoc(vers = "0.7-pre");
extern mod rusti(vers = "0.7-pre");
extern mod rustc(vers = "0.7-pre");
use core::run;
enum ValidUsage {
Valid, Invalid
}
impl ValidUsage {
fn is_valid(&self) -> bool {
match *self {
Valid => true,
Invalid => false
}
}
}
enum Action<'self> {
Call(&'self fn(args: &[~str]) -> ValidUsage),
CallMain(&'static str, &'self fn()),
}
enum UsageSource<'self> {
UsgStr(&'self str),
UsgCall(&'self fn()),
}
struct Command<'self> {
cmd: &'self str,
action: Action<'self>,
usage_line: &'self str,
usage_full: UsageSource<'self>,
}
static commands: &'static [Command<'static>] = &[
Command{
cmd: "build",
action: CallMain("rustc", rustc::main),
usage_line: "compile rust source files",
usage_full: UsgCall(rustc_help),
},
Command{
cmd: "run",
action: Call(cmd_run),
usage_line: "build an executable, and run it",
usage_full: UsgStr(
"The run command is an shortcut for the command line \n\
\"rustc <filename> -o <filestem>~ && ./<filestem>~ [<arguments>...]\".\
\n\nUsage:\trust run <filename> [<arguments>...]"
)
},
Command{
cmd: "test",
action: Call(cmd_test),
usage_line: "build a test executable, and run it",
usage_full: UsgStr(
"The test command is an shortcut for the command line \n\
\"rustc --test <filename> -o <filestem>test~ && \
./<filestem>test~\"\n\nUsage:\trust test <filename>"
)
},
Command{
cmd: "doc",
action: CallMain("rustdoc", rustdoc::main),
usage_line: "generate documentation from doc comments",
usage_full: UsgCall(rustdoc::config::usage),
},
Command{
cmd: "pkg",
action: CallMain("rustpkg", rustpkg::main),
usage_line: "download, build, install rust packages",
usage_full: UsgCall(rustpkg::usage::general),
},
Command{
cmd: "sketch",
action: CallMain("rusti", rusti::main),
usage_line: "run a rust interpreter",
usage_full: UsgStr("\nUsage:\trusti"),
},
Command{
cmd: "help",
action: Call(cmd_help),
usage_line: "show detailed usage of a command",
usage_full: UsgStr(
"The help command displays the usage text of another command.\n\
The text is either build in, or provided by the corresponding \
program.\n\nUsage:\trust help <command>"
)
}
];
fn rustc_help() {
rustc::usage(copy os::args()[0])
}
fn find_cmd(command_string: &str) -> Option<Command> {
do commands.find |command| {
command.cmd == command_string
}
}
fn cmd_help(args: &[~str]) -> ValidUsage {
fn print_usage(command_string: ~str) -> ValidUsage {
match find_cmd(command_string) {
Some(command) => {
match command.action {
CallMain(prog, _) => io::println(fmt!(
"The %s command is an alias for the %s program.",
command.cmd, prog)),
_ => ()
}
match command.usage_full {
UsgStr(msg) => io::println(fmt!("%s\n", msg)),
UsgCall(f) => f(),
}
Valid
},
None => Invalid
}
}
match args {
[command_string] => print_usage(command_string),
_ => Invalid
}
}
fn cmd_test(args: &[~str]) -> ValidUsage {
match args {
[filename] => {
let test_exec = Path(filename).filestem().unwrap() + "test~";
invoke("rustc", &[~"--test", filename.to_owned(),
~"-o", test_exec.to_owned()], rustc::main);
run::run_program(~"./" + test_exec, []);
Valid
}
_ => Invalid
}
}
fn cmd_run(args: &[~str]) -> ValidUsage {
match args {
[filename, ..prog_args] => {
let exec = Path(filename).filestem().unwrap() + "~";
invoke("rustc", &[filename.to_owned(), ~"-o", exec.to_owned()],
rustc::main);
run::run_program(~"./"+exec, prog_args);
Valid
}
_ => Invalid
}
}
fn invoke(prog: &str, args: &[~str], f: &fn()) {
let mut osargs = ~[prog.to_owned()];
osargs.push_all_move(args.to_owned());
os::set_args(osargs);
f();
}
fn do_command(command: &Command, args: &[~str]) -> ValidUsage {
match command.action {
Call(f) => f(args),
CallMain(prog, f) => {
invoke(prog, args, f);
Valid
}
}
}
fn usage() {
static indent: uint = 8;
io::print(
"The rust tool is a convenience for managing rust source code.\n\
It acts as a shortcut for programs of the rust tool chain.\n\
\n\
Usage:\trust <command> [arguments]\n\
\n\
The commands are:\n\
\n"
);
for commands.each |command| {
let padding = str::repeat(" ", indent - command.cmd.len());
io::println(fmt!(" %s%s%s",
command.cmd, padding, command.usage_line));
}
io::print(
"\n\
Use \"rust help <command>\" for more information about a command.\n\
\n"
);
}
pub fn main() {
let os_args = os::args();
let args = os_args.tail();
if !args.is_empty() {
for find_cmd(*args.head()).each |command| {
let result = do_command(command, args.tail());
if result.is_valid() { return; }
}
}
usage();
}