Skip to content

Commit

Permalink
feat: import local packages
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Jan 22, 2024
1 parent 5dcc688 commit c6f84f0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
15 changes: 13 additions & 2 deletions crates/els/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,19 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
return cfg;
};
let version = version.token.content.replace('\"', "");
let package =
erg_common::config::Package::new(name.leak(), as_name.leak(), version.leak());
let path = rec.get("path").and_then(|path| {
if let Some(ast::Expr::Literal(lit)) = path.body.block.first() {
Some(lit.token.content.replace('\"', ""))
} else {
None
}
});
let package = erg_common::config::Package::new(
name.leak(),
as_name.leak(),
version.leak(),
path.map(|p| &*p.leak()),
);
cfg.packages.push(package);
}
cfg
Expand Down
34 changes: 33 additions & 1 deletion crates/erg_common/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,21 @@ pub struct Package {
pub name: &'static str,
pub as_name: &'static str,
pub version: &'static str,
pub path: Option<&'static str>,
}

impl Package {
pub const fn new(name: &'static str, as_name: &'static str, version: &'static str) -> Self {
pub const fn new(
name: &'static str,
as_name: &'static str,
version: &'static str,
path: Option<&'static str>,
) -> Self {
Self {
name,
as_name,
version,
path,
}
}
}
Expand Down Expand Up @@ -303,6 +310,31 @@ impl ErgConfig {
Box::leak(name),
Box::leak(as_name),
Box::leak(version),
None,
));
}
"--use-local-package" => {
let name = args
.next()
.expect("`name` of `--use-package` is not passed")
.into_boxed_str();
let as_name = args
.next()
.expect("`as_name` of `--use-package` is not passed")
.into_boxed_str();
let version = args
.next()
.expect("`version` of `--use-package` is not passed")
.into_boxed_str();
let path = args
.next()
.expect("`path` of `--use-package` is not passed")
.into_boxed_str();
cfg.packages.push(Package::new(
Box::leak(name),
Box::leak(as_name),
Box::leak(version),
Some(Box::leak(path)),
));
}
"--ping" => {
Expand Down
12 changes: 6 additions & 6 deletions crates/erg_common/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,12 @@ impl Input {
fn resolve_project_dep_path(&self, path: &Path, cfg: &ErgConfig) -> Option<PathBuf> {
let name = format!("{}", path.display());
let pkg = cfg.packages.iter().find(|p| p.as_name == name)?;
let path = erg_pkgs_path()
.join(pkg.name)
.join(pkg.version)
.join("src")
.join("lib.er");
Some(path)
let path = if let Some(path) = pkg.path {
PathBuf::from(path).canonicalize().ok()?
} else {
erg_pkgs_path().join(pkg.name).join(pkg.version)
};
Some(path.join("src").join("lib.er"))
}

/// resolution order:
Expand Down

0 comments on commit c6f84f0

Please sign in to comment.