Skip to content

Commit

Permalink
feat(source-scan): precise docker build wasm input/destination (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yfo authored Apr 24, 2024
1 parent 7c51acd commit 7585514
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 56 deletions.
2 changes: 1 addition & 1 deletion cargo-near/src/commands/abi_command/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub(crate) fn write_to_file(

let out_path_abi = crate_metadata.target_directory.join(format!(
"{}_abi.{}",
crate_metadata.root_package.name.replace('-', "_"),
crate_metadata.formatted_package_name(),
abi_file_extension(format, compression)
));
fs::write(&out_path_abi, near_abi_compressed)?;
Expand Down
8 changes: 6 additions & 2 deletions cargo-near/src/commands/build_command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::types::{manifest::CargoManifestPath, metadata::CrateMetadata};
use crate::util;
use crate::{commands::abi_command::abi, util::wasm32_target_libdir_exists};

use super::ArtifactMessages;
use super::{ArtifactMessages, INSIDE_DOCKER_ENV_KEY};

const COMPILATION_TARGET: &str = "wasm32-unknown-unknown";

Expand Down Expand Up @@ -105,7 +105,11 @@ pub(super) fn run(
wasm_artifact.path = util::copy(&wasm_artifact.path, &out_dir)?;

// todo! if we embedded, check that the binary exports the __contract_abi symbol
util::print_success("Contract successfully built!");

util::print_success(&format!(
"Contract successfully built! (in CARGO_NEAR_BUILD_ENVIRONMENT={})",
std::env::var(INSIDE_DOCKER_ENV_KEY).unwrap_or("host".into())
));
let mut messages = ArtifactMessages::default();
messages.push_binary(&wasm_artifact);
if let Some(mut abi) = abi {
Expand Down
79 changes: 30 additions & 49 deletions cargo-near/src/commands/build_command/docker/cloned_repo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ffi::OsStr;

use crate::{
commands::build_command::{ArtifactMessages, BuildCommand},
types::{
Expand All @@ -9,7 +7,7 @@ use crate::{
util::{self, CompilationArtifact},
};
use camino::Utf8PathBuf;
use color_eyre::{eyre::WrapErr, owo_colors::OwoColorize};
use colored::Colorize;

pub(super) struct ClonedRepo {
pub tmp_repo: git2::Repository,
Expand Down Expand Up @@ -67,12 +65,13 @@ impl ClonedRepo {

let destination_dir = destination_crate_metadata.resolve_output_dir(cli_override)?;

search_and_copy(tmp_out_dir, destination_dir)
copy(tmp_out_dir, self.tmp_crate_metadata, destination_dir)
}
}

fn search_and_copy(
fn copy(
tmp_out_dir: Utf8PathBuf,
tmp_crate_metadata: CrateMetadata,
mut destination_dir: Utf8PathBuf,
) -> color_eyre::eyre::Result<CompilationArtifact> {
println!(
Expand All @@ -81,52 +80,34 @@ fn search_and_copy(
tmp_out_dir
);

let dir = tmp_out_dir
.read_dir()
.wrap_err_with(|| format!("No artifacts directory found: `{:?}`.", tmp_out_dir))?;
let filename = format!("{}.wasm", tmp_crate_metadata.formatted_package_name());

for entry in dir.flatten() {
if entry
.path()
.extension()
.unwrap_or(OsStr::new("not_wasm"))
.to_str()
.unwrap_or("not_wasm")
== "wasm"
{
let out_wasm_path = {
let filename = entry
.path()
.file_name()
.unwrap()
.to_string_lossy()
.into_owned();
destination_dir.push(filename);
destination_dir
};
if out_wasm_path.exists() {
println!(" {}", "removing previous artifact".cyan());
std::fs::remove_file(&out_wasm_path)?;
}
std::fs::copy::<std::path::PathBuf, camino::Utf8PathBuf>(
entry.path(),
out_wasm_path.clone(),
)?;
let result = CompilationArtifact {
path: out_wasm_path,
fresh: true,
from_docker: true,
};
let mut messages = ArtifactMessages::default();
messages.push_binary(&result);
messages.pretty_print();
let in_wasm_path = tmp_out_dir.join(filename.clone());

return Ok(result);
}
if !in_wasm_path.exists() {
return Err(color_eyre::eyre::eyre!(
"Temporary build site result wasm file not found: `{:?}`.",
in_wasm_path
));
}

Err(color_eyre::eyre::eyre!(
"Wasm file not found in directory: `{:?}`.",
tmp_out_dir
))
let out_wasm_path = {
destination_dir.push(filename);
destination_dir
};
if out_wasm_path.exists() {
println!(" {}", "removing previous artifact".cyan());
std::fs::remove_file(&out_wasm_path)?;
}
std::fs::copy::<camino::Utf8PathBuf, camino::Utf8PathBuf>(in_wasm_path, out_wasm_path.clone())?;
let result = CompilationArtifact {
path: out_wasm_path,
fresh: true,
from_docker: true,
};
let mut messages = ArtifactMessages::default();
messages.push_binary(&result);
messages.pretty_print();

Ok(result)
}
14 changes: 10 additions & 4 deletions cargo-near/src/types/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@ impl CrateMetadata {
&self,
cli_override: Option<crate::types::utf8_path_buf::Utf8PathBuf>,
) -> color_eyre::eyre::Result<Utf8PathBuf> {
if let Some(cli_override) = cli_override {
let result = if let Some(cli_override) = cli_override {
let out_dir = Utf8PathBuf::from(cli_override);
return util::force_canonicalize_dir(&out_dir);
}
util::force_canonicalize_dir(&out_dir)?
} else {
self.target_directory.clone()
};
log::debug!("resolved output directory: {}", result);
Ok(result)
}

Ok(self.target_directory.clone())
pub fn formatted_package_name(&self) -> String {
self.root_package.name.replace('-', "_")
}
}

Expand Down

0 comments on commit 7585514

Please sign in to comment.