Skip to content

Commit

Permalink
don't hardcode dependency path while building
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicBurkart committed Oct 14, 2020
1 parent 713366b commit 54161c4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
1 change: 0 additions & 1 deletion turbolift_internals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ async-std = "1.6"
async-trait = "0.1"
get_if_addrs = "0.5.3"
regex = "1"
pathdiff = "0.2.0"

# kubernetes-specific requirements
kube = "0.42.0"
Expand Down
30 changes: 13 additions & 17 deletions turbolift_internals/src/build_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ use std::path::{Path, PathBuf};
use std::process::Command;
use std::str::FromStr;

use pathdiff::diff_paths;

use crate::utils::symlink_dir;

pub fn edit_cargo_file(cargo_path: &Path, function_name: &str) -> anyhow::Result<()> {
pub fn edit_cargo_file(
original_project_source_dir: &Path,
cargo_path: &Path,
function_name: &str,
) -> anyhow::Result<()> {
let local_deps_dir_name = ".local_deps";
let mut parsed_toml: cargo_toml2::CargoToml = cargo_toml2::from_path(cargo_path)
.unwrap_or_else(|_| panic!("toml at {:?} could not be read", cargo_path));
let relative_local_deps_cache = cargo_path.parent().unwrap().join(".local_deps");
let relative_local_deps_cache = cargo_path.parent().unwrap().join(local_deps_dir_name);
fs::create_dir_all(&relative_local_deps_cache)?;
let local_deps_cache = relative_local_deps_cache.canonicalize()?;

Expand All @@ -26,21 +29,17 @@ pub fn edit_cargo_file(cargo_path: &Path, function_name: &str) -> anyhow::Result
let details = deps
.iter_mut()
// only full dependency descriptions (not simple version number)
.filter_map(|(_name, dep)| match dep {
.filter_map(|(name, dep)| match dep {
cargo_toml2::Dependency::Simple(_) => None,
cargo_toml2::Dependency::Full(detail) => Some(detail),
cargo_toml2::Dependency::Full(detail) => Some((name, detail)),
});
let mut completed_locations = HashSet::new();
for detail in details {
for (name, detail) in details {
// only descriptions with a path
if let Some(ref mut buf) = detail.path {
// determine what the symlink for this dependency should be
let canonical = buf.canonicalize()?;
let dep_location = local_deps_cache.join(
&canonical
.file_name()
.unwrap_or_else(|| canonical.as_os_str()),
);
let canonical = original_project_source_dir.join(&buf).canonicalize()?;
let dep_location = local_deps_cache.join(name);

// check that we don't have a naming error
// todo: automatically handle naming conflicts by mangling the dep for one
Expand All @@ -63,10 +62,7 @@ pub fn edit_cargo_file(cargo_path: &Path, function_name: &str) -> anyhow::Result
symlink_dir(&canonical, &dep_location)?;
}

let proj_folder = cargo_path.parent().unwrap().canonicalize().unwrap();
let rel_dep_location = diff_paths(&dep_location, &proj_folder).unwrap();
let relative_path = PathBuf::from_str(".")?.join(&rel_dep_location);
*buf = relative_path;
*buf = PathBuf::from_str(".")?.join(local_deps_dir_name).join(name);
}
}

Expand Down
5 changes: 5 additions & 0 deletions turbolift_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ pub fn on(distribution_platform_: TokenStream, function_: TokenStream) -> TokenS

// modify cargo.toml (edit package info & add actix + json_serde deps)
build_project::edit_cargo_file(
PathBuf::from_str(".")
.expect("could not find project dir")
.canonicalize()
.expect("could not canonicalize path to project dir")
.as_path(),
&function_cache_proj_path.join("Cargo.toml"),
&original_target_function_name,
)
Expand Down

0 comments on commit 54161c4

Please sign in to comment.