Skip to content

Commit

Permalink
Use relative path for target dir
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniusnaumann committed Oct 15, 2023
1 parent 3a55698 commit 40a5acc
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub(crate) mod console {
mod bindings;
mod lib_type;
mod metadata;
mod path;
mod swiftpackage;
mod targets;
mod templating;
Expand Down
17 changes: 9 additions & 8 deletions src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::borrow::Cow;

use camino::Utf8Path;
use cargo_metadata::{Metadata, MetadataCommand, Package};
use lazy_static::lazy_static;

use crate::path::PathExt;

pub(crate) fn metadata() -> &'static Metadata {
lazy_static! {
static ref METADATA: Metadata = MetadataCommand::new()
Expand All @@ -16,21 +20,18 @@ pub(crate) fn metadata() -> &'static Metadata {
}

pub(crate) trait MetadataExt {
fn target_dir(&self) -> &Utf8Path;
fn target_dir(&self) -> Cow<Utf8Path>;
fn uniffi_crates(&self) -> Vec<&Package>;
}

impl MetadataExt for Metadata {
fn target_dir(&self) -> &Utf8Path {
fn target_dir(&self) -> Cow<Utf8Path> {
let target_dir = self.target_directory.as_path();
let relative = target_dir
// TODO: Error handling
.strip_prefix(std::env::current_dir().unwrap())
.ok();
let relative = target_dir.to_relative();

match relative {
Some(dir) => dir,
None => target_dir,
Ok(relative) => Cow::from(relative),
Err(_) => Cow::from(target_dir),
}
}

Expand Down
49 changes: 49 additions & 0 deletions src/path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::ops::Deref;

use camino::{FromPathBufError, Utf8Path, Utf8PathBuf};

use crate::Result;

pub(crate) trait PathExt {
fn to_relative(&self) -> Result<Utf8PathBuf>;
fn find_common_path(&self, other: &Utf8Path) -> Utf8PathBuf;
}

impl PathExt for Utf8Path {
fn to_relative(&self) -> Result<Utf8PathBuf> {
let cwd = std::env::current_dir()?;
let cwd: Utf8PathBuf = cwd.try_into().map_err(|e: FromPathBufError| {
format!(
"Current working directory is not a valid UTF-8 path: {}",
e.into_path_buf().to_string_lossy()
)
})?;
let common = self.find_common_path(&cwd);
let remaining = cwd.strip_prefix(common.deref()).unwrap();
let prefix = remaining
.components()
.map(|_| "..")
.collect::<Utf8PathBuf>();

let relative = prefix.join(self.strip_prefix(common).unwrap());

Ok(relative)
}

fn find_common_path(&self, other: &Utf8Path) -> Utf8PathBuf {
let mut self_components = self.components();
let mut other_components = other.components();
let mut common_path = Utf8PathBuf::new();
while let (Some(self_component), Some(other_component)) =
(self_components.next(), other_components.next())
{
if self_component == other_component {
common_path.push(self_component);
} else {
break;
}
}

common_path
}
}

0 comments on commit 40a5acc

Please sign in to comment.