Skip to content

Commit

Permalink
Impl depdendencis partially #36
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Jan 23, 2023
1 parent 195958b commit 18b2080
Show file tree
Hide file tree
Showing 13 changed files with 1,440 additions and 38 deletions.
1,333 changes: 1,318 additions & 15 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Veryl.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ target = {type = "directory", path = "testcases/sv"}

[format]
indent_width = 4

[dependencies]
veryl_sample = {git = "https://github.com/dalance/veryl_sample"}
1 change: 1 addition & 0 deletions crates/metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ serde = {workspace = true}
spdx = "0.10.0"
thiserror = {workspace = true}
toml = "0.5.10"
url = {version = "2.3", features = ["serde"]}
veryl-parser = {version = "0.2.1", path = "../parser"}
12 changes: 12 additions & 0 deletions crates/metadata/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use regex::Regex;
use semver::Version;
use serde::{Deserialize, Serialize};
use spdx::Expression;
use std::collections::HashMap;
use std::env;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use url::Url;

#[derive(Debug, Serialize, Deserialize)]
pub struct Metadata {
Expand All @@ -14,6 +16,8 @@ pub struct Metadata {
pub build: Build,
#[serde(default)]
pub format: Format,
#[serde(default)]
pub dependencies: HashMap<String, Dependency>,
#[serde(skip)]
pub metadata_path: PathBuf,
}
Expand Down Expand Up @@ -152,6 +156,14 @@ fn default_indent_width() -> usize {
DEFAULT_INDENT_WIDTH
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Dependency {
pub git: Option<Url>,
pub rev: Option<String>,
pub tag: Option<String>,
pub branch: Option<String>,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions crates/veryl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ pre-release-replacements = [
[dependencies]
clap = {workspace = true}
console = "0.15.4"
directories = "4.0"
env_logger = "0.10.0"
git-repository = {version = "0.33.0", features = ["blocking-network-client", "blocking-http-transport-reqwest", "blocking-http-transport-reqwest-rust-tls"]}
miette = {workspace = true}
serde_json = {workspace = true}
similar = {version = "2.2.1", features = ["text", "inline"]}
Expand Down
6 changes: 1 addition & 5 deletions crates/veryl/src/cmd_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ impl CmdBuild {
}

pub fn exec(&self, metadata: &Metadata) -> Result<bool> {
let files = if self.opt.files.is_empty() {
utils::gather_files("./")?
} else {
self.opt.files.clone()
};
let files = utils::gather_files(&self.opt.files, metadata)?;

let now = Instant::now();

Expand Down
8 changes: 2 additions & 6 deletions crates/veryl/src/cmd_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ impl CmdCheck {
Self { opt }
}

pub fn exec(&self, _metadata: &Metadata) -> Result<bool> {
let files = if self.opt.files.is_empty() {
utils::gather_files("./")?
} else {
self.opt.files.clone()
};
pub fn exec(&self, metadata: &Metadata) -> Result<bool> {
let files = utils::gather_files(&self.opt.files, metadata)?;

let now = Instant::now();

Expand Down
8 changes: 2 additions & 6 deletions crates/veryl/src/cmd_dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ impl CmdDump {
Self { opt }
}

pub fn exec(&self, _metadata: &Metadata) -> Result<bool> {
let files = if self.opt.files.is_empty() {
utils::gather_files("./")?
} else {
self.opt.files.clone()
};
pub fn exec(&self, metadata: &Metadata) -> Result<bool> {
let files = utils::gather_files(&self.opt.files, metadata)?;

let now = Instant::now();

Expand Down
6 changes: 1 addition & 5 deletions crates/veryl/src/cmd_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ impl CmdFmt {
}

pub fn exec(&self, metadata: &Metadata) -> Result<bool> {
let files = if self.opt.files.is_empty() {
utils::gather_files("./")?
} else {
self.opt.files.clone()
};
let files = utils::gather_files(&self.opt.files, metadata)?;

let mut all_pass = true;
let now = Instant::now();
Expand Down
83 changes: 83 additions & 0 deletions crates/veryl/src/dependency_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use directories::ProjectDirs;
use git_repository::progress::Discard;
use git_repository::remote::ref_map::Options;
use git_repository::remote::Direction;
use miette::{IntoDiagnostic, Result};
use std::sync::atomic::AtomicBool;
use veryl_metadata::Metadata;

pub struct DependencyManager;

impl DependencyManager {
pub fn update(metadata: &Metadata) -> Result<()> {
let project_dir = ProjectDirs::from("", "dalance", "veryl").unwrap();
let cache_dir = project_dir.cache_dir();

//let path = metadata
// .metadata_path
// .parent()
// .unwrap()
// .join("dependencies");
//if path.exists() {
// std::fs::remove_dir_all(&path).into_diagnostic()?;
// std::fs::create_dir(&path).into_diagnostic()?;
//} else {
// std::fs::create_dir(&path).into_diagnostic()?;
//}

std::env::set_var("GIT_COMMITTER_NAME", "veryl");
std::env::set_var("GIT_COMMITTER_EMAIL", "veryl");

for (name, url) in &metadata.dependencies {
if let Some(ref url) = url.git {
let url: git_repository::Url = url.as_str().try_into().into_diagnostic()?;

let mut path = cache_dir.to_path_buf();
path.push("repository");
if let Some(host) = url.host() {
path.push(host);
}
path.push(url.path.to_string().trim_start_matches('/'));

let parent = path.parent().unwrap();
if !parent.exists() {
std::fs::create_dir_all(parent).into_diagnostic()?;
}

let repo = if path.exists() {
git_repository::open(&path).into_diagnostic()?
} else {
let mut repo = git_repository::prepare_clone(url, &path).into_diagnostic()?;
let (mut repo, _) = repo
.fetch_then_checkout(Discard, &AtomicBool::new(false))
.into_diagnostic()?;
let (repo, _) = repo
.main_worktree(Discard, &AtomicBool::new(false))
.into_diagnostic()?;
repo
};

let remote = repo
.find_default_remote(Direction::Fetch)
.unwrap()
.into_diagnostic()?;
let connect = remote
.connect(Direction::Fetch, Discard)
.into_diagnostic()?;
let prepare = connect
.prepare_fetch(Options::default())
.into_diagnostic()?;
let outcome = prepare.receive(&AtomicBool::new(false)).into_diagnostic()?;
dbg!(&outcome);

dbg!(repo
.head_commit()
.into_diagnostic()?
.short_id()
.into_diagnostic()?);
}
}

Ok(())
}
}
3 changes: 3 additions & 0 deletions crates/veryl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod cmd_fmt;
mod cmd_init;
mod cmd_metadata;
mod cmd_new;
mod dependency_manager;
mod utils;

// ---------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -185,6 +186,8 @@ fn main() -> Result<ExitCode> {
}
};

dependency_manager::DependencyManager::update(&metadata)?;

let ret = match opt.command {
Commands::New(x) => cmd_new::CmdNew::new(x).exec()?,
Commands::Init(x) => cmd_init::CmdInit::new(x).exec()?,
Expand Down
11 changes: 10 additions & 1 deletion crates/veryl/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
use miette::{IntoDiagnostic, Result};
use std::path::{Path, PathBuf};
use veryl_metadata::Metadata;
use walkdir::WalkDir;

pub fn gather_files<T: AsRef<Path>>(base_dir: T) -> Result<Vec<PathBuf>> {
pub fn gather_files(files: &[PathBuf], metadata: &Metadata) -> Result<Vec<PathBuf>> {
if files.is_empty() {
gather_vl_files(metadata.metadata_path.parent().unwrap())
} else {
Ok(files.to_vec())
}
}

fn gather_vl_files<T: AsRef<Path>>(base_dir: T) -> Result<Vec<PathBuf>> {
let mut ret = Vec::new();
for entry in WalkDir::new(base_dir) {
let entry = entry.into_diagnostic()?;
Expand Down
2 changes: 2 additions & 0 deletions crates/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use miette::{ErrReport, GraphicalReportHandler, GraphicalTheme, ThemeCharacters, ThemeStyles};
use semver::Version;
use std::collections::HashMap;
use veryl_emitter::Emitter;
use veryl_formatter::Formatter;
use veryl_metadata::{Build, Format, Metadata, Project};
Expand Down Expand Up @@ -54,6 +55,7 @@ fn metadata() -> Metadata {
},
build: Build::default(),
format: Format::default(),
dependencies: HashMap::new(),
metadata_path: "".into(),
}
}
Expand Down

0 comments on commit 18b2080

Please sign in to comment.