]> git.lizzy.rs Git - rust.git/blob - src/librust/rust.rs
auto merge of #8001 : crnobog/rust/xfail-win32-7999, r=cmr
[rust.git] / src / librust / rust.rs
1 // Copyright 2013 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 // rust - central access to other rust tools
12 // FIXME #2238 Make commands run and test emit proper file endings on windows
13 // FIXME #2238 Make run only accept source that emits an executable
14
15 #[link(name = "rust",
16        vers = "0.8-pre",
17        uuid = "4a24da33-5cc8-4037-9352-2cbe9bd9d27c",
18        url = "https://github.com/mozilla/rust/tree/master/src/rust")];
19
20 #[license = "MIT/ASL2"];
21 #[crate_type = "lib"];
22
23 extern mod rustpkg;
24 extern mod rustdoc;
25 extern mod rusti;
26 extern mod rustc;
27
28 use std::io;
29 use std::os;
30 use std::run;
31 use std::libc::exit;
32
33 enum ValidUsage {
34     Valid(int), Invalid
35 }
36
37 impl ValidUsage {
38     fn is_valid(&self) -> bool {
39         match *self {
40             Valid(_)   => true,
41             Invalid    => false
42         }
43     }
44 }
45
46 enum Action {
47     Call(extern "Rust" fn(args: &[~str]) -> ValidUsage),
48     CallMain(&'static str, extern "Rust" fn()),
49 }
50
51 enum UsageSource<'self> {
52     UsgStr(&'self str),
53     UsgCall(extern "Rust" fn()),
54 }
55
56 struct Command<'self> {
57     cmd: &'self str,
58     action: Action,
59     usage_line: &'self str,
60     usage_full: UsageSource<'self>,
61 }
62
63 static COMMANDS: &'static [Command<'static>] = &[
64     Command{
65         cmd: "build",
66         action: CallMain("rustc", rustc::main),
67         usage_line: "compile rust source files",
68         usage_full: UsgCall(rustc_help),
69     },
70     Command{
71         cmd: "run",
72         action: Call(cmd_run),
73         usage_line: "build an executable, and run it",
74         usage_full: UsgStr(
75             "The run command is an shortcut for the command line \n\
76              \"rustc <filename> -o <filestem>~ && ./<filestem>~ [<arguments>...]\".\
77             \n\nUsage:\trust run <filename> [<arguments>...]"
78         )
79     },
80     Command{
81         cmd: "test",
82         action: Call(cmd_test),
83         usage_line: "build a test executable, and run it",
84         usage_full: UsgStr(
85             "The test command is an shortcut for the command line \n\
86             \"rustc --test <filename> -o <filestem>test~ && \
87             ./<filestem>test~\"\n\nUsage:\trust test <filename>"
88         )
89     },
90     Command{
91         cmd: "doc",
92         action: CallMain("rustdoc", rustdoc::main),
93         usage_line: "generate documentation from doc comments",
94         usage_full: UsgCall(rustdoc::config::usage),
95     },
96     Command{
97         cmd: "pkg",
98         action: CallMain("rustpkg", rustpkg::main),
99         usage_line: "download, build, install rust packages",
100         usage_full: UsgCall(rustpkg::usage::general),
101     },
102     Command{
103         cmd: "sketch",
104         action: CallMain("rusti", rusti::main),
105         usage_line: "run a rust interpreter",
106         usage_full: UsgStr("\nUsage:\trusti"),
107     },
108     Command{
109         cmd: "help",
110         action: Call(cmd_help),
111         usage_line: "show detailed usage of a command",
112         usage_full: UsgStr(
113             "The help command displays the usage text of another command.\n\
114             The text is either build in, or provided by the corresponding \
115             program.\n\nUsage:\trust help <command>"
116         )
117     }
118 ];
119
120 fn rustc_help() {
121     rustc::usage(os::args()[0].clone())
122 }
123
124 fn find_cmd(command_string: &str) -> Option<Command> {
125     do COMMANDS.iter().find_ |command| {
126         command.cmd == command_string
127     }.map_consume(|x| *x)
128 }
129
130 fn cmd_help(args: &[~str]) -> ValidUsage {
131     fn print_usage(command_string: ~str) -> ValidUsage {
132         match find_cmd(command_string) {
133             Some(command) => {
134                 match command.action {
135                     CallMain(prog, _) => printfln!(
136                         "The %s command is an alias for the %s program.",
137                         command.cmd, prog),
138                     _       => ()
139                 }
140                 match command.usage_full {
141                     UsgStr(msg) => printfln!("%s\n", msg),
142                     UsgCall(f)  => f(),
143                 }
144                 Valid(0)
145             },
146             None => Invalid
147         }
148     }
149
150     match args {
151         [ref command_string] => print_usage((*command_string).clone()),
152         _                    => Invalid
153     }
154 }
155
156 fn cmd_test(args: &[~str]) -> ValidUsage {
157     match args {
158         [ref filename] => {
159             let test_exec = Path(*filename).filestem().unwrap() + "test~";
160             invoke("rustc", &[~"--test", filename.to_owned(),
161                               ~"-o", test_exec.to_owned()], rustc::main);
162             let exit_code = run::process_status(~"./" + test_exec, []);
163             Valid(exit_code)
164         }
165         _ => Invalid
166     }
167 }
168
169 fn cmd_run(args: &[~str]) -> ValidUsage {
170     match args {
171         [ref filename, ..prog_args] => {
172             let exec = Path(*filename).filestem().unwrap() + "~";
173             invoke("rustc", &[filename.to_owned(), ~"-o", exec.to_owned()],
174                    rustc::main);
175             let exit_code = run::process_status(~"./"+exec, prog_args);
176             Valid(exit_code)
177         }
178         _ => Invalid
179     }
180 }
181
182 fn invoke(prog: &str, args: &[~str], f: &fn()) {
183     let mut osargs = ~[prog.to_owned()];
184     osargs.push_all_move(args.to_owned());
185     os::set_args(osargs);
186     f();
187 }
188
189 fn do_command(command: &Command, args: &[~str]) -> ValidUsage {
190     match command.action {
191         Call(f) => f(args),
192         CallMain(prog, f) => {
193             invoke(prog, args, f);
194             Valid(0)
195         }
196     }
197 }
198
199 fn usage() {
200     static INDENT: uint = 8;
201
202     io::print(
203         "The rust tool is a convenience for managing rust source code.\n\
204         It acts as a shortcut for programs of the rust tool chain.\n\
205         \n\
206         Usage:\trust <command> [arguments]\n\
207         \n\
208         The commands are:\n\
209         \n"
210     );
211
212     for COMMANDS.iter().advance |command| {
213         let padding = " ".repeat(INDENT - command.cmd.len());
214         printfln!("    %s%s%s", command.cmd, padding, command.usage_line);
215     }
216
217     io::print(
218         "\n\
219         Use \"rust help <command>\" for more information about a command.\n\
220         \n"
221     );
222
223 }
224
225 pub fn main() {
226     let os_args = os::args();
227
228     if (os_args.len() > 1 && (os_args[1] == ~"-v" || os_args[1] == ~"--version")) {
229         rustc::version(os_args[0]);
230         unsafe { exit(0); }
231     }
232
233     let args = os_args.tail();
234
235     if !args.is_empty() {
236         let r = find_cmd(*args.head());
237         for r.iter().advance |command| {
238             let result = do_command(command, args.tail());
239             match result {
240                 Valid(exit_code) => unsafe { exit(exit_code.to_i32()) },
241                 _                => loop
242             }
243         }
244     }
245
246     usage();
247 }