Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use log crate for cargo-vita output #7

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ clap = { version = "4.4.2", features = ["derive", "env"] }
colored = "2.0.4"
either = "1.9.0"
enum_dispatch = "0.3.12"
env_logger = "0.10.0"
log = "0.4.20"
rustc_version = "0.4.0"
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.105"
Expand Down
6 changes: 3 additions & 3 deletions src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use std::{
};

use anyhow::{anyhow, Context};
use log::error;
use rustc_version::Channel;

pub fn check_rust_version() {
let rust_version = rustc_version::version_meta().unwrap();

if rust_version.channel > Channel::Nightly {
eprintln!("cargo-vita requires a nightly rustc version.\n");
eprintln!(
"Do one of the following:\n \
error!(
"cargo-vita requires a nightly rustc version. Do one of the following:\n \
- Run `rustup override set nightly` to use nightly in the current directory\n \
- Run cargo with +nightly flag.\n \
- Create a rust-toolchain.toml in the root with the following content:\n \
Expand Down
52 changes: 17 additions & 35 deletions src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use cargo_metadata::{camino::Utf8PathBuf, Artifact, Message, Package};
use clap::{Args, Subcommand};
use colored::Colorize;
use either::Either;
use log::info;
use tee::TeeReader;
use walkdir::WalkDir;

Expand Down Expand Up @@ -75,12 +76,10 @@ struct Vpk {
struct BuildContext<'a> {
command: &'a Build,
sdk: String,

verbose: u8,
}

impl<'a> BuildContext<'a> {
pub fn new(command: &'a Build, verbose: u8) -> anyhow::Result<Self> {
pub fn new(command: &'a Build) -> anyhow::Result<Self> {
let sdk = std::env::var("VITASDK");
let sdk = sdk.or_else(|_| {
bail!(
Expand All @@ -89,11 +88,7 @@ impl<'a> BuildContext<'a> {
)
})?;

Ok(Self {
command,
sdk,
verbose,
})
Ok(Self { command, sdk })
}

fn sdk(&self, path: &str) -> PathBuf {
Expand Down Expand Up @@ -135,8 +130,8 @@ impl ExecutableArtifact {
}

impl Executor for Build {
fn execute(&self, verbose: u8) -> anyhow::Result<()> {
let ctx = BuildContext::new(self, verbose)?;
fn execute(&self) -> anyhow::Result<()> {
let ctx = BuildContext::new(self)?;

match &self.cmd {
BuildCmd::Elf => {
Expand Down Expand Up @@ -221,7 +216,8 @@ impl<'a> BuildContext<'a> {
command.env("PATH", path);
}

// FIXME: A horrible solution, the same -Z flag will be used for all of the crates in a workspace.
// FIXME: move build-std to env/config.toml, since it is shared by all of the crates built
// This still works correctly when building only a single workspace crate though
let (meta, _, _) = parse_crate_metadata(None)?;

command
Expand Down Expand Up @@ -249,14 +245,12 @@ impl<'a> BuildContext<'a> {
.stdout(Stdio::piped())
.stderr(Stdio::inherit());

if self.verbose > 0 {
println!("{} {command:?}", "Running cargo:".blue());
}
info!("{}: {command:?}", "Running cargo".blue());

let mut process = command.spawn().context("Unable to spawn build process")?;
let command_stdout = process.stdout.take().context("Build failed")?;

let reader = if self.verbose > 1 {
let reader = if log::max_level() >= log::LevelFilter::Trace {
Either::Left(BufReader::new(TeeReader::new(command_stdout, io::stdout())))
} else {
Either::Right(BufReader::new(command_stdout))
Expand Down Expand Up @@ -293,9 +287,7 @@ impl<'a> BuildContext<'a> {
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());

if self.verbose > 0 {
println!("{} {command:?}", "Stripping elf:".blue());
}
info!("{}: {command:?}", "Stripping elf".blue());

if !command.status()?.success() {
bail!("arm-vita-eabi-strip failed");
Expand All @@ -316,9 +308,7 @@ impl<'a> BuildContext<'a> {
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());

if self.verbose > 0 {
println!("{} {command:?}", "Creating velf:".blue());
}
info!("{}: {command:?}", "Creating velf".blue());

if !command.status()?.success() {
bail!("vita-elf-create failed");
Expand All @@ -341,9 +331,7 @@ impl<'a> BuildContext<'a> {
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());

if self.verbose > 0 {
println!("{} {command:?}", "Creating eboot:".blue());
}
info!("{}: {command:?}", "Creating eboot".blue());

if !command.status()?.success() {
bail!("vita-make-fself failed");
Expand Down Expand Up @@ -382,9 +370,7 @@ impl<'a> BuildContext<'a> {
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());

if self.verbose > 0 {
println!("{} {command:?}", "Creating sfo:".blue());
}
info!("{}: {command:?}", "Creating sfo".blue());

if !command.status()?.success() {
bail!("vita-mksfoex failed");
Expand Down Expand Up @@ -434,9 +420,7 @@ impl<'a> BuildContext<'a> {
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());

if self.verbose > 0 {
println!("{} {command:?}", "Building vpk:".blue());
}
info!("{}: {command:?}", "Building vpk".blue());

if !command.status()?.success() {
bail!("vita-pack-vpk failed")
Expand Down Expand Up @@ -493,12 +477,10 @@ impl<'a> BuildContext<'a> {
return Ok(());
}

let mut ftp = ftp::connect(conn, self.verbose)?;
let mut ftp = ftp::connect(conn)?;

for (src, dest) in files {
if self.verbose > 0 {
println!("{} {src} {} {dest}", "Uploading file".blue(), "to".blue())
}
info!("{} {src} {} {dest}", "Uploading".blue(), "file to".blue());

let src = File::open(src).context("Unable to open source file")?;
ftp.put_file(dest, &mut BufReader::new(src))
Expand All @@ -521,7 +503,7 @@ impl<'a> BuildContext<'a> {
title_id: Some(title_id.clone()),
connection: conn.clone(),
}
.execute(self.verbose)?;
.execute()?;
}
}

Expand Down
28 changes: 12 additions & 16 deletions src/commands/coredump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
use anyhow::{bail, Context};
use clap::{Args, Subcommand};
use colored::Colorize;
use log::{info, warn};
use suppaftp::FtpError;
use tempfile::NamedTempFile;

Expand Down Expand Up @@ -51,18 +52,17 @@ pub struct Clean {
}

impl Executor for Coredump {
fn execute(&self, verbose: u8) -> anyhow::Result<()> {
fn execute(&self) -> anyhow::Result<()> {
match &self.cmd {
CoredumpCmd::Parse(args) => {
let mut ftp = ftp::connect(&args.connection, verbose)?;
let mut ftp = ftp::connect(&args.connection)?;

ftp.cwd("ux0:/data/")
.context("Unable to cwd to ux0:/data/")?;
let files = ftp.list(None).context("Unable to list files in cwd")?;

if let Some(coredump) = find_core_dumps(&files).max() {
if verbose > 0 {
println!("{} {coredump}", "Downloading file:".blue())
}
info!("{}: {coredump}", "Downloading file".blue());
let mut reader = ftp
.retr_as_buffer(coredump)
.context("Unable to download coredump")?;
Expand Down Expand Up @@ -113,19 +113,17 @@ impl Executor for Coredump {
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());

if verbose > 0 {
println!("{} {command:?}", "Parsing coredump:".blue());
}
info!("{}: {command:?}", "Parsing coredump".blue());

if !command.status()?.success() {
bail!("vita-parse-core failed");
}
} else if verbose > 0 {
println!("{}", "No coredump files found.".yellow())
} else {
warn!("{}", "No coredump files found.".yellow())
}
}
CoredumpCmd::Clean(args) => {
let mut ftp = ftp::connect(&args.connection, verbose)?;
let mut ftp = ftp::connect(&args.connection)?;
ftp.cwd("ux0:/data/")
.context("Unable to cwd to ux0:/data/")?;

Expand All @@ -134,9 +132,7 @@ impl Executor for Coredump {

for file in find_core_dumps(&files) {
counter += 1;
if verbose > 0 {
println!("{} {file}", "Deleting file:".blue())
}
info!("{}: {file}", "Deleting file".blue());

match ftp.rm(file) {
Ok(_) => {}
Expand All @@ -146,8 +142,8 @@ impl Executor for Coredump {
}
}

if counter == 0 && verbose > 0 {
println!("{}", "No coredump files found.".yellow())
if counter == 0 {
warn!("{}", "No coredump files found.".yellow())
}
}
}
Expand Down
Loading