1
1
use std:: env;
2
2
use std:: path:: { Path , PathBuf } ;
3
3
4
- use clap:: { ArgMatches , Command , arg, crate_version} ;
5
- use mdbook:: MDBook ;
4
+ use clap:: { arg, crate_version, ArgMatches , Command } ;
6
5
use mdbook:: errors:: Result as Result3 ;
6
+ use mdbook:: MDBook ;
7
7
use mdbook_i18n_helpers:: preprocessors:: Gettext ;
8
8
use mdbook_spec:: Spec ;
9
9
use mdbook_trpl_listing:: TrplListing ;
@@ -26,6 +26,20 @@ fn main() {
26
26
(Defaults to the current directory when omitted)")
27
27
. value_parser ( clap:: value_parser!( PathBuf ) ) ;
28
28
29
+ // Note: we don't parse this into a `PathBuf` because it is comma separated
30
+ // strings *and* we will ultimately pass it into `MDBook::test()`, which
31
+ // accepts `Vec<&str>`. Although it is a bit annoying that `-l/--lang` and
32
+ // `-L/--library-path` are so close, this is the same set of arguments we
33
+ // would pass when invoking mdbook on the CLI, so making them match when
34
+ // invoking rustbook makes for good consistency.
35
+ let library_path_arg = arg ! (
36
+ -L --"library-path"
37
+ "A comma-separated list of directories to add to the crate search\n \
38
+ path when building tests"
39
+ )
40
+ . required ( false )
41
+ . value_parser ( parse_library_paths) ;
42
+
29
43
let matches = Command :: new ( "rustbook" )
30
44
. about ( "Build a book with mdBook" )
31
45
. author ( "Steve Klabnik <[email protected] >" )
@@ -42,7 +56,8 @@ fn main() {
42
56
. subcommand (
43
57
Command :: new ( "test" )
44
58
. about ( "Tests that a book's Rust code samples compile" )
45
- . arg ( dir_arg) ,
59
+ . arg ( dir_arg)
60
+ . arg ( library_path_arg) ,
46
61
)
47
62
. get_matches ( ) ;
48
63
@@ -106,14 +121,22 @@ pub fn build(args: &ArgMatches) -> Result3<()> {
106
121
107
122
fn test ( args : & ArgMatches ) -> Result3 < ( ) > {
108
123
let book_dir = get_book_dir ( args) ;
124
+ let library_paths = args
125
+ . try_get_one :: < Vec < String > > ( "library_path" ) ?
126
+ . map ( |v| v. iter ( ) . map ( |s| s. as_str ( ) ) . collect :: < Vec < & str > > ( ) )
127
+ . unwrap_or_default ( ) ;
109
128
let mut book = load_book ( & book_dir) ?;
110
- book. test ( vec ! [ ] )
129
+ book. test ( library_paths )
111
130
}
112
131
113
132
fn get_book_dir ( args : & ArgMatches ) -> PathBuf {
114
133
if let Some ( p) = args. get_one :: < PathBuf > ( "dir" ) {
115
134
// Check if path is relative from current dir, or absolute...
116
- if p. is_relative ( ) { env:: current_dir ( ) . unwrap ( ) . join ( p) } else { p. to_path_buf ( ) }
135
+ if p. is_relative ( ) {
136
+ env:: current_dir ( ) . unwrap ( ) . join ( p)
137
+ } else {
138
+ p. to_path_buf ( )
139
+ }
117
140
} else {
118
141
env:: current_dir ( ) . unwrap ( )
119
142
}
@@ -125,12 +148,16 @@ fn load_book(book_dir: &Path) -> Result3<MDBook> {
125
148
Ok ( book)
126
149
}
127
150
151
+ fn parse_library_paths ( input : & str ) -> Result < Vec < String > , String > {
152
+ Ok ( input. split ( "," ) . map ( String :: from) . collect ( ) )
153
+ }
154
+
128
155
fn handle_error ( error : mdbook:: errors:: Error ) -> ! {
129
156
eprintln ! ( "Error: {}" , error) ;
130
157
131
158
for cause in error. chain ( ) . skip ( 1 ) {
132
159
eprintln ! ( "\t Caused By: {}" , cause) ;
133
160
}
134
161
135
- :: std:: process:: exit ( 101 ) ;
162
+ std:: process:: exit ( 101 ) ;
136
163
}
0 commit comments