Skip to content

Commit

Permalink
Update tool versions; Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
lpotthast authored and ctron committed Nov 24, 2023
1 parent 925423b commit 126b054
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 84 deletions.
8 changes: 4 additions & 4 deletions Trunk.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ cargo = false

[tools]
# Default dart-sass version to download.
sass = "1.54.9"
sass = "1.69.5"
# Default wasm-bindgen version to download.
wasm_bindgen = "0.2.83"
wasm_bindgen = "0.2.88"
# Default wasm-opt version to download.
wasm_opt = "version_110"
wasm_opt = "version_116"
# Default tailwindcss-cli version to download.
tailwindcss = "3.3.2"
tailwindcss = "3.3.5"

## proxy
# Proxies are optional, and default to `None`.
Expand Down
26 changes: 19 additions & 7 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ impl BuildSystem {
// Ensure the output dist directories are in place.
fs::create_dir_all(self.cfg.final_dist.as_path())
.await
.with_context(|| "error creating build environment directory: dist")?;
.with_context(|| {
format!(
"error creating build environment directory: {}",
self.cfg.final_dist.display()
)
})?;

self.prepare_staging_dist()
.await
Expand Down Expand Up @@ -94,12 +99,19 @@ impl BuildSystem {
let staging_dist = self.cfg.staging_dist.as_path();

// Clean staging area, if applicable
remove_dir_all(staging_dist.into())
.await
.context("error cleaning staging dist dir")?;
fs::create_dir_all(staging_dist)
.await
.with_context(|| "error creating build environment directory: staging dist dir")?;
remove_dir_all(staging_dist.into()).await.with_context(|| {
format!(
"error cleaning staging dist dir: {}",
staging_dist.display()
)
})?;

fs::create_dir_all(staging_dist).await.with_context(|| {
format!(
"error creating build environment directory: {}",
staging_dist.display()
)
})?;

Ok(())
}
Expand Down
22 changes: 19 additions & 3 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,17 @@ pub async fn remove_dir_all(from_dir: PathBuf) -> Result<()> {

/// Checks if path exists.
pub async fn path_exists(path: impl AsRef<Path>) -> Result<bool> {
path_exists_and(path, |_| true).await
}

/// Checks if path exists and metadata matches the given predicate.
pub async fn path_exists_and(
path: impl AsRef<Path>,
and: impl FnOnce(Metadata) -> bool,
) -> Result<bool> {
tokio::fs::metadata(path.as_ref())
.await
.map(|_| true)
.map(and)
.or_else(|error| {
if error.kind() == ErrorKind::NotFound {
Ok(false)
Expand Down Expand Up @@ -167,12 +175,20 @@ pub async fn run_command(
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
.with_context(|| format!("error spawning {name} call ({path})", path = path.display()))?
.with_context(|| {
format!(
"error running {name} using executable '{}' with args: '{args:?}'",
path.display(),
)
})?
.wait()
.await
.with_context(|| format!("error during {name} call"))?;
if !status.success() {
bail!("{name} call returned a bad status");
bail!(
"{name} call to executable '{}' with args: '{args:?}' returned a bad status: {status}",
path.display()
);
}
Ok(())
}
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use anyhow::{Context, Result};
use clap::{ArgAction, Parser, Subcommand};
use common::STARTING;
use std::path::PathBuf;
use tracing::log;
use tracing_subscriber::prelude::*;

#[tokio::main]
Expand All @@ -43,7 +42,7 @@ async fn main() -> Result<()> {
.try_init()
.context("error initializing logging")?;

log::info!(
tracing::info!(
"{} Starting {} {}",
STARTING,
env!("CARGO_PKG_NAME"),
Expand Down
13 changes: 5 additions & 8 deletions src/pipelines/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,16 +495,13 @@ impl RustApp {

tracing::debug!(
"copying {js_loader_path} to {}",
js_loader_path_dist.to_string_lossy()
js_loader_path_dist.display()
);
self.copy_or_minify_js(js_loader_path, &js_loader_path_dist)
.await
.context("error minifying or copying JS loader file to stage dir")?;

tracing::debug!(
"copying {wasm_path} to {}",
wasm_path_dist.to_string_lossy()
);
tracing::debug!("copying {wasm_path} to {}", wasm_path_dist.display());

fs::copy(wasm_path, wasm_path_dist)
.await
Expand All @@ -514,14 +511,14 @@ impl RustApp {
let ts_path = bindgen_out.join(&hashed_ts_name);
let ts_path_dist = self.cfg.staging_dist.join(&hashed_ts_name);

tracing::debug!("copying {ts_path} to {}", ts_path_dist.to_string_lossy());
tracing::debug!("copying {ts_path} to {}", ts_path_dist.display());
fs::copy(ts_path, ts_path_dist)
.await
.context("error copying TS files to stage dir")?;
}

if let Some(ref m) = loader_shim_path {
tracing::debug!("creating {}", m.to_string_lossy());
tracing::debug!("creating {}", m.display());
let mut loader_f = fs::File::create(m)
.await
.context("error creating loader shim script")?;
Expand Down Expand Up @@ -554,7 +551,7 @@ impl RustApp {
let snippets_dir_dest = self.cfg.staging_dist.join(SNIPPETS_DIR);
tracing::debug!(
"recursively copying from '{snippets_dir_src}' to '{}'",
snippets_dir_dest.to_string_lossy()
snippets_dir_dest.display()
);
copy_dir_recursive(snippets_dir_src, snippets_dir_dest)
.await
Expand Down
50 changes: 30 additions & 20 deletions src/pipelines/sass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
processing::integrity::{IntegrityType, OutputDigest},
tools::{self, Application},
};
use anyhow::{Context, Result};
use anyhow::{ensure, Context, Result};
use nipper::Document;
use std::path::PathBuf;
use std::str::FromStr;
Expand Down Expand Up @@ -77,31 +77,41 @@ impl Sass {
/// Run this pipeline.
#[tracing::instrument(level = "trace", skip(self))]
async fn run(self) -> Result<TrunkAssetPipelineOutput> {
// tracing::info!("downloading sass");
let version = self.cfg.tools.sass.as_deref();
let sass = tools::get(Application::Sass, version, self.cfg.offline).await?;

// Compile the target SASS/SCSS file.
let style = if self.cfg.release {
"compressed"
} else {
"expanded"
};
let path_str = dunce::simplified(&self.asset.path).display().to_string();
let file_name = format!("{}.css", &self.asset.file_stem.to_string_lossy());
let file_path = dunce::simplified(&self.cfg.staging_dist.join(&file_name))
.display()
.to_string();
let args = &["--no-source-map", "-s", style, &path_str, &file_path];

let rel_path = crate::common::strip_prefix(&self.asset.path);
let source_path_str = dunce::simplified(&self.asset.path).display().to_string();
let source_test = common::path_exists_and(&source_path_str, |m| m.is_file()).await;
ensure!(
source_test.ok() == Some(true),
"SASS source path '{source_path_str}' does not exist / is not a file"
);

let temp_target_file_name = format!("{}.css", &self.asset.file_stem.to_string_lossy());
let temp_target_file_path =
dunce::simplified(&self.cfg.staging_dist.join(&temp_target_file_name))
.display()
.to_string();

let args = &[
"--no-source-map",
"--style",
match &self.cfg.release {
true => "compressed",
false => "expanded",
},
&source_path_str,
&temp_target_file_path,
];

let rel_path = common::strip_prefix(&self.asset.path);
tracing::info!(path = ?rel_path, "compiling sass/scss");
common::run_command(Application::Sass.name(), &sass, args).await?;

let css = fs::read_to_string(&file_path)
let css = fs::read_to_string(&temp_target_file_path)
.await
.with_context(|| format!("error reading CSS result file '{file_path}'"))?;
fs::remove_file(&file_path).await?;
.with_context(|| format!("error reading CSS result file '{temp_target_file_path}'"))?;
fs::remove_file(&temp_target_file_path).await?;

// Check if the specified SASS/SCSS file should be inlined.
let css_ref = if self.use_inline {
Expand All @@ -115,7 +125,7 @@ impl Sass {
.cfg
.filehash
.then(|| format!("{}-{:x}.css", &self.asset.file_stem.to_string_lossy(), hash))
.unwrap_or(file_name);
.unwrap_or(temp_target_file_name);
let file_path = self.cfg.staging_dist.join(&file_name);

let integrity = OutputDigest::generate_from(self.integrity, css.as_bytes());
Expand Down
Loading

0 comments on commit 126b054

Please sign in to comment.