Skip to content

Commit 32025ea

Browse files
committed
Refactor component installation to conditionally include ForcDoc based on version support.
1 parent 6734116 commit 32025ea

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

src/handlers/upload.rs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use flate2::{
66
{read::GzDecoder, write::GzEncoder},
77
};
88
use forc_util::bytecode::get_bytecode_id;
9+
use semver::Version;
910
use serde::Serialize;
1011
use std::fmt;
1112
use std::fs::{self, File};
@@ -23,9 +24,6 @@ const README_FILE: &str = "README.md";
2324
const FORC_MANIFEST_FILE: &str = "Forc.toml";
2425
const MAX_UPLOAD_SIZE_STR: &str = "10MB";
2526
pub const TARBALL_NAME: &str = "project.tgz";
26-
// These are the components that can be installed with cargo-binstall.
27-
const FORC_COMPONENTS: &[Component; 2] = &[Component::Forc, Component::ForcDoc];
28-
2927
#[derive(Error, Debug, PartialEq, Eq, Serialize)]
3028
pub enum UploadError {
3129
#[error("Failed to create temporary directory.")]
@@ -395,7 +393,9 @@ pub fn install_binaries_at_path(forc_version: &str, forc_path: &Path) -> Result<
395393
}
396394
};
397395

398-
for component in missing_components(forc_path, FORC_COMPONENTS) {
396+
let components = components_for_version(forc_version);
397+
398+
for component in missing_components(forc_path, &components) {
399399
install_component(forc_version, forc_path, os, arch, component)?;
400400

401401
if !forc_path.join("bin").join(component.binary_name()).exists() {
@@ -406,8 +406,7 @@ pub fn install_binaries_at_path(forc_version: &str, forc_path: &Path) -> Result<
406406
Ok(())
407407
}
408408

409-
/// These are the components that can be installed with cargo-binstall.
410-
#[derive(Copy, Clone, Debug)]
409+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
411410
enum Component {
412411
Forc,
413412
ForcDoc,
@@ -428,6 +427,27 @@ impl fmt::Display for Component {
428427
}
429428
}
430429

430+
fn components_for_version(forc_version: &str) -> Vec<Component> {
431+
let mut components = vec![Component::Forc];
432+
433+
if supports_forc_doc(forc_version) {
434+
components.push(Component::ForcDoc);
435+
} else {
436+
tracing::debug!(
437+
"Skipping forc-doc installation for unsupported version {}",
438+
forc_version
439+
);
440+
}
441+
442+
components
443+
}
444+
445+
fn supports_forc_doc(forc_version: &str) -> bool {
446+
Version::parse(forc_version.trim_start_matches('v'))
447+
.map(|version| version >= Version::new(0, 70, 0))
448+
.unwrap_or(false)
449+
}
450+
431451
fn install_component(
432452
forc_version: &str,
433453
forc_path: &Path,
@@ -491,7 +511,9 @@ mod tests {
491511
let forc_root = tempdir().expect("tempdir ok");
492512
install_binaries_at_path("0.70.1", forc_root.path()).expect("install ok");
493513

494-
for component in FORC_COMPONENTS.iter().copied() {
514+
let components = components_for_version("0.70.1");
515+
516+
for component in components.iter().copied() {
495517
let binary_name = component.binary_name();
496518
let binary_path = forc_root.path().join("bin").join(binary_name);
497519
assert!(
@@ -517,6 +539,15 @@ mod tests {
517539
}
518540
}
519541

542+
#[test]
543+
fn components_for_version_only_enables_forc_doc_when_supported() {
544+
let modern = components_for_version("0.70.1");
545+
assert!(modern.contains(&Component::ForcDoc));
546+
547+
let legacy = components_for_version("0.65.0");
548+
assert_eq!(legacy, vec![Component::Forc]);
549+
}
550+
520551
#[tokio::test]
521552
#[serial]
522553
async fn handle_project_upload_success() {

0 commit comments

Comments
 (0)