Skip to content

Commit 3192cd9

Browse files
committed
license should be from relative dir
1 parent daef88a commit 3192cd9

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/config/config_type.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ macro_rules! create_config {
6464
// if a license_template_path has been specified, successfully read, parsed and compiled
6565
// into a regex, it will be stored here
6666
pub license_template: Option<Regex>,
67+
// The location from which to resolve relative dirs.
68+
root_dir: Option<PathBuf>,
6769
// For each config item, we store a bool indicating whether it has
6870
// been accessed and the value, and a bool whether the option was
6971
// manually initialised, or taken from the default,
@@ -166,6 +168,7 @@ macro_rules! create_config {
166168
self.set_license_template();
167169
self.set_ignore(dir);
168170
self.set_merge_imports();
171+
self.set_root_dir(dir);
169172
self
170173
}
171174

@@ -390,7 +393,7 @@ macro_rules! create_config {
390393
if self.was_set().license_template_path() {
391394
let lt_path = self.license_template_path();
392395
if lt_path.len() > 0 {
393-
match license::load_and_compile_template(self.get_ignore(), &lt_path) {
396+
match license::load_and_compile_template(&self.root_dir, &lt_path) {
394397
Ok(re) => self.license_template = Some(re),
395398
Err(msg) => eprintln!("Warning for license template file {:?}: {}",
396399
lt_path, msg),
@@ -405,6 +408,10 @@ macro_rules! create_config {
405408
self.ignore.2.add_prefix(dir);
406409
}
407410

411+
fn set_root_dir(&mut self, dir: &Path) {
412+
self.root_dir = Some(dir.to_path_buf());
413+
}
414+
408415
fn set_merge_imports(&mut self) {
409416
if self.was_set().merge_imports() {
410417
eprintln!(
@@ -438,6 +445,7 @@ macro_rules! create_config {
438445
fn default() -> Config {
439446
Config {
440447
license_template: None,
448+
root_dir: None,
441449
$(
442450
$i: (Cell::new(false), false, $def, $stb),
443451
)+

src/config/license.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,22 @@ impl TemplateParser {
211211
}
212212
}
213213

214-
pub(crate) fn load_and_compile_template(base_dir: &str, path: &str) -> Result<Regex, LicenseError> {
215-
let mut path = Path(path);
214+
pub(crate) fn load_and_compile_template(
215+
root_dir: &Option<std::path::PathBuf>,
216+
path: &str,
217+
) -> Result<Regex, LicenseError> {
218+
let mut path = std::path::PathBuf::from(path);
216219
if path.is_relative() {
217-
// The tempate path needs to be resolved relative to the .rustfmt config file
218-
// rather than whatever dir cargo fmt was run from.
219-
let current = std::env::current_dir().expect("pwd");
220-
std::env::set_current_dir(base_dir);
221-
path = path.canonicalize();
222-
std::env::set_current_dir(current).expect("change pwd");
220+
if let Some(root_dir) = root_dir {
221+
// The tempate path needs to be resolved relative to the .rustfmt config file
222+
// rather than whatever dir cargo fmt was run from.
223+
let current = std::env::current_dir().expect("pwd");
224+
std::env::set_current_dir(root_dir).expect("alter pwd");
225+
if let Ok(abs_path) = path.canonicalize() {
226+
path = abs_path;
227+
}
228+
std::env::set_current_dir(current).expect("change pwd");
229+
}
223230
}
224231
let mut lt_file = File::open(&path)?;
225232
let mut lt_str = String::new();

0 commit comments

Comments
 (0)