Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self-hosted cargo-cp-artifact #971

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 164 additions & 10 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
members = [
"crates/*",
"test/*",
"pkgs/cargo-cp-artifact",
]
13 changes: 13 additions & 0 deletions crates/cargo-cp-artifact/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "cargo-cp-artifact"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[[bin]]
name = "cargo-cp-artifact"
path = "src/bin.rs"

[dependencies]
cargo_metadata = "0.15.3"
96 changes: 96 additions & 0 deletions crates/cargo-cp-artifact/src/artifact.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use std::ffi::OsStr;
use std::fs::{create_dir_all, remove_file};
use std::io::ErrorKind;
use std::path::Path;

#[derive(Debug, PartialEq, Eq, Hash)]
pub enum ArtifactKind {
Bin,
CDylib,
Dylib,
}

impl ArtifactKind {
pub fn parse(str: &impl AsRef<str>) -> Option<Self> {
match str.as_ref() {
"bin" => Some(ArtifactKind::Bin),
"cdylib" => Some(ArtifactKind::CDylib),
"dylib" => Some(ArtifactKind::Dylib),
_ => None,
}
}
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub struct Artifact {
pub kind: ArtifactKind,
pub crate_name: String,
}

pub enum ArtifactError {
MkdirFailed(std::io::Error),
OverwriteFailed(std::io::Error),
CopyFailed(std::io::Error),
}

impl std::fmt::Display for ArtifactError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ArtifactError::MkdirFailed(err) => {
write!(f, "Could not create directory: {}", err)
}
ArtifactError::OverwriteFailed(err) => {
write!(f, "Could not delete .node file: {}", err)
}
ArtifactError::CopyFailed(err) => {
write!(f, "Could not copy artifact: {}", err)
}
}
}
}

fn is_newer(from: &Path, to: &Path) -> bool {
if let (Ok(from_meta), Ok(to_meta)) = (from.metadata(), to.metadata()) {
if let (Ok(from_mtime), Ok(to_mtime)) = (from_meta.modified(), to_meta.modified()) {
return from_mtime > to_mtime;
}
}

return true;
}

impl Artifact {
pub fn copy(&self, from: &Path, to: &Path) -> Result<(), ArtifactError> {
if !is_newer(from, to) {
return Ok(());
}

if let Some(basename) = to.parent() {
create_dir_all(basename).map_err(ArtifactError::MkdirFailed)?;
}

// Apple Silicon (M1, etc.) requires shared libraries to be signed. However,
// the macOS code signing cache isn't cleared when overwriting a file.
// Deleting the file before copying works around the issue.
//
// Unfortunately, this workaround is incomplete because the file must be
// deleted from the location it is loaded. If further steps in the user's
// build process copy or move the file in place, the code signing cache
// will not be cleared.
//
// https://github.com/neon-bindings/neon/issues/911
if to.extension() == Some(OsStr::new("node")) {
if let Err(err) = remove_file(to) {
match err.kind() {
ErrorKind::NotFound => {}
_ => {
return Err(ArtifactError::OverwriteFailed(err));
}
}
}
}

std::fs::copy(from, to).map_err(ArtifactError::CopyFailed)?;
Ok(())
}
}
Loading