Skip to content

Commit

Permalink
feat: ignore header path if present (#141)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun authored Mar 31, 2024
1 parent ee20c4b commit 5547176
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 29 deletions.
67 changes: 45 additions & 22 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::path::{Path, PathBuf};

use clap::Parser;
use clap::{Args, Parser};
use hawkeye_fmt::{
document::Document,
header::matcher::HeaderMatcher,
Expand All @@ -33,6 +33,14 @@ pub enum SubCommand {
Remove(CommandRemove),
}

#[derive(Args)]
struct SharedOptions {
#[arg(long, help = "path to the config file")]
config: Option<PathBuf>,
#[arg(long, help = "fail if process unknown files", default_value_t = false)]
fail_if_unknown: bool,
}

impl SubCommand {
pub fn run(self) -> Result<()> {
match self {
Expand All @@ -45,8 +53,8 @@ impl SubCommand {

#[derive(Parser)]
pub struct CommandCheck {
#[arg(long, help = "path to the config file")]
pub config: Option<PathBuf>,
#[command(flatten)]
shared: SharedOptions,
}

struct CheckContext {
Expand All @@ -72,18 +80,19 @@ impl Callback for CheckContext {

impl CommandCheck {
fn run(self) -> Result<()> {
let config = self.config.unwrap_or_else(default_config);
let config = self.shared.config.unwrap_or_else(default_config);
let mut context = CheckContext {
unknown: vec![],
missing: vec![],
};
check_license_header(config, &mut context)?;
if !context.unknown.is_empty() {
warn!("Processing unknown files: {:?}", context.unknown);
}
let mut exit_code = check_unknown_files(context.unknown, self.shared.fail_if_unknown);
if !context.missing.is_empty() {
error!("Found missing header files: {:?}", context.missing);
std::process::exit(1);
exit_code = 1;
}
if exit_code != 0 {
std::process::exit(exit_code);
}
info!("No missing header file has been found.");
Ok(())
Expand All @@ -92,8 +101,8 @@ impl CommandCheck {

#[derive(Parser)]
pub struct CommandFormat {
#[arg(long, help = "path to the config file")]
pub config: Option<PathBuf>,
#[command(flatten)]
shared: SharedOptions,

#[arg(long, help = "whether update file in place", default_value_t = false)]
pub dry_run: bool,
Expand Down Expand Up @@ -139,22 +148,23 @@ impl Callback for FormatContext {

impl CommandFormat {
fn run(self) -> Result<()> {
let config = self.config.unwrap_or_else(default_config);
let config = self.shared.config.unwrap_or_else(default_config);
let mut context = FormatContext {
dry_run: self.dry_run,
unknown: vec![],
updated: vec![],
};
check_license_header(config, &mut context)?;
if !context.unknown.is_empty() {
warn!("Processing unknown files: {:?}", context.unknown);
}
let mut exit_code = check_unknown_files(context.unknown, self.shared.fail_if_unknown);
if !context.updated.is_empty() {
error!(
"Updated header for files (dryRun={}): {:?}",
self.dry_run, context.updated
);
std::process::exit(1);
exit_code = 1;
}
if exit_code != 0 {
std::process::exit(exit_code);
}
info!("All files have proper header.");
Ok(())
Expand All @@ -163,8 +173,8 @@ impl CommandFormat {

#[derive(Parser)]
pub struct CommandRemove {
#[arg(long, help = "path to the config file")]
pub config: Option<PathBuf>,
#[command(flatten)]
shared: SharedOptions,

#[arg(long, help = "whether update file in place", default_value_t = false)]
pub dry_run: bool,
Expand Down Expand Up @@ -212,28 +222,41 @@ impl Callback for RemoveContext {

impl CommandRemove {
fn run(self) -> Result<()> {
let config = self.config.unwrap_or_else(default_config);
let config = self.shared.config.unwrap_or_else(default_config);
let mut context = RemoveContext {
dry_run: self.dry_run,
unknown: vec![],
removed: vec![],
};
check_license_header(config, &mut context)?;
if !context.unknown.is_empty() {
warn!("Processing unknown files: {:?}", context.unknown);
}
let mut exit_code = check_unknown_files(context.unknown, self.shared.fail_if_unknown);
if !context.removed.is_empty() {
error!(
"Removed header for files (dryRun={}): {:?}",
self.dry_run, context.removed
);
std::process::exit(1);
exit_code = 1;
}
if exit_code != 0 {
std::process::exit(exit_code);
}
info!("No file has been removed header.");
Ok(())
}
}

fn check_unknown_files(unknown: Vec<String>, fail_if_unknown: bool) -> i32 {
if !unknown.is_empty() {
if fail_if_unknown {
error!("Processing unknown files: {:?}", unknown);
return 1;
} else {
warn!("Processing unknown files: {:?}", unknown);
}
}
0
}

fn default_config() -> PathBuf {
PathBuf::new().join("licenserc.toml")
}
1 change: 1 addition & 0 deletions fmt/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub fn check_license_header<C: Callback>(run_config: PathBuf, callback: &mut C)
let selected_files = {
let selection = Selection::new(
basedir,
config.header_path.as_ref(),
&config.includes,
&config.excludes,
config.use_default_excludes,
Expand Down
16 changes: 10 additions & 6 deletions fmt/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct Selection {
impl Selection {
pub fn new(
basedir: PathBuf,
header_path: Option<&String>,
includes: &[String],
excludes: &[String],
use_default_excludes: bool,
Expand All @@ -50,12 +51,15 @@ impl Selection {
includes.to_vec()
};

let used_default_excludes = if use_default_excludes {
EXCLUDES.iter().map(|s| s.to_string()).collect()
} else {
vec![]
};
let excludes = [used_default_excludes, excludes.to_vec()].concat();
let input_excludes = excludes;
let mut excludes = vec![];
if let Some(path) = header_path.cloned() {
excludes.push(path);
}
if use_default_excludes {
excludes.extend(EXCLUDES.iter().map(ToString::to_string));
}
excludes.extend(input_excludes.to_vec());

Selection {
basedir,
Expand Down
2 changes: 1 addition & 1 deletion tests/it.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@

subprocess.run(["cargo", "build", "--bin", "hawkeye"], cwd=rootdir, check=True)
hawkeye = rootdir / "target" / "debug" / "hawkeye"
subprocess.run([hawkeye, "check"], cwd=(basedir / "load_header_path"), check=True)
subprocess.run([hawkeye, "check", "--fail-if-unknown"], cwd=(basedir / "load_header_path"), check=True)

0 comments on commit 5547176

Please sign in to comment.