Skip to content

Commit ac63279

Browse files
committed
wip
1 parent f75e929 commit ac63279

File tree

8 files changed

+78
-69
lines changed

8 files changed

+78
-69
lines changed

rewatch/src/build.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,7 @@ pub fn incremental_build(
440440
pub fn write_build_ninja(build_state: &BuildState) {
441441
for package in build_state.packages.values() {
442442
// write empty file:
443-
let mut f = File::create(std::path::Path::new(&package.get_build_path()).join("build.ninja"))
444-
.expect("Unable to write file");
443+
let mut f = File::create(package.get_build_path().join("build.ninja")).expect("Unable to write file");
445444
f.write_all(b"").expect("unable to write to ninja file");
446445
}
447446
}

rewatch/src/build/compile.rs

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,14 @@ pub fn compiler_args(
455455
.unwrap()
456456
.to_string()
457457
} else {
458-
format!(
459-
"lib/{}",
460-
Path::join(
458+
Path::new("lib")
459+
.join(Path::join(
461460
Path::new(&spec.get_out_of_source_dir()),
462-
Path::new(file_path).parent().unwrap()
463-
)
461+
Path::new(file_path).parent().unwrap(),
462+
))
464463
.to_str()
465464
.unwrap()
466-
)
465+
.to_string()
467466
},
468467
root_config.get_suffix(spec),
469468
),
@@ -645,41 +644,46 @@ fn compile_file(
645644
// perhaps we can do this copying somewhere else
646645
if !is_interface {
647646
let _ = std::fs::copy(
648-
std::path::Path::new(&package.get_build_path())
647+
package
648+
.get_build_path()
649649
.join(dir)
650650
// because editor tooling doesn't support namespace entries yet
651651
// we just remove the @ for now. This makes sure the editor support
652652
// doesn't break
653-
.join(module_name.to_string() + ".cmi"),
654-
ocaml_build_path_abs.join(module_name.to_string() + ".cmi"),
653+
.join(format!("{}.cmi", module_name)),
654+
ocaml_build_path_abs.join(format!("{}.cmi", module_name)),
655655
);
656656
let _ = std::fs::copy(
657-
std::path::Path::new(&package.get_build_path())
657+
package
658+
.get_build_path()
658659
.join(dir)
659-
.join(module_name.to_string() + ".cmj"),
660-
ocaml_build_path_abs.join(module_name.to_string() + ".cmj"),
660+
.join(format!("{}.cmj", module_name)),
661+
ocaml_build_path_abs.join(format!("{}.cmj", module_name)),
661662
);
662663
let _ = std::fs::copy(
663-
std::path::Path::new(&package.get_build_path())
664+
package
665+
.get_build_path()
664666
.join(dir)
665667
// because editor tooling doesn't support namespace entries yet
666668
// we just remove the @ for now. This makes sure the editor support
667669
// doesn't break
668-
.join(module_name.to_string() + ".cmt"),
669-
ocaml_build_path_abs.join(module_name.to_string() + ".cmt"),
670+
.join(format!("{}.cmt", module_name)),
671+
ocaml_build_path_abs.join(format!("{}.cmt", module_name)),
670672
);
671673
} else {
672674
let _ = std::fs::copy(
673-
std::path::Path::new(&package.get_build_path())
675+
package
676+
.get_build_path()
674677
.join(dir)
675-
.join(module_name.to_string() + ".cmti"),
676-
ocaml_build_path_abs.join(module_name.to_string() + ".cmti"),
678+
.join(format!("{}.cmti", module_name)),
679+
ocaml_build_path_abs.join(format!("{}.cmti", module_name)),
677680
);
678681
let _ = std::fs::copy(
679-
std::path::Path::new(&package.get_build_path())
682+
package
683+
.get_build_path()
680684
.join(dir)
681-
.join(module_name.to_string() + ".cmi"),
682-
ocaml_build_path_abs.join(module_name.to_string() + ".cmi"),
685+
.join(format!("{}.cmi", module_name)),
686+
ocaml_build_path_abs.join(format!("{}.cmi", module_name)),
683687
);
684688
}
685689

@@ -692,14 +696,15 @@ fn compile_file(
692696
// editor tools expects the source file in lib/bs for finding the current package
693697
// and in lib/ocaml when referencing modules in other packages
694698
let _ = std::fs::copy(
695-
std::path::Path::new(&package.path).join(path),
696-
std::path::Path::new(&package.get_build_path()).join(path),
699+
Path::new(&package.path).join(path),
700+
package.get_build_path().join(path),
697701
)
698702
.expect("copying source file failed");
699703

700704
let _ = std::fs::copy(
701-
std::path::Path::new(&package.path).join(path),
702-
std::path::Path::new(&package.get_ocaml_build_path())
705+
Path::new(&package.path).join(path),
706+
package
707+
.get_ocaml_build_path()
703708
.join(std::path::Path::new(path).file_name().unwrap()),
704709
)
705710
.expect("copying source file failed");
@@ -715,14 +720,15 @@ fn compile_file(
715720
// editor tools expects the source file in lib/bs for finding the current package
716721
// and in lib/ocaml when referencing modules in other packages
717722
let _ = std::fs::copy(
718-
std::path::Path::new(&package.path).join(path),
719-
std::path::Path::new(&package.get_build_path()).join(path),
723+
Path::new(&package.path).join(path),
724+
package.get_build_path().join(path),
720725
)
721726
.expect("copying source file failed");
722727

723728
let _ = std::fs::copy(
724-
std::path::Path::new(&package.path).join(path),
725-
std::path::Path::new(&package.get_ocaml_build_path())
729+
Path::new(&package.path).join(path),
730+
package
731+
.get_ocaml_build_path()
726732
.join(std::path::Path::new(path).file_name().unwrap()),
727733
)
728734
.expect("copying source file failed");
@@ -739,11 +745,11 @@ fn compile_file(
739745
..
740746
}) => {
741747
let source = helpers::get_source_file_from_rescript_file(
742-
&std::path::Path::new(&package.path).join(path),
748+
&Path::new(&package.path).join(path),
743749
&root_package.config.get_suffix(spec),
744750
);
745751
let destination = helpers::get_source_file_from_rescript_file(
746-
&std::path::Path::new(&package.get_build_path()).join(path),
752+
&package.get_build_path().join(path),
747753
&root_package.config.get_suffix(spec),
748754
);
749755

rewatch/src/build/namespaces.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn gen_mlmap(
3030
// recompile in a different way but we need to put it in the file for it to
3131
// be readable.
3232

33-
let path = build_path_abs.join(namespace.to_string() + ".mlmap");
33+
let path = build_path_abs.join(format!("{}.mlmap", namespace));
3434
let mut file = File::create(&path).expect("Unable to create mlmap");
3535

3636
file.write_all(b"randjbuildsystem\n")

rewatch/src/build/packages.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,19 @@ impl Package {
9797
}
9898

9999
pub fn get_mlmap_path(&self) -> PathBuf {
100-
self.get_build_path().join(
101-
self.namespace
102-
.to_suffix()
103-
.expect("namespace should be set for mlmap module")
104-
+ ".mlmap",
105-
)
100+
let suffix = self
101+
.namespace
102+
.to_suffix()
103+
.expect("namespace should be set for mlmap module");
104+
self.get_build_path().join(format!("{}.mlmap", suffix))
106105
}
107106

108107
pub fn get_mlmap_compile_path(&self) -> PathBuf {
109-
self.get_build_path().join(
110-
self.namespace
111-
.to_suffix()
112-
.expect("namespace should be set for mlmap module")
113-
+ ".cmi",
114-
)
108+
let suffix = self
109+
.namespace
110+
.to_suffix()
111+
.expect("namespace should be set for mlmap module");
112+
self.get_build_path().join(format!("{}.cmi", suffix))
115113
}
116114
}
117115

rewatch/src/build/parse.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,13 @@ pub fn generate_asts(
7474
} else {
7575
(
7676
Ok((
77-
Path::new(
78-
&(helpers::get_basename(&source_file.implementation.path).to_string()
79-
+ ".ast"),
80-
)
81-
.to_path_buf(),
77+
PathBuf::from(helpers::get_basename(&source_file.implementation.path))
78+
.with_extension("ast"),
8279
None,
8380
)),
8481
Ok(source_file.interface.as_ref().map(|i| {
8582
(
86-
Path::new(&(helpers::get_basename(&i.path).to_string() + ".iast"))
87-
.to_path_buf(),
83+
PathBuf::from(helpers::get_basename(&i.path)).with_extension("iast"),
8884
None,
8985
)
9086
})),
@@ -356,7 +352,7 @@ fn generate_ast(
356352
if let Ok((ast_path, _)) = &result {
357353
let _ = std::fs::copy(
358354
Path::new(&build_path_abs).join(&ast_path),
359-
std::path::Path::new(&package.get_ocaml_build_path()).join(ast_path.file_name().unwrap()),
355+
package.get_ocaml_build_path().join(ast_path.file_name().unwrap()),
360356
);
361357
}
362358
result

rewatch/src/build/read_compile_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState {
4545
.packages
4646
.par_iter()
4747
.map(|(_, package)| {
48-
let read_dir = fs::read_dir(std::path::Path::new(&package.get_ocaml_build_path())).unwrap();
48+
let read_dir = fs::read_dir(&package.get_ocaml_build_path()).unwrap();
4949
read_dir
5050
.filter_map(|entry| match entry {
5151
Ok(entry) => {

rewatch/src/helpers.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,13 @@ fn path_to_ast_extension(path: &Path) -> &str {
192192

193193
pub fn get_ast_path(source_file: &Path) -> PathBuf {
194194
let source_path = source_file;
195+
let basename = file_path_to_compiler_asset_basename(source_file, &packages::Namespace::NoNamespace);
196+
let extension = path_to_ast_extension(source_path);
195197

196-
source_path.parent().unwrap().join(
197-
file_path_to_compiler_asset_basename(source_file, &packages::Namespace::NoNamespace)
198-
+ path_to_ast_extension(source_path),
199-
)
198+
source_path
199+
.parent()
200+
.unwrap()
201+
.join(format!("{}{}", basename, extension))
200202
}
201203

202204
pub fn get_compiler_asset(
@@ -209,9 +211,10 @@ pub fn get_compiler_asset(
209211
"ast" | "iast" => &packages::Namespace::NoNamespace,
210212
_ => namespace,
211213
};
214+
let basename = file_path_to_compiler_asset_basename(source_file, namespace);
212215
package
213216
.get_ocaml_build_path()
214-
.join(file_path_to_compiler_asset_basename(source_file, namespace) + "." + extension)
217+
.join(format!("{}.{}", basename, extension))
215218
}
216219

217220
pub fn canonicalize_string_path(path: &str) -> Option<PathBuf> {
@@ -229,11 +232,13 @@ pub fn get_bs_compiler_asset(
229232
_ => namespace,
230233
};
231234

232-
let dir = std::path::Path::new(&source_file).parent().unwrap();
235+
let dir = source_file.parent().unwrap();
236+
let basename = file_path_to_compiler_asset_basename(source_file, namespace);
233237

234-
std::path::Path::new(&package.get_build_path())
238+
package
239+
.get_build_path()
235240
.join(dir)
236-
.join(file_path_to_compiler_asset_basename(source_file, namespace) + extension)
241+
.join(format!("{}{}", basename, extension))
237242
.to_str()
238243
.unwrap()
239244
.to_owned()

rewatch/src/lock.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@ impl std::fmt::Display for Error {
2222
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
2323
let msg = match self {
2424
Error::Locked(pid) => format!("Rewatch is already running. The process ID (PID) is {}", pid),
25-
Error::ParsingLockfile(e) => format!("Could not parse lockfile: \n {} \n (try removing it and running the command again)", e),
26-
Error::ReadingLockfile(e) => format!("Could not read lockfile: \n {} \n (try removing it and running the command again)", e),
25+
Error::ParsingLockfile(e) => format!(
26+
"Could not parse lockfile: \n {} \n (try removing it and running the command again)",
27+
e
28+
),
29+
Error::ReadingLockfile(e) => format!(
30+
"Could not read lockfile: \n {} \n (try removing it and running the command again)",
31+
e
32+
),
2733
Error::WritingLockfile(e) => format!("Could not write lockfile: \n {}", e),
2834
};
2935
write!(f, "{}", msg)
@@ -54,15 +60,14 @@ fn create(lockfile_location: &Path, pid: u32) -> Lock {
5460
}
5561

5662
pub fn get(folder: &str) -> Lock {
57-
let location = format!("{}/lib/{}", folder, LOCKFILE);
58-
let path = Path::new(&location);
63+
let location = Path::new(folder).join("lib").join(LOCKFILE);
5964
let pid = process::id();
6065

6166
match fs::read_to_string(&location) {
62-
Err(e) if (e.kind() == std::io::ErrorKind::NotFound) => create(path, pid),
67+
Err(e) if (e.kind() == std::io::ErrorKind::NotFound) => create(&location, pid),
6368
Err(e) => Lock::Error(Error::ReadingLockfile(e)),
6469
Ok(s) => match s.parse::<u32>() {
65-
Ok(parsed_pid) if !exists(parsed_pid) => create(path, pid),
70+
Ok(parsed_pid) if !exists(parsed_pid) => create(&location, pid),
6671
Ok(parsed_pid) => Lock::Error(Error::Locked(parsed_pid)),
6772
Err(e) => Lock::Error(Error::ParsingLockfile(e)),
6873
},

0 commit comments

Comments
 (0)