Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Reanalyze #60

Merged
merged 3 commits into from
Apr 29, 2024
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: 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
114 changes: 114 additions & 0 deletions src/sourcedirs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
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::*;
use std::path::PathBuf;

type Dir = String;
type PackageName = String;
type AbsolutePath = String;
type Pkg = (PackageName, AbsolutePath);

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

fn package_to_dirs<'a>(package: &'a Package, root_package_path: &String) -> AHashSet<Dir> {
let relative_path = PathBuf::from(&package.path)
.strip_prefix(PathBuf::from(&root_package_path))
.unwrap()
.to_string_lossy()
.to_string();

package
.dirs
.as_ref()
.unwrap_or(&AHashSet::new())
.iter()
.filter_map(|path| path.to_str().map(|path| format!("{relative_path}/{path}")))
.collect::<AHashSet<String>>()
}

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

fn write_sourcedirs_files(path: String, source_dirs: &SourceDirs) -> Result<usize, std::io::Error> {
let mut source_dirs_json = File::create(path + "/.sourcedirs.json")?;
source_dirs_json.write(json!(source_dirs).to_string().as_bytes())
}

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

// Take all packages apart from the root package
let (dirs, pkgs): (Vec<AHashSet<Dir>>, Vec<AHashMap<PackageName, AbsolutePath>>) = buildstate
.packages
.par_iter()
.filter(|(_name, package)| !package.is_root)
.map(|(_name, package)| {
// Extract Directories
let dirs = package_to_dirs(&package, &root_package.path);

// Extract Pkgs
let pkgs = [
&package.bsconfig.pinned_dependencies,
&package.bsconfig.bs_dependencies,
&package.bsconfig.bs_dev_dependencies,
]
.into_iter()
.map(|dependencies| deps_to_pkgs(&buildstate.packages, dependencies));

// Write sourcedirs.json
write_sourcedirs_files(
package.get_build_path(),
&SourceDirs {
dirs: &dirs.clone().into_iter().collect::<Vec<Dir>>(),
pkgs: &pkgs.clone().flatten().collect::<Vec<Pkg>>(),
generated: &vec![],
},
)
.expect("Could not write sourcedirs.json");

(
dirs,
pkgs.flatten().collect::<AHashMap<PackageName, AbsolutePath>>(),
)
})
.unzip();

// Write sourcedirs.json
write_sourcedirs_files(
root_package.get_bs_build_path(),
&SourceDirs {
dirs: &dirs.into_iter().flatten().collect::<Vec<Dir>>(),
pkgs: &pkgs.into_iter().flatten().collect::<Vec<Pkg>>(),
generated: &vec![],
},
)
.expect("Could not write sourcedirs.json");
}
Loading