Skip to content

Commit

Permalink
feat(fetch): fall back to fetching .src.rock
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jan 12, 2025
1 parent 645e0aa commit ab38e53
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
6 changes: 3 additions & 3 deletions rocks-bin/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ pub async fn fetch_remote(data: UnpackRemote, config: Config) -> Result<()> {
let destination = data
.path
.unwrap_or_else(|| PathBuf::from(format!("{}-{}", &rockspec.package, &rockspec.version)));
let rock_source = rockspec.source.current_platform();
rocks_lib::operations::FetchSrc::new(destination.clone().as_path(), rock_source, &bar)
.fetch_src()
rocks_lib::operations::FetchSrc::new(destination.clone().as_path(), &rockspec, &config, &bar)
.fetch()
.await?;

let rock_source = rockspec.source.current_platform();
let build_dir = rock_source
.unpack_dir
.as_ref()
Expand Down
39 changes: 11 additions & 28 deletions rocks-lib/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
hash::HasIntegrity,
lockfile::{LocalPackage, LocalPackageHashes, LockConstraint, PinnedState},
lua_installation::LuaInstallation,
operations::{self, FetchSrcRockError},
operations::{self, FetchSrcError},
package::PackageSpec,
progress::{Progress, ProgressBar},
remote_package_source::RemotePackageSource,
Expand Down Expand Up @@ -94,7 +94,7 @@ pub enum BuildError {
#[error(transparent)]
LuaVersionError(#[from] LuaVersionError),
#[error("failed to fetch rock source: {0}")]
FetchSrcRockError(#[from] FetchSrcRockError),
FetchSrcError(#[from] FetchSrcError),
#[error("compilation failed.\nstatus: {status}\nstdout: {stdout}\nstderr: {stderr}")]
CommandFailure {
status: ExitStatus,
Expand Down Expand Up @@ -242,32 +242,14 @@ async fn do_build(build: Build<'_>) -> Result<LocalPackage, BuildError> {

let temp_dir = tempdir::TempDir::new(&build.rockspec.package.to_string())?;

// Install the source in order to build.
let rock_source = build.rockspec.source.current_platform();
if let Err(err) = operations::FetchSrc::new(temp_dir.path(), rock_source, build.progress)
.fetch_src()
.await
{
let package = PackageSpec::new(
build.rockspec.package.clone(),
build.rockspec.version.clone(),
);
build.progress.map(|p| {
p.println(format!(
"⚠️ WARNING: Failed to fetch source for {}: {}",
&package, err
))
});
build.progress.map(|p| {
p.println(format!(
"⚠️ Falling back to .src.rock archive from {}",
&build.config.server()
))
});
operations::FetchSrcRock::new(&package, temp_dir.path(), build.config, build.progress)
.fetch()
.await?;
}
operations::FetchSrc::new(
temp_dir.path(),
build.rockspec,
build.config,
build.progress,
)
.fetch()
.await?;

let hashes = LocalPackageHashes {
rockspec: build.rockspec.hash()?,
Expand Down Expand Up @@ -303,6 +285,7 @@ async fn do_build(build: Build<'_>) -> Result<LocalPackage, BuildError> {

let lua = LuaInstallation::new(&lua_version, build.config);

let rock_source = build.rockspec.source.current_platform();
let build_dir = match &rock_source.unpack_dir {
Some(unpack_dir) => temp_dir.path().join(unpack_dir),
None => temp_dir.path().into(),
Expand Down
38 changes: 31 additions & 7 deletions rocks-lib/src/operations/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use crate::operations;
use crate::package::PackageSpec;
use crate::progress::Progress;
use crate::progress::ProgressBar;
use crate::{rockspec::RockSource, rockspec::RockSourceSpec};
use crate::rockspec::RockSourceSpec;
use crate::rockspec::Rockspec;

use super::DownloadSrcRockError;

Expand All @@ -30,7 +31,9 @@ pub struct FetchSrc<'a> {
#[builder(start_fn)]
dest_dir: &'a Path,
#[builder(start_fn)]
rock_source: &'a RockSource,
rockspec: &'a Rockspec,
#[builder(start_fn)]
config: &'a Config,
#[builder(start_fn)]
progress: &'a Progress<ProgressBar>,
}
Expand All @@ -39,8 +42,27 @@ impl<State> FetchSrcBuilder<'_, State>
where
State: fetch_src_builder::State + fetch_src_builder::IsComplete,
{
pub async fn fetch_src(self) -> Result<(), FetchSrcError> {
do_fetch_src(self._build()).await
pub async fn fetch(self) -> Result<(), FetchSrcError> {
let fetch = self._build();
if let Err(err) = do_fetch_src(&fetch).await {
let package = PackageSpec::new(
fetch.rockspec.package.clone(),
fetch.rockspec.version.clone(),
);
fetch.progress.map(|p| {
p.println(format!(
"⚠️ WARNING: Failed to fetch source for {}: {}",
&package, err
))
});
fetch
.progress
.map(|p| p.println("⚠️ Falling back to .src.rock archive"));
FetchSrcRock::new(&package, fetch.dest_dir, fetch.config, fetch.progress)
.fetch()
.await?;
}
Ok(())
}
}

Expand All @@ -54,13 +76,15 @@ pub enum FetchSrcError {
Request(#[from] reqwest::Error),
#[error(transparent)]
Unpack(#[from] UnpackError),
#[error(transparent)]
FetchSrcRock(#[from] FetchSrcRockError),
}

/// A rocks package source fetcher, providing fine-grained control
/// over how a package should be fetched.
#[derive(Builder)]
#[builder(start_fn = new, finish_fn(name = _build, vis = ""))]
pub(crate) struct FetchSrcRock<'a> {
struct FetchSrcRock<'a> {
#[builder(start_fn)]
package: &'a PackageSpec,
#[builder(start_fn)]
Expand All @@ -87,8 +111,8 @@ pub enum FetchSrcRockError {
Unpack(#[from] UnpackError),
}

async fn do_fetch_src(fetch: FetchSrc<'_>) -> Result<(), FetchSrcError> {
let rock_source = fetch.rock_source;
async fn do_fetch_src(fetch: &FetchSrc<'_>) -> Result<(), FetchSrcError> {
let rock_source = fetch.rockspec.source.current_platform();
let progress = fetch.progress;
let dest_dir = fetch.dest_dir;
match &rock_source.source_spec {
Expand Down

0 comments on commit ab38e53

Please sign in to comment.