Skip to content

Commit

Permalink
[WIP] start work on lint terminal output
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed Oct 8, 2024
1 parent 664b087 commit e57dbd6
Show file tree
Hide file tree
Showing 5 changed files with 420 additions and 375 deletions.
64 changes: 35 additions & 29 deletions src/dependency.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use std::{cell::RefCell, cmp::Ordering, rc::Rc, vec};
use std::{
cell::RefCell,
cmp::Ordering,
collections::{BTreeMap, HashSet},
rc::Rc,
vec,
};

use crate::{
instance::Instance,
instance::{Instance, InstanceState},
package_json::PackageJson,
specifier::{orderable::IsOrderable, semver::Semver, simple_semver::SimpleSemver, Specifier},
version_group::VersionGroupVariant,
};

#[derive(Debug, Eq, PartialEq)]
pub enum DependencyState {
Valid,
Suspect,
Invalid,
}

#[derive(Debug)]
pub struct Dependency {
/// The expected version specifier which all instances of this dependency
Expand All @@ -29,8 +28,6 @@ pub struct Dependency {
pub pinned_specifier: Option<Specifier>,
/// package.json files developed in the monorepo when variant is `SnappedTo`
pub snapped_to_packages: Option<Vec<Rc<RefCell<PackageJson>>>>,
/// The state of whether this dependency is valid, suspect, or invalid
pub state: RefCell<DependencyState>,
/// What behaviour has this group been configured to exhibit?
pub variant: VersionGroupVariant,
}
Expand All @@ -49,34 +46,43 @@ impl Dependency {
name,
pinned_specifier,
snapped_to_packages,
state: RefCell::new(DependencyState::Valid),
variant,
}
}

pub fn has_state(&self, state: DependencyState) -> bool {
*self.state.borrow() == state
pub fn add_instance(&self, instance: Rc<Instance>) {
self.instances.borrow_mut().push(Rc::clone(&instance));
if instance.is_local {
*self.local_instance.borrow_mut() = Some(Rc::clone(&instance));
}
}

pub fn set_state(&self, state: DependencyState) -> &Self {
fn get_severity(state: &DependencyState) -> i32 {
match state {
DependencyState::Valid => 0,
DependencyState::Suspect => 1,
DependencyState::Invalid => 2,
}
}
if get_severity(&state) > get_severity(&self.state.borrow()) {
*self.state.borrow_mut() = state;
}
pub fn get_unique_specifiers(&self) -> Vec<Specifier> {
let set: HashSet<Specifier> = self
.instances
.borrow()
.iter()
.map(|instance| instance.actual_specifier.clone())
.collect();
set.into_iter().collect()
}

/// Return the most severe state of all instances in this group
pub fn get_state(&self) -> InstanceState {
self
.instances
.borrow()
.iter()
.fold(InstanceState::Unknown, |acc, instance| acc.max(instance.state.borrow().clone()))
}

pub fn add_instance(&self, instance: Rc<Instance>) {
self.instances.borrow_mut().push(Rc::clone(&instance));
if instance.is_local {
*self.local_instance.borrow_mut() = Some(Rc::clone(&instance));
pub fn get_instances_by_specifier(&self) -> BTreeMap<String, Vec<Rc<Instance>>> {
let mut map = BTreeMap::new();
for instance in self.instances.borrow().iter() {
let specifier = instance.actual_specifier.unwrap();
map.entry(specifier).or_insert_with(Vec::new).push(Rc::clone(instance));
}
map
}

pub fn set_expected_specifier(&self, specifier: &Specifier) -> &Self {
Expand Down
31 changes: 22 additions & 9 deletions src/effects/lint.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,58 @@
use colored::*;
use log::info;

use crate::{context::Context, effects::ui, version_group::VersionGroupVariant};
use crate::{context::Context, effects::ui::Ui, version_group::VersionGroupVariant};

/// Run the lint command side effects
pub fn run(ctx: Context) -> Context {
// @TODO: move values to config file
let ui = Ui {
show_ignored: true,
show_instances: true,
show_status_codes: true,
};

if ctx.config.cli.options.versions {
info!("{}", "= SEMVER RANGES AND VERSION MISMATCHES".dimmed());
ctx.version_groups.iter().for_each(|group| {
ui::group_header(group);
ui.print_group_header(group);
group.dependencies.borrow().values().for_each(|dependency| {
ui::dependency_header(dependency);
dependency.sort_instances();
match dependency.variant {
VersionGroupVariant::Banned => {
ui.print_dependency_header(dependency);
dependency.instances.borrow().iter().for_each(|instance| {
println!(" {}", ui::status_code_link(&instance.state.borrow()));
ui.print_instance_link(instance);
});
}
VersionGroupVariant::HighestSemver | VersionGroupVariant::LowestSemver => {
ui.print_dependency_header(dependency);
dependency.instances.borrow().iter().for_each(|instance| {
println!(" {}", ui::status_code_link(&instance.state.borrow()));
ui.print_instance_link(instance);
});
}
VersionGroupVariant::Ignored => {
ui.print_dependency_header(dependency);
dependency.instances.borrow().iter().for_each(|instance| {
println!(" {}", ui::status_code_link(&instance.state.borrow()));
ui.print_instance_link(instance);
});
}
VersionGroupVariant::Pinned => {
ui.print_dependency_header(dependency);
dependency.instances.borrow().iter().for_each(|instance| {
println!(" {}", ui::status_code_link(&instance.state.borrow()));
ui.print_instance_link(instance);
});
}
VersionGroupVariant::SameRange => {
ui.print_dependency_header(dependency);
dependency.instances.borrow().iter().for_each(|instance| {
println!(" {}", ui::status_code_link(&instance.state.borrow()));
ui.print_instance_link(instance);
});
}
VersionGroupVariant::SnappedTo => {
ui.print_dependency_header(dependency);
dependency.instances.borrow().iter().for_each(|instance| {
println!(" {}", ui::status_code_link(&instance.state.borrow()));
ui.print_instance_link(instance);
});
}
}
Expand Down
Loading

0 comments on commit e57dbd6

Please sign in to comment.