-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(imports): implement
imports
from local paths
- Loading branch information
Showing
22 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# `path` | ||
|
||
A `path` import allows using a local directory as import source. | ||
|
||
If `path` is not absolute, it is considered relative to the project root. | ||
|
||
Optionally, `path` can be symlinked into the `imports` folder inside the laze | ||
build directory by setting `symlink: true` in the import. | ||
The symlink name within `$build_dir/imports` defaults to the last path component | ||
of `path`. This can be changed by setting `name`. | ||
Using a symlink helps turning absolute pathnames into relative ones. This might | ||
be desirable for privacy reasons, or help with reproducible builds. | ||
|
||
Example: | ||
|
||
```yaml | ||
imports: | ||
- path: /path/to/local/folder | ||
- path: /path/to/another/local/folder | ||
symlink: true | ||
- path: /path/to/a/third/local/folder | ||
symlink: true | ||
name: folder3 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ imports: | |
|
||
- [`git`](./import/git.md) | ||
- [`laze`](./import/laze.md) | ||
- [`path`](./import/path.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use anyhow::{Context, Error}; | ||
use camino::{Utf8Path, Utf8PathBuf}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::serde_bool_helpers::default_as_false; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Hash)] | ||
pub struct Local { | ||
name: Option<String>, | ||
path: Utf8PathBuf, | ||
dldir: Option<String>, | ||
#[serde(default = "default_as_false")] | ||
symlink: bool, | ||
} | ||
|
||
impl super::Import for Local { | ||
fn get_path<T: AsRef<Utf8Path>>(&self, build_dir: T) -> Result<Utf8PathBuf, Error> { | ||
let mut res = Utf8PathBuf::from(build_dir.as_ref()); | ||
res.push("imports"); | ||
if let Some(dldir) = self.get_dldir() { | ||
res.push(dldir); | ||
} else if let Some(name) = self.get_name() { | ||
res.push(name); | ||
} else { | ||
res.push(self.path.file_name().unwrap()); | ||
} | ||
Ok(res) | ||
} | ||
|
||
fn get_name(&self) -> Option<String> { | ||
self.name.clone() | ||
} | ||
|
||
fn get_dldir(&self) -> Option<&String> { | ||
self.dldir.as_ref() | ||
} | ||
|
||
fn handle<T: AsRef<camino::Utf8Path>>( | ||
&self, | ||
build_dir: T, | ||
) -> Result<camino::Utf8PathBuf, anyhow::Error> { | ||
if self.symlink { | ||
let path = self.get_path(&build_dir)?; | ||
|
||
let path_parent = path.parent().unwrap(); | ||
std::fs::create_dir_all(path_parent).with_context(|| format!("creating {path}"))?; | ||
|
||
let link_target = if self.path.is_relative() { | ||
let relpath = pathdiff::diff_utf8_paths(&self.path, path_parent).unwrap(); | ||
relpath | ||
} else { | ||
self.path.clone() | ||
}; | ||
|
||
let mut link_is_missing = true; | ||
if path.is_symlink() { | ||
if path.read_link().unwrap() == link_target { | ||
link_is_missing = false; | ||
} else { | ||
std::fs::remove_file(&path).with_context(|| format!("removing {path}"))?; | ||
} | ||
} else if path.exists() { | ||
return Err(anyhow!("import target {path} exists and is not empty!")); | ||
} | ||
|
||
if link_is_missing { | ||
// TODO: windows support | ||
std::os::unix::fs::symlink(&link_target, &path) | ||
.with_context(|| format!("creating symlink {link_target}")) | ||
.with_context(|| format!("importing path {}", self.path))?; | ||
} | ||
// using `path` here as that is the path relative to the project root. | ||
super::get_lazefile(&path) | ||
} else { | ||
super::get_lazefile(&self.path) | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/tests/45_import_from_local_path/build_expected/build-global.ninja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
builddir = build | ||
build ALWAYS: phony | ||
rule CC_7950216292150676175 | ||
command = cat ${in} > ${out} | ||
description = CC | ||
|
||
build build/objects/testpath/foo.8352856137977294963.o: $ | ||
CC_7950216292150676175 $ | ||
testpath/foo.c | ||
|
||
rule LINK_5506617845631750009 | ||
command = cat ${in} > ${out} | ||
description = LINK | ||
|
||
build build/single_builder/imported_app/imported_app.elf: $ | ||
LINK_5506617845631750009 $ | ||
build/objects/testpath/foo.8352856137977294963.o | ||
|
||
build build/objects/build/imports/testpath2/foo.8352856137977294963.o: $ | ||
CC_7950216292150676175 $ | ||
build/imports/testpath2/foo.c | ||
|
||
build build/single_builder/imported_app2/imported_app2.elf: $ | ||
LINK_5506617845631750009 $ | ||
build/objects/build/imports/testpath2/foo.8352856137977294963.o | ||
|
||
build build/objects/build/imports/testpath3_renamed/foo.8352856137977294963.o: $ | ||
CC_7950216292150676175 $ | ||
build/imports/testpath3_renamed/foo.c | ||
|
||
build build/single_builder/imported_app3/imported_app3.elf: $ | ||
LINK_5506617845631750009 $ | ||
build/objects/build/imports/testpath3_renamed/foo.8352856137977294963.o | ||
|
1 change: 1 addition & 0 deletions
1
src/tests/45_import_from_local_path/build_expected/imports/testpath2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../testpath2 |
1 change: 1 addition & 0 deletions
1
src/tests/45_import_from_local_path/build_expected/imports/testpath3_renamed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../testpath3 |
1 change: 1 addition & 0 deletions
1
..._from_local_path/build_expected/objects/build/imports/testpath2/foo.8352856137977294963.o
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
1 change: 1 addition & 0 deletions
1
...cal_path/build_expected/objects/build/imports/testpath3_renamed/foo.8352856137977294963.o
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
1 change: 1 addition & 0 deletions
1
...tests/45_import_from_local_path/build_expected/objects/testpath/foo.8352856137977294963.o
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
1 change: 1 addition & 0 deletions
1
...sts/45_import_from_local_path/build_expected/single_builder/imported_app/imported_app.elf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
1 change: 1 addition & 0 deletions
1
...s/45_import_from_local_path/build_expected/single_builder/imported_app2/imported_app2.elf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
1 change: 1 addition & 0 deletions
1
...s/45_import_from_local_path/build_expected/single_builder/imported_app3/imported_app3.elf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
builders: | ||
- name: single_builder | ||
rules: | ||
- name: CC | ||
in: 'c' | ||
out: 'o' | ||
cmd: 'cat ${in} > ${out}' | ||
- name: LINK | ||
in: 'o' | ||
cmd: 'cat ${in} > ${out}' | ||
|
||
env: | ||
bindir: build/${builder}/${app} | ||
|
||
imports: | ||
# test a relative import | ||
- path: testpath | ||
# test a relative import, symlinked | ||
- path: testpath2 | ||
symlink: true | ||
# test a relative import, symlinked, renamed | ||
- path: testpath3 | ||
symlink: true | ||
name: testpath3_renamed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/sh | ||
|
||
. ../test-common.sh | ||
|
||
cleanup | ||
|
||
${LAZE} build -g | ||
|
||
clean_temp_files | ||
|
||
diff -r build build_expected | ||
|
||
echo TEST_OK | ||
|
||
cleanup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
apps: | ||
- name: imported_app | ||
sources: | ||
- foo.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
apps: | ||
- name: imported_app2 | ||
sources: | ||
- foo.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
apps: | ||
- name: imported_app3 | ||
sources: | ||
- foo.c |