use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use std::process::Command;
use telar_project::BuildFlavour;
use super::bake::bake_workspace;
use super::cli::{PackageArgs, PublishArgs};
use super::config::{find_package_dir, read_package_manifest_in};
use super::transpile::transpile_workspace;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct WorkspacePackage {
pub(super) name: String,
pub(super) dir: PathBuf,
pub(super) publishable: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Member {
pub(super) name: String,
pub(super) dir: PathBuf,
pub(super) library: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Shipment {
Package,
Publish,
}
impl Shipment {
fn subcommand(self) -> &'static str {
match self {
Shipment::Package => "package",
Shipment::Publish => "publish",
}
}
}
pub(crate) fn run_package_cmd(args: PackageArgs) {
let PackageArgs {
package,
workspace,
check,
cargo_args,
} = args;
ship(
Shipment::Package,
package.as_deref(),
workspace,
check,
&cargo_args,
);
}
pub(crate) fn run_publish_cmd(args: PublishArgs) {
let PublishArgs {
package,
workspace,
dry_run,
cargo_args,
} = args;
ship(
Shipment::Publish,
package.as_deref(),
workspace,
false,
&publish_args(dry_run, cargo_args),
);
}
pub(super) fn publish_args(dry_run: bool, mut cargo_args: Vec<String>) -> Vec<String> {
if dry_run && !cargo_args.iter().any(|arg| arg == "--dry-run") {
cargo_args.insert(0, "--dry-run".to_string());
}
cargo_args
}
pub(super) fn workspace_packages(metadata: &str) -> Result<Vec<WorkspacePackage>, String> {
let value: serde_json::Value = serde_json::from_str(metadata)
.map_err(|e| format!("`cargo metadata` printed something that is not its JSON: {e}"))?;
let packages = value["packages"]
.as_array()
.ok_or("`cargo metadata` printed no `packages` list")?;
packages
.iter()
.map(|package| {
let name = package["name"]
.as_str()
.ok_or("`cargo metadata` printed a package without a name")?;
let dir = package["manifest_path"]
.as_str()
.and_then(|manifest| Path::new(manifest).parent())
.ok_or_else(|| format!("`cargo metadata` printed no manifest path for `{name}`"))?;
Ok(WorkspacePackage {
name: name.to_string(),
dir: dir.to_path_buf(),
publishable: !package["publish"].as_array().is_some_and(Vec::is_empty),
})
})
.collect()
}
fn workspace_packages_in(workspace_root: &Path) -> Result<Vec<WorkspacePackage>, String> {
let output = Command::new("cargo")
.args([
"metadata",
"--format-version",
"1",
"--no-deps",
"--manifest-path",
])
.arg(workspace_root.join("Cargo.toml"))
.output()
.map_err(|e| format!("could not run `cargo metadata`: {e}"))?;
if !output.status.success() {
return Err(format!(
"`cargo metadata` failed for {}:\n{}",
workspace_root.display(),
String::from_utf8_lossy(&output.stderr).trim_end()
));
}
workspace_packages(&String::from_utf8_lossy(&output.stdout))
}
fn ship(
shipment: Shipment,
package: Option<&str>,
workspace: bool,
check_only: bool,
cargo_args: &[String],
) {
let here = find_package_dir(&[]);
let workspace_root = telar_project::find_workspace_root(&here).unwrap_or_else(|| here.clone());
let packages = workspace_packages_in(&workspace_root).unwrap_or_else(|message| fail(&message));
let members = select_members(
&here,
&workspace_root,
&packages,
shipment,
package,
workspace,
)
.unwrap_or_else(|message| fail(&message));
let verb = shipment.subcommand();
if members.is_empty() {
eprintln!("[cargo-telar] every workspace member forbids publishing; nothing to {verb}");
return;
}
let libraries: Vec<&Member> = members.iter().filter(|member| member.library).collect();
if !libraries.is_empty() {
check_libraries(&workspace_root, &libraries, verb);
} else if check_only {
eprintln!("[cargo-telar] no selected member declares `library = true`; nothing to check");
}
if check_only {
return;
}
run_cargo(shipment, &workspace_root, &members, cargo_args);
}
fn check_libraries(workspace_root: &Path, libraries: &[&Member], verb: &str) {
if !bake_workspace() {
fail(
"the bake reported errors, so no library can ship an artifact that answers for its sources; fix the errors above and run this again",
);
}
if !transpile_workspace() {
fail(
"the transpile failed, so no library can ship an artifact that answers for its sources; fix the errors above and run this again",
);
}
let telar_version = telar_project::resolve_telar_version(workspace_root)
.unwrap_or_else(|| env!("CARGO_PKG_VERSION").to_string());
let mut ready = true;
for library in libraries {
let problems = readiness(library, &telar_version);
if problems.is_empty() {
eprintln!("[cargo-telar] {}: ready to {verb}", library.name);
continue;
}
ready = false;
eprintln!(
"[cargo-telar] error: {} is not ready to {verb}:",
library.name
);
for problem in &problems {
eprintln!("\n{}", indent(problem));
}
eprintln!();
}
if !ready {
std::process::exit(1);
}
}
pub(super) fn select_members(
here: &Path,
workspace_root: &Path,
packages: &[WorkspacePackage],
shipment: Shipment,
package: Option<&str>,
workspace: bool,
) -> Result<Vec<Member>, String> {
if workspace {
return packages
.iter()
.filter(|package| package.publishable)
.map(|package| {
Ok(Member {
name: package.name.clone(),
dir: package.dir.clone(),
library: is_library(&package.dir)?,
})
})
.collect();
}
let name = match package {
Some(name) => name.to_string(),
None => {
read_package_manifest_in(here)
.ok_or_else(|| {
format!(
"{} is not a package: run this in a library's directory, or pass `-p <name>` or `--workspace`",
here.display()
)
})?
.name
}
};
let selected = packages
.iter()
.find(|package| package.name == name)
.ok_or_else(|| {
format!(
"no package named `{name}` in the workspace at {}",
workspace_root.display()
)
})?;
if !is_library(&selected.dir)? {
return Err(format!(
"`{name}` is not a telar library, so nothing ships a transpiled artifact for it. A package other crates depend on declares it in {}:\n [telar]\n library = true",
selected
.dir
.join(telar_project::MANIFEST_FILENAME)
.display()
));
}
if shipment == Shipment::Publish && !selected.publishable {
return Err(format!(
"`{name}` cannot be published: `package.publish` is false or an empty list in its Cargo.toml"
));
}
Ok(vec![Member {
name,
dir: selected.dir.clone(),
library: true,
}])
}
fn is_library(dir: &Path) -> Result<bool, String> {
telar_project::TelarManifest::load(dir)
.map(|manifest| manifest.telar.library)
.map_err(|e| e.to_string())
}
fn readiness(library: &Member, telar_version: &str) -> Vec<String> {
let listed = match packaged_files(&library.dir) {
Ok(listed) => listed,
Err(message) => return vec![message],
};
let mut problems: Vec<String> = missing_from_package(library, &listed).into_iter().collect();
if !problems.is_empty() {
problems.extend(artifact_problems(&library.dir, telar_version));
return problems;
}
let scratch = match crate::runner::scratch::scratch_dir("package") {
Ok(scratch) => scratch,
Err(message) => {
problems.push(message);
return problems;
}
};
let staged = scratch.path().join(&library.name);
match stage(&library.dir, &listed, &staged) {
Ok(()) => problems.extend(artifact_problems(&staged, telar_version)),
Err(e) => problems.push(format!(
"could not copy the packaged files to {} to check them: {e}",
staged.display()
)),
}
problems
}
pub(super) fn packaged_files(dir: &Path) -> Result<BTreeSet<String>, String> {
let output = Command::new("cargo")
.args([
"package",
"--list",
"--allow-dirty",
"--quiet",
"--manifest-path",
])
.arg(dir.join("Cargo.toml"))
.output()
.map_err(|e| format!("could not run `cargo package --list`: {e}"))?;
if !output.status.success() {
return Err(format!(
"`cargo package --list` failed for {}:\n{}",
dir.display(),
String::from_utf8_lossy(&output.stderr).trim_end()
));
}
Ok(String::from_utf8_lossy(&output.stdout)
.lines()
.map(|line| line.trim().replace('\\', "/"))
.filter(|line| !line.is_empty())
.collect())
}
pub(super) fn missing_from_package(library: &Member, listed: &BTreeSet<String>) -> Option<String> {
let missing: Vec<String> = telar_project::library_files(&library.dir)
.into_iter()
.filter(|file| !listed.contains(file))
.collect();
if missing.is_empty() {
return None;
}
let manifest = library.dir.join("Cargo.toml");
let entries: Vec<String> = telar_project::library_include()
.into_iter()
.filter(|entry| {
missing
.iter()
.any(|file| telar_project::library_include_covers(entry, file))
})
.collect();
let mut message = format!(
"`cargo package` would leave out {} file(s) that a build compiling `{}` as a dependency reads:\n{}",
missing.len(),
library.name,
listing(&missing)
);
match declared_include(&manifest) {
Some(_) => message.push_str(&format!(
"\nAdd to `include` under `[package]` in {}:\n{}",
manifest.display(),
entries
.iter()
.map(|entry| format!(" \"{entry}\","))
.collect::<Vec<_>>()
.join("\n")
)),
None => message.push_str(&format!(
"\n{} declares no `include`, and without one cargo leaves every dot-directory out of a package, `.telar/` among them. Add under `[package]`, keeping any other file the package needs, such as a build script, in the list:\n{}",
manifest.display(),
include_block()
)),
}
Some(message)
}
pub(super) fn include_block() -> String {
let entries: Vec<String> = telar_project::library_include()
.into_iter()
.map(|entry| format!(" \"{entry}\","))
.collect();
format!("include = [\n{}\n]", entries.join("\n"))
}
fn declared_include(manifest: &Path) -> Option<Vec<String>> {
let table: toml::Table = std::fs::read_to_string(manifest).ok()?.parse().ok()?;
let include = table.get("package")?.get("include")?.as_array()?;
Some(
include
.iter()
.filter_map(|entry| entry.as_str().map(str::to_string))
.collect(),
)
}
fn listing(files: &[String]) -> String {
const SHOWN: usize = 12;
let mut lines: Vec<String> = files
.iter()
.take(SHOWN)
.map(|file| format!(" {file}"))
.collect();
if files.len() > SHOWN {
lines.push(format!(" … and {} more", files.len() - SHOWN));
}
lines.join("\n")
}
fn stage(dir: &Path, listed: &BTreeSet<String>, staged: &Path) -> std::io::Result<()> {
let _ = std::fs::remove_dir_all(staged);
std::fs::create_dir_all(staged)?;
for file in listed {
let source = dir.join(file);
if !source.is_file() {
continue;
}
let target = staged.join(file);
if let Some(parent) = target.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::copy(&source, &target)?;
}
Ok(())
}
pub(super) fn artifact_problems(package_dir: &Path, telar_version: &str) -> Vec<String> {
let mut problems = Vec::new();
let telar = match telar_project::TelarManifest::load(package_dir) {
Ok(manifest) => manifest.telar,
Err(e) => return vec![e.to_string()],
};
let prelude = telar.prelude();
let theme = telar_transpiler::resolve_theme_type(package_dir, &telar);
let src_dir = package_dir.join("src");
let plain = BuildFlavour::Plain;
let generated_dir = telar_project::generated_dir(package_dir, plain);
let telar_dir = package_dir.join(".telar");
let assets = telar_project::AssetContext::load(package_dir, &telar, telar_version);
const RETRANSPILE: &str = "Run `cargo telar transpile` and package again.";
if !telar_project::find_rsx_files(&src_dir).is_empty() {
let index_file = format!(".telar/{}", plain.index_filename());
match telar_project::read_build_index(package_dir, plain) {
None => problems.push(format!(
"there is no transpiled artifact: `{index_file}` is missing or unreadable. {RETRANSPILE}"
)),
Some(index) if index.format != telar_project::BUILD_ARTIFACT_FORMAT => {
problems.push(format!(
"`{index_file}` is in artifact format {}, and this telar reads format {}. {RETRANSPILE}",
index.format,
telar_project::BUILD_ARTIFACT_FORMAT
))
}
Some(index) if index.telar_version != telar_version => problems.push(format!(
"the `.rsx` were transpiled for telar {}, and this workspace builds telar {telar_version}; a dependency is refused unless the two agree. {RETRANSPILE}",
index.telar_version
)),
Some(index) if !index.was_transpiled_with(prelude) => problems.push(format!(
"the `.rsx` were transpiled with `prelude = {:?}`, and telar.toml now declares {:?}. {RETRANSPILE}",
index.prelude,
prelude
.iter()
.map(telar_project::PreludeEntry::path)
.collect::<Vec<_>>()
)),
Some(index) if index.theme.as_deref() != theme.as_deref() => problems.push(format!(
"the `.rsx` were transpiled against the theme {:?}, and the package now names {theme:?}. {RETRANSPILE}",
index.theme
)),
Some(index)
if !index.answers_for(
&src_dir,
&generated_dir,
theme.as_deref(),
prelude,
telar_version,
) =>
{
problems.push(format!(
"the transpiled Rust in `.telar/{}/` no longer answers for `src/`: a `.rsx` was added, removed or edited after the transpile, or a generated file was changed. {RETRANSPILE}",
plain.dir_name()
))
}
Some(index) if index.uses_assets && assets.module_file().is_none() => {
problems.push(format!(
"the transpiled Rust reaches into the baked assets, and `.telar/{}` is missing or unusable. Run `cargo telar bake` and package again.",
telar_project::ASSETS_SOURCE_FILENAME
))
}
Some(_) => {}
}
}
if telar_project::invokes_placement_macro(&src_dir)
&& let Some(stale) =
telar_project::ModuleTree::discover(&src_dir, &generated_dir, plain.site_file_name())
.first_difference()
{
let shown = telar_project::relative_slashed(stale, package_dir)
.unwrap_or_else(|| stale.display().to_string());
problems.push(format!(
"the module tree file `{shown}` is missing or out of date: a Rust module was added, moved or removed after the transpile. {RETRANSPILE}"
));
}
problems.extend(catalog_problems(package_dir, &telar_dir, telar_version));
problems.extend(asset_problems(
&telar.assets_root(package_dir),
&telar_dir,
telar_version,
));
problems.extend(icon_record_problem(package_dir));
problems
}
fn icon_record_problem(package_dir: &Path) -> Option<String> {
telar_baker::read_library_icons(package_dir).err().map(|problem| {
format!(
"the icons it baked could not be credited in the licence notice of an application built with it: {problem}. Run `cargo telar bake` and package again."
)
})
}
fn catalog_problems(package_dir: &Path, telar_dir: &Path, telar_version: &str) -> Vec<String> {
const REBAKE: &str = "Run `cargo telar bake` and package again.";
let index = match telar_project::read_catalog_index(telar_dir) {
Ok(None) => return Vec::new(),
Ok(Some(index)) => index,
Err(e) => {
return vec![format!(
"the baked translation catalog cannot be read: {e}. {REBAKE}"
)];
}
};
if index.format != telar_project::CATALOG_ARTIFACT_FORMAT
|| index.telar_version != telar_version
{
return vec![format!(
"the translation catalog was baked for telar {} in format {}, and this workspace builds telar {telar_version}, which reads format {}. {REBAKE}",
index.telar_version,
index.format,
telar_project::CATALOG_ARTIFACT_FORMAT
)];
}
let catalog = telar_project::CatalogContext::load(package_dir, telar_version);
if catalog.module_file().is_none() {
return vec![format!(
"the translation catalog has an index and no `.telar/{}`. {REBAKE}",
telar_project::CATALOG_SOURCE_FILENAME
)];
}
catalog.staleness().into_iter().collect()
}
fn asset_problems(assets_root: &Path, telar_dir: &Path, telar_version: &str) -> Vec<String> {
const REBAKE: &str = "Run `cargo telar bake` and package again.";
let index = match telar_project::check_artifact(telar_dir, telar_version) {
Ok(telar_project::ArtifactHandshake::NotBaked) => return Vec::new(),
Ok(telar_project::ArtifactHandshake::UpToDate(index)) => index,
Ok(telar_project::ArtifactHandshake::FormatMismatch { found, expected }) => {
return vec![format!(
"the baked assets are in format {found}, and this telar reads format {expected}. {REBAKE}"
)];
}
Ok(telar_project::ArtifactHandshake::VersionMismatch { found, expected }) => {
return vec![format!(
"the assets were baked for telar {found}, and this workspace builds telar {expected}. {REBAKE}"
)];
}
Err(e) => {
return vec![format!("the baked assets cannot be read: {e}. {REBAKE}")];
}
};
let mut problems = Vec::new();
if !telar_dir
.join(telar_project::ASSETS_SOURCE_FILENAME)
.is_file()
{
problems.push(format!(
"the baked assets have an index and no `.telar/{}`. {REBAKE}",
telar_project::ASSETS_SOURCE_FILENAME
));
}
problems.extend(index.entries.iter().filter_map(|entry| {
let bytes = std::fs::read(assets_root.join(&entry.path)).ok()?;
(telar_project::content_hash(&bytes) != entry.hash).then(|| {
format!(
"the asset `{}` changed after it was baked, so the package would carry its old contents. {REBAKE}",
entry.path
)
})
}));
problems
}
fn run_cargo(shipment: Shipment, workspace_root: &Path, members: &[Member], cargo_args: &[String]) {
let verb = shipment.subcommand();
if !cargo_args.iter().any(|arg| arg == "--allow-dirty") {
let mut dirty_members = Vec::new();
for member in members {
let listed = packaged_files(&member.dir).unwrap_or_else(|message| fail(&message));
let dirty = uncommitted(&member.dir, &listed).unwrap_or_else(|message| {
fail(&format!(
"{message}\nThe committed-sources check could not be made; pass `-- --allow-dirty` to `cargo telar {verb}` to skip it."
))
});
if !dirty.is_empty() {
dirty_members.push(format!(
"{} file(s) of `{}` have changes not yet committed to git:\n{}",
dirty.len(),
member.name,
listing(&dirty)
));
}
}
if !dirty_members.is_empty() {
fail(&format!(
"{}\nCommit them, or {verb} anyway by passing `-- --allow-dirty` to `cargo telar {verb}`.",
dirty_members.join("\n")
));
}
}
let names: Vec<&str> = members.iter().map(|member| member.name.as_str()).collect();
eprintln!("[cargo-telar] cargo {verb} -p {}", names.join(" -p "));
let status = Command::new("cargo")
.args(cargo_invocation(
shipment,
workspace_root,
members,
cargo_args,
))
.status();
match status {
Ok(status) if status.success() => {}
Ok(status) => std::process::exit(status.code().unwrap_or(1)),
Err(e) => fail(&format!("could not run `cargo {verb}`: {e}")),
}
}
pub(super) fn cargo_invocation(
shipment: Shipment,
workspace_root: &Path,
members: &[Member],
cargo_args: &[String],
) -> Vec<String> {
let mut args = vec![
shipment.subcommand().to_string(),
"--allow-dirty".to_string(),
"--manifest-path".to_string(),
workspace_root.join("Cargo.toml").display().to_string(),
];
for member in members {
args.push("-p".to_string());
args.push(member.name.clone());
}
args.extend(
cargo_args
.iter()
.filter(|arg| *arg != "--allow-dirty")
.cloned(),
);
args
}
fn uncommitted(dir: &Path, listed: &BTreeSet<String>) -> Result<Vec<String>, String> {
uncommitted_with("git", dir, listed)
}
fn uncommitted_with(
program: impl AsRef<std::ffi::OsStr>,
dir: &Path,
listed: &BTreeSet<String>,
) -> Result<Vec<String>, String> {
let git = |args: &[&str]| -> Result<Option<String>, String> {
let output = Command::new(program.as_ref())
.env("LC_ALL", "C")
.arg("-C")
.arg(dir)
.args(args)
.output()
.map_err(|e| format!("could not run `git` to check {}: {e}", dir.display()))?;
if output.status.success() {
return Ok(Some(String::from_utf8_lossy(&output.stdout).into_owned()));
}
let stderr = String::from_utf8_lossy(&output.stderr);
if stderr.contains("not a git repository") {
return Ok(None);
}
Err(format!(
"`git {}` failed in {}: {}",
args.join(" "),
dir.display(),
stderr.trim_end()
))
};
let Some(prefix) = git(&["rev-parse", "--show-prefix"])? else {
return Ok(Vec::new());
};
let Some(status) = git(&[
"status",
"--porcelain",
"-z",
"--untracked-files=all",
"--",
".",
])?
else {
return Ok(Vec::new());
};
Ok(uncommitted_sources(&status, prefix.trim(), listed))
}
pub(super) fn uncommitted_sources(
status: &str,
prefix: &str,
listed: &BTreeSet<String>,
) -> Vec<String> {
changed_paths(status)
.into_iter()
.filter_map(|path| path.strip_prefix(prefix))
.filter(|path| !path.starts_with(".telar/"))
.filter(|path| listed.contains(*path))
.map(str::to_string)
.collect()
}
fn changed_paths(status: &str) -> Vec<&str> {
let mut paths = Vec::new();
let mut entries = status.split('\0').filter(|entry| !entry.is_empty());
while let Some(entry) = entries.next() {
let Some(path) = entry.get(3..) else {
continue;
};
paths.push(path);
if entry.starts_with(['R', 'C']) {
entries.next();
}
}
paths
}
fn indent(text: &str) -> String {
text.lines()
.map(|line| format!(" {line}"))
.collect::<Vec<_>>()
.join("\n")
}
fn fail(message: &str) -> ! {
eprintln!("[cargo-telar] error: {message}");
std::process::exit(1);
}
#[cfg(test)]
#[path = "library_test.rs"]
mod tests;