cargo-dist 0.32.0

Shippable application packaging for Rust
Documentation
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
//! conversion layer from v0 config to v1 config

use artifacts::archives::ArchiveLayer;
use artifacts::ArtifactLayer;
use builds::cargo::CargoBuildLayer;
use builds::{BuildLayer, CommonBuildLayer};
use ci::github::GithubCiLayer;
use ci::{CiLayer, CommonCiLayer};
use hosts::github::GithubHostLayer;
use hosts::{CommonHostLayer, HostLayer};
use installers::homebrew::HomebrewInstallerLayer;
use installers::npm::NpmInstallerLayer;
use installers::pkg::PkgInstallerLayer;
use installers::{CommonInstallerLayer, InstallerLayer};
use layer::BoolOr;
use publishers::{CommonPublisherLayer, PublisherLayer};

use crate::config::v1::hosts::simple::SimpleHostLayer;

use super::v0::DistMetadata;
use super::{v1::*, CiStyle, HostingStyle, InstallerStyle, JobStyle, MacPkgConfig, PublishStyle};

impl DistMetadata {
    /// Convert the v0 config format to v1
    pub fn to_toml_layer(&self, is_global: bool) -> TomlLayer {
        let DistMetadata {
            cargo_dist_version,
            cargo_dist_url_override,
            rust_toolchain_version,
            dist,
            ci,
            pr_run_mode,
            allow_dirty,
            installers,
            install_success_msg,
            tap,
            formula,
            system_dependencies,
            targets,
            include,
            auto_includes,
            msvc_crt_static,
            windows_archive,
            unix_archive,
            npm_package,
            npm_shrinkwrap,
            npm_scope,
            checksum,
            precise_builds,
            merge_tasks,
            fail_fast,
            cache_builds,
            build_local_artifacts,
            dispatch_releases,
            release_branch,
            install_path,
            features,
            default_features,
            all_features,
            plan_jobs,
            local_artifacts_jobs,
            global_artifacts_jobs,
            source_tarball,
            recursive_tarball,
            host_jobs,
            publish_jobs,
            post_announce_jobs,
            publish_prereleases,
            force_latest,
            create_release,
            github_releases_repo,
            github_releases_submodule_path,
            github_release,
            github_action_commits,
            ssldotcom_windows_sign,
            macos_sign,
            mac_pkg_config,
            github_attestations,
            github_attestations_filters,
            github_attestations_phase,
            hosting,
            extra_artifacts,
            github_custom_runners,
            github_custom_job_permissions,
            bin_aliases,
            tag_namespace,
            install_updater,
            always_use_latest_updater,
            display,
            display_name,
            package_libraries,
            install_libraries,
            github_build_setup,
            min_glibc_version,
            binaries,
            cargo_auditable,
            cargo_cyclonedx,
            omnibor,
            simple_download_url,
        } = self.clone();

        // Archives
        let needs_archive_layer = include.is_some()
            || auto_includes.is_some()
            || windows_archive.is_some()
            || unix_archive.is_some()
            || package_libraries.is_some()
            || binaries.is_some();
        let archive_layer = needs_archive_layer.then_some(ArchiveLayer {
            include,
            auto_includes,
            windows_archive,
            unix_archive,
            package_libraries,
            binaries,
        });
        let needs_artifacts = archive_layer.is_some()
            || source_tarball.is_some()
            || recursive_tarball.is_some()
            || extra_artifacts.is_some()
            || checksum.is_some();
        let artifacts_layer = needs_artifacts.then_some(ArtifactLayer {
            archives: archive_layer,
            source_tarball,
            recursive_tarball,
            extra: extra_artifacts,
            checksum,
        });

        // Builds
        let needs_cargo_build_layer = rust_toolchain_version.is_some()
            || precise_builds.is_some()
            || features.is_some()
            || default_features.is_some()
            || all_features.is_some()
            || cargo_auditable.is_some()
            || cargo_cyclonedx.is_some()
            || msvc_crt_static.is_some();
        let cargo_layer = needs_cargo_build_layer.then_some(BoolOr::Val(CargoBuildLayer {
            common: CommonBuildLayer::default(),
            rust_toolchain_version,
            precise_builds,
            features,
            default_features,
            all_features,
            msvc_crt_static,
            cargo_auditable,
            cargo_cyclonedx,
        }));
        let needs_build_layer = cargo_layer.is_some()
            || system_dependencies.is_some()
            || ssldotcom_windows_sign.is_some()
            || macos_sign.is_some()
            || msvc_crt_static.is_some()
            || min_glibc_version.is_some()
            || omnibor.is_some();
        let build_layer = needs_build_layer.then_some(BuildLayer {
            common: CommonBuildLayer {},
            ssldotcom_windows_sign,
            macos_sign,
            system_dependencies,
            cargo: cargo_layer,
            generic: None,
            min_glibc_version,
            omnibor,
        });

        // CI
        let github_ci_layer = list_to_bool_layer(is_global, &ci, CiStyle::Github, || {
            if github_custom_runners.is_some()
                || github_custom_job_permissions.is_some()
                || github_build_setup.is_some()
                || github_action_commits.is_some()
            {
                Some(GithubCiLayer {
                    common: CommonCiLayer::default(),
                    runners: github_custom_runners,
                    permissions: github_custom_job_permissions,
                    build_setup: github_build_setup,
                    action_commits: github_action_commits,
                })
            } else {
                None
            }
        });
        let has_github_ci = github_ci_layer.is_some();
        let custom_publish_jobs = publish_jobs.as_ref().map(|jobs| {
            jobs.iter()
                .filter_map(|p| {
                    if let PublishStyle::User(path) = p {
                        Some(JobStyle::User(path.to_owned()))
                    } else {
                        None
                    }
                })
                .collect::<Vec<_>>()
        });
        let custom_publish_jobs = if custom_publish_jobs
            .as_deref()
            .unwrap_or_default()
            .is_empty()
        {
            None
        } else {
            custom_publish_jobs
        };
        let needs_ci_layer = github_ci_layer.is_some()
            || merge_tasks.is_some()
            || fail_fast.is_some()
            || cache_builds.is_some()
            || build_local_artifacts.is_some()
            || dispatch_releases.is_some()
            || release_branch.is_some()
            || pr_run_mode.is_some()
            || tag_namespace.is_some()
            || plan_jobs.is_some()
            || local_artifacts_jobs.is_some()
            || global_artifacts_jobs.is_some()
            || host_jobs.is_some()
            || custom_publish_jobs.is_some();
        let ci_layer = needs_ci_layer.then_some(CiLayer {
            common: CommonCiLayer {
                merge_tasks,
                fail_fast,
                cache_builds,
                build_local_artifacts,
                dispatch_releases,
                release_branch,
                pr_run_mode,
                tag_namespace,
                plan_jobs,
                build_local_jobs: local_artifacts_jobs,
                build_global_jobs: global_artifacts_jobs,
                host_jobs,
                publish_jobs: custom_publish_jobs,
                post_announce_jobs,
            },
            github: github_ci_layer,
        });

        // hosts
        let mut github_host_layer =
            list_to_bool_layer(is_global, &hosting, HostingStyle::Github, || {
                if create_release.is_some()
                    || github_releases_repo.is_some()
                    || github_releases_submodule_path.is_some()
                    || github_release.is_some()
                    || github_attestations.is_some()
                {
                    Some(GithubHostLayer {
                        common: CommonHostLayer::default(),
                        create: create_release,
                        repo: github_releases_repo,
                        submodule_path: github_releases_submodule_path.map(|p| p.into()),
                        during: github_release,
                        attestations: github_attestations,
                        attestations_filters: github_attestations_filters,
                        attestations_phase: github_attestations_phase,
                    })
                } else {
                    None
                }
            });
        if github_host_layer.is_none() && has_github_ci {
            github_host_layer = Some(BoolOr::Bool(true));
        }
        let simple_host_layer =
            list_to_bool_layer(is_global, &hosting, HostingStyle::Simple, || {
                if simple_download_url.is_some() {
                    Some(SimpleHostLayer {
                        common: CommonHostLayer::default(),
                        download_url: simple_download_url,
                    })
                } else {
                    None
                }
            });

        let needs_host_layer = github_host_layer.is_some()
            || simple_host_layer.is_some()
            || force_latest.is_some()
            || display.is_some()
            || display_name.is_some();
        let host_layer = needs_host_layer.then_some(HostLayer {
            common: CommonHostLayer {},
            order: hosting,
            github: github_host_layer,
            simple: simple_host_layer,
            force_latest,
            display,
            display_name,
        });

        // installers
        let homebrew_installer_layer =
            list_to_bool_layer(is_global, &installers, InstallerStyle::Homebrew, || {
                if tap.is_some() || formula.is_some() {
                    Some(HomebrewInstallerLayer {
                        common: CommonInstallerLayer::default(),
                        tap,
                        formula,
                    })
                } else {
                    None
                }
            });
        let npm_installer_layer =
            list_to_bool_layer(is_global, &installers, InstallerStyle::Npm, || {
                if npm_package.is_some() || npm_scope.is_some() || npm_shrinkwrap.is_some() {
                    Some(NpmInstallerLayer {
                        common: CommonInstallerLayer::default(),
                        package: npm_package,
                        scope: npm_scope,
                        shrinkwrap: npm_shrinkwrap,
                    })
                } else {
                    None
                }
            });
        let msi_installer_layer =
            list_to_bool_layer(is_global, &installers, InstallerStyle::Msi, || None);
        let pkg_installer_layer =
            list_to_bool_layer(is_global, &installers, InstallerStyle::Pkg, || {
                let MacPkgConfig {
                    identifier,
                    install_location,
                } = mac_pkg_config?;
                if install_location.is_some() || identifier.is_some() {
                    Some(PkgInstallerLayer {
                        common: CommonInstallerLayer::default(),
                        identifier,
                        install_location,
                    })
                } else {
                    None
                }
            });
        let powershell_installer_layer =
            list_to_bool_layer(is_global, &installers, InstallerStyle::Powershell, || None);
        let shell_installer_layer =
            list_to_bool_layer(is_global, &installers, InstallerStyle::Shell, || None);
        let needs_installer_layer = homebrew_installer_layer.is_some()
            || npm_installer_layer.is_some()
            || msi_installer_layer.is_some()
            || powershell_installer_layer.is_some()
            || shell_installer_layer.is_some()
            || pkg_installer_layer.is_some()
            || install_path.is_some()
            || install_success_msg.is_some()
            || install_libraries.is_some()
            || bin_aliases.is_some()
            || install_updater.is_some()
            || always_use_latest_updater.is_some();
        let installer_layer = needs_installer_layer.then_some(InstallerLayer {
            common: CommonInstallerLayer {
                install_path,
                install_success_msg,
                install_libraries,
                bin_aliases,
            },
            homebrew: homebrew_installer_layer,
            msi: msi_installer_layer,
            npm: npm_installer_layer,
            powershell: powershell_installer_layer,
            shell: shell_installer_layer,
            pkg: pkg_installer_layer,
            updater: install_updater,
            always_use_latest_updater,
        });

        // publish
        let homebrew_publisher_layer =
            list_to_bool_layer(is_global, &publish_jobs, PublishStyle::Homebrew, || None);
        let npm_publisher_layer =
            list_to_bool_layer(is_global, &publish_jobs, PublishStyle::Npm, || None);
        let user_layer = list_to_bool_layer_predicate(is_global, &publish_jobs, |job| {
            matches!(job, PublishStyle::User(_))
        });
        let needs_publisher_layer = homebrew_publisher_layer.is_some()
            || npm_publisher_layer.is_some()
            || user_layer.is_some()
            || publish_prereleases.is_some();
        let publisher_layer = needs_publisher_layer.then_some(PublisherLayer {
            common: CommonPublisherLayer {
                prereleases: publish_prereleases,
            },
            homebrew: homebrew_publisher_layer,
            npm: npm_publisher_layer,
            user: user_layer,
        });

        // done!

        TomlLayer {
            dist_version: cargo_dist_version,
            dist_url_override: cargo_dist_url_override,
            dist,
            allow_dirty,
            targets,
            artifacts: artifacts_layer,
            builds: build_layer,
            ci: ci_layer,
            hosts: host_layer,
            installers: installer_layer,
            publishers: publisher_layer,
        }
    }
}

fn list_to_bool_layer<I, T>(
    is_global: bool,
    list: &Option<Vec<I>>,
    item: I,
    val: impl FnOnce() -> Option<T>,
) -> Option<BoolOr<T>>
where
    I: Eq,
{
    // If this thing has values that need to be set, force the layer to exist
    if let Some(val) = val() {
        return Some(BoolOr::Val(val));
    };
    // If the list doesn't exist, don't mention it
    let list = list.as_ref()?;
    // Otherwise treat "is in the list" as a simple boolean
    let is_in_list = list.contains(&item);
    if is_global && !is_in_list {
        // ... with the exception of an omitted value in the global list.
        // here None and Some(false) are the same, so Some(false) is Noise
        // we want to hide.
        None
    } else {
        Some(BoolOr::Bool(is_in_list))
    }
}

fn list_to_bool_layer_predicate<I, T>(
    is_global: bool,
    list: &Option<Vec<I>>,
    predicate: impl FnMut(&I) -> bool,
) -> Option<BoolOr<T>>
where
    I: Eq,
{
    // If the list doesn't exist, don't mention it
    let list = list.as_ref()?;
    // Otherwise treat "is in the list" as a simple boolean
    let is_in_list = list.iter().any(predicate);
    if is_global && !is_in_list {
        // ... with the exception of an omitted value in the global list.
        // here None and Some(false) are the same, so Some(false) is Noise
        // we want to hide.
        None
    } else {
        Some(BoolOr::Bool(is_in_list))
    }
}