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

Aot macro compilation - magic script #1789

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 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
34 changes: 31 additions & 3 deletions scarb/src/ops/package.rs
wawel37 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use core::str;
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{Seek, SeekFrom, Write};
use std::path::Path;

use anyhow::{bail, ensure, Context, Result};
use anyhow::{anyhow, bail, ensure, Context, Result};
use camino::Utf8PathBuf;
use indoc::{formatdoc, indoc, writedoc};

Expand All @@ -18,7 +20,9 @@ use crate::compiler::plugin::proc_macro::compilation::{
};
use crate::core::publishing::manifest_normalization::prepare_manifest_for_publish;
use crate::core::publishing::source::list_source_files;
use crate::core::{Config, Package, PackageId, PackageName, Target, TargetKind, Workspace};
use crate::core::{
Config, Package, PackageId, PackageName, ScriptDefinition, Target, TargetKind, Workspace,
};
use crate::flock::{FileLockGuard, Filesystem};
use crate::internal::restricted_names;
use crate::{
Expand Down Expand Up @@ -168,12 +172,36 @@ fn package_one_impl(

let recipe = prepare_archive_recipe(pkg, opts, ws)?;
let num_files = recipe.len();
let target_dir = ws.target_dir().child("package");

if let Some(script_definition) = pkg.manifest.scripts.get("package") {
ws.config().ui().print(Status::new(
"Running package script with package",
&pkg_id.to_string(),
));
let script_path_string = script_definition.to_string();
let script_path = Path::new(&script_path_string);
if !script_path.exists() || !script_path.is_file() {
return Err(anyhow!("Package script must be a path to an existing file"));
};
wawel37 marked this conversation as resolved.
Show resolved Hide resolved
let absolute_path_script_definition = ScriptDefinition::new(
pkg.root()
.join(format!("{}", script_definition))
.to_string(),
);
ops::execute_script(
&absolute_path_script_definition,
&[],
ws,
target_dir.path_existent()?,
None,
)?;
}

// Package up and test a temporary tarball and only move it to the final location if it actually
// passes all verification checks. Any previously existing tarball can be assumed as corrupt
// or invalid, so we can overwrite it if it exists.
let filename = pkg_id.tarball_name();
let target_dir = ws.target_dir().child("package");

let mut dst =
target_dir.create_rw(format!(".{filename}"), "package scratch space", ws.config())?;
Expand Down
67 changes: 66 additions & 1 deletion scarb/tests/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use scarb::DEFAULT_TARGET_DIR_NAME;
use scarb_build_metadata::CAIRO_VERSION;
use scarb_test_support::cairo_plugin_project_builder::CairoPluginProjectBuilder;
use scarb_test_support::command::Scarb;
use scarb_test_support::fsx::unix_paths_to_os_lossy;
use scarb_test_support::fsx::{make_executable, unix_paths_to_os_lossy};
use scarb_test_support::gitx;
use scarb_test_support::project_builder::{Dep, DepBuilder, ProjectBuilder};
use scarb_test_support::registry::local::LocalRegistry;
Expand Down Expand Up @@ -1525,3 +1525,68 @@ fn package_with_publish_disabled() {
[..]Packaged [..] files, [..] ([..] compressed)
"#});
}

#[test]
fn package_with_package_script() {
let t = TempDir::new().unwrap();

#[cfg(not(windows))]
let script_name = "script.sh";
#[cfg(not(windows))]
let script_code = indoc! { r#"
touch text.txt
mkaput marked this conversation as resolved.
Show resolved Hide resolved
cd ../..
wawel37 marked this conversation as resolved.
Show resolved Hide resolved
cargo build
"#};

#[cfg(windows)]
let script_name = "script.bat";
#[cfg(windows)]
let script_code = indoc! { r#"
@echo off
copy NUL text.txt
cd ../..
cargo build
"#};

CairoPluginProjectBuilder::start()
.name("foo")
.scarb_project(|b| {
b.name("foo")
.version("1.0.0")
.manifest_extra(formatdoc! {r#"
[cairo-plugin]

[scripts]
package = "{script_name}"
"#})
})
.lib_rs(indoc! {r#"
use cairo_lang_macro::{ProcMacroResult, TokenStream, attribute_macro};

#[attribute_macro]
pub fn some(_attr: TokenStream, token_stream: TokenStream) -> ProcMacroResult {
ProcMacroResult::new(token_stream)
}
"#})
.src(script_name, script_code)
.build(&t);

#[cfg(not(windows))]
make_executable(&t.to_path_buf().join(script_name));

Scarb::quick_snapbox()
.arg("package")
.current_dir(&t)
.assert()
.success();

assert!(Path::new(
&t.to_path_buf()
.join("target")
.join("package")
.join("text.txt"),
wawel37 marked this conversation as resolved.
Show resolved Hide resolved
)
.exists());
assert!(Path::new(&t.to_path_buf().join("target").join("debug")).exists())
}
Loading