Skip to content

Commit

Permalink
Merge pull request #139 from dalance/change_dep_format
Browse files Browse the repository at this point in the history
Change dependencies format
  • Loading branch information
dalance authored Feb 21, 2023
2 parents 49fb060 + ff002c3 commit f931fca
Show file tree
Hide file tree
Showing 27 changed files with 773 additions and 240 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Veryl.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This file is automatically @generated by Veryl.
# It is not intended for manual editing.
[[projects]]
name = "veryl_sample1"
uuid = "99a66c38-e44a-5f9d-923c-7081983db730"
version = "0.2.3"
url = "https://github.com/dalance/veryl_sample"
revision = "ec120584fc6bce41fe0ee9ad23fa2710e13c29a8"
dependencies = []

[[projects]]
name = "veryl_sample2"
uuid = "734c17b0-eee3-5161-bf22-54b3b421e881"
version = "0.3.1"
url = "https://github.com/dalance/veryl_sample"
revision = "913747181a20b0d0c6111c054ce661cfd91cbea0"
dependencies = []
6 changes: 4 additions & 2 deletions Veryl.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ target = {type = "directory", path = "testcases/sv"}
indent_width = 4

[dependencies]
veryl_sample1 = {git = "https://github.com/dalance/veryl_sample", version = "0.1.0"}
veryl_sample2 = {git = "https://github.com/dalance/veryl_sample", version = "0.2.0"}
"https://github.com/dalance/veryl_sample" = [
{version = "0.2.1", name = "veryl_sample1"},
{version = "0.3.0", name = "veryl_sample2"},
]
24 changes: 13 additions & 11 deletions book/src/05_development_environment/02_dependencies.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
# Dependencies

If you want to add other Veryl projects to dependencies of your project, you can add them to `[dependencies]` section in `Veryl.toml`.
The left hand side of entry is path to the dependency, and the right hand side is version.

```toml
[dependencies]
veryl_sample = {git = "https://github.com/dalance/veryl_sample", version = "0.1.0"}
"https://github.com/dalance/veryl_sample" = "0.1.0"
```

Each entry of `[dependencies]` has the key which is used as dependency namespace, and the value which specifies the path to the dependency.
In the above example,
By default, the namespace of the dependency is the same as the project name of the dependency.
If you want to specify namespace, you can use `name` field.

* `veryl_sample` -- the dependency's namespace
* `https://github.com/dalance/veryl_sample` -- the git URL to get the dependency
* `0.1.0` -- the version requirement of the dependency
```toml
[dependencies]
"https://github.com/dalance/veryl_sample" = {version = "0.1.0", name = "veryl_sample_alt"}
```

We recommend to use `version` if it is available because it can represent breaking changes between versions.
If not, you can use `rev`, `tag` and `branch` instead of `version`.
If you want to use many versions of the same dependency path, you can specify each name.

```toml
[dependencies]
veryl_sample1 = {git = "https://github.com/dalance/veryl_sample", rev = "9e9a30a"}
veryl_sample2 = {git = "https://github.com/dalance/veryl_sample", tag = "v0.4"}
veryl_sample3 = {git = "https://github.com/dalance/veryl_sample", branch = "branch"}
"https://github.com/dalance/veryl_sample" = [
{version = "0.1.0", name = "veryl_sample1"},
{version = "0.2.0", name = "veryl_sample2"},
]
```

## Usage of dependency
Expand Down
19 changes: 14 additions & 5 deletions crates/analyzer/src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,21 @@ pub struct Analyzer {
}

impl Analyzer {
pub fn new<T: AsRef<str>>(project_paths: &[T], metadata: &Metadata) -> Self {
let mut ids = Vec::new();
for path in project_paths {
ids.push(resource_table::insert_str(path.as_ref()));
}
pub fn new<T: AsRef<str>>(project_name: &T, metadata: &Metadata) -> Self {
let ids = vec![resource_table::insert_str(project_name.as_ref())];
namespace_table::set_default(&ids);
for locks in metadata.lockfile.lock_table.values() {
for lock in locks {
let prj = resource_table::insert_str(&lock.name);
for lock_dep in &lock.dependencies {
let from = resource_table::insert_str(&lock_dep.name);
let to = metadata.lockfile.lock_table.get(&lock_dep.url).unwrap();
let to = to.iter().find(|x| x.version == lock_dep.version).unwrap();
let to = resource_table::insert_str(&to.name);
symbol_table::add_project_local(prj, from, to);
}
}
}
Analyzer {
lint_opt: metadata.lint.clone(),
}
Expand Down
28 changes: 27 additions & 1 deletion crates/analyzer/src/symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ impl ResolveError {
#[derive(Clone, Default, Debug)]
pub struct SymbolTable {
table: HashMap<StrId, Vec<Symbol>>,
project_local_table: HashMap<StrId, HashMap<StrId, StrId>>,
}

impl SymbolTable {
Expand All @@ -186,6 +187,18 @@ impl SymbolTable {
let mut full_path = Vec::new();
let mut namespace = namespace.clone();
let mut inner = false;

let mut path = path.clone();

// replace project local name
let prj = namespace.paths[0];
let path_head = path.0[0];
if let Some(map) = self.project_local_table.get(&prj) {
if let Some(id) = map.get(&path_head) {
path.0[0] = *id;
}
}

for name in path.as_slice() {
let mut max_depth = 0;
ret = None;
Expand Down Expand Up @@ -307,6 +320,15 @@ impl SymbolTable {
}
}
}

pub fn add_project_local(&mut self, prj: StrId, from: StrId, to: StrId) {
self.project_local_table
.entry(prj)
.and_modify(|x| {
x.insert(from, to);
})
.or_insert(HashMap::from([(from, to)]));
}
}

impl fmt::Display for SymbolTable {
Expand Down Expand Up @@ -384,6 +406,10 @@ pub fn add_reference(target: TokenId, token: &Token) {
SYMBOL_TABLE.with(|f| f.borrow_mut().add_reference(target, token))
}

pub fn add_project_local(prj: StrId, from: StrId, to: StrId) {
SYMBOL_TABLE.with(|f| f.borrow_mut().add_project_local(prj, from, to))
}

#[cfg(test)]
mod tests {
use crate::namespace::Namespace;
Expand Down Expand Up @@ -432,7 +458,7 @@ mod tests {
fn parse() {
let metadata: Metadata = toml::from_str(&Metadata::create_default_toml("")).unwrap();
let parser = Parser::parse(&CODE, &"").unwrap();
let analyzer = Analyzer::new(&["prj"], &metadata);
let analyzer = Analyzer::new(&"prj", &metadata);
analyzer.analyze_pass1(&CODE, &"", &parser.veryl);
}

Expand Down
6 changes: 3 additions & 3 deletions crates/languageserver/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Backend {
symbol_table::drop(path);
namespace_table::drop(path);
}
let analyzer = Analyzer::new(&[""], metadata);
let analyzer = Analyzer::new(&"", metadata);
let mut errors = analyzer.analyze_pass1(&text, path, &x.veryl);
errors.append(&mut analyzer.analyze_pass2(&text, path, &x.veryl));
errors.append(&mut analyzer.analyze_pass3(&text, path, &x.veryl));
Expand Down Expand Up @@ -274,7 +274,7 @@ impl LanguageServer for Backend {
}

if let Ok(metadata_path) = Metadata::search_from(uri) {
if let Ok(metadata) = Metadata::load(metadata_path) {
if let Ok(mut metadata) = Metadata::load(metadata_path) {
self.metadata_map.insert(uri.to_string(), metadata.clone());
self.on_change(
TextDocumentItem {
Expand All @@ -286,7 +286,7 @@ impl LanguageServer for Backend {
)
.await;

if let Ok(paths) = metadata.paths::<&str>(&[], false) {
if let Ok(paths) = metadata.paths::<&str>(&[]) {
for path in &paths {
self.background_analyze(path, &metadata).await;
}
Expand Down
1 change: 1 addition & 0 deletions crates/metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ spdx = "0.10.0"
thiserror = {workspace = true}
toml = {workspace = true}
url = {workspace = true}
uuid = {version = "1.3", default-features = false, features = ["v5", "serde"]}
veryl-parser = {version = "0.4.0", path = "../parser"}
walkdir = "2.3.2"

Expand Down
9 changes: 5 additions & 4 deletions crates/metadata/src/git/command.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::metadata_error::MetadataError;
use log::info;
use log::debug;
use std::path::{Path, PathBuf};
use std::process::Command;
use thiserror::Error;
Expand Down Expand Up @@ -55,6 +55,7 @@ impl Git {
pub fn clone(url: &Url, path: &Path) -> Result<Self, MetadataError> {
let current_dir = path.parent().unwrap();
let target = path.file_name().unwrap();

if !path.exists() {
let output = Command::new(GIT_COMMAND)
.arg("clone")
Expand All @@ -67,7 +68,7 @@ impl Git {
let msg = format!("failed to clone repository: {}", url.as_str());
return Err(GitCommandError { msg, context }.into());
}
info!("Cloned repository ({})", url);
debug!("Cloned repository ({})", url);
}

Ok(Git {
Expand All @@ -89,7 +90,7 @@ impl Git {
return Err(GitCommandError { msg, context }.into());
}

info!("Fetched repository ({})", self.path.to_string_lossy());
debug!("Fetched repository ({})", self.path.to_string_lossy());

Ok(())
}
Expand All @@ -115,7 +116,7 @@ impl Git {
return Err(GitCommandError { msg, context }.into());
}

info!(
debug!(
"Checkouted repository ({} @ {})",
self.path.to_string_lossy(),
dst
Expand Down
8 changes: 4 additions & 4 deletions crates/metadata/src/git/gitoxide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use git_repository::remote::ref_map::Options;
use git_repository::remote::Direction;
use git_repository::worktree::index;
use git_repository::Repository;
use log::info;
use log::debug;
use std::path::Path;
use std::sync::atomic::AtomicBool;
use thiserror::Error;
Expand Down Expand Up @@ -130,7 +130,7 @@ impl Git {
let mut repo = git_repository::prepare_clone(git_url, path)?;
let (mut repo, _) = repo.fetch_then_checkout(Discard, &AtomicBool::new(false))?;
let (repo, _) = repo.main_worktree(Discard, &AtomicBool::new(false))?;
info!("Cloned repository ({})", url);
debug!("Cloned repository ({})", url);
repo
};

Expand All @@ -148,7 +148,7 @@ impl Git {
let prepare = connect.prepare_fetch(Options::default())?;
let _outcome = prepare.receive(&AtomicBool::new(false))?;

info!(
debug!(
"Fetched repository ({})",
self.repo.work_dir().unwrap().to_string_lossy()
);
Expand Down Expand Up @@ -187,7 +187,7 @@ impl Git {
Default::default(),
)?;

info!(
debug!(
"Checkouted repository ({})",
self.repo.work_dir().unwrap().to_string_lossy()
);
Expand Down
7 changes: 5 additions & 2 deletions crates/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ mod build;
mod format;
mod git;
mod lint;
mod lockfile;
mod metadata;
mod metadata_error;
mod project;
mod pubdata;
mod pubfile;
mod publish;
#[cfg(test)]
mod tests;
mod utils;
pub use build::{Build, BuiltinType, ClockType, FilelistType, ResetType, Target};
pub use format::Format;
pub use lint::{Case, Lint};
pub use lockfile::Lockfile;
pub use metadata::{BumpKind, Metadata, PathPair};
pub use metadata_error::MetadataError;
pub use project::Project;
pub use pubdata::{Pubdata, Release};
pub use pubfile::{Pubfile, Release};
pub use publish::Publish;
pub use semver;
Loading

0 comments on commit f931fca

Please sign in to comment.