-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
What? ===== This introduces a 'doctor' subcommand which runs a small set of checks to provide context to the developer about the status of the codebase and analysis. This includes information like whether the tags file generated was generated via Universal Ctags, how many tokens and files are being processed, and the types of projects made available from configuration.
- Loading branch information
1 parent
9ce77a3
commit c7ca2db
Showing
17 changed files
with
343 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
mod check_up; | ||
mod files_count; | ||
mod loaded_project_configurations; | ||
mod tags_included_in_files_searched; | ||
mod tokens_count; | ||
mod using_universal_ctags; | ||
|
||
use super::doctor::{ | ||
check_up::*, files_count::*, loaded_project_configurations::*, | ||
tags_included_in_files_searched::*, tokens_count::*, using_universal_ctags::*, | ||
}; | ||
use colored::*; | ||
|
||
pub struct Doctor { | ||
checks: Vec<Box<dyn CheckUp>>, | ||
} | ||
|
||
impl Doctor { | ||
pub fn new() -> Self { | ||
Self { | ||
checks: vec![ | ||
Box::new(IncludingTagsInFilesSearched::new()), | ||
Box::new(TokensCount::new()), | ||
Box::new(FilesCount::new()), | ||
Box::new(UsingUniversalCtags::new()), | ||
Box::new(LoadedProjectConfigurations::new()), | ||
], | ||
} | ||
} | ||
|
||
pub fn render(&self) { | ||
println!("Unused Doctor"); | ||
println!(""); | ||
|
||
let mut oks = 0; | ||
let mut warnings = 0; | ||
let mut errors = 0; | ||
|
||
for check in self.checks.iter() { | ||
match check.status() { | ||
Status::OK(_) => oks += 1, | ||
Status::Warn(_) => warnings += 1, | ||
Status::Error(_) => errors += 1, | ||
} | ||
|
||
Self::render_check_up(check) | ||
} | ||
|
||
println!(""); | ||
println!( | ||
"{}: {}, {}, {}", | ||
Self::colorized_outcome(warnings, errors), | ||
format!("{} OK", oks).green(), | ||
format!("{} warnings", warnings).yellow(), | ||
format!("{} errors", errors).red(), | ||
); | ||
} | ||
|
||
fn colorized_outcome(warnings: u16, errors: u16) -> colored::ColoredString { | ||
if errors > 0 { | ||
"Outcome".red() | ||
} else { | ||
if warnings > 0 { | ||
"Outcome".yellow() | ||
} else { | ||
"Outcome".green() | ||
} | ||
} | ||
} | ||
|
||
fn render_check_up(check_up: &Box<dyn CheckUp>) { | ||
match check_up.status() { | ||
Status::OK(message) => Self::render_status("OK".green(), check_up.name(), message), | ||
Status::Warn(message) => { | ||
Self::render_status("Warning".yellow(), check_up.name(), message) | ||
} | ||
Status::Error(message) => Self::render_status("Error".red(), check_up.name(), message), | ||
} | ||
} | ||
|
||
fn render_status(status: colored::ColoredString, name: &str, message: String) { | ||
print!("[{}] ", status); | ||
println!("Check: {}", name.cyan()); | ||
println!(" {}", message.yellow()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
pub enum Status { | ||
OK(String), | ||
Warn(String), | ||
Error(String), | ||
} | ||
|
||
pub trait CheckUp { | ||
fn name(&self) -> &str; | ||
fn status(&self) -> Status; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use super::check_up::{CheckUp, Status}; | ||
use codebase_files::CodebaseFiles; | ||
|
||
pub struct FilesCount(usize); | ||
|
||
impl FilesCount { | ||
pub fn new() -> Self { | ||
let file_paths = CodebaseFiles::all().paths; | ||
Self(file_paths.len()) | ||
} | ||
} | ||
|
||
impl CheckUp for FilesCount { | ||
fn name(&self) -> &str { | ||
"Are files found in the application?" | ||
} | ||
|
||
fn status(&self) -> Status { | ||
let message = format!("{} file(s) found", self.0); | ||
if self.0 == 0 { | ||
Status::Warn(message) | ||
} else { | ||
Status::OK(message) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use super::{ | ||
super::project_configurations_loader::load_and_parse_config, | ||
check_up::{CheckUp, Status}, | ||
}; | ||
use project_configuration::ProjectConfigurations; | ||
|
||
pub struct LoadedProjectConfigurations(ProjectConfigurations); | ||
|
||
impl LoadedProjectConfigurations { | ||
pub fn new() -> Self { | ||
LoadedProjectConfigurations(load_and_parse_config()) | ||
} | ||
|
||
fn config_keys(&self) -> Vec<String> { | ||
self.0.project_config_names() | ||
} | ||
} | ||
|
||
impl CheckUp for LoadedProjectConfigurations { | ||
fn name(&self) -> &str { | ||
"Does the loaded configuration have available project types?" | ||
} | ||
|
||
fn status(&self) -> Status { | ||
if self.config_keys().is_empty() { | ||
Status::Warn( | ||
"No project configurations were loaded; using default config instead.".to_string(), | ||
) | ||
} else { | ||
Status::OK(format!( | ||
"Loaded the following project configurations: {}", | ||
self.config_keys().join(", ") | ||
)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use super::check_up::{CheckUp, Status}; | ||
use codebase_files::CodebaseFiles; | ||
use std::path::PathBuf; | ||
use token_search::Token; | ||
|
||
pub enum IncludingTagsInFilesSearched { | ||
Success { | ||
ctags_path: PathBuf, | ||
files_searched: Vec<PathBuf>, | ||
}, | ||
Failure(String), | ||
} | ||
|
||
impl IncludingTagsInFilesSearched { | ||
pub fn new() -> Self { | ||
match Token::all() { | ||
Ok((ctags_path, _)) => IncludingTagsInFilesSearched::Success { | ||
files_searched: CodebaseFiles::all().paths, | ||
ctags_path, | ||
}, | ||
Err(e) => IncludingTagsInFilesSearched::Failure(format!("{}", e)), | ||
} | ||
} | ||
|
||
fn tags_searched(&self) -> Result<(&PathBuf, bool), String> { | ||
match &self { | ||
Self::Success { | ||
files_searched, | ||
ctags_path, | ||
} => Ok((ctags_path, files_searched.iter().any(|v| v == ctags_path))), | ||
Self::Failure(e) => Err(e.to_string()), | ||
} | ||
} | ||
} | ||
|
||
impl CheckUp for IncludingTagsInFilesSearched { | ||
fn name(&self) -> &str { | ||
"Is the tags file not present in the list of files searched?" | ||
} | ||
|
||
fn status(&self) -> Status { | ||
match self.tags_searched() { | ||
Ok((ctags_path, true)) => Status::Warn(format!( | ||
"The tags file loaded ({:?}) is present in the list of files searched", | ||
ctags_path | ||
)), | ||
Ok((ctags_path, false)) => Status::OK(format!( | ||
"The tags file loaded ({:?}) is not present in the list of files searched", | ||
ctags_path | ||
)), | ||
Err(e) => Status::Error(e), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use super::check_up::{CheckUp, Status}; | ||
use token_search::Token; | ||
|
||
pub enum TokensCount { | ||
Success(usize), | ||
Failure(String), | ||
} | ||
|
||
impl TokensCount { | ||
pub fn new() -> Self { | ||
match Token::all() { | ||
Ok((_, results)) => Self::Success(results.len()), | ||
Err(e) => Self::Failure(format!("{}", e)), | ||
} | ||
} | ||
} | ||
|
||
impl CheckUp for TokensCount { | ||
fn name(&self) -> &str { | ||
"Are tokens found in the application?" | ||
} | ||
|
||
fn status(&self) -> Status { | ||
match &self { | ||
Self::Success(ct) => { | ||
let message = format!("{} token(s) found", ct); | ||
if ct < &5 { | ||
Status::Warn(message) | ||
} else { | ||
Status::OK(message) | ||
} | ||
} | ||
Self::Failure(e) => Status::Error(e.to_string()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use super::check_up::{CheckUp, Status}; | ||
use read_ctags::TagsReader; | ||
|
||
pub struct UsingUniversalCtags(Option<String>); | ||
|
||
impl UsingUniversalCtags { | ||
pub fn new() -> Self { | ||
match TagsReader::default().load() { | ||
Ok(outcome) => Self(outcome.program.name), | ||
Err(_) => Self(None), | ||
} | ||
} | ||
} | ||
|
||
impl CheckUp for UsingUniversalCtags { | ||
fn name(&self) -> &str { | ||
"Is the tags file generated with Universal Ctags?" | ||
} | ||
|
||
fn status(&self) -> Status { | ||
match &self.0 { | ||
None => Status::Error("Could not determine tags program name".to_string()), | ||
Some(v) => { | ||
let message = format!("Using tags program: {}", v); | ||
if v.contains("Universal Ctags") { | ||
Status::OK(message) | ||
} else { | ||
Status::Warn(message) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.