Skip to content

Commit 654512f

Browse files
committed
file times based build
1 parent 47dcd0e commit 654512f

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/dfx/src/lib/builders/motoko.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::lib::metadata::names::{CANDID_ARGS, CANDID_SERVICE};
99
use crate::lib::models::canister::CanisterPool;
1010
use crate::lib::package_arguments::{self, PackageArguments};
1111
use crate::util::assets::management_idl;
12+
use crate::lib::builders::bail;
1213
use anyhow::{anyhow, Context};
1314
use candid::Principal as CanisterId;
1415
use dfx_core::config::cache::Cache;
@@ -154,6 +155,18 @@ impl CanisterBuilder for MotokoBuilder {
154155

155156
let package_arguments =
156157
package_arguments::load(cache.as_ref(), motoko_info.get_packtool())?;
158+
let mut package_arguments_map = BTreeMap::<String, String>::new(); // TODO: Can we deal without cloning strings?
159+
{ // block
160+
let mut i = 0;
161+
while i + 3 <= package_arguments.len() {
162+
if package_arguments[i] == "--package" {
163+
package_arguments_map.insert(package_arguments[i+1].clone(), package_arguments[i+2].clone());
164+
i += 3;
165+
} else {
166+
i += 1;
167+
}
168+
};
169+
}
157170

158171
let wasm_file_metadata = metadata(output_wasm_path)?;
159172
let wasm_file_time = wasm_file_metadata.modified()?;
@@ -189,8 +202,28 @@ impl CanisterBuilder for MotokoBuilder {
189202
}
190203
}
191204
MotokoImport::Lib(path) => {
192-
// FIXME: `lib` in name
193-
Some(Path::new(path).to_owned())
205+
let i = path.find('/');
206+
let pre_path = if let Some(i) = i {
207+
let expanded = Path::new(
208+
package_arguments_map.get(&path[..i]).ok_or_else(|| anyhow!("nonexisting package"))?
209+
).join(Path::new("src"));
210+
expanded.join(&path[i+1..])
211+
} else {
212+
Path::new(path).to_owned()
213+
};
214+
let path2 = pre_path.to_string_lossy() + ".mo"; // TODO: Is `lossy` OK?
215+
let path2 = path2.to_string();
216+
let path2 = Path::new(&path2);
217+
if path2.exists() { // TODO: Is it correct order of two variants?
218+
Some(Path::new(path2).to_owned())
219+
} else {
220+
let path3 = pre_path.join(Path::new("lib.mo"));
221+
if path3.exists() {
222+
Some(path3.to_owned())
223+
} else {
224+
bail!("source file has been deleted");
225+
}
226+
}
194227
}
195228
MotokoImport::Relative(path) => {
196229
Some(Path::new(path).to_owned())
@@ -204,7 +237,7 @@ impl CanisterBuilder for MotokoBuilder {
204237
};
205238
};
206239
} else {
207-
return Err(anyhow!("already compiled")); // TODO: Ensure that `dfx` command doesn't return false because of this.
240+
bail!("already compiled"); // FIXME: Ensure that `dfx` command doesn't return false because of this.
208241
}
209242
}
210243

0 commit comments

Comments
 (0)