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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kit"
version = "0.6.7"
version = "0.6.8"
edition = "2021"

[build-dependencies]
Expand Down
104 changes: 104 additions & 0 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::SystemTime;

use color_eyre::{
Section,
Expand Down Expand Up @@ -235,6 +236,82 @@ fn copy_dir(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
Ok(())
}

fn file_with_extension_exists(dir: &Path, extension: &str) -> bool {
if let Ok(entries) = fs::read_dir(dir) {
for entry in entries.filter_map(Result::ok) {
let path = entry.path();
if path.is_file() && path.extension().and_then(|ext| ext.to_str()) == Some(extension) {
return true;
}
}
}
false
}

#[instrument(level = "trace", skip_all)]
fn get_most_recent_modified_time(
dir: &Path,
exclude_files: &HashSet<&str>,
exclude_extensions: &HashSet<&str>,
exclude_dirs: &HashSet<&str>,
) -> Result<(Option<SystemTime>, Option<SystemTime>)> {
let mut most_recent: Option<SystemTime> = None;
let mut most_recent_excluded: Option<SystemTime> = None;

for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();

let file_name = path.file_name().unwrap_or_default().to_str().unwrap_or_default();

if exclude_files.contains(file_name) {
let file_time = get_file_modified_time(&path)?;
most_recent_excluded = Some(most_recent_excluded.map_or(file_time, |t| t.max(file_time)));
continue;
}

if path.is_dir() {
let dir_name = path.file_name().unwrap_or_default().to_str().unwrap_or_default();
if exclude_dirs.contains(dir_name) {
continue;
}

let (sub_time, sub_time_excluded) = get_most_recent_modified_time(
&path,
exclude_files,
exclude_extensions,
exclude_dirs,
)?;

if let Some(st) = sub_time {
most_recent = Some(most_recent.map_or(st, |t| t.max(st)));
}
if let Some(ste) = sub_time_excluded {
most_recent_excluded = Some(most_recent_excluded.map_or(ste, |t| t.max(ste)));
}
} else {
if let Some(extension) = path.extension() {
if exclude_extensions.contains(&extension.to_str().unwrap_or_default()) {
let file_time = get_file_modified_time(&path)?;
most_recent_excluded = Some(most_recent_excluded.map_or(file_time, |t| t.max(file_time)));
continue;
}
}

let file_time = get_file_modified_time(&path)?;
most_recent = Some(most_recent.map_or(file_time, |t| t.max(file_time)));
}
}

Ok((most_recent, most_recent_excluded))
}

#[instrument(level = "trace", skip_all)]
fn get_file_modified_time(file_path: &Path) -> Result<SystemTime> {
let metadata = fs::metadata(file_path)?;
Ok(metadata.modified()?)
}

#[instrument(level = "trace", skip_all)]
async fn compile_javascript_wasm_process(
process_dir: &Path,
Expand Down Expand Up @@ -908,6 +985,33 @@ pub async fn execute(
)
.with_suggestion(|| "Please re-run targeting a package."));
}
let build_with_features_path = package_dir.join("target").join("build_with_features.txt");
let old_features = fs::read_to_string(&build_with_features_path).ok();
if old_features == Some(features.to_string())
&& package_dir.join("Cargo.lock").exists()
&& package_dir.join("pkg").exists()
&& package_dir.join("pkg").join("api.zip").exists()
&& file_with_extension_exists(&package_dir.join("pkg"), "wasm")
{
let (source_time, build_time) = get_most_recent_modified_time(
package_dir,
&HashSet::from(["Cargo.lock", "api.zip"]),
&HashSet::from(["wasm"]),
&HashSet::from(["target"]),
)?;
if let Some(source_time) = source_time {
if let Some(build_time) = build_time {
if build_time.duration_since(source_time).is_ok() {
// build_time - source_time >= 0
// -> current build is up-to-date: don't rebuild
info!("Build up-to-date.");
return Ok(());
}
}
}
}
fs::create_dir_all(package_dir.join("target"))?;
fs::write(&build_with_features_path, features)?;

let ui_dir = package_dir.join("ui");
if !ui_dir.exists() {
Expand Down