Skip to content

Commit

Permalink
fix(volo-cli): modify the local init service path relative to the yml (
Browse files Browse the repository at this point in the history
…#404)

fix(volo-cli): modify the local init service path relative to the yml when the given path is relative after initialization
  • Loading branch information
Ggiggle committed Apr 7, 2024
1 parent f6f5c36 commit a0e28dd
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion volo-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "volo-build"
version = "0.10.1"
version = "0.10.2"
edition.workspace = true
homepage.workspace = true
repository.workspace = true
Expand Down
49 changes: 29 additions & 20 deletions volo-build/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,34 +323,43 @@ pub fn strip_slash_prefix(p: &Path) -> PathBuf {
}

pub fn init_local_service(idl: impl AsRef<Path>, includes: &[PathBuf]) -> anyhow::Result<Service> {
let includes = includes
.iter()
.map(|i| {
if i.is_absolute() {
i.clone()
} else {
PathBuf::new().join("../").join(i.clone())
}
})
.collect();
let path = if idl.as_ref().is_absolute() {
idl.as_ref().to_path_buf()
} else {
PathBuf::new().join("../").join(idl.as_ref())
};
let local_idl = Idl {
let raw_idl = Idl {
source: Source::Local,
path,
includes,
path: PathBuf::new().join(idl.as_ref()),
includes: includes.to_vec(),
};
// only ensure readable when idl is from local
local_idl.ensure_readable()?;
raw_idl.ensure_readable()?;

Ok(Service {
idl: local_idl,
idl: raw_idl,
codegen_option: Default::default(),
})
}

// the yml file will move into the volo-gen directory generated at the init cmd execution path,
// so the path of the idl file and includes should be modified to relative path by add '../' if the
// raw is relative
pub fn modify_local_init_service_path_relative_to_yml(service: &mut Service) {
if let Source::Local = service.idl.source {
if service.idl.path.is_relative() {
service.idl.path = PathBuf::new().join("../").join(service.idl.path.clone());
}
service.idl.includes = service
.idl
.includes
.iter()
.map(|i| {
if i.is_relative() {
PathBuf::new().join("../").join(i.clone())
} else {
i.clone()
}
})
.collect();
}
}

pub fn init_git_repo(
repo: &Option<String>,
git: &str,
Expand Down
2 changes: 1 addition & 1 deletion volo-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "volo-cli"
version = "0.10.1"
version = "0.10.2"
edition.workspace = true
homepage.workspace = true
repository.workspace = true
Expand Down
11 changes: 7 additions & 4 deletions volo-cli/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use volo_build::{
model::{Entry, DEFAULT_FILENAME},
util::{
create_git_service, detect_protocol, git_repo_init, init_git_repo, init_local_service,
DEFAULT_CONFIG_FILE,
modify_local_init_service_path_relative_to_yml, DEFAULT_CONFIG_FILE,
},
};

Expand Down Expand Up @@ -185,19 +185,19 @@ impl CliCommand for Init {

volo_build::util::with_config(|config| {
let mut repos = HashMap::new();
let service = if let Some(git) = self.git.as_ref() {
let mut service = if let Some(git) = self.git.as_ref() {
let (repo_name, repo) = init_git_repo(&self.repo, git, &self.r#ref)?;
repos.insert(repo_name.clone(), repo);
create_git_service(&repo_name, self.idl.as_path(), &self.includes)
} else {
init_local_service(self.idl.as_path(), &self.includes)?
};

let entry = Entry {
let mut entry = Entry {
filename: PathBuf::from(DEFAULT_FILENAME),
protocol: detect_protocol(service.idl.path.as_path()),
repos,
services: vec![service],
services: vec![service.clone()],
common_option: Default::default(),
};

Expand All @@ -207,6 +207,9 @@ impl CliCommand for Init {
self.copy_thrift_template(entry.clone())?;
}

modify_local_init_service_path_relative_to_yml(&mut service);
entry.services = vec![service];

config.entries.insert(cx.entry_name.clone(), entry);

Ok(())
Expand Down

0 comments on commit a0e28dd

Please sign in to comment.