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

feat!(fetch): fall back to fetching .src.rock #326

Merged
merged 2 commits into from
Jan 12, 2025
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
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
37 changes: 11 additions & 26 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,30 +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::fetch_src_rock(&package, temp_dir.path(), build.config, build.progress).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 @@ -301,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
85 changes: 66 additions & 19 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,10 +76,43 @@ 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 = ""))]
struct FetchSrcRock<'a> {
#[builder(start_fn)]
package: &'a PackageSpec,
#[builder(start_fn)]
dest_dir: &'a Path,
#[builder(start_fn)]
config: &'a Config,
#[builder(start_fn)]
progress: &'a Progress<ProgressBar>,
}

impl<State> FetchSrcRockBuilder<'_, State>
where
State: fetch_src_rock_builder::State + fetch_src_rock_builder::IsComplete,
{
pub async fn fetch(self) -> Result<(), FetchSrcRockError> {
do_fetch_src_rock(self._build()).await
}
}

#[derive(Error, Debug)]
#[error(transparent)]
pub enum FetchSrcRockError {
DownloadSrcRock(#[from] DownloadSrcRockError),
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 Expand Up @@ -148,19 +203,11 @@ async fn do_fetch_src(fetch: FetchSrc<'_>) -> Result<(), FetchSrcError> {
Ok(())
}

#[derive(Error, Debug)]
#[error(transparent)]
pub enum FetchSrcRockError {
DownloadSrcRock(#[from] DownloadSrcRockError),
Unpack(#[from] UnpackError),
}

pub async fn fetch_src_rock(
package: &PackageSpec,
dest_dir: &Path,
config: &Config,
progress: &Progress<ProgressBar>,
) -> Result<(), FetchSrcRockError> {
async fn do_fetch_src_rock(fetch: FetchSrcRock<'_>) -> Result<(), FetchSrcRockError> {
let package = fetch.package;
let dest_dir = fetch.dest_dir;
let config = fetch.config;
let progress = fetch.progress;
let src_rock = operations::download_src_rock(package, config.server(), progress).await?;
let cursor = Cursor::new(src_rock.bytes);
let mime_type = infer::get(cursor.get_ref()).map(|file_type| file_type.mime_type());
Expand Down
Loading