use anyhow::{bail, Context, Result};
use clap::{Args, ValueEnum};
use sha2::{Digest, Sha256};
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use crate::config::CcgoConfig;
use crate::registry::{PackageEntry, PackageIndex, VersionEntry};
#[path = "publish_input.rs"]
mod input;
#[path = "publish_maven.rs"]
mod maven;
#[path = "publish_kmp_sdk.rs"]
mod kmp_sdk;
#[path = "publish_kmp_maven.rs"]
mod kmp_maven;
#[path = "publish_android_maven.rs"]
mod android_maven;
#[path = "publish_central.rs"]
mod central;
#[path = "publish_apple_artifact.rs"]
mod apple_artifact;
#[path = "publish_artifact.rs"]
mod artifact;
#[path = "publish_doc_artifact.rs"]
mod doc_artifact;
#[path = "publish_conan.rs"]
mod conan;
#[path = "publish_windows_nuget.rs"]
mod windows_nuget;
#[path = "publish_vcpkg.rs"]
mod vcpkg;
#[path = "publish_index_destination.rs"]
mod index_destination;
#[path = "publish_index_recovery.rs"]
mod index_recovery;
#[derive(Debug, Clone, ValueEnum)]
pub enum ArtifactProvider {
Maven,
Github,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, ValueEnum)]
pub enum MavenRegistry {
Local,
Official,
#[default]
Private,
}
#[derive(Debug, Clone, ValueEnum)]
pub enum PublishTarget {
Android,
Ohos,
Apple,
Conan,
Kmp,
Windows,
Vcpkg,
Doc,
#[value(name = "package", alias = "index")]
Index,
}
impl std::fmt::Display for PublishTarget {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PublishTarget::Android => write!(f, "android"),
PublishTarget::Ohos => write!(f, "ohos"),
PublishTarget::Apple => write!(f, "apple"),
PublishTarget::Conan => write!(f, "conan"),
PublishTarget::Kmp => write!(f, "kmp"),
PublishTarget::Windows => write!(f, "windows"),
PublishTarget::Vcpkg => write!(f, "vcpkg"),
PublishTarget::Doc => write!(f, "doc"),
PublishTarget::Index => write!(f, "package"),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, ValueEnum)]
pub enum RegistryType {
#[default]
Local,
Official,
Private,
}
impl std::fmt::Display for RegistryType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RegistryType::Local => write!(f, "local"),
RegistryType::Official => write!(f, "official"),
RegistryType::Private => write!(f, "private"),
}
}
}
#[derive(Debug, Clone, Default, ValueEnum)]
pub enum AppleManager {
Cocoapods,
Spm,
#[default]
All,
Nuget,
}
impl std::fmt::Display for AppleManager {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AppleManager::Cocoapods => write!(f, "cocoapods"),
AppleManager::Spm => write!(f, "spm"),
AppleManager::All => write!(f, "all"),
AppleManager::Nuget => write!(f, "nuget"),
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, ValueEnum)]
pub enum AppleLinkage {
Static,
Shared,
#[default]
#[value(alias = "both")]
All,
}
impl std::fmt::Display for AppleLinkage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AppleLinkage::Static => write!(f, "static"),
AppleLinkage::Shared => write!(f, "shared"),
AppleLinkage::All => write!(f, "all"),
}
}
}
#[derive(Args, Debug)]
pub struct PublishCommand {
#[arg(value_enum)]
pub target: PublishTarget,
#[arg(long, value_enum, default_value_t = RegistryType::Local)]
pub registry: RegistryType,
#[arg(long, visible_aliases = ["url", "remote-name"], value_name = "URL_OR_NAME")]
pub remote: Option<String>,
#[arg(long)]
pub skip_build: bool,
#[arg(long, value_name = "PATH")]
pub path: Option<PathBuf>,
#[arg(long, value_enum)]
pub artifact_provider: Option<ArtifactProvider>,
#[arg(long, value_enum, default_value_t = MavenRegistry::Private)]
pub maven_registry: MavenRegistry,
#[arg(long, requires = "artifact_provider")]
pub artifact_group: Option<String>,
#[arg(long, requires = "artifact_provider")]
pub artifact_id: Option<String>,
#[arg(long, requires = "artifact_provider")]
pub github_repo: Option<String>,
#[arg(long)]
pub release: bool,
#[arg(long, value_enum)]
pub stl: Option<crate::commands::build::StlKind>,
#[arg(short = 'y', long)]
pub yes: bool,
#[arg(long, value_enum, default_value_t = AppleManager::All)]
pub manager: AppleManager,
#[arg(long, visible_alias = "index-push")]
pub push: bool,
#[arg(long)]
pub platform: Option<String>,
#[arg(long, value_enum, default_value_t = AppleLinkage::All)]
pub linkage: AppleLinkage,
#[arg(long, value_name = "NAME")]
pub pod_name: Option<String>,
#[arg(long, value_name = "NAME")]
pub shared_pod_name: Option<String>,
#[arg(long, default_value_t = true)]
pub allow_warnings: bool,
#[command(flatten)]
pub conan: conan::ConanOptions,
#[arg(long, default_value = "default")]
pub profile: String,
#[arg(long, default_value = "both")]
pub link_type: String,
#[arg(long, default_value = "gh-pages")]
pub doc_branch: String,
#[arg(long)]
pub doc_force: bool,
#[arg(long)]
pub doc_open: bool,
#[arg(long)]
pub index_repo: Option<String>,
#[arg(long)]
pub index_name: Option<String>,
#[arg(long)]
pub index_message: Option<String>,
#[arg(long, visible_alias = "index-version")]
pub version: Option<String>,
#[arg(long, visible_alias = "index-tag")]
pub tag: Option<String>,
#[arg(long)]
pub checksum: bool,
#[arg(long)]
pub archive_url_template: Option<String>,
#[arg(long, default_value = "zip")]
pub archive_format: String,
}
fn substitute_archive_url(template: &str, name: &str, version: &str, tag: &str) -> String {
template
.replace("{name}", name)
.replace("{version}", version)
.replace("{tag}", tag)
}
fn derive_version_from_tag(tag: &str) -> String {
tag.strip_prefix('v')
.or_else(|| tag.strip_prefix('V'))
.unwrap_or(tag)
.to_string()
}
fn default_tag_for_version(version: &str) -> String {
format!("v{}", version)
}
fn merge_version_entry(
mut existing: Vec<VersionEntry>,
new: VersionEntry,
) -> Result<Vec<VersionEntry>> {
if let Some(dup) = existing.iter().find(|v| v.version == new.version) {
anyhow::bail!(
"version '{}' is already in the index (tag '{}'); the index is \
append-only by design. Publish a new version to change its content.",
new.version,
dup.tag
);
}
existing.push(new);
existing.sort_by(|a, b| b.version.cmp(&a.version));
Ok(existing)
}
impl PublishCommand {
fn validate_registry_usage(&self) -> Result<()> {
if self.maven_registry != MavenRegistry::Private
&& !matches!(self.artifact_provider, Some(ArtifactProvider::Maven))
{
bail!("--maven-registry local|official requires --artifact-provider maven");
}
if matches!(self.target, PublishTarget::Doc) {
if self.artifact_provider.is_some() && self.registry != RegistryType::Local {
bail!(
"Doc artifact publishing does not use --registry; select the storage \
with --artifact-provider (and --maven-registry for Maven)"
);
}
if self.artifact_provider.is_none() && self.registry != RegistryType::Local {
bail!(
"Doc publishing without --artifact-provider deploys to GitHub Pages \
and does not use --registry"
);
}
}
if matches!(self.target, PublishTarget::Apple | PublishTarget::Index)
&& matches!(self.artifact_provider, Some(ArtifactProvider::Maven))
&& self.registry == RegistryType::Official
&& self.maven_registry == MavenRegistry::Private
{
eprintln!(
"Note: --registry official selects the Apple/Index destination; the Maven \
artifact still goes to CCGO_MAVEN_URL. Use --maven-registry official for \
Maven Central."
);
}
Ok(())
}
pub fn execute(mut self, verbose: bool) -> Result<()> {
self.validate_version_options()?;
self.validate_registry_usage()?;
if self.linkage != AppleLinkage::All && !matches!(self.target, PublishTarget::Apple) {
bail!("--linkage is supported only for apple publishing");
}
if (self.pod_name.is_some() || self.shared_pod_name.is_some())
&& !matches!(self.target, PublishTarget::Apple)
{
bail!("--pod-name/--shared-pod-name are supported only for apple publishing");
}
if matches!(self.target, PublishTarget::Apple)
&& matches!(self.manager, AppleManager::Nuget)
{
bail!("--manager nuget is supported only for windows publishing");
}
if matches!(self.target, PublishTarget::Windows)
&& matches!(self.manager, AppleManager::Cocoapods | AppleManager::Spm)
{
bail!("Windows publishing supports --manager nuget only");
}
if (self.pod_name.is_some() || self.shared_pod_name.is_some())
&& self.artifact_provider.is_none()
{
bail!("--pod-name/--shared-pod-name require --artifact-provider");
}
if matches!(self.target, PublishTarget::Apple)
&& matches!(self.manager, AppleManager::Spm | AppleManager::All)
{
self.validate_spm_registry()?;
}
if self.artifact_provider.is_some() {
if !matches!(
self.target,
PublishTarget::Apple | PublishTarget::Index | PublishTarget::Doc
) {
bail!("--artifact-provider is supported only for apple, index and doc");
}
if matches!(self.target, PublishTarget::Doc) {
let _temporary_input = self.prepare_publish_path()?;
return doc_artifact::publish(&self, verbose);
}
if matches!(self.target, PublishTarget::Apple) {
return apple_artifact::publish(&self, verbose);
}
if self.archive_url_template.is_some() {
bail!("--artifact-provider generates the archive URL; omit --archive-url-template");
}
if self.archive_format != "zip" {
bail!("Index artifact providers support ZIP archives only");
}
}
let temporary_input = self.prepare_publish_path()?;
println!("Publishing library project...\n");
self.publish_target(verbose, temporary_input.is_some())
}
fn publish_target(&self, verbose: bool, temporary_input: bool) -> Result<()> {
match self.target {
PublishTarget::Android => self.publish_android(verbose),
PublishTarget::Ohos => self.publish_ohos(verbose),
PublishTarget::Kmp => self.publish_kmp(verbose),
PublishTarget::Conan => self.publish_conan(verbose),
PublishTarget::Windows => windows_nuget::publish(self, verbose),
PublishTarget::Vcpkg => vcpkg::publish(self, verbose),
PublishTarget::Apple => self.publish_apple(verbose),
PublishTarget::Doc => self.publish_doc(verbose, temporary_input),
PublishTarget::Index => self.publish_index(verbose),
}
}
fn validate_version_options(&self) -> Result<()> {
if (self.version.is_some() || self.tag.is_some())
&& !(matches!(self.target, PublishTarget::Doc) && self.artifact_provider.is_some())
&& !matches!(
self.target,
PublishTarget::Index | PublishTarget::Windows | PublishTarget::Vcpkg
)
&& !(matches!(self.target, PublishTarget::Apple)
&& (matches!(self.manager, AppleManager::Spm) || self.artifact_provider.is_some()))
{
bail!("--version/--tag are supported for index, apple --manager spm, or Apple/Doc artifact hosting");
}
Ok(())
}
fn accepts_publish_file(&self, path: &Path, extension: &str) -> bool {
match self.target {
PublishTarget::Android => matches!(extension, "aar" | "zip"),
PublishTarget::Ohos => matches!(extension, "har" | "zip"),
PublishTarget::Kmp | PublishTarget::Index => extension == "zip",
PublishTarget::Apple => match self.manager {
AppleManager::Cocoapods => extension == "podspec",
AppleManager::Spm => {
path.file_name().and_then(|s| s.to_str()) == Some("Package.swift")
}
AppleManager::All | AppleManager::Nuget => false,
},
PublishTarget::Conan => matches!(extension, "py" | "zip"),
PublishTarget::Windows => matches!(extension, "zip" | "nupkg"),
PublishTarget::Vcpkg => extension == "zip",
PublishTarget::Doc => matches!(extension, "yml" | "yaml" | "zip"),
}
}
fn open_published_documentation(&self, project_dir: &Path) {
if self.doc_open {
if let Ok(output) = Command::new("git")
.current_dir(project_dir)
.args(["remote", "get-url", "origin"])
.output()
{
if let Ok(url) = String::from_utf8(output.stdout) {
let url = url.trim();
if let Some(pages_url) = self.git_url_to_pages_url(url) {
println!("Opening documentation at: {}", pages_url);
let _ = open::that(pages_url);
}
}
}
}
}
fn validate_publication_worktree(
work_path: &Path,
destination: &index_destination::Destination,
) -> Result<()> {
if work_path.join(".git/ccgo-yank.json").exists() {
bail!("An interrupted yank is pending; retry that yank command before publishing");
}
if !destination.local && work_path.join(".git").exists() {
crate::registry::index_writer::verify_origin(work_path, &destination.repository)?;
}
Ok(())
}
fn validate_publish_path(&self, path: &Path) -> Result<()> {
if !path.exists() {
bail!("Publish path does not exist: {}", path.display());
}
if matches!(self.target, PublishTarget::Index) {
if self.artifact_provider.is_none()
&& (!self.checksum || self.archive_url_template.is_none())
{
bail!("--path for index requires --checksum and --archive-url-template; the archive must be hosted separately");
}
if self.archive_format != "zip" {
bail!("--path for index currently supports --archive-format zip only");
}
}
if path.is_dir() {
return Ok(());
}
let extension = path.extension().and_then(|s| s.to_str()).unwrap_or("");
if matches!(self.target, PublishTarget::Apple) && extension == "zip" {
bail!("Apple ZIP input requires --artifact-provider maven or github");
}
let valid = self.accepts_publish_file(path, extension);
if !path.is_file() || !valid {
bail!("Unsupported --path input for {}: {}. Use a project/SDK directory or a platform-specific file (apple files require the matching --manager)", self.target, path.display());
}
Ok(())
}
fn prepare_publish_path(&mut self) -> Result<Option<tempfile::TempDir>> {
let Some(path) = self.path.as_deref() else {
return Ok(None);
};
self.validate_publish_path(path)?;
let mut path = fs::canonicalize(path)
.with_context(|| format!("Failed to resolve publish path {}", path.display()))?;
let mut temporary = None;
if matches!(
self.target,
PublishTarget::Android
| PublishTarget::Ohos
| PublishTarget::Conan
| PublishTarget::Doc
) && path.is_file()
&& path.extension().and_then(|s| s.to_str()) == Some("zip")
{
let directory = tempfile::tempdir()?;
input::extract_zip(&path, directory.path())?;
path = directory.path().to_path_buf();
temporary = Some(directory);
}
if path.is_dir() {
path = self.resolve_publish_directory(path, temporary.is_some())?;
}
self.path = Some(fs::canonicalize(&path).with_context(|| {
format!(
"Failed to resolve prepared publish input {}",
path.display()
)
})?);
Ok(temporary)
}
fn resolve_publish_directory(&self, path: PathBuf, extracted: bool) -> Result<PathBuf> {
Ok(match self.target {
PublishTarget::Android if !extracted && path.join("gradlew").is_file() => path,
PublishTarget::Android => input::unique_file(&path, "aar", true)?,
PublishTarget::Ohos if !extracted && path.join("oh-package.json5").is_file() => path,
PublishTarget::Ohos => input::unique_file(&path, "har", true)?,
PublishTarget::Kmp => path,
PublishTarget::Apple => self.validate_apple_directory(path)?,
PublishTarget::Conan => Self::resolve_conan_directory(path, extracted)?,
PublishTarget::Windows => path,
PublishTarget::Vcpkg => path,
PublishTarget::Doc => Self::resolve_doc_directory(path, extracted)?,
PublishTarget::Index => input::unique_file(&path, "zip", true)?,
})
}
fn resolve_doc_directory(path: PathBuf, extracted: bool) -> Result<PathBuf> {
if extracted {
input::archive_descriptor(&path, &["mkdocs.yml", "mkdocs.yaml"])
} else {
input::mkdocs_config(&path)
}
}
fn resolve_conan_directory(path: PathBuf, extracted: bool) -> Result<PathBuf> {
if extracted {
return input::archive_descriptor(&path, &["conanfile.py"]);
}
let recipe = path.join("conanfile.py");
if !recipe.is_file() {
bail!("conanfile.py not found in {}", path.display());
}
Ok(recipe)
}
fn validate_apple_directory(&self, path: PathBuf) -> Result<PathBuf> {
if matches!(self.manager, AppleManager::Cocoapods | AppleManager::All) {
input::unique_file(&path, "podspec", false)?;
}
if matches!(self.manager, AppleManager::Spm | AppleManager::All)
&& !path.join("Package.swift").is_file()
{
bail!("Package.swift not found in {}", path.display());
}
Ok(path)
}
fn publish_android(&self, verbose: bool) -> Result<()> {
println!("=== Publishing Android to Maven ===\n");
if let Some(aar) = self.path.as_ref().filter(|path| path.is_file()) {
return self.publish_android_aar(aar, verbose);
}
if let Some(path) = &self.path {
let root = find_project_root(path).with_context(|| {
format!(
"Cannot find CCGO.toml for the Android project directory {}",
path.display()
)
})?;
let _guard = CurrentDirGuard::change(&root)?;
return self.publish_android_built(verbose);
}
self.publish_android_built(verbose)
}
fn publish_android_built(&self, verbose: bool) -> Result<()> {
if self.skip_build {
bail!(
"--skip-build without an AAR cannot choose an artifact; \
pass --path <aar>, or omit --skip-build to build it first"
);
}
if matches!(self.registry, RegistryType::Official) {
let cwd = std::env::current_dir().context("Failed to get current directory")?;
let config = maven::load_config(&cwd)?;
central::ensure_ready(&config, &cwd)?;
}
println!("Building the release AAR with ccgo...");
let build = crate::commands::build::BuildCommand::for_android_publish(self.stl.clone())?;
let results = build.build_artifacts(verbose)?;
let aar = results
.iter()
.find_map(|result| result.aar_archive.as_deref())
.context("Android build did not produce an AAR")?;
self.publish_android_aar(aar, verbose)
}
fn publish_android_aar(&self, aar: &Path, verbose: bool) -> Result<()> {
match self.registry {
RegistryType::Official => central::publish_android(self, aar, verbose),
RegistryType::Local | RegistryType::Private => {
android_maven::publish(self, aar, verbose)
}
}
}
fn publish_ohos(&self, verbose: bool) -> Result<()> {
println!("=== Publishing OHOS to OHPM ===\n");
let cwd = std::env::current_dir().context("Failed to get current directory")?;
let ohos_dir = self
.path
.as_ref()
.filter(|path| path.is_dir())
.cloned()
.unwrap_or_else(|| cwd.join("ohos"));
if self.path.is_none() && (!ohos_dir.exists() || !ohos_dir.is_dir()) {
bail!("Error: ohos directory not found at {}", ohos_dir.display());
}
let artifact = match self.path.as_ref().filter(|path| path.is_file()) {
Some(path) => path.clone(),
None => input::unique_file(&ohos_dir, "har", true).context(
"OHPM publish/prepublish requires a built HAR; use --path to select one",
)?,
};
let mut cmd = Command::new("ohpm");
cmd.current_dir(if self.path.as_ref().is_some_and(|path| path.is_file()) {
&cwd
} else {
&ohos_dir
});
match self.registry {
RegistryType::Local => {
println!("Validating HAR locally with ohpm prepublish...");
cmd.arg("prepublish");
}
RegistryType::Official => {
cmd.args([
"publish",
"--publish_registry",
"https://ohpm.openharmony.cn/ohpm/",
]);
}
RegistryType::Private => {
let url = self
.remote
.as_deref()
.context("Private OHPM registry requires --remote")?;
cmd.args(["publish", "--publish_registry", url]);
}
}
cmd.arg(&artifact);
if verbose {
println!("Running: {:?}", cmd);
}
let status = cmd.status().context("Failed to execute ohpm")?;
if !status.success() {
bail!(
"Publish failed with exit code: {}",
status.code().unwrap_or(-1)
);
}
println!("\n✅ Publish completed successfully!");
Ok(())
}
fn publish_kmp(&self, verbose: bool) -> Result<()> {
println!("=== Publishing KMP to Maven ===\n");
let sdk = self
.path
.as_deref()
.filter(|path| !path.join("gradlew").is_file())
.map(kmp_sdk::PreparedSdk::prepare)
.transpose()?;
if let Some(sdk) = sdk.as_ref() {
return self.publish_kmp_sdk(sdk, verbose);
}
if let Some(path) = &self.path {
let root = find_project_root(path).with_context(|| {
format!(
"Cannot find CCGO.toml for the KMP project directory {}",
path.display()
)
})?;
let _guard = CurrentDirGuard::change(&root)?;
return self.publish_kmp_built(verbose);
}
self.publish_kmp_built(verbose)
}
fn publish_kmp_built(&self, verbose: bool) -> Result<()> {
if self.skip_build {
bail!(
"--skip-build without a KMP SDK cannot choose an artifact; \
pass --path <sdk.zip>, or omit --skip-build to build it first"
);
}
if matches!(self.registry, RegistryType::Official) {
let cwd = std::env::current_dir().context("Failed to get current directory")?;
let config = maven::load_config(&cwd)?;
central::ensure_ready(&config, &cwd)?;
}
println!("Building the release KMP SDK with ccgo...");
let build = crate::commands::build::BuildCommand::for_kmp_publish()?;
let results = build.build_artifacts(verbose)?;
let sdk_zip = results
.first()
.map(|result| result.sdk_archive.as_path())
.context("KMP build did not produce an SDK archive")?;
let prepared = kmp_sdk::PreparedSdk::prepare(sdk_zip)?;
self.publish_kmp_sdk(&prepared, verbose)
}
fn publish_kmp_sdk(&self, sdk: &kmp_sdk::PreparedSdk, verbose: bool) -> Result<()> {
match self.registry {
RegistryType::Official => central::publish_kmp(self, sdk, verbose),
RegistryType::Local | RegistryType::Private => kmp_maven::publish(self, sdk, verbose),
}
}
fn publish_conan(&self, verbose: bool) -> Result<()> {
conan::publish(self, verbose)
}
fn publish_apple(&self, verbose: bool) -> Result<()> {
println!("=== Publishing Apple platforms ===\n");
let cwd = std::env::current_dir().context("Failed to get current directory")?;
let project_dir = match &self.path {
Some(path) if path.is_dir() => path.as_path(),
Some(path) => path
.parent()
.context("Apple input has no parent directory")?,
None => cwd.as_path(),
};
match self.manager {
AppleManager::Cocoapods | AppleManager::All => {
self.publish_cocoapods(
project_dir,
verbose,
matches!(self.manager, AppleManager::All) && !self.push,
)?;
}
_ => {}
}
match self.manager {
AppleManager::Spm | AppleManager::All => {
self.publish_spm(project_dir, verbose)?;
}
_ => {}
}
println!("\n✅ Publish completed successfully!");
Ok(())
}
fn configure_pod_options(&self, cmd: &mut Command, verbose: bool) {
if self.allow_warnings {
cmd.arg("--allow-warnings");
}
if verbose {
cmd.arg("--verbose");
}
}
fn publish_cocoapods(
&self,
project_dir: &Path,
verbose: bool,
validation_only: bool,
) -> Result<()> {
println!("Publishing to CocoaPods...");
let podspec = match &self.path {
Some(path) if path.is_file() => path.clone(),
Some(path) => input::unique_file(path, "podspec", false)?,
None => self.find_file_with_extension(project_dir, ".podspec")?,
};
let project_dir = if self.path.is_some() {
podspec
.parent()
.context("Podspec has no parent directory")?
} else {
project_dir
};
let registry = if validation_only {
RegistryType::Local
} else {
self.registry.clone()
};
self.run_pod_publish(project_dir, &podspec, registry, verbose)
}
fn run_pod_publish(
&self,
project_dir: &Path,
podspec: &Path,
registry: RegistryType,
verbose: bool,
) -> Result<()> {
match registry {
RegistryType::Local => {
println!("Validating podspec locally...");
let mut cmd = Command::new("pod");
cmd.current_dir(project_dir);
cmd.args(["lib", "lint", podspec.to_str().unwrap()]);
self.configure_pod_options(&mut cmd, verbose);
let status = cmd.status().context("Failed to execute pod lib lint")?;
if !status.success() {
bail!("Pod validation failed");
}
}
RegistryType::Official => {
println!("Publishing to CocoaPods Trunk...");
let mut cmd = Command::new("pod");
cmd.current_dir(project_dir);
cmd.args(["trunk", "push", podspec.to_str().unwrap()]);
self.configure_pod_options(&mut cmd, verbose);
let status = cmd.status().context("Failed to execute pod trunk push")?;
if !status.success() {
bail!("Pod trunk push failed");
}
}
RegistryType::Private => {
if let Some(remote_name) = &self.remote {
println!("Publishing to private specs repo: {}", remote_name);
let mut cmd = Command::new("pod");
cmd.current_dir(project_dir);
cmd.args(["repo", "push", remote_name, podspec.to_str().unwrap()]);
self.configure_pod_options(&mut cmd, verbose);
let status = cmd.status().context("Failed to execute pod repo push")?;
if !status.success() {
bail!("Pod repo push failed");
}
} else {
bail!("Private registry requires --remote");
}
}
}
Ok(())
}
fn validate_spm_registry(&self) -> Result<()> {
match self.registry {
RegistryType::Official => bail!("SPM has no official central publishing registry. Use --registry local to validate, or --registry private --remote <git-remote> --push to publish a version tag"),
RegistryType::Private => {
let remote = self.remote.as_deref().context("Private SPM publishing requires --remote <git-remote>")?;
if remote.is_empty() || remote.starts_with('-') { bail!("Invalid SPM Git remote name"); }
}
RegistryType::Local => {}
}
Ok(())
}
fn publish_spm(&self, project_dir: &Path, verbose: bool) -> Result<()> {
self.validate_spm_registry()?;
if !project_dir.join("Package.swift").is_file() {
bail!("Package.swift not found");
}
if matches!(self.registry, RegistryType::Private) {
let remote = self.remote.as_deref().unwrap();
if !Command::new("git")
.current_dir(project_dir)
.args(["remote", "get-url", remote])
.output()?
.status
.success()
{
bail!("SPM Git remote '{remote}' not found; configure it with git remote add");
}
}
println!("Validating Swift package manifest...");
let mut swift = Command::new("swift");
swift
.current_dir(project_dir)
.args(["package", "dump-package"]);
if !verbose {
swift.stdout(Stdio::null());
}
if !swift
.status()
.context("Failed to execute swift package dump-package")?
.success()
{
bail!("Swift package manifest validation failed");
}
let configured_version = if self.version.is_some() || self.tag.is_some() {
String::new()
} else {
self.get_project_version(project_dir)?
};
let (version, tag) = self.resolve_version_and_tag(&configured_version)?;
if !Command::new("git")
.args(["check-ref-format", &format!("refs/tags/{tag}")])
.status()?
.success()
{
bail!("Invalid release Git tag: {tag}");
}
if !self.push {
println!("SPM validated: version {version}, tag {tag}. No tag created or pushed; use --push to publish.");
return Ok(());
}
self.publish_spm_tag(project_dir, &tag)
}
fn publish_spm_tag(&self, project_dir: &Path, tag: &str) -> Result<()> {
let git_output = |args: &[&str]| -> Result<std::process::Output> {
Command::new("git")
.current_dir(project_dir)
.args(args)
.output()
.context("Failed to execute git")
};
let status = git_output(&["status", "--porcelain"])?;
if !status.status.success() || !status.stdout.is_empty() {
bail!("Commit all SPM wrapper changes before creating/pushing its version tag");
}
let head = git_output(&["rev-parse", "HEAD"])?;
if !head.status.success() {
bail!("SPM repository has no committed HEAD");
}
let tag_ref = format!("refs/tags/{tag}");
let existing = git_output(&["rev-parse", "--verify", &format!("{tag_ref}^{{commit}}")])?;
if existing.status.success() {
if existing.stdout != head.stdout {
bail!("SPM tag {tag} already points to a different commit");
}
} else if !git_output(&["tag", tag])?.status.success() {
bail!("Failed to create SPM tag {tag}");
}
if matches!(self.registry, RegistryType::Local) {
println!("Created/validated local SPM tag {tag}; no remote was contacted.");
return Ok(());
}
let remote = self.remote.as_deref().unwrap();
let refspec = format!("{tag_ref}:{tag_ref}");
if !Command::new("git")
.current_dir(project_dir)
.args(["push", remote, &refspec])
.status()?
.success()
{
bail!("Failed to push SPM tag {tag} to {remote}");
}
println!("Pushed SPM tag {tag} to {remote}");
Ok(())
}
fn publish_doc(&self, verbose: bool, archive_input: bool) -> Result<()> {
println!("=== Publishing Documentation ===\n");
let cwd = std::env::current_dir().context("Failed to get current directory")?;
let project_dir = match &self.path {
_ if archive_input => cwd.as_path(),
Some(path) => path
.parent()
.context("MkDocs config has no parent directory")?,
None => cwd.as_path(),
};
let docs_dir = project_dir.join("site");
if self.path.is_none() && !docs_dir.exists() {
println!("Building documentation...");
let mut cmd = Command::new("mkdocs");
cmd.current_dir(&cwd);
cmd.args(["build", "--clean"]);
if verbose {
cmd.arg("--verbose");
}
let status = cmd.status().context("Failed to build documentation")?;
if !status.success() {
bail!("Documentation build failed");
}
}
println!("Deploying to GitHub Pages (branch: {})...", self.doc_branch);
let mut cmd = Command::new("mkdocs");
cmd.current_dir(project_dir);
cmd.args(["gh-deploy", "--branch", &self.doc_branch]);
if let Some(path) = &self.path {
cmd.arg("--config-file").arg(path);
}
if self.doc_force {
cmd.arg("--force");
}
if verbose {
cmd.arg("--verbose");
}
let status = cmd.status().context("Failed to deploy documentation")?;
if !status.success() {
bail!("Documentation deployment failed");
}
self.open_published_documentation(project_dir);
println!("\n✅ Documentation published successfully!");
Ok(())
}
fn resolve_version_and_tag(&self, project_version: &str) -> Result<(String, String)> {
let (version, tag) = match (&self.version, &self.tag) {
(Some(v), Some(t)) => (v.clone(), t.clone()),
(Some(v), None) => (v.clone(), default_tag_for_version(v)),
(None, Some(t)) => (derive_version_from_tag(t), t.clone()),
(None, None) => (
project_version.to_owned(),
default_tag_for_version(project_version),
),
};
semver::Version::parse(&version)
.context("Invalid release version; use --version with --tag for custom tag prefixes")?;
if tag.is_empty() || tag.starts_with('-') {
bail!("Invalid release Git tag: {tag}");
}
Ok((version, tag))
}
fn write_package_entry(
&self,
index_path: &std::path::Path,
config: &crate::config::CcgoConfig,
package: &crate::config::PackageConfig,
git_url: &str,
new_version_entry: crate::registry::VersionEntry,
) -> Result<PackageEntry> {
let package_rel_path = PackageIndex::package_index_path(&package.name);
let package_file = index_path.join(&package_rel_path);
if let Some(parent) = package_file.parent() {
fs::create_dir_all(parent).context("Failed to create package directory")?;
}
let existing_versions = Self::read_existing_versions(&package_file)?;
let merged_versions = merge_version_entry(existing_versions, new_version_entry)?;
let package_entry = PackageEntry {
name: package.name.clone(),
description: package.description.clone().unwrap_or_default(),
repository: git_url.to_string(),
homepage: package.repository.clone(),
license: package.license.clone(),
keywords: Vec::new(),
platforms: self.get_supported_platforms(config),
versions: merged_versions,
};
let json = serde_json::to_string_pretty(&package_entry)
.context("Failed to serialize package entry")?;
fs::write(&package_file, &json).context("Failed to write package file")?;
println!("✅ Written: {}", package_rel_path.display());
Ok(package_entry)
}
fn publish_index(&self, verbose: bool) -> Result<()> {
println!("=== Publishing to Package Index ===\n");
let cwd = std::env::current_dir().context("Failed to get current directory")?;
let project_dir = Self::find_project_dir(&cwd)?;
let config_path = project_dir.join("CCGO.toml");
let config =
CcgoConfig::load_from_path(&config_path).context("Failed to load CCGO.toml")?;
let destination = index_destination::resolve(self, &config)?;
let package = config
.package
.as_ref()
.ok_or_else(|| anyhow::anyhow!("No [package] section in CCGO.toml"))?;
println!("📦 Package: {}", package.name);
println!(
"📝 Description: {}",
package.description.as_deref().unwrap_or("No description")
);
let git_url = self.get_git_remote_url(&project_dir)?;
println!("🔗 Repository: {}", git_url);
let (version, tag) = self.resolve_version_and_tag(&package.version)?;
println!("\n🔖 Publishing single version:");
println!(" version: {}", version);
println!(" tag: {}", tag);
self.validate_tag_exists(&project_dir, &tag)?;
let mut new_version_entry =
self.build_single_version_entry(&project_dir, &package.name, &version, &tag, verbose)?;
let artifact = self.prepare_index_artifact(
&project_dir,
&config_path,
&package.name,
&version,
&tag,
&mut new_version_entry,
)?;
self.publish_prepared_index(
&project_dir,
&config,
&git_url,
destination,
(new_version_entry, artifact),
verbose,
)
}
fn publish_prepared_index(
&self,
project_dir: &Path,
config: &CcgoConfig,
git_url: &str,
destination: index_destination::Destination,
prepared: (
VersionEntry,
Option<(artifact::PreparedUpload, artifact::ArchiveSnapshot)>,
),
verbose: bool,
) -> Result<()> {
let package = config
.package
.as_ref()
.context("Missing package configuration")?;
let (mut new_version_entry, artifact) = prepared;
let index_repo = &destination.repository;
let index_name = &destination.name;
println!("\nIndex repository: {}", index_repo);
if !self.push {
Self::validate_index_preview(&destination, &package.name, new_version_entry)?;
return Ok(());
}
let work_path = if destination.local {
PathBuf::from(index_repo)
} else {
crate::registry::index_writer::publish_work_dir(index_name)
};
let _write_lock = crate::registry::index_writer::lock_index(&work_path)?;
Self::validate_publication_worktree(&work_path, &destination)?;
let pending = index_recovery::Publication::resume(
&work_path,
&package.name,
git_url,
&new_version_entry,
verbose,
)?;
if let Some(pending) = &pending {
new_version_entry = pending.version.clone();
}
let index_path = Self::prepare_publication_index(&destination, project_dir, verbose)?;
crate::registry::index_writer::require_clean(&index_path)?;
if let Some(receipt) = &pending {
if self.complete_pending_publication(
&index_path,
(index_name, index_repo),
&package.name,
receipt,
verbose,
)? {
return Ok(());
}
}
let mut pending = Self::prepare_publication_receipt(
&index_path,
&package.name,
git_url,
&new_version_entry,
pending,
)?;
Self::upload_index_artifact(&index_path, &mut pending, artifact.as_ref())?;
let package_entry =
self.write_package_entry(&index_path, config, package, git_url, new_version_entry)?;
self.commit_index_publication(&index_path, index_name, &package_entry, verbose)?;
self.push_and_report(
&index_path,
index_name,
index_repo,
&package.name,
&package_entry,
verbose,
)?;
pending.finish(&index_path)
}
fn prepare_publication_index(
destination: &index_destination::Destination,
project_dir: &Path,
verbose: bool,
) -> Result<PathBuf> {
if destination.local {
index_destination::prepare_local(&destination.repository, project_dir)
} else {
crate::registry::index_writer::prepare_index_repo(
&destination.repository,
&destination.name,
verbose,
)
}
}
fn prepare_publication_receipt(
index_path: &Path,
package_name: &str,
git_url: &str,
new_version_entry: &VersionEntry,
pending: Option<index_recovery::Publication>,
) -> Result<index_recovery::Publication> {
merge_version_entry(
Self::read_existing_versions(
&index_path.join(PackageIndex::package_index_path(package_name)),
)?,
new_version_entry.clone(),
)?;
Ok(match pending {
Some(pending) => pending,
None => index_recovery::Publication::start(
index_path,
package_name,
git_url,
new_version_entry.clone(),
)?,
})
}
fn commit_index_publication(
&self,
index_path: &Path,
index_name: &str,
package_entry: &PackageEntry,
verbose: bool,
) -> Result<()> {
crate::registry::index_writer::update_index_metadata(index_path, index_name)?;
let commit_message = self.index_message.clone().unwrap_or_else(|| {
format!(
"Update {} to {}",
package_entry.name,
package_entry
.versions
.first()
.map(|v| v.version.as_str())
.unwrap_or("unknown")
)
});
crate::registry::index_writer::commit_changes(index_path, &commit_message, verbose)?;
Ok(())
}
fn validate_index_preview(
destination: &index_destination::Destination,
package: &str,
version: VersionEntry,
) -> Result<()> {
if destination.local {
let file =
Path::new(&destination.repository).join(PackageIndex::package_index_path(package));
merge_version_entry(Self::read_existing_versions(&file)?, version.clone())?;
}
println!("Index validated: version {}, tag {}. No index files, commits, or remotes changed; use --push to publish. Remote duplicate versions are checked at publication.", version.version, version.tag);
Ok(())
}
fn upload_index_artifact(
index_path: &Path,
pending: &mut index_recovery::Publication,
artifact: Option<&(artifact::PreparedUpload, artifact::ArchiveSnapshot)>,
) -> Result<()> {
if let Some((upload, snapshot)) = artifact {
if !pending.uploaded {
upload.upload_registry(&snapshot.path)?;
}
println!(
"Artifact uploaded. If a later index step fails, the artifact remains at {}",
upload.url
);
}
pending.uploaded = true;
pending.save(index_path)?;
Ok(())
}
fn complete_pending_publication(
&self,
index_path: &Path,
destination: (&str, &str),
package_name: &str,
pending: &index_recovery::Publication,
verbose: bool,
) -> Result<bool> {
let (index_name, index_repo) = destination;
let file = index_path.join(PackageIndex::package_index_path(package_name));
let versions = Self::read_existing_versions(&file)?;
if versions
.iter()
.any(|version| version.version == pending.version.version)
{
pending.verify_entry(index_path)?;
if !pending.uploaded {
bail!("Pending publication has no completed artifact upload");
}
let entry = serde_json::from_slice(&fs::read(file)?)?;
self.push_and_report(
index_path,
index_name,
index_repo,
package_name,
&entry,
verbose,
)?;
pending.finish(index_path)?;
return Ok(true);
}
Ok(false)
}
fn prepare_index_artifact(
&self,
project_dir: &Path,
config_path: &Path,
package_name: &str,
version: &str,
tag: &str,
new_version_entry: &mut VersionEntry,
) -> Result<Option<(artifact::PreparedUpload, artifact::ArchiveSnapshot)>> {
Ok(if self.artifact_provider.is_some() {
let archive = self.path.clone().unwrap_or_else(|| {
project_dir.join("target/release/package").join(format!(
"{}_CCGO_PACKAGE-{version}.zip",
package_name.to_uppercase()
))
});
let snapshot = artifact::ArchiveSnapshot::new(&archive)?;
let raw_config: toml::Value = toml::from_str(&fs::read_to_string(config_path)?)?;
let upload = artifact::PreparedUpload::prepare(
self,
&raw_config,
package_name,
version,
tag,
"index",
)?;
new_version_entry.archive_url = Some(upload.url.clone());
new_version_entry.archive_format = Some("zip".to_owned());
new_version_entry.checksum = Some(snapshot.checksum.clone());
println!(
"Artifact URL: {}\nChecksum: {}",
upload.url, snapshot.checksum
);
Some((upload, snapshot))
} else {
None
})
}
fn push_and_report(
&self,
index_path: &std::path::Path,
index_name: &str,
index_repo: &str,
package_name: &str,
package_entry: &PackageEntry,
verbose: bool,
) -> Result<()> {
if self.push && !matches!(self.registry, RegistryType::Local) {
println!("\n📤 Pushing to remote...");
crate::registry::index_writer::push_changes_retry(index_path, index_name, verbose)?;
println!("✅ Pushed successfully!");
} else {
if matches!(self.registry, RegistryType::Local) {
println!("\nIndex published locally; no remote was contacted.");
} else {
println!("\nChanges committed locally. Use --push to push to remote.");
}
}
println!("\n✅ Package index updated successfully!");
println!("\n📋 To use this package:");
println!(
" 1. Add registry: ccgo registry add {} {}",
index_name, index_repo
);
println!(" 2. Add dependency: [dependencies]");
println!(
" {} = \"^{}\"",
package_name,
package_entry
.versions
.first()
.map(|v| v.version.as_str())
.unwrap_or("1.0.0")
);
Ok(())
}
fn get_git_remote_url(&self, project_dir: &Path) -> Result<String> {
let output = Command::new("git")
.current_dir(project_dir)
.args(["remote", "get-url", "origin"])
.output()
.context("Failed to get git remote URL")?;
if !output.status.success() {
bail!("No git remote 'origin' found");
}
let url = String::from_utf8_lossy(&output.stdout).trim().to_string();
if url.starts_with("git@github.com:") {
let path = url.trim_start_matches("git@github.com:");
Ok(format!("https://github.com/{}", path))
} else {
Ok(url)
}
}
fn validate_tag_exists(&self, project_dir: &Path, tag: &str) -> Result<()> {
let refspec = format!("refs/tags/{}", tag);
let output = Command::new("git")
.current_dir(project_dir)
.args(["rev-parse", "--verify", "--quiet", &refspec])
.output()
.context("Failed to spawn `git rev-parse`")?;
if !output.status.success() {
bail!(
"git tag '{}' not found in {}. Create the tag locally with \
`git tag {}` (and `git push --tags`) before publishing it \
to the index.",
tag,
project_dir.display(),
tag
);
}
Ok(())
}
fn build_single_version_entry(
&self,
project_dir: &Path,
package_name: &str,
version: &str,
tag: &str,
verbose: bool,
) -> Result<VersionEntry> {
let archive_url = self
.archive_url_template
.as_deref()
.map(|tpl| substitute_archive_url(tpl, package_name, version, tag));
let archive_format = archive_url.as_ref().map(|_| self.archive_format.clone());
let checksum = if self.checksum && self.artifact_provider.is_none() {
if verbose {
println!(" Computing checksum for {}...", tag);
}
if archive_url.is_some() {
let result = self.compute_archive_zip_checksum(project_dir, package_name, version);
if self.path.is_some() {
Some(result?)
} else {
result.ok()
}
} else {
self.compute_tag_checksum(project_dir, tag).ok()
}
} else {
None
};
Ok(VersionEntry {
version: version.to_string(),
tag: tag.to_string(),
checksum,
archive_url,
archive_format,
released_at: self.get_tag_date(project_dir, tag).ok(),
yanked: false,
yanked_reason: None,
})
}
fn read_existing_versions(package_file: &Path) -> Result<Vec<VersionEntry>> {
if !package_file.exists() {
return Ok(Vec::new());
}
let bytes = std::fs::read_to_string(package_file)
.with_context(|| format!("failed to read {}", package_file.display()))?;
let entry: PackageEntry = serde_json::from_str(&bytes).with_context(|| {
format!(
"failed to parse existing index entry at {}",
package_file.display()
)
})?;
Ok(entry.versions)
}
fn compute_archive_zip_checksum(
&self,
project_dir: &Path,
package_name: &str,
version: &str,
) -> Result<String> {
let zip_name = format!(
"{}_CCGO_PACKAGE-{}.zip",
package_name.to_uppercase(),
version
);
let zip_path = self.path.clone().unwrap_or_else(|| {
project_dir
.join("target")
.join("release")
.join("package")
.join(&zip_name)
});
if !zip_path.is_file() {
anyhow::bail!(
"expected archive at {} for index checksum; \
run `ccgo package --release` first or skip this tag",
zip_path.display()
);
}
let bytes = std::fs::read(&zip_path)
.with_context(|| format!("failed to read {}", zip_path.display()))?;
Ok(format!("sha256:{:x}", Sha256::digest(&bytes)))
}
fn compute_tag_checksum(&self, project_dir: &Path, tag: &str) -> Result<String> {
let mut child = Command::new("git")
.current_dir(project_dir)
.args(["archive", "--format=tar.gz", tag])
.stdout(Stdio::piped())
.spawn()
.context("Failed to run git archive")?;
let stdout = child
.stdout
.take()
.ok_or_else(|| anyhow::anyhow!("Failed to capture git archive output"))?;
let mut hasher = Sha256::new();
let mut reader = std::io::BufReader::new(stdout);
let mut buffer = [0u8; 8192];
loop {
let bytes_read = reader
.read(&mut buffer)
.context("Failed to read git archive output")?;
if bytes_read == 0 {
break;
}
hasher.update(&buffer[..bytes_read]);
}
let status = child.wait().context("Failed to wait for git archive")?;
if !status.success() {
bail!("git archive failed for tag {}", tag);
}
let hash = hasher.finalize();
Ok(format!("sha256:{:x}", hash))
}
fn get_tag_date(&self, project_dir: &Path, tag: &str) -> Result<String> {
let output = Command::new("git")
.current_dir(project_dir)
.args(["log", "-1", "--format=%aI", tag])
.output()
.context("Failed to get tag date")?;
if output.status.success() {
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
} else {
bail!("Failed to get tag date")
}
}
fn get_supported_platforms(&self, config: &CcgoConfig) -> Vec<String> {
let mut platforms = Vec::new();
if let Some(ref platform_configs) = config.platforms {
if platform_configs.android.is_some() {
platforms.push("android".to_string());
}
if platform_configs.ios.is_some() {
platforms.push("ios".to_string());
}
if platform_configs.macos.is_some() {
platforms.push("macos".to_string());
}
if platform_configs.windows.is_some() {
platforms.push("windows".to_string());
}
if platform_configs.linux.is_some() {
platforms.push("linux".to_string());
}
if platform_configs.ohos.is_some() {
platforms.push("ohos".to_string());
}
}
if platforms.is_empty() {
platforms = vec![
"android".to_string(),
"ios".to_string(),
"macos".to_string(),
"linux".to_string(),
"windows".to_string(),
];
}
platforms
}
fn find_project_dir(current_dir: &Path) -> Result<PathBuf> {
if let Ok(entries) = fs::read_dir(current_dir) {
for entry in entries.flatten() {
if let Ok(file_type) = entry.file_type() {
if file_type.is_dir() {
let config_path = entry.path().join("CCGO.toml");
if config_path.exists() {
return Ok(entry.path());
}
}
}
}
}
let config_path = current_dir.join("CCGO.toml");
if config_path.exists() {
return Ok(current_dir.to_path_buf());
}
bail!("CCGO.toml not found in project directory");
}
fn find_file_with_extension(&self, dir: &Path, extension: &str) -> Result<PathBuf> {
if let Ok(entries) = fs::read_dir(dir) {
for entry in entries.flatten() {
let path = entry.path();
if path.extension().and_then(|s| s.to_str())
== Some(extension.trim_start_matches('.'))
{
return Ok(path);
}
}
}
bail!("No {} file found", extension);
}
fn get_project_version(&self, project_dir: &Path) -> Result<String> {
let toml_path = project_dir.join("CCGO.toml");
let content = fs::read_to_string(toml_path).context("Failed to read CCGO.toml")?;
let config: toml::Value = toml::from_str(&content)?;
config
.get("project")
.or_else(|| config.get("package"))
.and_then(|package| package.get("version"))
.and_then(|version| version.as_str())
.map(str::to_owned)
.context("Project version not found in CCGO.toml")
}
fn git_url_to_pages_url(&self, git_url: &str) -> Option<String> {
if git_url.starts_with("git@github.com:") {
let parts: Vec<&str> = git_url
.trim_end_matches(".git")
.trim_start_matches("git@github.com:")
.split('/')
.collect();
if parts.len() == 2 {
return Some(format!("https://{}.github.io/{}/", parts[0], parts[1]));
}
}
else if git_url.starts_with("https://github.com/") {
let parts: Vec<&str> = git_url
.trim_end_matches(".git")
.trim_start_matches("https://github.com/")
.split('/')
.collect();
if parts.len() == 2 {
return Some(format!("https://{}.github.io/{}/", parts[0], parts[1]));
}
}
None
}
}
fn find_project_root(input: &Path) -> Option<PathBuf> {
let start = if input.is_file() {
input.parent()?
} else {
input
};
start
.ancestors()
.find(|dir| dir.join("CCGO.toml").is_file())
.map(Path::to_path_buf)
}
struct CurrentDirGuard(PathBuf);
impl CurrentDirGuard {
fn change(root: &Path) -> Result<Self> {
let previous = std::env::current_dir()?;
std::env::set_current_dir(root)?;
Ok(Self(previous))
}
}
impl Drop for CurrentDirGuard {
fn drop(&mut self) {
let _ = std::env::set_current_dir(&self.0);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn finds_the_nearest_project_root_for_directories() {
let tmp = tempfile::tempdir().unwrap();
fs::create_dir_all(tmp.path().join("android")).unwrap();
fs::write(tmp.path().join("CCGO.toml"), "").unwrap();
assert_eq!(
find_project_root(&tmp.path().join("android")),
Some(tmp.path().to_path_buf())
);
assert_eq!(
find_project_root(tmp.path()),
Some(tmp.path().to_path_buf())
);
let outside = tempfile::tempdir().unwrap();
assert_eq!(find_project_root(outside.path()), None);
}
#[test]
fn remote_and_legacy_aliases_share_one_destination() {
use clap::Parser;
#[derive(Parser)]
struct Cli {
#[command(flatten)]
publish: PublishCommand,
}
for flag in ["--remote", "--url", "--remote-name"] {
let parsed =
Cli::try_parse_from(["ccgo", "ohos", flag, "https://repo.example"]).unwrap();
assert_eq!(
parsed.publish.remote.as_deref(),
Some("https://repo.example")
);
}
for (first, second) in [
("--remote", "--url"),
("--remote", "--remote-name"),
("--url", "--remote-name"),
] {
assert!(Cli::try_parse_from(["ccgo", "ohos", first, "one", second, "two"]).is_err());
}
}
fn parse_publish(args: &[&str]) -> PublishCommand {
use clap::Parser;
let cli = crate::cli::Cli::try_parse_from(
["ccgo", "publish"].into_iter().chain(args.iter().copied()),
)
.unwrap();
match cli.command {
crate::cli::Commands::Publish(command) => *command,
_ => unreachable!(),
}
}
#[test]
fn package_publish_accepts_legacy_index_alias() {
for target in ["package", "index"] {
let command = parse_publish(&[target]);
assert!(matches!(command.target, PublishTarget::Index));
assert_eq!(command.target.to_string(), "package");
}
let targets: Vec<_> = PublishTarget::value_variants()
.iter()
.map(|target| target.to_possible_value().unwrap().get_name().to_owned())
.collect();
assert!(targets.iter().any(|target| target == "package"));
assert!(!targets.iter().any(|target| target == "index"));
}
#[test]
fn publish_path_validates_platform_and_file_type() {
let tmp = tempfile::tempdir().unwrap();
let aar = tmp.path().join("custom.aar");
fs::write(&aar, b"aar").unwrap();
assert!(parse_publish(&["android"])
.validate_publish_path(&aar)
.is_ok());
assert!(parse_publish(&["ohos"])
.validate_publish_path(&aar)
.is_err());
assert!(parse_publish(&["android"])
.validate_publish_path(tmp.path())
.is_ok());
assert!(parse_publish(&["android"])
.validate_publish_path(&tmp.path().join("missing.aar"))
.is_err());
for target in ["conan", "doc", "kmp", "apple"] {
assert!(parse_publish(&[target])
.validate_publish_path(&aar)
.is_err());
}
let podspec = tmp.path().join("Library.podspec");
fs::write(&podspec, b"spec").unwrap();
assert!(parse_publish(&["apple", "--manager", "cocoapods"])
.validate_publish_path(&podspec)
.is_ok());
fs::write(tmp.path().join("gradlew"), b"wrapper").unwrap();
assert!(parse_publish(&["kmp"])
.validate_publish_path(tmp.path())
.is_ok());
}
#[test]
fn index_path_hashes_explicit_archive_and_propagates_read_errors() {
let tmp = tempfile::tempdir().unwrap();
let zip = tmp.path().join("custom.zip");
let payload = b"custom archive";
fs::write(&zip, payload).unwrap();
assert!(parse_publish(&["index"])
.validate_publish_path(&zip)
.is_err());
let mut command = parse_publish(&[
"index",
"--checksum",
"--archive-url-template",
"https://cdn.example/{version}.zip",
]);
command.validate_publish_path(&zip).unwrap();
command.path = Some(zip.clone());
let entry = command
.build_single_version_entry(tmp.path(), "lib", "1.0.0", "v1.0.0", false)
.unwrap();
assert_eq!(
entry.checksum,
Some(format!("sha256:{:x}", Sha256::digest(payload)))
);
fs::remove_file(zip).unwrap();
assert!(command
.build_single_version_entry(tmp.path(), "lib", "1.0.0", "v1.0.0", false)
.is_err());
}
#[test]
fn index_directory_resolves_one_archive_and_rejects_ambiguity() {
let tmp = tempfile::tempdir().unwrap();
let archive = tmp.path().join("SDK.zip");
fs::write(&archive, b"sdk").unwrap();
let mut command = parse_publish(&[
"index",
"--checksum",
"--archive-url-template",
"https://cdn.example/sdk.zip",
]);
command.path = Some(tmp.path().to_path_buf());
command.prepare_publish_path().unwrap();
assert_eq!(command.path, Some(fs::canonicalize(&archive).unwrap()));
fs::write(tmp.path().join("another.zip"), b"sdk").unwrap();
command.path = Some(tmp.path().to_path_buf());
assert!(command
.prepare_publish_path()
.err()
.unwrap()
.to_string()
.contains("Multiple .zip"));
}
#[test]
fn substitute_archive_url_replaces_all_placeholders() {
let template = "https://cdn.example.com/{name}/{name}_CCGO_PACKAGE-{version}.zip";
let result = substitute_archive_url(template, "stdcomm", "1.0.0", "v1.0.0");
assert_eq!(
result,
"https://cdn.example.com/stdcomm/stdcomm_CCGO_PACKAGE-1.0.0.zip"
);
}
#[test]
fn substitute_archive_url_with_tag_placeholder() {
let template =
"https://gh.example.com/{name}/releases/download/{tag}/{name}-{version}.tar.gz";
let result = substitute_archive_url(template, "leaf", "1.0.0", "v1.0.0");
assert_eq!(
result,
"https://gh.example.com/leaf/releases/download/v1.0.0/leaf-1.0.0.tar.gz"
);
}
#[test]
fn compute_archive_zip_checksum_hashes_local_package_zip() {
let tmp = tempfile::TempDir::new().unwrap();
let project_dir = tmp.path();
let pkg_dir = project_dir.join("target/release/package");
std::fs::create_dir_all(&pkg_dir).unwrap();
let payload = b"synthetic-zip-bytes-for-checksum-test";
std::fs::write(pkg_dir.join("STDCOMM_CCGO_PACKAGE-1.0.0.zip"), payload).unwrap();
let cmd = PublishCommand {
target: PublishTarget::Index,
registry: RegistryType::Local,
remote: None,
skip_build: false,
path: None,
artifact_provider: None,
maven_registry: MavenRegistry::Private,
artifact_group: None,
artifact_id: None,
github_repo: None,
release: false,
stl: None,
yes: true,
manager: AppleManager::All,
push: false,
platform: None,
linkage: AppleLinkage::Static,
pod_name: None,
shared_pod_name: None,
allow_warnings: true,
conan: Default::default(),
profile: "default".into(),
link_type: "both".into(),
doc_branch: "gh-pages".into(),
doc_force: false,
doc_open: false,
index_repo: None,
index_name: None,
index_message: None,
checksum: true,
archive_url_template: None,
archive_format: "zip".into(),
version: None,
tag: None,
};
let got = cmd
.compute_archive_zip_checksum(project_dir, "stdcomm", "1.0.0")
.expect("local zip should be hashable");
let expected = format!("sha256:{:x}", Sha256::digest(payload));
assert_eq!(got, expected);
}
#[test]
fn compute_archive_zip_checksum_errors_when_local_zip_missing() {
let tmp = tempfile::TempDir::new().unwrap();
let cmd = PublishCommand {
target: PublishTarget::Index,
registry: RegistryType::Local,
remote: None,
skip_build: false,
path: None,
artifact_provider: None,
maven_registry: MavenRegistry::Private,
artifact_group: None,
artifact_id: None,
github_repo: None,
release: false,
stl: None,
yes: true,
manager: AppleManager::All,
push: false,
platform: None,
linkage: AppleLinkage::Static,
pod_name: None,
shared_pod_name: None,
allow_warnings: true,
conan: Default::default(),
profile: "default".into(),
link_type: "both".into(),
doc_branch: "gh-pages".into(),
doc_force: false,
doc_open: false,
index_repo: None,
index_name: None,
index_message: None,
checksum: true,
archive_url_template: None,
archive_format: "zip".into(),
version: None,
tag: None,
};
let err = cmd
.compute_archive_zip_checksum(tmp.path(), "stdcomm", "9.9.9")
.expect_err("missing local zip should error");
let msg = err.to_string();
assert!(
msg.contains("STDCOMM_CCGO_PACKAGE-9.9.9.zip"),
"expected expected-path in error, got: {msg}"
);
assert!(
msg.contains("ccgo package --release"),
"expected reproduction hint in error, got: {msg}"
);
}
fn entry(version: &str, tag: &str) -> VersionEntry {
VersionEntry {
version: version.into(),
tag: tag.into(),
checksum: None,
archive_url: None,
archive_format: None,
released_at: None,
yanked: false,
yanked_reason: None,
}
}
#[test]
fn derive_version_strips_v_prefix() {
assert_eq!(derive_version_from_tag("v1.0.0"), "1.0.0");
assert_eq!(derive_version_from_tag("V25.2.9519653"), "25.2.9519653");
}
#[test]
fn derive_version_keeps_tag_without_v_prefix_unchanged() {
assert_eq!(derive_version_from_tag("1.0.0"), "1.0.0");
}
#[test]
fn derive_version_does_not_touch_tags_with_other_prefixes() {
assert_eq!(derive_version_from_tag("release-1.0.0"), "release-1.0.0");
}
#[test]
fn default_tag_prepends_v() {
assert_eq!(default_tag_for_version("1.0.0"), "v1.0.0");
assert_eq!(default_tag_for_version("25.2.9519653"), "v25.2.9519653");
}
#[test]
fn index_version_defaults_to_config_and_preserves_explicit_overrides() {
for (args, expected_version, expected_tag) in [
(vec!["index"], "1.2.3", "v1.2.3"),
(vec!["index", "--version", "2.0.0"], "2.0.0", "v2.0.0"),
(vec!["index", "--tag", "v3.0.0"], "3.0.0", "v3.0.0"),
(
vec!["index", "--version", "4.0.0", "--tag", "release-4"],
"4.0.0",
"release-4",
),
] {
let command = parse_publish(&args);
assert_eq!(
command.resolve_version_and_tag("1.2.3").unwrap(),
(expected_version.to_owned(), expected_tag.to_owned())
);
}
}
#[test]
fn merge_appends_to_empty_existing_versions() {
let merged = merge_version_entry(Vec::new(), entry("1.0.0", "v1.0.0")).unwrap();
assert_eq!(merged.len(), 1);
assert_eq!(merged[0].version, "1.0.0");
}
#[test]
fn merge_appends_to_existing_and_sorts_descending() {
let existing = vec![entry("1.0.0", "v1.0.0"), entry("0.9.0", "v0.9.0")];
let merged = merge_version_entry(existing, entry("2.0.0", "v2.0.0")).unwrap();
assert_eq!(merged.len(), 3);
assert_eq!(merged[0].version, "2.0.0");
assert_eq!(merged[1].version, "1.0.0");
assert_eq!(merged[2].version, "0.9.0");
}
#[test]
fn merge_rejects_duplicate_version() {
let existing = vec![entry("1.0.0", "v1.0.0")];
let err = merge_version_entry(existing, entry("1.0.0", "v1.0.0")).unwrap_err();
let msg = err.to_string();
assert!(
msg.contains("'1.0.0'") && msg.contains("already in the index"),
"expected duplicate-version error, got: {msg}"
);
}
}