mirror of
https://github.com/Noratrieb/cargo-minimize.git
synced 2026-01-15 00:45:02 +01:00
ctrlc
This commit is contained in:
parent
729deb5110
commit
961ba86e59
6 changed files with 129 additions and 27 deletions
12
src/lib.rs
12
src/lib.rs
|
|
@ -1,7 +1,11 @@
|
|||
#[macro_use]
|
||||
extern crate tracing;
|
||||
|
||||
use std::{path::PathBuf, str::FromStr};
|
||||
use std::{
|
||||
path::PathBuf,
|
||||
str::FromStr,
|
||||
sync::{atomic::AtomicBool, Arc},
|
||||
};
|
||||
|
||||
mod build;
|
||||
mod dylib_flag;
|
||||
|
|
@ -94,7 +98,7 @@ pub struct Options {
|
|||
|
||||
/// Do not touch the following files.
|
||||
#[arg(long)]
|
||||
pub ignore_file: Vec<PathBuf>
|
||||
pub ignore_file: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
@ -119,7 +123,7 @@ impl FromStr for EnvVar {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn minimize(options: Options) -> Result<()> {
|
||||
pub fn minimize(options: Options, stop: Arc<AtomicBool>) -> Result<()> {
|
||||
for ignore_file in &options.ignore_file {
|
||||
if !ignore_file.try_exists()? {
|
||||
warn!("Ignored path {} does not exist", ignore_file.display());
|
||||
|
|
@ -128,7 +132,7 @@ pub fn minimize(options: Options) -> Result<()> {
|
|||
|
||||
let build = build::Build::new(&options)?;
|
||||
|
||||
let mut minimizer = Minimizer::new_glob_dir(options, build)?;
|
||||
let mut minimizer = Minimizer::new_glob_dir(options, build, stop)?;
|
||||
|
||||
minimizer.run_passes([
|
||||
passes::Privatize::default().boxed(),
|
||||
|
|
|
|||
30
src/main.rs
30
src/main.rs
|
|
@ -1,3 +1,11 @@
|
|||
#[macro_use]
|
||||
extern crate tracing;
|
||||
|
||||
use std::sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
};
|
||||
|
||||
use cargo_minimize::{Cargo, Parser};
|
||||
use tracing::{error, Level};
|
||||
|
||||
|
|
@ -6,7 +14,27 @@ fn main() {
|
|||
|
||||
cargo_minimize::init_recommended_tracing_subscriber(Level::INFO);
|
||||
|
||||
if let Err(err) = cargo_minimize::minimize(options) {
|
||||
let cancel = Arc::new(AtomicBool::new(false));
|
||||
let cancel2 = Arc::clone(&cancel);
|
||||
|
||||
let mut ctrl_c_pressed = false;
|
||||
let result = ctrlc::set_handler(move || {
|
||||
// If ctrl c was pressed already, kill it now.
|
||||
if ctrl_c_pressed {
|
||||
warn!("Process killed");
|
||||
std::process::exit(130);
|
||||
}
|
||||
|
||||
warn!("Shutting down gracefully, press CTRL-C again to kill");
|
||||
cancel.store(true, Ordering::SeqCst);
|
||||
ctrl_c_pressed = true;
|
||||
});
|
||||
|
||||
if let Err(err) = result {
|
||||
error!("Failed to install CTRL-C handler: {err}");
|
||||
}
|
||||
|
||||
if let Err(err) = cargo_minimize::minimize(options, cancel2) {
|
||||
error!("An error occured:\n{err}");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,9 @@ pub(crate) use self::files::SourceFile;
|
|||
use crate::{build::Build, processor::files::Changes, Options};
|
||||
use anyhow::{bail, Context, Result};
|
||||
use owo_colors::OwoColorize;
|
||||
use std::{collections::HashSet, ffi::OsStr, fmt::Debug};
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::Arc;
|
||||
use std::{collections::HashSet, ffi::OsStr, fmt::Debug, sync::atomic::AtomicBool};
|
||||
|
||||
pub(crate) use self::checker::PassController;
|
||||
|
||||
|
|
@ -53,6 +55,7 @@ pub(crate) struct Minimizer {
|
|||
files: Vec<SourceFile>,
|
||||
build: Build,
|
||||
options: Options,
|
||||
cancel: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl Minimizer {
|
||||
|
|
@ -65,7 +68,11 @@ impl Minimizer {
|
|||
false
|
||||
}
|
||||
|
||||
pub(crate) fn new_glob_dir(options: Options, build: Build) -> Result<Self> {
|
||||
pub(crate) fn new_glob_dir(
|
||||
options: Options,
|
||||
build: Build,
|
||||
cancel: Arc<AtomicBool>,
|
||||
) -> Result<Self> {
|
||||
let path = &options.path;
|
||||
let walk = walkdir::WalkDir::new(path);
|
||||
|
||||
|
|
@ -111,6 +118,7 @@ impl Minimizer {
|
|||
files,
|
||||
build,
|
||||
options,
|
||||
cancel,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -216,6 +224,11 @@ impl Minimizer {
|
|||
}
|
||||
}
|
||||
|
||||
if self.cancel.load(Ordering::SeqCst) {
|
||||
info!("Exiting early.");
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
if checker.is_finished() {
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue