use std::path::{Path, PathBuf};
use cuno_build::sources;
use cuno_build::toolchain;
use cuno_core::{CunoError, Env, Shell, paths};
use cuno_manifest::{ConfigError, Settings};
use cuno_toolchain::llvm::Tool;
use cuno_toolchain::select;
use cuno_toolchain::{HostOs, SystemRunner};
use cuno_tools::format::{self, ClangFormat};
use cuno_tools::targets::{self, Targets, relative};
use crate::build::{boxed, in_project};
pub fn fmt(
check: bool,
resolved: Result<Settings, ConfigError>,
env: &Env,
cwd: &Path,
host: HostOs,
temp: &Path,
shell: &mut Shell,
) -> Result<i32, Box<dyn CunoError>> {
let mut settings = in_project(resolved, cwd)?;
let named = match (
settings.paths.get(&cuno_manifest::Tool::ClangFormat),
settings.user_paths.get(&cuno_manifest::Tool::ClangFormat),
) {
(Some(project), Some(user)) => !paths::same(project, user, host.name_case()),
(project, user) => project.is_some() != user.is_some(),
};
if named {
shell.warning(
"cuno fmt does not run the clang-format .cuno/config.toml names; it is chosen as if that line were not there",
);
}
settings.paths = settings.user_paths.clone();
let (project, manifest) = settings
.project
.as_ref()
.expect("in_project found the project the settings were read from");
let root = project.root.as_path();
let code = sources::code_files(root, manifest, &settings.target_dir, host).map_err(boxed)?;
for warning in &code.warnings {
shell.warning(warning);
}
let Targets { files, links } = targets::select(
root,
code.files.into_iter().map(|file| file.path).collect(),
&manifest.fmt.exclude,
host.name_case(),
);
for link in &links {
shell.warning(format!(
"`{}` is a link; cuno fmt leaves it",
relative(root, link)
));
}
let runner = SystemRunner::new();
let llvm = toolchain::detect(Some(&settings), env, host, &runner, false)
.llvm
.map_err(boxed)?;
let clang_format = select::select_tool(Tool::ClangFormat, &llvm, host).map_err(boxed)?;
if !check {
for warning in cuno_tools::write_editor_files(root, manifest) {
shell.warning(warning);
}
}
let scratch = cuno_core::scratch::in_temp(temp, "fmt", "format").map_err(boxed)?;
let style = scratch.path().join(format::FILE);
let written = std::fs::write(
&style,
format::clang_format_config(&manifest.fmt, manifest.package.lang),
);
let result = written
.map_err(|e| boxed(scratch_error(&style, e)))
.and_then(|()| {
let jobs = settings.jobs.get() as usize;
let tool = ClangFormat {
program: &clang_format.path,
style: &style,
env,
jobs,
};
format::check_style(&tool, &runner).map_err(boxed)?;
Ok(format::format(&files, &tool, check, &runner))
});
drop(scratch);
let formatted = result?;
for failure in &formatted.failed {
shell.error(format!(
"could not format `{}`: {}",
relative(root, &failure.file),
failure.reason
));
}
let failed = !formatted.failed.is_empty();
let changed = formatted.changed;
if check {
for file in &changed {
shell.error(format!("`{}` is not formatted", relative(root, file)));
}
if !changed.is_empty() {
shell.help("run cuno fmt to format them");
}
return Ok(i32::from(failed || !changed.is_empty()));
}
let noun = if changed.len() == 1 { "file" } else { "files" };
shell.status("Formatted", format!("{} {noun}", changed.len()));
Ok(i32::from(failed))
}
fn scratch_error(path: &Path, e: std::io::Error) -> ScratchError {
ScratchError {
path: path.to_owned(),
reason: e.to_string(),
}
}
#[derive(Debug)]
struct ScratchError {
path: PathBuf,
reason: String,
}
impl std::fmt::Display for ScratchError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "cannot write {}: {}", self.path.display(), self.reason)
}
}
impl std::error::Error for ScratchError {}
impl CunoError for ScratchError {
fn exit_class(&self) -> cuno_core::ExitClass {
cuno_core::ExitClass::Setup
}
}