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

Allow overriding target directory #102

Merged
merged 1 commit into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions scarb/src/bin/scarb/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ pub struct ScarbArgs {
#[arg(long, env = "SCARB_OFFLINE", global = true)]
pub offline: bool,

/// Directory for all generated artifacts.
#[arg(
long,
env = "SCARB_TARGET_DIR",
value_name = "DIRECTORY",
global = true
)]
pub target_dir: Option<Utf8PathBuf>,

/// Subcommand and its arguments.
#[command(subcommand)]
pub command: Command,
Expand Down
2 changes: 1 addition & 1 deletion scarb/src/bin/scarb/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn cli_main(args: ScarbArgs) -> Result<()> {
let ui = Ui::new(args.ui_verbosity(), args.output_format());

let manifest_path = ops::find_manifest_path(args.manifest_path.as_deref())?;
let mut config = Config::init(manifest_path, dirs, ui)?;
let mut config = Config::init(manifest_path, dirs, ui, args.target_dir)?;
config.set_offline(args.offline);
commands::run(args.command, &mut config)
}
13 changes: 9 additions & 4 deletions scarb/src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ pub struct Config {
}

impl Config {
pub fn init(manifest_path: Utf8PathBuf, dirs: AppDirs, ui: Ui) -> Result<Self> {
pub fn init(
manifest_path: Utf8PathBuf,
dirs: AppDirs,
ui: Ui,
target_dir_override: Option<Utf8PathBuf>,
) -> Result<Self> {
let creation_time = Instant::now();

if tracing::enabled!(tracing::Level::TRACE) {
Expand All @@ -41,12 +46,12 @@ impl Config {
}
}

let target_dir = RootFilesystem::new_output_dir(
let target_dir = RootFilesystem::new_output_dir(target_dir_override.unwrap_or_else(|| {
manifest_path
.parent()
.expect("parent of manifest path must always exist")
.join(DEFAULT_TARGET_DIR_NAME),
);
.join(DEFAULT_TARGET_DIR_NAME)
}));

let dirs = Arc::new(dirs);

Expand Down
1 change: 1 addition & 0 deletions scarb/src/ops/subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub fn execute_external_subcommand(cmd: &str, args: &[&OsStr], config: &Config)
cmd.env("PATH", config.dirs().path_env());
cmd.env("SCARB_CACHE", config.dirs().cache_dir.path_unchecked());
cmd.env("SCARB_CONFIG", config.dirs().config_dir.path_unchecked());
cmd.env("SCARB_TARGET_DIR", config.target_dir().path_unchecked());
cmd.env("SCARB_MANIFEST_DIR", config.root());
cmd.env("SCARB_UI_VERBOSITY", config.ui().verbosity().to_string());
cmd.env("SCARB_LOG", config.scarb_log());
Expand Down
21 changes: 21 additions & 0 deletions scarb/tests/e2e/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,24 @@ fn compile_with_custom_lib_target() {
t.child("target/release/hello.casm")
.assert(predicates::path::exists().not());
}

#[test]
fn override_target_dir() {
let target_dir = assert_fs::TempDir::new().unwrap();

let t = assert_fs::TempDir::new().unwrap();
ProjectBuilder::start().name("hello").build(&t);

Scarb::quick_snapbox()
.arg("--target-dir")
.arg(target_dir.path())
.arg("build")
.current_dir(&t)
.assert()
.success();

t.child("target").assert(predicates::path::exists().not());
target_dir
.child("release/hello.sierra")
.assert(predicates::path::exists());
}
1 change: 1 addition & 0 deletions scarb/tests/e2e/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn env_variables_are_passed() {
SCARB
SCARB_CACHE
SCARB_CONFIG
SCARB_TARGET_DIR
SCARB_MANIFEST_DIR
SCARB_UI_VERBOSITY
)
Expand Down
1 change: 1 addition & 0 deletions scarb/tests/e2e/support/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl Scarb {
path_dirs: Vec::new(),
},
Ui::new(Verbosity::Verbose, OutputFormat::Text),
None,
)
.unwrap()
}
Expand Down