Skip to content

Commit 94db6e8

Browse files
committed
Generate sourcedirs.json
1 parent a423415 commit 94db6e8

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

rewatch/src/build.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@ use crate::build::compile::{mark_modules_with_deleted_deps_dirty, mark_modules_w
1414
use crate::helpers::emojis::*;
1515
use crate::helpers::{self, get_workspace_root};
1616
use crate::sourcedirs;
17+
use ahash::AHashSet;
1718
use anyhow::{Result, anyhow};
1819
use build_types::*;
1920
use console::style;
2021
use indicatif::{ProgressBar, ProgressStyle};
2122
use log::log_enabled;
2223
use serde::Serialize;
2324
use std::ffi::OsString;
24-
use std::fmt;
2525
use std::fs::File;
2626
use std::io::{Write, stdout};
2727
use std::path::{Path, PathBuf};
2828
use std::process::Stdio;
2929
use std::time::{Duration, Instant};
30+
use std::{fmt, fs};
3031

3132
fn is_dirty(module: &Module) -> bool {
3233
match module.source_type {
@@ -469,6 +470,50 @@ pub fn write_build_ninja(build_state: &BuildState) {
469470
}
470471
}
471472

473+
#[derive(Serialize, Debug)]
474+
pub struct SourceDirsMeta {
475+
pub dirs: Vec<String>,
476+
pub pkgs: Vec<(String, String)>,
477+
pub generated: Vec<String>,
478+
}
479+
480+
fn generate_sourcedirs_meta(build_state: &BuildState) {
481+
let mut dir_set: AHashSet<String> = AHashSet::new();
482+
let mut pkgs: Vec<(String, String)> = vec![];
483+
for package in build_state.packages.values() {
484+
if package.is_local_dep {
485+
if let Some(source_files) = &package.source_files {
486+
for source_file in source_files.keys() {
487+
if let Some(parent) = source_file.parent() {
488+
dir_set.insert(parent.to_string_lossy().into_owned());
489+
}
490+
}
491+
}
492+
} else {
493+
pkgs.push((package.name.clone(), package.path.to_string_lossy().into_owned()));
494+
}
495+
}
496+
497+
let meta = SourceDirsMeta {
498+
dirs: dir_set.into_iter().collect(),
499+
pkgs,
500+
generated: vec![],
501+
};
502+
503+
let json = serde_json::to_string(&meta);
504+
match json {
505+
Err(err) => {
506+
println!("Error serializing .sourcedirs.json: {}", err);
507+
}
508+
Ok(json) => {
509+
let output_path = build_state.project_root.join("lib/bs/.sourcedirs.json");
510+
if let Err(e) = fs::write(output_path, json) {
511+
println!("Error writing sourcedirs.json: {}", e);
512+
}
513+
}
514+
}
515+
}
516+
472517
pub fn build(
473518
filter: &Option<regex::Regex>,
474519
path: &Path,
@@ -494,6 +539,8 @@ pub fn build(
494539
)
495540
.map_err(|e| anyhow!("Could not initialize build. Error: {e}"))?;
496541

542+
generate_sourcedirs_meta(&build_state);
543+
497544
match incremental_build(
498545
&mut build_state,
499546
default_timing,

0 commit comments

Comments
 (0)