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

generalize compile_project helper, add automatic target dir separation per contract #401

Open
dj8yfo opened this issue Jan 21, 2025 · 2 comments

Comments

@dj8yfo
Copy link
Collaborator

dj8yfo commented Jan 21, 2025

to automate target dir separation per contract in the spirit of
near/cargo-near#292, one might replace

pub async fn compile_project(project_path: &str) -> crate::Result<Vec<u8>> {
let project_path = std::fs::canonicalize(project_path).map_err(|e| match e.kind() {
std::io::ErrorKind::NotFound => ErrorKind::Io.message(format!(
"Incorrect file supplied to compile_project('{}')",
project_path
)),
_ => ErrorKind::Io.custom(e),
})?;
// `no_abi` has become flipped true -> false
let cargo_opts = cargo_near_build::BuildOpts {
no_locked: true,
manifest_path: Some(
cargo_near_build::camino::Utf8PathBuf::from_path_buf(project_path.join("Cargo.toml"))
.map_err(|error_path| {
ErrorKind::Io.custom(format!(
"Unable to construct UTF-8 path from: {}",
error_path.display()
))
})?,
),
..Default::default()
};
let compile_artifact =
cargo_near_build::build(cargo_opts).map_err(|e| ErrorKind::Io.custom(e))?;
let file = compile_artifact
.path
.canonicalize()
.map_err(|e| ErrorKind::Io.custom(e))?;
tokio::fs::read(file)
.await
.map_err(|e| ErrorKind::Io.custom(e))
}
with following

pub async fn compile_project(project_path: &str) -> Result<Vec<u8>, color_eyre::Report> {
    let project_path = std::fs::canonicalize(project_path).map_err(|e| match e.kind() {
        std::io::ErrorKind::NotFound => color_eyre::eyre::eyre!(
            "Incorrect file supplied to compile_project('{}')",
            project_path
        ),
        _ => color_eyre::eyre::eyre!("{:#?}", e),
    })?;

    let manifest_path =
        cargo_near_build::camino::Utf8PathBuf::from_path_buf(project_path.join("Cargo.toml"))
            .map_err(|error_path| {
            color_eyre::eyre::eyre!(
                "Unable to construct UTF-8 path from: {}",
                error_path.display()
            )
        })?;

    let build_opts = cargo_near_build::BuildOpts::builder()
        .no_locked(true)
        .manifest_path(manifest_path)
        .build();

    compile_project_with_opts(build_opts).await
}

pub async fn compile_project_with_opts(
    mut build_opts: cargo_near_build::BuildOpts,
) -> Result<Vec<u8>, color_eyre::Report> {
    if build_opts.override_cargo_target_dir.is_none() {

        // should run `cargo metadata --frozen --no-deps` when `no_locked` is false
        // should run `cargo metadata --offline --no-deps` when `no_locked` is true
        let (contract_crate_name, target_dir) = cargo_near_build::get_min_metadata(
            build_opts.manifest_path, build_opts.no_locked
        );

        let path_buf = {
            let pwd = Path::new(target_dir).canonicalize()?;
            let sub_target = pwd.join(format!("test-target-for-{contract_crate_name}"));
            sub_target
                .to_str()
                .ok_or_eyre(color_eyre::eyre::eyre!("non valid utf-8 path"))?
                .to_string()
        };

        build_opts.override_cargo_target_dir = Some(path_buf);
    }

    let compile_result =
        tokio::task::spawn_blocking(move || cargo_near_build::build(build_opts)).await?;
    let compile_artifact = compile_result?;

    let file = compile_artifact
        .path
        .canonicalize()
        .map_err(|e| color_eyre::eyre::eyre!("{:#?}", e))?;
    tokio::fs::read(file).await.map_err(|e| color_eyre::eyre::eyre!("{:#?}", e))
}

without losing much flexibility

@dj8yfo
Copy link
Collaborator Author

dj8yfo commented Jan 21, 2025

this needs adding cargo_near_build::get_min_metadata export to cargo_near_build

@dj8yfo
Copy link
Collaborator Author

dj8yfo commented Jan 21, 2025

color_eyre was added to snippet just to check if it mostly compiles, near_workspaces::Result should be used as it is now as return value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: NEW❗
Development

No branches or pull requests

1 participant