Skip to content
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
24 changes: 11 additions & 13 deletions packages/zpm-switch/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use std::{future::Future, io::Write};

use serde::{Deserialize, Serialize};
use zpm_parsers::JsonDocument;
use zpm_semver::{Version, VersionRc};
use zpm_utils::{DataType, Hash64, Path, ToFileString, ToHumanString, Unit, is_terminal};
use zpm_utils::eco_vec;
use zpm_semver::{Range, VersionRc};
use zpm_utils::{DataType, FromFileString, Hash64, Path, ToFileString, ToHumanString, Unit, is_terminal};

use crate::errors::Error;

pub const CACHE_VERSION: usize = 1;
pub const CACHE_VERSION: usize = 2;

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -26,18 +25,17 @@ fn get_npm_registry_server() -> String {
impl CacheKey {
pub fn to_npm_url(&self) -> Option<String> {
if self.version.rc.as_ref().map_or(true, |rc| !rc.starts_with(&[VersionRc::String("git".into())])) {
// Older RC versions (<6.0.0-rc.9) are not available in npm
let first_npm_release = Version::new_from_components(
6,
0,
0,
Some(eco_vec![VersionRc::String("rc".into()), VersionRc::Number(9)]),
);

if self.version >= first_npm_release {
// zpm is available on npm since 6.0.0-rc.9
if Range::from_file_string(">=6.0.0-rc.9").unwrap().check_ignore_rc(&self.version) {
let registry = get_npm_registry_server();
return Some(format!("{}/@yarnpkg/yarn-{}/-/yarn-{}-{}.tgz", registry, self.platform, self.platform, self.version.to_file_string()));
}

// berry has been published to npm since 2.4.1
if Range::from_file_string(">=2.4.1 <6.0.0-0").unwrap().check_ignore_rc(&self.version) {
let registry = get_npm_registry_server();
return Some(format!("{}/@yarnpkg/cli-dist/-/cli-dist-{}.tgz", registry, self.version.to_file_string()));
}
}

None
Expand Down
20 changes: 16 additions & 4 deletions packages/zpm-switch/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ async fn install_node_js_from_url(source: &cache::CacheKey) -> Result<Command, E
Ok(command)
}

async fn install_node_js_from_package(source: &cache::CacheKey, main_file: &Path) -> Result<Command, Error> {
async fn install_node_js_from_package(source: &cache::CacheKey, url: &str, main_file: &Path) -> Result<Command, Error> {
let cache_path = cache::ensure(source, |p| async move {
let compressed_data
= fetch(&source.to_url()).await?;
= fetch(url).await?;

tokio::task::spawn_blocking(move || -> Result<(), Error> {
let data
Expand Down Expand Up @@ -153,6 +153,18 @@ async fn install_node_js_from_package(source: &cache::CacheKey, main_file: &Path
Ok(command)
}

async fn install_berry(source: &cache:: CacheKey) -> Result<Command, Error> {
if let Some(npm_url) = source.to_npm_url() {
install_node_js_from_package(source, &npm_url, &Path::from_str("bin/yarn.js").unwrap()).await
} else {
install_node_js_from_url(source).await
}
}

async fn install_yarnpkg_legacy(source: &cache:: CacheKey) -> Result<Command, Error> {
install_node_js_from_package(source, &source.to_url(), &Path::from_str("bin/yarn.js").unwrap()).await
}

pub async fn install_package_manager(package_manager: &VersionPackageManagerReference) -> Result<Command, Error> {
let version_platform = cache::CacheKey {
cache_version: cache::CACHE_VERSION,
Expand All @@ -165,11 +177,11 @@ pub async fn install_package_manager(package_manager: &VersionPackageManagerRefe
}

if zpm_semver::Range::from_file_string(">=2.0.0-0").unwrap().check_ignore_rc(&package_manager.version) {
return install_node_js_from_url(&version_platform).await;
return install_berry(&version_platform).await;
}

if zpm_semver::Range::from_file_string(">=0.0.0-0").unwrap().check_ignore_rc(&package_manager.version) {
return install_node_js_from_package(&version_platform, &Path::from_str("bin/yarn.js").unwrap()).await;
return install_yarnpkg_legacy(&version_platform).await;
}

unreachable!()
Expand Down
20 changes: 10 additions & 10 deletions packages/zpm-switch/tests/postinstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ fn empty_profile_file() -> Result<(), Box<dyn std::error::Error>> {
cmd.assert()
.success();

let profile_content = tmp_dir
.with_join_str(".profile")
let bashrc_content = tmp_dir
.with_join_str(".bashrc")
.fs_read_text_prealloc()
.expect("Failed to read .profile");
.expect("Failed to read .bashrc");

assert_eq!(profile_content, format!(". \"{}/.yarn/switch/env\"\n", tmp_dir.to_file_string()));
assert_eq!(bashrc_content, format!("# Added by Yarn Switch\nsource \"{}/.yarn/switch/env\"\n", tmp_dir.to_file_string()));
Comment on lines +38 to +43
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those changes don't seem related

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are in a separate commit, these tests failed and now they no longer do. I can split this out into a separate pr if you prefer.
Won't be until Thursday evening though, won't have any time to spare before then.


Ok(())
}
Expand All @@ -53,22 +53,22 @@ fn profile_file_with_existing_path() -> Result<(), Box<dyn std::error::Error>> {
} = init_test_env();

tmp_dir
.with_join_str(".profile")
.with_join_str(".bashrc")
.fs_write_text("# Hello world!\n")
.expect("Failed to write .profile");
.expect("Failed to write .bashrc");

cmd.args(vec!["switch", "postinstall", "--home-dir", tmp_dir.as_str()]);
cmd.env("SHELL", "/bin/bash");

cmd.assert()
.success();

let profile_content = tmp_dir
.with_join_str(".profile")
let bashrc_content = tmp_dir
.with_join_str(".bashrc")
.fs_read_text_prealloc()
.expect("Failed to read .profile");
.expect("Failed to read .bashrc");

assert_eq!(profile_content, format!("# Hello world!\n. \"{}/.yarn/switch/env\"\n", tmp_dir.to_file_string()));
assert_eq!(bashrc_content, format!("# Hello world!\n\n# Added by Yarn Switch\nsource \"{}/.yarn/switch/env\"\n", tmp_dir.to_file_string()));

Ok(())
}
Expand Down
Loading