Skip to content

Commit

Permalink
👷 - Scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
Roland Peelen committed Apr 19, 2024
1 parent 0657270 commit 85560bb
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod read_compile_state;
use crate::build::compile::{mark_modules_with_deleted_deps_dirty, mark_modules_with_expired_deps_dirty};
use crate::helpers::emojis::*;
use crate::helpers::{self, get_workspace_root};
use crate::sourcedirs;
use ahash::AHashSet;
use build_types::*;
use console::style;
Expand Down Expand Up @@ -344,6 +345,7 @@ pub fn incremental_build(
let compile_duration = start_compiling.elapsed();

logs::finalize(&build_state.packages);
sourcedirs::print(&build_state);
pb.finish();
if !compile_errors.is_empty() {
if helpers::contains_ascii_characters(&compile_warnings) {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pub mod cmd;
pub mod helpers;
pub mod lock;
pub mod queue;
pub mod sourcedirs;
pub mod watcher;
8 changes: 1 addition & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
use clap::{Parser, ValueEnum};
use regex::Regex;

pub mod bsconfig;
pub mod build;
pub mod cmd;
pub mod helpers;
pub mod lock;
pub mod queue;
pub mod watcher;
use rewatch::{build, cmd, lock, watcher};

#[derive(Debug, Clone, ValueEnum)]
enum Command {
Expand Down
117 changes: 117 additions & 0 deletions src/sourcedirs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use crate::build::build_types::BuildState;
use crate::build::packages::Package;
use ahash::{AHashMap, AHashSet};
use rayon::prelude::*;
use serde::Serialize;
use serde_json::json;
use std::fs::File;
use std::io::prelude::*;

type Dir = String;
type PackageName = String;
type AbsolutePath = String;
type PackagePath = String;

#[derive(Serialize, Debug, Clone, PartialEq, Hash)]
pub struct SourceDirs {
pub dirs: Vec<Dir>,
pub pkgs: Vec<(PackageName, AbsolutePath)>,
pub generated: Vec<String>,
}

pub fn print(buildstate: &BuildState) {
let (_name, package) = buildstate
.packages
.iter()
.find(|(_name, package)| package.is_root)
.expect("Could not find root package");

// First do all child packages
let child_packages = buildstate
.packages
.par_iter()
.filter(|(_name, package)| !package.is_root)
.map(|(_name, package)| {
let path = package.get_bs_build_path();

let dirs = package
.dirs
.to_owned()
.unwrap_or(AHashSet::new())
.iter()
.filter_map(|path| path.to_str().map(|x| x.to_string()))
.collect::<AHashSet<String>>();

fn deps_to_pkgs<'a>(
packages: &'a AHashMap<String, Package>,
xs: &'a Option<Vec<String>>,
) -> AHashSet<(String, PackagePath)> {
xs.as_ref()
.unwrap_or(&vec![])
.iter()
.filter_map(|name| {
packages
.get(&name.to_owned())
.map(|package| (name.clone(), package.path.clone()))
})
.collect::<AHashSet<(String, PackagePath)>>()
}

let pinned_dependencies =
deps_to_pkgs(&buildstate.packages, &package.bsconfig.pinned_dependencies);
let bs_dependencies = deps_to_pkgs(&buildstate.packages, &package.bsconfig.bs_dependencies);
let bs_dev_dependencies =
deps_to_pkgs(&buildstate.packages, &package.bsconfig.bs_dev_dependencies);

let mut pkgs = AHashMap::new();
pkgs.extend(pinned_dependencies);
pkgs.extend(bs_dependencies);
pkgs.extend(bs_dev_dependencies);

let name = path + "/.sourcedirs.json";
let _ = File::create(&name).map(|mut file| {
let source_files = SourceDirs {
dirs: dirs.clone().into_iter().collect::<Vec<Dir>>(),
pkgs: pkgs
.clone()
.into_iter()
.collect::<Vec<(PackageName, AbsolutePath)>>(),
generated: vec![],
};

file.write(json!(source_files).to_string().as_bytes())
});
let _ = std::fs::copy(package.get_bs_build_path(), package.get_build_path());

(&package.path, dirs, pkgs)
})
.collect::<Vec<(
&PackagePath,
AHashSet<String>,
AHashMap<PackageName, AbsolutePath>,
)>>();

let mut all_dirs = AHashSet::new();
let mut all_pkgs: AHashMap<PackageName, AbsolutePath> = AHashMap::new();

child_packages.iter().for_each(|(package_path, dirs, pkgs)| {
dirs.iter().for_each(|dir| {
all_dirs.insert(format!("{package_path}/{dir}"));
});

all_pkgs.extend(pkgs.to_owned());
});

let path = package.get_bs_build_path();
let name = path + "/.sourcedirs.json";
let _ = File::create(name.clone()).map(|mut file| {
let all_source_files = SourceDirs {
dirs: all_dirs.into_iter().collect::<Vec<String>>(),
pkgs: all_pkgs.into_iter().collect::<Vec<(PackageName, AbsolutePath)>>(),
generated: vec![],
};
file.write(json!(all_source_files).to_string().as_bytes())
});

let _ = std::fs::copy(package.get_bs_build_path(), package.get_build_path());
}

0 comments on commit 85560bb

Please sign in to comment.