Skip to content

Commit d62c883

Browse files
committed
Refactor version module
commit-id:f673da00
1 parent b5c96e0 commit d62c883

File tree

10 files changed

+163
-79
lines changed

10 files changed

+163
-79
lines changed

crates/docs/src/snippet.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::sync::LazyLock;
22

33
use regex::Regex;
4-
use scarb_api::ScarbCommand;
4+
use scarb_api::version::scarb_version;
55
use semver::VersionReq;
66
use serde::{Deserialize, Serialize};
77

@@ -101,10 +101,7 @@ impl Default for SnippetConfig {
101101
impl SnippetConfig {
102102
fn check_scarb_compatibility(&mut self) {
103103
if let Some(ref scarb_version_req) = self.scarb_version {
104-
let current_scarb_version = ScarbCommand::version()
105-
.run()
106-
.expect("Failed to get scarb version")
107-
.scarb;
104+
let current_scarb_version = scarb_version().expect("Failed to get scarb version").scarb;
108105

109106
if !scarb_version_req.matches(&current_scarb_version) {
110107
self.ignored = true;

crates/forge/src/new.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use anyhow::{Context, Ok, Result, anyhow, bail, ensure};
44
use camino::Utf8PathBuf;
55
use include_dir::{Dir, DirEntry, include_dir};
66
use indoc::formatdoc;
7+
use scarb_api::version::scarb_version;
78
use scarb_api::{ScarbCommand, ensure_scarb_available};
89
use semver::Version;
910
use shared::consts::FREE_RPC_PROVIDER_URL;
@@ -108,7 +109,7 @@ impl TryFrom<&Template> for TemplateManifestConfig {
108109
type Error = anyhow::Error;
109110

110111
fn try_from(template: &Template) -> Result<Self> {
111-
let cairo_version = ScarbCommand::version().run()?.cairo;
112+
let cairo_version = scarb_version()?.cairo;
112113
match template {
113114
Template::CairoProgram => Ok(TemplateManifestConfig {
114115
dependencies: vec![],
@@ -256,7 +257,7 @@ fn set_cairo_edition(document: &mut DocumentMut, cairo_edition: &str) {
256257
}
257258

258259
fn add_assert_macros(document: &mut DocumentMut) -> Result<()> {
259-
let versions = ScarbCommand::version().run()?;
260+
let versions = scarb_version()?;
260261
let version = if versions.scarb < MINIMAL_SCARB_FOR_CORRESPONDING_ASSERT_MACROS {
261262
DEFAULT_ASSERT_MACROS
262263
} else {
@@ -361,7 +362,7 @@ pub fn new(
361362
);
362363
}
363364
let name = infer_name(name, &path)?;
364-
let scarb_version = ScarbCommand::version().run()?.scarb;
365+
let scarb_version = scarb_version()?.scarb;
365366

366367
fs::create_dir_all(&path)?;
367368
let project_path = path.canonicalize()?;
@@ -455,7 +456,7 @@ fn get_template_dir(template: &Template) -> Result<Dir<'_>> {
455456
}
456457

457458
fn get_oz_version() -> Result<Version> {
458-
let scarb_version = ScarbCommand::version().run()?.scarb;
459+
let scarb_version = scarb_version()?.scarb;
459460

460461
let oz_version = match scarb_version {
461462
ver if ver >= Version::new(2, 9, 4) => Version::new(1, 0, 0),

crates/forge/src/run_tests/workspace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use forge_runner::test_case_summary::AnyTestCaseSummary;
1919
use forge_runner::{CACHE_DIR, test_target_summary::TestTargetSummary};
2020
use foundry_ui::UI;
2121
use scarb_api::metadata::{MetadataOpts, metadata_with_opts};
22+
use scarb_api::version::scarb_version;
2223
use scarb_api::{
23-
ScarbCommand,
2424
metadata::{Metadata, PackageMetadata},
2525
target_dir_for_workspace,
2626
};
@@ -46,7 +46,7 @@ pub async fn run_for_workspace(args: TestArgs, ui: Arc<UI>) -> Result<ExitStatus
4646

4747
check_profile_compatibility(&args, &scarb_metadata)?;
4848

49-
let scarb_version = ScarbCommand::version().run()?.scarb;
49+
let scarb_version = scarb_version()?.scarb;
5050
if scarb_version >= MINIMAL_SCARB_VERSION_FOR_V2_MACROS_REQUIREMENT {
5151
error_if_snforge_std_not_compatible(&scarb_metadata)?;
5252
warn_if_snforge_std_does_not_match_package_version(&scarb_metadata, &ui)?;

crates/forge/src/warn.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use forge_runner::package_tests::with_config_resolved::TestTargetWithResolvedCon
55
use foundry_ui::UI;
66
use foundry_ui::components::warning::WarningMessage;
77
use indoc::formatdoc;
8-
use scarb_api::{ScarbCommand, package_matches_version_requirement};
8+
use scarb_api::package_matches_version_requirement;
9+
use scarb_api::version::scarb_version;
910
use scarb_metadata::Metadata;
1011
use semver::{Comparator, Op, Version, VersionReq};
1112
use shared::rpc::create_rpc_client;
@@ -25,7 +26,7 @@ pub(crate) fn warn_if_available_gas_used_with_incompatible_scarb_version(
2526
.config
2627
.available_gas
2728
.as_ref().is_some_and(cheatnet::runtime_extensions::forge_config_extension::config::RawAvailableResourceBoundsConfig::is_zero)
28-
&& ScarbCommand::version().run()?.scarb <= Version::new(2, 4, 3)
29+
&& scarb_version()?.scarb <= Version::new(2, 4, 3)
2930
{
3031
ui.println(&WarningMessage::new("`available_gas` attribute was probably specified when using Scarb ~2.4.3 \
3132
Make sure to use Scarb >=2.4.4"

crates/forge/test_utils/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use assert_fs::fixture::PathCopy;
66
use camino::Utf8PathBuf;
77
use forge::MINIMAL_SCARB_VERSION_FOR_V2_MACROS_REQUIREMENT;
88
use project_root::get_project_root;
9-
use scarb_api::ScarbCommand;
9+
use scarb_api::version::scarb_version;
1010
use semver::Version;
1111
use std::str::FromStr;
1212

@@ -21,7 +21,7 @@ pub fn tempdir_with_tool_versions() -> Result<assert_fs::TempDir> {
2121
}
2222

2323
pub fn get_assert_macros_version() -> Result<Version> {
24-
let scarb_version_output = ScarbCommand::version().run()?;
24+
let scarb_version_output = scarb_version()?;
2525
let assert_macros_version =
2626
if scarb_version_output.scarb < MINIMAL_SCARB_FOR_CORRESPONDING_ASSERT_MACROS {
2727
DEFAULT_ASSERT_MACROS
@@ -56,8 +56,6 @@ pub fn get_snforge_std_entry() -> Result<String> {
5656

5757
#[must_use]
5858
pub fn use_snforge_std_deprecated() -> bool {
59-
let scarb_version_output = ScarbCommand::version()
60-
.run()
61-
.expect("Failed to get scarb version");
59+
let scarb_version_output = scarb_version().expect("Failed to get scarb version");
6260
scarb_version_output.scarb < MINIMAL_SCARB_VERSION_FOR_V2_MACROS_REQUIREMENT
6361
}

crates/forge/tests/e2e/plugin_versions.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::e2e::common::runner::{runner, test_runner};
22
use camino::Utf8PathBuf;
33
use indoc::{formatdoc, indoc};
44
use scarb_api::ScarbCommand;
5+
use scarb_api::version::scarb_version_for_dir;
56
use shared::test_utils::output_assert::assert_stdout_contains;
67
use snapbox::cmd::Command;
78
use test_utils::tempdir_with_tool_versions;
@@ -146,9 +147,7 @@ fn new_scarb_deprecated_macros() {
146147
.command()
147148
.output()
148149
.unwrap();
149-
let scarb_version = ScarbCommand::version()
150-
.current_dir(temp.path())
151-
.run()
150+
let scarb_version = scarb_version_for_dir(temp.path())
152151
.unwrap()
153152
.scarb
154153
.to_string();

crates/forge/tests/e2e/requirements.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::e2e::common::runner::{runner, setup_package};
22
use indoc::{formatdoc, indoc};
3-
use scarb_api::ScarbCommand;
3+
use scarb_api::version::scarb_version;
44
use semver::Version;
55
use shared::test_utils::output_assert::assert_stdout_contains;
66

@@ -9,7 +9,7 @@ fn happy_path() {
99
let temp = setup_package("simple_package");
1010
let output = runner(&temp).arg("check-requirements").assert();
1111

12-
let scarb_version = ScarbCommand::version().run().unwrap().scarb;
12+
let scarb_version = scarb_version().unwrap().scarb;
1313

1414
let rust_check = if scarb_version < Version::new(2, 10, 0) {
1515
indoc! {"

crates/forge/tests/integration/meta_tx_v0.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use forge_runner::forge_config::ForgeTrackedResource;
22
use indoc::indoc;
3-
use scarb_api::ScarbCommand;
3+
use scarb_api::version::scarb_version;
44
use semver::Version;
55
use std::path::Path;
66
use test_utils::runner::{Contract, assert_passed};
@@ -9,9 +9,7 @@ use test_utils::test_case;
99

1010
// TODO(#3704) Remove scarb version check
1111
fn skip_scarb_lt_2_11_0() -> bool {
12-
let version_info = ScarbCommand::version()
13-
.run()
14-
.expect("Failed to get Scarb version");
12+
let version_info = scarb_version().expect("Failed to get Scarb version");
1513

1614
if version_info.scarb < Version::new(2, 11, 0) {
1715
eprintln!("[IGNORED] `meta_tx_v0` syscall is not supported in Scarb < 2.11.0");

crates/scarb-api/src/command.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::version::VersionCommand;
21
use scarb_ui::args::{FeaturesSpec, PackagesFilter, ProfileSpec, ToEnvVars};
32
use std::collections::HashMap;
43
use std::ffi::{OsStr, OsString};
@@ -50,12 +49,6 @@ impl ScarbCommand {
5049
cmd
5150
}
5251

53-
/// Creates [`VersionCommand`] command
54-
#[must_use]
55-
pub fn version() -> VersionCommand {
56-
VersionCommand::new()
57-
}
58-
5952
/// Path to `Scarb.toml`.
6053
///
6154
/// If not set, this will look for `Scarb.toml` in the current directory or its ancestors.

0 commit comments

Comments
 (0)