@@ -8,7 +8,7 @@ mod gltf;
8
8
9
9
use error:: Error ;
10
10
11
- use ansi_term:: Color :: Red ;
11
+ use ansi_term:: Color :: { Red , Yellow } ;
12
12
use ordered_float:: NotNan ;
13
13
use std:: {
14
14
collections:: HashMap ,
@@ -50,6 +50,30 @@ struct ConvertCommand {
50
50
/// Output line segments in addition of surfaces (triangles/quads).
51
51
#[ structopt( long = "lines" , short = "l" , takes_value = false ) ]
52
52
with_lines : bool ,
53
+
54
+ /// Add the given location to the list of search paths for resolving parts.
55
+ ///
56
+ /// The location is appended to the list of search paths, in addition of the canonical paths derived
57
+ /// from the catalog location. This option can be used multiple times to add more search paths.
58
+ #[ structopt(
59
+ long = "include-path" ,
60
+ short = "I" ,
61
+ value_name = "PATH" ,
62
+ // multiple=true + number_of_values=1 => can appear multiple times, with one value per occurrence
63
+ multiple = true ,
64
+ number_of_values = 1
65
+ ) ]
66
+ include_paths : Option < Vec < PathBuf > > ,
67
+
68
+ /// Set the root location of the official LDraw catalog for resolving parts.
69
+ ///
70
+ /// This option adds the following search paths to the list of paths to resolve sub-file references from:
71
+ /// - <PATH>/p
72
+ /// - <PATH>/p/48
73
+ /// - <PATH>/parts
74
+ /// - <PATH>/parts/s
75
+ #[ structopt( long = "catalog-path" , short = "C" , value_name = "PATH" ) ]
76
+ catalog_path : Option < PathBuf > ,
53
77
}
54
78
55
79
#[ derive( StructOpt ) ]
@@ -92,8 +116,8 @@ impl DiskResolver {
92
116
DiskResolver { base_paths : vec ! [ ] }
93
117
}
94
118
95
- fn new_from_root ( root_path : & str ) -> DiskResolver {
96
- let root = PathBuf :: from ( root_path) ;
119
+ fn new_from_root < P : AsRef < Path > > ( root_path : P ) -> DiskResolver {
120
+ let root = root_path. as_ref ( ) . to_path_buf ( ) ;
97
121
let base_paths = vec ! [
98
122
root. join( "p" ) ,
99
123
root. join( "p" ) . join( "48" ) ,
@@ -103,6 +127,10 @@ impl DiskResolver {
103
127
DiskResolver { base_paths }
104
128
}
105
129
130
+ fn add_path < P : AsRef < Path > > ( & mut self , path : P ) {
131
+ self . base_paths . push ( path. as_ref ( ) . to_path_buf ( ) ) ;
132
+ }
133
+
106
134
/// Resolve a relative LDraw filename reference into an actual path on disk.
107
135
fn resolve_path ( & self , filename : & str ) -> Result < PathBuf , ResolveError > {
108
136
for prefix in & self . base_paths {
@@ -432,8 +460,29 @@ fn convert(app: &mut App, args: &ConvertCommand) -> Result<(), Error> {
432
460
}
433
461
let input = input. unwrap ( ) ;
434
462
463
+ let mut resolver = DiskResolver :: new ( ) ;
464
+ if let Some ( catalog_path) = & args. catalog_path {
465
+ resolver = DiskResolver :: new_from_root ( catalog_path) ;
466
+ } else if let Ok ( cwd) = std:: env:: current_dir ( ) {
467
+ eprintln ! (
468
+ "{}: No catalog path specified; using current working directory: {}" ,
469
+ Yellow . paint( "warning" ) ,
470
+ cwd. to_str( ) . unwrap_or( "(invalid path)" )
471
+ ) ;
472
+ resolver = DiskResolver :: new_from_root ( cwd) ;
473
+ } else {
474
+ app. print_error_and_exit (
475
+ "No include/catalog path specified, and cannot use current directory. \
476
+ Use -C/--catalog-path to specify the location of the catalog.",
477
+ )
478
+ }
479
+ if let Some ( include_paths) = & args. include_paths {
480
+ for path in include_paths {
481
+ resolver. add_path ( path) ;
482
+ }
483
+ }
484
+
435
485
eprintln ! ( "Parsing file '{}'" , input) ;
436
- let resolver = DiskResolver :: new_from_root ( "F:\\ dev\\ weldr\\ data" ) ;
437
486
let mut source_map = weldr:: SourceMap :: new ( ) ;
438
487
let source_file_ref = weldr:: parse ( input, & resolver, & mut source_map) ?;
439
488
0 commit comments