Skip to content

Commit 6ef21e2

Browse files
committed
moved tempfile method into separate function
1 parent 80dd587 commit 6ef21e2

2 files changed

Lines changed: 35 additions & 25 deletions

File tree

crates/wkg/src/main.rs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ mod wit;
2424
use oci::OciCommands;
2525
use wit::WitCommands;
2626

27+
use crate::wit::temp_wit_file;
28+
2729
#[derive(Parser, Debug)]
2830
#[command(version)]
2931
struct Cli {
@@ -252,15 +254,15 @@ impl PublishArgs {
252254
pub async fn run(self) -> anyhow::Result<()> {
253255
let client = self.common.get_client().await?;
254256

255-
let package = if let Some(package) = self.package {
256-
Some((
257-
package.package,
258-
package.version.ok_or_else(|| {
259-
anyhow::anyhow!("version is required when manually overriding the package ID")
260-
})?,
261-
))
262-
} else {
263-
None
257+
let package = match self.package {
258+
Some(PackageSpec {
259+
package,
260+
version: Some(v),
261+
}) => Some((package, v)),
262+
Some(PackageSpec { version: None, .. }) => {
263+
anyhow::bail!("version is required when manually overriding the package ID");
264+
}
265+
None => None,
264266
};
265267

266268
// If the input is a directory, build a WIT package from it into a temp
@@ -269,7 +271,7 @@ impl PublishArgs {
269271
let (publish_path, _tmp) = if self.path.is_dir() {
270272
let mut lock_file = LockFile::load(true).await?;
271273
let prev_lock_ref = (lock_file.version, lock_file.packages.clone());
272-
let (build_ref, _, bytes) =
274+
let (pkg_ref, _, bytes) =
273275
wit::build_wit_dir(&self.path, client.clone(), &mut lock_file).await?;
274276
// There is no way to check if we are in a git repository unlike `cargo publish --allow-dirty` so
275277
// check against previous values.
@@ -285,22 +287,9 @@ impl PublishArgs {
285287
});
286288
}
287289

288-
// Sanitize the package ref for use as a filename prefix: `namespace:name`
289-
// contains characters (`:`, `/`) that are invalid in filenames on some
290-
// platforms (notably Windows).
291-
let prefix: String = build_ref.to_string().replace([':', '/'], "_");
292-
let tmp = tempfile::Builder::new()
293-
.prefix(&prefix)
294-
.suffix(".wasm")
295-
.tempfile()
296-
.context("Failed to create temporary file for built WIT package")?;
297-
tokio::fs::write(tmp.path(), &bytes)
298-
.await
299-
.context("Failed to write built WIT package to temp file")?;
300-
let tmp_pkg_path = tmp.path().to_path_buf();
301-
tracing::debug!(tmp_pkg_path = %tmp_pkg_path.display(), "Wrote temporary WIT package file");
290+
let tmp = temp_wit_file(&pkg_ref, &bytes).await?;
302291

303-
(tmp_pkg_path, Some(tmp))
292+
(tmp.path().to_path_buf(), Some(tmp))
304293
} else {
305294
(self.path.clone(), None)
306295
};

crates/wkg/src/wit.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::path::{Path, PathBuf};
33

44
use anyhow::Context;
55
use clap::{Args, Subcommand};
6+
use tempfile::NamedTempFile;
67
use wasm_pkg_client::caching::{CachingClient, FileCache};
78
use wasm_pkg_common::package::{PackageRef, Version};
89
use wasm_pkg_core::{
@@ -123,6 +124,26 @@ pub async fn build_wit_dir(
123124
Ok(result)
124125
}
125126

127+
pub async fn temp_wit_file(package: &PackageRef, bytes: &[u8]) -> anyhow::Result<NamedTempFile> {
128+
// Sanitize the package ref for use as a filename prefix: `namespace:name`
129+
// contains characters (`:`, `/`) that are invalid in filenames on some
130+
// platforms (notably Windows).
131+
let prefix: String = package.to_string().replace([':', '/'], "_");
132+
let tmp_handle = tempfile::Builder::new()
133+
.prefix(&prefix)
134+
.suffix(".wasm")
135+
.tempfile()
136+
.context("Failed to create temporary file for built WIT package")
137+
.with_context(|| format!("package: {package}"))?;
138+
tokio::fs::write(tmp_handle.path(), &bytes)
139+
.await
140+
.context("Failed to write built WIT package to temp file")
141+
.with_context(|| format!("package: {package}"))?;
142+
143+
tracing::debug!(tmp_pkg_path = %tmp_handle.path().display(), "Wrote temporary WIT package file");
144+
Ok(tmp_handle)
145+
}
146+
126147
impl FetchArgs {
127148
pub async fn run(self) -> anyhow::Result<()> {
128149
check_dir(&self.dir).await?;

0 commit comments

Comments
 (0)