ccgo 3.10.5

A high-performance C++ cross-platform build CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
//! Shared Maven and GitHub storage for Apple binaries and CCGO packages.
use super::{ArtifactProvider, MavenRegistry, PublishCommand};
use anyhow::{bail, Context, Result};
use reqwest::blocking::Client;
use sha2::{Digest, Sha256};
use std::{
    fs,
    path::{Path, PathBuf},
    process::Command,
};

pub(super) fn coordinate(value: &str) -> Result<&str> {
    if value.is_empty()
        || value == "."
        || value == ".."
        || !value
            .bytes()
            .all(|c| c.is_ascii_alphanumeric() || b"._-".contains(&c))
    {
        bail!("Invalid Maven coordinate: {value}");
    }
    Ok(value)
}

fn write_local(root: &Path, relative: &Path, bytes: &[u8]) -> Result<()> {
    let path = root.join(relative);
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }
    fs::write(&path, bytes)?;
    Ok(())
}

fn append_suffix(path: &Path, suffix: &str) -> PathBuf {
    PathBuf::from(format!("{}{suffix}", path.display()))
}

fn maven_url(base: &str, group: &str, artifact: &str, version: &str) -> Result<String> {
    for part in group.split('.') {
        coordinate(part)?;
    }
    coordinate(artifact)?;
    coordinate(version)?;
    let parsed = reqwest::Url::parse(base).context("Invalid CCGO_MAVEN_URL")?;
    if !matches!(parsed.scheme(), "https" | "http")
        || parsed.host_str().is_none()
        || !parsed.username().is_empty()
        || parsed.password().is_some()
        || parsed.query().is_some()
        || parsed.fragment().is_some()
    {
        bail!("CCGO_MAVEN_URL must be an HTTP(S) repository URL without credentials, query or fragment");
    }
    Ok(format!(
        "{}/{}/{artifact}/{version}/{artifact}-{version}",
        base.trim_end_matches('/'),
        group.replace('.', "/")
    ))
}

fn put(client: &Client, url: &str, bytes: Vec<u8>, username: &str, password: &str) -> Result<()> {
    let mut request = client.put(url).body(bytes);
    if !username.is_empty() {
        request = request.basic_auth(username, Some(password));
    }
    let response = request.send().context("Maven upload failed")?;
    if !response.status().is_success() {
        bail!(
            "Maven upload failed (HTTP {}); check hosted repository permissions",
            response.status()
        );
    }
    Ok(())
}

fn get_existing(
    client: &Client,
    url: &str,
    username: &str,
    password: &str,
) -> Result<Option<Vec<u8>>> {
    let mut request = client.get(url);
    if !username.is_empty() {
        request = request.basic_auth(username, Some(password));
    }
    let response = request
        .send()
        .context("Failed to inspect existing Maven artifact")?;
    if response.status() == reqwest::StatusCode::NOT_FOUND {
        return Ok(None);
    }
    if !response.status().is_success() {
        bail!("Cannot inspect Maven artifact (HTTP {})", response.status());
    }
    Ok(Some(response.bytes()?.to_vec()))
}

fn put_immutable(
    client: &Client,
    url: &str,
    bytes: Vec<u8>,
    username: &str,
    password: &str,
) -> Result<()> {
    if let Some(existing) = get_existing(client, url, username, password)? {
        if existing != bytes {
            bail!("Existing Maven artifact differs; refusing to overwrite {url}");
        }
        return Ok(());
    }
    let mut request = client
        .put(url)
        .header(reqwest::header::IF_NONE_MATCH, "*")
        .body(bytes.clone());
    if !username.is_empty() {
        request = request.basic_auth(username, Some(password));
    }
    let response = request.send().context("Maven upload failed")?;
    if !response.status().is_success() && !matches!(response.status().as_u16(), 409 | 412) {
        bail!("Maven upload failed (HTTP {})", response.status());
    }
    if get_existing(client, url, username, password)?.as_deref() != Some(bytes.as_slice()) {
        bail!("Maven artifact verification failed after upload: {url}");
    }
    Ok(())
}

enum Backend {
    Maven {
        root: String,
        group: String,
        username: String,
        password: String,
    },
    Central {
        group: String,
        artifact: String,
        version: String,
    },
    Local {
        root: PathBuf,
        relative: String,
        group: String,
    },
    Github {
        repo: String,
        tag: String,
    },
}

pub(super) struct PreparedUpload {
    pub url: String,
    archive_name: String,
    artifact: String,
    version: String,
    config: toml::Value,
    backend: Backend,
}

fn maven_group(command: &PublishCommand, config: &toml::Value, scope: &str) -> Result<String> {
    let group = command
        .artifact_group
        .as_deref()
        .or_else(|| {
            config
                .get("publish")?
                .get(scope)?
                .get("maven")?
                .get("group_id")?
                .as_str()
        })
        .with_context(|| {
            format!("Maven upload requires --artifact-group or [publish.{scope}.maven].group_id")
        })?;
    coordinate(group)?;
    Ok(group.to_owned())
}

fn maven_destination(
    command: &PublishCommand,
    config: &toml::Value,
    group: &str,
    artifact: &str,
    version: &str,
) -> Result<(String, Backend)> {
    match command.maven_registry {
        MavenRegistry::Official => {
            let root = maven_url("https://repo1.maven.org/maven2", group, artifact, version)?;
            Ok((
                format!("{root}.zip"),
                Backend::Central {
                    group: group.to_owned(),
                    artifact: artifact.to_owned(),
                    version: version.to_owned(),
                },
            ))
        }
        MavenRegistry::Local => {
            let root = super::maven::local_repository(config)?;
            let relative = super::maven::layout_path(group, artifact, version, None, "zip");
            let path = root.join(&relative);
            let url = reqwest::Url::from_file_path(&path)
                .map(|url| url.to_string())
                .unwrap_or_else(|_| path.display().to_string());
            Ok((
                url,
                Backend::Local {
                    root,
                    relative,
                    group: group.to_owned(),
                },
            ))
        }
        MavenRegistry::Private => {
            let get = |key: &str| std::env::var(key).unwrap_or_default();
            let base = get("CCGO_MAVEN_URL");
            if base.trim().is_empty() {
                bail!(
                    "Maven artifact upload requires CCGO_MAVEN_URL \
                     (for example https://repo.example.com/releases); set \
                     CCGO_MAVEN_USERNAME/CCGO_MAVEN_PASSWORD if the repository needs auth"
                );
            }
            if base.contains(',') {
                bail!("Artifact upload requires a single CCGO_MAVEN_URL");
            }
            let root = maven_url(&base, group, artifact, version)?;
            Ok((
                format!("{root}.zip"),
                Backend::Maven {
                    root,
                    group: group.to_owned(),
                    username: get("CCGO_MAVEN_USERNAME"),
                    password: get("CCGO_MAVEN_PASSWORD"),
                },
            ))
        }
    }
}

fn github_destination(
    command: &PublishCommand,
    tag: &str,
    archive_name: &str,
) -> Result<(String, Backend)> {
    let repo = command
        .github_repo
        .as_deref()
        .context("GitHub upload requires --github-repo owner/repo")?;
    let parts: Vec<_> = repo.split('/').collect();
    if parts.len() != 2 {
        bail!("--github-repo must be owner/repo");
    }
    for part in parts {
        coordinate(part)?;
    }
    let mut url = reqwest::Url::parse(&format!("https://github.com/{repo}/releases/download/"))?;
    url.path_segments_mut()
        .unwrap()
        .pop_if_empty()
        .push(tag)
        .push(archive_name);
    Ok((
        url.to_string(),
        Backend::Github {
            repo: repo.to_owned(),
            tag: tag.to_owned(),
        },
    ))
}

impl PreparedUpload {
    pub fn prepare(
        command: &PublishCommand,
        config: &toml::Value,
        name: &str,
        version: &str,
        tag: &str,
        scope: &str,
    ) -> Result<Self> {
        let suffix = if scope == "index" { "ccgo" } else { scope };
        let artifact = command
            .artifact_id
            .clone()
            .unwrap_or_else(|| format!("{name}-{suffix}"));
        Self::prepare_with_artifact(command, config, &artifact, version, tag, scope)
    }

    /// Prepare an upload with an explicit artifact ID (used for `-shared` variants).
    pub fn prepare_with_artifact(
        command: &PublishCommand,
        config: &toml::Value,
        artifact: &str,
        version: &str,
        tag: &str,
        scope: &str,
    ) -> Result<Self> {
        coordinate(artifact)?;
        coordinate(version)?;
        let archive_name = format!("{artifact}-{version}.zip");
        let (url, backend) = match command
            .artifact_provider
            .as_ref()
            .context("Missing artifact provider")?
        {
            ArtifactProvider::Maven => {
                let group = maven_group(command, config, scope)?;
                maven_destination(command, config, &group, artifact, version)?
            }
            ArtifactProvider::Github => github_destination(command, tag, &archive_name)?,
        };
        Ok(Self {
            url,
            archive_name,
            artifact: artifact.to_owned(),
            version: version.to_owned(),
            config: config.clone(),
            backend,
        })
    }

    pub fn upload(&self, archive: &Path) -> Result<()> {
        self.upload_with_policy(archive, false)
    }

    pub fn upload_registry(&self, archive: &Path) -> Result<()> {
        self.upload_with_policy(archive, true)
    }

    fn upload_with_policy(&self, archive: &Path, immutable: bool) -> Result<()> {
        println!("Uploading artifact to {}", self.url);
        match &self.backend {
            Backend::Maven {
                root,
                group,
                username,
                password,
            } => self.upload_maven(archive, immutable, root, group, username, password)?,
            Backend::Central {
                group,
                artifact,
                version,
            } => {
                super::central::publish_artifact(
                    &self.config,
                    group,
                    artifact,
                    version,
                    "zip",
                    archive,
                    false,
                )?;
            }
            Backend::Local {
                root,
                relative,
                group,
            } => self.upload_local(archive, root, relative, group)?,
            Backend::Github { repo, tag } => {
                let status = Command::new("gh")
                    .args(["release", "view", tag, "--repo", repo])
                    .output()
                    .context("Failed to execute gh release view")?;
                if !status.status.success() {
                    bail!("GitHub release {tag} must already exist; create it before uploading");
                }
                // The asset name is independent of the caller's input filename.
                let temporary = tempfile::tempdir()?;
                let named_archive = temporary.path().join(&self.archive_name);
                fs::copy(archive, &named_archive)?;
                if immutable && self.github_existing_matches(repo, tag, archive)? {
                    return Ok(());
                }
                if !Command::new("gh")
                    .args(["release", "upload", tag, "--repo", repo])
                    .arg(&named_archive)
                    .status()?
                    .success()
                {
                    if immutable && self.github_existing_matches(repo, tag, archive)? {
                        return Ok(());
                    }
                    bail!("GitHub release asset upload failed");
                }
            }
        }
        Ok(())
    }

    fn upload_maven(
        &self,
        archive: &Path,
        immutable: bool,
        root: &str,
        group: &str,
        username: &str,
        password: &str,
    ) -> Result<()> {
        let client = Client::builder()
            .redirect(reqwest::redirect::Policy::none())
            .timeout(std::time::Duration::from_secs(300))
            .build()?;
        let artifact = &self.artifact;
        let version = &self.version;
        let pom = format!("<project><modelVersion>4.0.0</modelVersion><groupId>{group}</groupId><artifactId>{artifact}</artifactId><version>{version}</version><packaging>zip</packaging></project>\n");
        for (extension, content) in [("zip", fs::read(archive)?), ("pom", pom.into_bytes())] {
            let hash = format!("{:x}", Sha256::digest(&content));
            let upload = if immutable { put_immutable } else { put };
            upload(
                &client,
                &format!("{root}.{extension}"),
                content,
                username,
                password,
            )?;
            upload(
                &client,
                &format!("{root}.{extension}.sha256"),
                hash.into_bytes(),
                username,
                password,
            )?;
        }
        Ok(())
    }

    fn upload_local(&self, archive: &Path, root: &Path, relative: &str, group: &str) -> Result<()> {
        let artifact = &self.artifact;
        let version = &self.version;
        let pom = format!("<project><modelVersion>4.0.0</modelVersion><groupId>{group}</groupId><artifactId>{artifact}</artifactId><version>{version}</version><packaging>zip</packaging></project>\n");
        let zip = fs::read(archive)?;
        write_local(root, Path::new(relative), &zip)?;
        let pom_relative = Path::new(relative).with_extension("pom");
        write_local(root, &pom_relative, pom.as_bytes())?;
        let zip_hash = format!("{:x}", Sha256::digest(&zip));
        write_local(
            root,
            &append_suffix(Path::new(relative), ".sha256"),
            zip_hash.as_bytes(),
        )?;
        let pom_hash = format!("{:x}", Sha256::digest(pom.as_bytes()));
        write_local(
            root,
            &append_suffix(&pom_relative, ".sha256"),
            pom_hash.as_bytes(),
        )?;
        Ok(())
    }

    fn github_existing_matches(&self, repo: &str, tag: &str, archive: &Path) -> Result<bool> {
        let temporary = tempfile::tempdir()?;
        let output = Command::new("gh")
            .args([
                "release",
                "download",
                tag,
                "--repo",
                repo,
                "--pattern",
                &self.archive_name,
                "--dir",
            ])
            .arg(temporary.path())
            .output()
            .context("Failed to inspect GitHub release asset")?;
        if !output.status.success() {
            return Ok(false); // Upload without --clobber remains a create-only operation.
        }
        if fs::read(temporary.path().join(&self.archive_name))? != fs::read(archive)? {
            bail!(
                "Existing GitHub asset differs; refusing to overwrite {}",
                self.url
            );
        }
        Ok(true)
    }
}

/// Freeze the bytes used for validation, checksums, and upload without touching input files.
pub(super) struct ArchiveSnapshot {
    pub path: std::path::PathBuf,
    pub checksum: String,
    _directory: tempfile::TempDir,
}

impl ArchiveSnapshot {
    pub fn new(source: &Path) -> Result<Self> {
        use std::io::Read;
        let directory = tempfile::tempdir()?;
        let path = directory.path().join("archive.zip");
        fs::copy(source, &path).with_context(|| {
            format!(
                "Cannot read artifact ZIP {}; run ccgo package --release or specify --path",
                source.display()
            )
        })?;
        let mut zip =
            zip::ZipArchive::new(fs::File::open(&path)?).context("Invalid artifact ZIP")?;
        if zip.is_empty() {
            bail!("Artifact ZIP must not be empty");
        }
        for index in 0..zip.len() {
            zip.by_index(index)?
                .enclosed_name()
                .context("Unsafe path in artifact ZIP")?;
        }
        let mut file = fs::File::open(&path)?;
        let mut hasher = Sha256::new();
        let mut buffer = [0u8; 65536];
        loop {
            let read = file.read(&mut buffer)?;
            if read == 0 {
                break;
            }
            hasher.update(&buffer[..read]);
        }
        Ok(Self {
            path,
            checksum: format!("sha256:{:x}", hasher.finalize()),
            _directory: directory,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn urls_and_coordinates() {
        assert_eq!(
            maven_url(
                "https://repo.example/releases/",
                "com.example",
                "sdk-apple",
                "1.2.3"
            )
            .unwrap(),
            "https://repo.example/releases/com/example/sdk-apple/1.2.3/sdk-apple-1.2.3"
        );
        for group in ["../bad", "com..example", "a&b"] {
            assert!(maven_url("https://repo.example", group, "sdk", "1").is_err());
        }
        assert!(maven_url(
            "https://user:password@repo.example",
            "com.example",
            "sdk",
            "1"
        )
        .is_err());
    }

    #[test]
    fn maven_local_provider_uses_the_local_repository() {
        use clap::Parser;
        #[derive(Parser)]
        struct Cli {
            #[command(flatten)]
            command: PublishCommand,
        }
        let command = Cli::parse_from([
            "ccgo",
            "doc",
            "--artifact-provider",
            "maven",
            "--maven-registry",
            "local",
            "--artifact-group",
            "com.example",
        ])
        .command;
        let repository = tempfile::tempdir().unwrap();
        let config: toml::Value = toml::from_str(&format!(
            "[project]\nname='demo'\nversion='1.0.0'\n\
             [publish.maven]\nlocal_path='{}'\n",
            repository.path().display()
        ))
        .unwrap();
        let upload =
            PreparedUpload::prepare(&command, &config, "demo", "1.0.0", "v1.0.0", "doc").unwrap();
        assert!(upload.url.starts_with("file://"));
        assert!(upload
            .url
            .ends_with("com/example/demo-doc/1.0.0/demo-doc-1.0.0.zip"));

        let archive = tempfile::NamedTempFile::new().unwrap();
        std::fs::write(archive.path(), b"zip-bytes").unwrap();
        upload.upload(archive.path()).unwrap();
        let directory = repository.path().join("com/example/demo-doc/1.0.0");
        assert_eq!(
            std::fs::read(directory.join("demo-doc-1.0.0.zip")).unwrap(),
            b"zip-bytes"
        );
        assert!(directory.join("demo-doc-1.0.0.pom").is_file());
        assert!(directory.join("demo-doc-1.0.0.zip.sha256").is_file());
    }

    #[test]
    fn central_provider_uses_the_public_maven_url() {
        use clap::Parser;
        #[derive(Parser)]
        struct Cli {
            #[command(flatten)]
            command: PublishCommand,
        }
        let command = Cli::parse_from([
            "ccgo",
            "doc",
            "--artifact-provider",
            "maven",
            "--maven-registry",
            "official",
            "--artifact-group",
            "com.example",
        ])
        .command;
        let config: toml::Value =
            toml::from_str("[project]\nname='demo'\nversion='1.0.0'\n").unwrap();
        let upload =
            PreparedUpload::prepare(&command, &config, "demo", "1.0.0", "v1.0.0", "doc").unwrap();
        assert_eq!(
            upload.url,
            "https://repo1.maven.org/maven2/com/example/demo-doc/1.0.0/demo-doc-1.0.0.zip"
        );
    }
}