Skip to content

Commit

Permalink
code organization
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed May 16, 2023
1 parent 7fba87b commit 665af9e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 38 deletions.
1 change: 1 addition & 0 deletions crates/turbopack-cli-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ bench = false
anyhow = { workspace = true }
clap = { workspace = true, features = ["derive"] }
crossterm = "0.26.0"
ctrlc = "3.2.5"
owo-colors = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
Expand Down
24 changes: 24 additions & 0 deletions crates/turbopack-cli-utils/src/exit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::sync::{Arc, Mutex};

use anyhow::{Context, Result};

pub struct CloseGuard<T>(Arc<Mutex<Option<T>>>);

impl<T> Drop for CloseGuard<T> {
fn drop(&mut self) {
drop(self.0.lock().unwrap().take())
}
}

pub fn exit_guard<T: Send + 'static>(guard: T) -> Result<CloseGuard<T>> {
let guard = Arc::new(Mutex::new(Some(guard)));
{
let guard = guard.clone();
ctrlc::set_handler(move || {
drop(guard.lock().unwrap().take());
std::process::exit(0);
})
.context("Unable to set ctrl-c handler")?;
}
Ok(CloseGuard(guard))
}
1 change: 1 addition & 0 deletions crates/turbopack-cli-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![feature(round_char_boundary)]
#![feature(thread_id_value)]

pub mod exit;
pub mod issue;
pub mod raw_trace;
pub mod runtime_entry;
Expand Down
1 change: 0 additions & 1 deletion crates/turbopack-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ anyhow = { workspace = true, features = ["backtrace"] }
clap = { workspace = true, features = ["derive", "env"] }
console-subscriber = { workspace = true, optional = true }
criterion = { workspace = true, features = ["async_tokio"] }
ctrlc = "3.2.5"
dunce = { workspace = true }
futures = { workspace = true }
mime = { workspace = true }
Expand Down
13 changes: 12 additions & 1 deletion crates/turbopack-cli/src/arguments.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{net::IpAddr, path::PathBuf};
use std::{
net::IpAddr,
path::{Path, PathBuf},
};

use clap::{Args, Parser};
use turbopack_cli_utils::issue::IssueSeverityCliOption;
Expand All @@ -9,6 +12,14 @@ pub enum Arguments {
Dev(DevArguments),
}

impl Arguments {
pub fn dir(&self) -> Option<&Path> {
match self {
Arguments::Dev(args) => args.common.dir.as_deref(),
}
}
}

#[derive(Debug, Args)]
pub struct CommonArguments {
/// The directory of the application.
Expand Down
50 changes: 14 additions & 36 deletions crates/turbopack-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,22 @@
#![feature(future_join)]
#![feature(min_specialization)]

use std::{
borrow::Cow,
sync::{Arc, Mutex},
};
use std::{borrow::Cow, path::Path};

use anyhow::{Context, Result};
use clap::Parser;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
use turbopack_cli::{arguments::Arguments, register};
use turbopack_cli_utils::raw_trace::RawTraceLayer;
use turbopack_cli_utils::{exit::exit_guard, raw_trace::RawTraceLayer};

#[global_allocator]
static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc;

struct CloseGuard<T>(Arc<Mutex<Option<T>>>);

impl<T> Drop for CloseGuard<T> {
fn drop(&mut self) {
drop(self.0.lock().unwrap().take())
}
}

fn close_guard<T: Send + 'static>(guard: T) -> Result<CloseGuard<T>> {
let guard = Arc::new(Mutex::new(Some(guard)));
{
let guard = guard.clone();
ctrlc::set_handler(move || {
println!("Flushing trace file... (ctrl-c)");
drop(guard.lock().unwrap().take());
println!("Flushed trace file");
std::process::exit(0);
})
.context("Unable to set ctrl-c handler")?;
}
Ok(CloseGuard(guard))
}

fn main() {
use turbo_tasks_malloc::TurboMalloc;

let args = Arguments::parse();

let subscriber = Registry::default();

let subscriber = subscriber.with(
Expand All @@ -62,14 +38,19 @@ fn main() {
.unwrap(),
);

std::fs::create_dir_all("./.turbopack")
let internal_dir = args
.dir()
.unwrap_or_else(|| Path::new("."))
.join(".turbopack");
std::fs::create_dir_all(&internal_dir)
.context("Unable to create .turbopack directory")
.unwrap();
let trace_file = internal_dir.join("trace.log");
let (writer, guard) =
tracing_appender::non_blocking(std::fs::File::create("./.turbopack/trace.log").unwrap());
tracing_appender::non_blocking(std::fs::File::create(trace_file).unwrap());
let subscriber = subscriber.with(RawTraceLayer::new(writer));

let guard = close_guard(guard).unwrap();
let guard = exit_guard(guard).unwrap();

subscriber.init();

Expand All @@ -80,17 +61,14 @@ fn main() {
})
.build()
.unwrap()
.block_on(main_inner())
.block_on(main_inner(args))
.unwrap();

println!("Flushing trace file...");
drop(guard);
println!("Flushed trace file");
}

async fn main_inner() -> Result<()> {
async fn main_inner(args: Arguments) -> Result<()> {
register();
let args = Arguments::parse();

match args {
Arguments::Dev(args) => turbopack_cli::dev::start_server(&args).await,
Expand Down

0 comments on commit 665af9e

Please sign in to comment.