Skip to content

Commit 0d8a49a

Browse files
committed
Add an optional flag for automatically filling out the include path; #16
1 parent f8975ff commit 0d8a49a

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

src/cmd_line.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,17 @@ pub fn get_args() -> ArgMatches<'static> {
7373
.long("no-comments")
7474
.help("Generate the file without any comments"),
7575
)
76+
.arg(
77+
Arg::with_name("detect-directory")
78+
.short("D")
79+
.long("detect-directory")
80+
.help("Detect the include path, rather than using the <path> placeholder")
81+
)
7682
.arg(
7783
Arg::with_name("no-prefixes")
7884
.short("P")
7985
.long("no-prefixes")
80-
.help("Do not use module type prefixes (e.g. `proc_`) in file names"),
86+
.help("Do not use module type prefixes (such as `proc_`) in file names"),
8187
)
8288
.arg(
8389
Arg::with_name("target-dir")

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct Options {
1111
comments: bool,
1212
prefixes: bool,
1313
target_dir: String,
14+
detect_directory: bool,
1415
}
1516

1617
fn main() {
@@ -29,6 +30,7 @@ fn main() {
2930
} else {
3031
String::from(".")
3132
},
33+
detect_directory: cmdline_args.is_present("detect-directory"),
3234
};
3335

3436
// Store all modules except for the populated assembly that will be created in this Vec

src/module.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,24 @@ impl Input {
155155

156156
/// Prepare an include statement that can be used to include the generated file from elsewhere.
157157
fn include_statement(&self) -> String {
158-
// Determine the start of the include statement from the target path.
158+
let path_placeholder = Path::new("<path>").to_path_buf();
159+
160+
let include_path = if self.options.detect_directory {
161+
match self.infer_include_dir() {
162+
Some(path) => path,
163+
None => path_placeholder,
164+
}
165+
} else {
166+
path_placeholder
167+
};
168+
169+
format!("include::{}/{}[leveloffset=+1]", include_path.display(), &self.file_name())
170+
}
171+
172+
/// Determine the start of the include statement from the target path.
173+
/// Returns the relative path that can be used in the include statement, if it's possible
174+
/// to determine it automatically.
175+
fn infer_include_dir(&self) -> Option<PathBuf> {
159176
// The first directory in the include path is either `assemblies/` or `modules/`,
160177
// based on the module type.
161178
let include_root = match &self.mod_type {
@@ -179,19 +196,19 @@ impl Input {
179196

180197
// If there is such a root element in the path, construct the include path.
181198
// TODO: To be safe, check that the root path element still exists in a Git repository.
182-
let include_path = if let Some(position) = root_position {
183-
component_vec
199+
if let Some(position) = root_position {
200+
let include_path = component_vec
184201
.split_off(position)
185202
.iter()
186-
.collect::<PathBuf>()
203+
.collect::<PathBuf>();
204+
Some(include_path)
187205
// If no appropriate root element was found, use a generic placeholder.
188206
} else {
189-
Path::new("<path>").to_path_buf()
190-
};
191-
192-
format!("include::{}/{}[leveloffset=+1]", include_path.display(), &self.file_name())
207+
None
208+
}
193209
}
194210

211+
195212
/// Perform string replacements in the modular template that matches the `ModuleType`.
196213
/// Return the template text with all replacements.
197214
pub fn text(&self) -> String {

0 commit comments

Comments
 (0)