-
Notifications
You must be signed in to change notification settings - Fork 112
/
config.rs
1070 lines (935 loc) · 32.3 KB
/
config.rs
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::path::{Path, PathBuf};
use anyhow::Context as _;
use serde::{Deserialize, Serialize};
use crate::error::CargoResult;
use crate::ops::cargo;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields, default)]
#[serde(rename_all = "kebab-case")]
pub struct Config {
#[serde(skip)]
pub is_workspace: bool,
pub unstable: Unstable,
pub allow_branch: Option<Vec<String>>,
pub sign_commit: Option<bool>,
pub sign_tag: Option<bool>,
pub push_remote: Option<String>,
pub registry: Option<String>,
pub release: Option<bool>,
pub publish: Option<bool>,
pub verify: Option<bool>,
pub owners: Option<Vec<String>>,
pub push: Option<bool>,
pub push_options: Option<Vec<String>>,
pub shared_version: Option<SharedVersion>,
pub consolidate_commits: Option<bool>,
pub pre_release_commit_message: Option<String>,
pub pre_release_replacements: Option<Vec<Replace>>,
pub pre_release_hook: Option<Command>,
pub tag_message: Option<String>,
pub tag_prefix: Option<String>,
pub tag_name: Option<String>,
pub tag: Option<bool>,
pub enable_features: Option<Vec<String>>,
pub enable_all_features: Option<bool>,
pub dependent_version: Option<DependentVersion>,
pub metadata: Option<MetadataPolicy>,
pub target: Option<String>,
pub rate_limit: RateLimit,
pub certs_source: Option<CertsSource>,
}
impl Config {
pub fn new() -> Self {
Default::default()
}
pub fn from_defaults() -> Self {
let empty = Config::new();
Config {
is_workspace: true,
unstable: Unstable::from_defaults(),
allow_branch: Some(
empty
.allow_branch()
.map(|s| s.to_owned())
.collect::<Vec<String>>(),
),
sign_commit: Some(empty.sign_commit()),
sign_tag: Some(empty.sign_tag()),
push_remote: Some(empty.push_remote().to_owned()),
registry: empty.registry().map(|s| s.to_owned()),
release: Some(empty.release()),
publish: Some(empty.publish()),
verify: Some(empty.verify()),
owners: Some(empty.owners().to_vec()),
push: Some(empty.push()),
push_options: Some(
empty
.push_options()
.map(|s| s.to_owned())
.collect::<Vec<String>>(),
),
shared_version: empty
.shared_version()
.map(|s| SharedVersion::Name(s.to_owned())),
consolidate_commits: Some(empty.consolidate_commits()),
pre_release_commit_message: Some(empty.pre_release_commit_message().to_owned()),
pre_release_replacements: Some(empty.pre_release_replacements().to_vec()),
pre_release_hook: empty.pre_release_hook().cloned(),
tag_message: Some(empty.tag_message().to_owned()),
tag_prefix: None, // Skipping, its location dependent
tag_name: Some(empty.tag_name().to_owned()),
tag: Some(empty.tag()),
enable_features: Some(empty.enable_features().to_vec()),
enable_all_features: Some(empty.enable_all_features()),
dependent_version: Some(empty.dependent_version()),
metadata: Some(empty.metadata()),
target: None,
rate_limit: RateLimit::from_defaults(),
certs_source: Some(empty.certs_source()),
}
}
pub fn update(&mut self, source: &Config) {
self.unstable.update(&source.unstable);
if let Some(allow_branch) = source.allow_branch.as_deref() {
self.allow_branch = Some(allow_branch.to_owned());
}
if let Some(sign_commit) = source.sign_commit {
self.sign_commit = Some(sign_commit);
}
if let Some(sign_tag) = source.sign_tag {
self.sign_tag = Some(sign_tag);
}
if let Some(push_remote) = source.push_remote.as_deref() {
self.push_remote = Some(push_remote.to_owned());
}
if let Some(registry) = source.registry.as_deref() {
self.registry = Some(registry.to_owned());
}
if let Some(release) = source.release {
self.release = Some(release);
}
if let Some(publish) = source.publish {
self.publish = Some(publish);
}
if let Some(verify) = source.verify {
self.verify = Some(verify);
}
if let Some(owners) = source.owners.as_deref() {
self.owners = Some(owners.to_owned());
}
if let Some(push) = source.push {
self.push = Some(push);
}
if let Some(push_options) = source.push_options.as_deref() {
self.push_options = Some(push_options.to_owned());
}
if let Some(shared_version) = source.shared_version.clone() {
self.shared_version = Some(shared_version);
}
if let Some(consolidate_commits) = source.consolidate_commits {
self.consolidate_commits = Some(consolidate_commits);
}
if let Some(pre_release_commit_message) = source.pre_release_commit_message.as_deref() {
self.pre_release_commit_message = Some(pre_release_commit_message.to_owned());
}
if let Some(pre_release_replacements) = source.pre_release_replacements.as_deref() {
self.pre_release_replacements = Some(pre_release_replacements.to_owned());
}
if let Some(pre_release_hook) = source.pre_release_hook.as_ref() {
self.pre_release_hook = Some(pre_release_hook.to_owned());
}
if let Some(tag_message) = source.tag_message.as_deref() {
self.tag_message = Some(tag_message.to_owned());
}
if let Some(tag_prefix) = source.tag_prefix.as_deref() {
self.tag_prefix = Some(tag_prefix.to_owned());
}
if let Some(tag_name) = source.tag_name.as_deref() {
self.tag_name = Some(tag_name.to_owned());
}
if let Some(tag) = source.tag {
self.tag = Some(tag);
}
if let Some(enable_features) = source.enable_features.as_deref() {
self.enable_features = Some(enable_features.to_owned());
}
if let Some(enable_all_features) = source.enable_all_features {
self.enable_all_features = Some(enable_all_features);
}
if let Some(dependent_version) = source.dependent_version {
self.dependent_version = Some(dependent_version);
}
if let Some(metadata) = source.metadata {
self.metadata = Some(metadata);
}
if let Some(target) = source.target.as_deref() {
self.target = Some(target.to_owned());
}
self.rate_limit.update(&source.rate_limit);
if let Some(certs) = source.certs_source {
self.certs_source = Some(certs);
}
}
pub fn unstable(&self) -> &Unstable {
&self.unstable
}
pub fn allow_branch(&self) -> impl Iterator<Item = &str> {
self.allow_branch
.as_deref()
.map(|a| itertools::Either::Left(a.iter().map(|s| s.as_str())))
.unwrap_or_else(|| itertools::Either::Right(IntoIterator::into_iter(["*", "!HEAD"])))
}
pub fn sign_commit(&self) -> bool {
self.sign_commit.unwrap_or(false)
}
pub fn sign_tag(&self) -> bool {
self.sign_tag.unwrap_or(false)
}
pub fn push_remote(&self) -> &str {
self.push_remote.as_deref().unwrap_or("origin")
}
pub fn registry(&self) -> Option<&str> {
self.registry.as_deref()
}
pub fn release(&self) -> bool {
self.release.unwrap_or(true)
}
pub fn publish(&self) -> bool {
self.publish.unwrap_or(true)
}
pub fn verify(&self) -> bool {
self.verify.unwrap_or(true)
}
pub fn owners(&self) -> &[String] {
self.owners.as_ref().map(|v| v.as_ref()).unwrap_or(&[])
}
pub fn push(&self) -> bool {
self.push.unwrap_or(true)
}
pub fn push_options(&self) -> impl Iterator<Item = &str> {
self.push_options
.as_ref()
.into_iter()
.flat_map(|v| v.iter().map(|s| s.as_str()))
}
pub fn shared_version(&self) -> Option<&str> {
self.shared_version.as_ref().and_then(|s| s.as_name())
}
pub fn consolidate_commits(&self) -> bool {
self.consolidate_commits.unwrap_or(self.is_workspace)
}
pub fn pre_release_commit_message(&self) -> &str {
self.pre_release_commit_message
.as_deref()
.unwrap_or_else(|| {
if self.consolidate_commits() {
"chore: Release"
} else {
"chore: Release {{crate_name}} version {{version}}"
}
})
}
pub fn pre_release_replacements(&self) -> &[Replace] {
self.pre_release_replacements
.as_ref()
.map(|v| v.as_ref())
.unwrap_or(&[])
}
pub fn pre_release_hook(&self) -> Option<&Command> {
self.pre_release_hook.as_ref()
}
pub fn tag_message(&self) -> &str {
self.tag_message
.as_deref()
.unwrap_or("chore: Release {{crate_name}} version {{version}}")
}
pub fn tag_prefix(&self, is_root: bool) -> &str {
// crate_name as default tag prefix for multi-crate project
self.tag_prefix
.as_deref()
.unwrap_or(if !is_root { "{{crate_name}}-" } else { "" })
}
pub fn tag_name(&self) -> &str {
self.tag_name.as_deref().unwrap_or("{{prefix}}v{{version}}")
}
pub fn tag(&self) -> bool {
self.tag.unwrap_or(true)
}
pub fn enable_features(&self) -> &[String] {
self.enable_features
.as_ref()
.map(|v| v.as_ref())
.unwrap_or(&[])
}
pub fn enable_all_features(&self) -> bool {
self.enable_all_features.unwrap_or(false)
}
pub fn features(&self) -> cargo::Features {
if self.enable_all_features() {
cargo::Features::All
} else {
let features = self.enable_features();
cargo::Features::Selective(features.to_owned())
}
}
pub fn dependent_version(&self) -> DependentVersion {
self.dependent_version.unwrap_or_default()
}
pub fn metadata(&self) -> MetadataPolicy {
self.metadata.unwrap_or_default()
}
pub fn certs_source(&self) -> CertsSource {
self.certs_source.unwrap_or_default()
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields, default)]
#[serde(rename_all = "kebab-case")]
pub struct Unstable {
workspace_publish: Option<bool>,
}
impl Unstable {
pub fn new() -> Self {
Default::default()
}
pub fn from_defaults() -> Self {
let empty = Self::new();
Self {
workspace_publish: Some(empty.workspace_publish()),
}
}
pub fn update(&mut self, source: &Self) {
if let Some(workspace_publish) = source.workspace_publish {
self.workspace_publish = Some(workspace_publish);
}
}
pub fn workspace_publish(&self) -> bool {
self.workspace_publish.unwrap_or(false)
}
}
impl From<Vec<UnstableValues>> for Unstable {
fn from(values: Vec<UnstableValues>) -> Self {
let mut unstable = Unstable::new();
for value in values {
match value {
UnstableValues::WorkspacePublish(value) => unstable.workspace_publish = Some(value),
}
}
unstable
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Replace {
pub file: PathBuf,
pub search: String,
pub replace: String,
pub min: Option<usize>,
pub max: Option<usize>,
pub exactly: Option<usize>,
#[serde(default)]
pub prerelease: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Command {
Line(String),
Args(Vec<String>),
}
impl Command {
pub fn args(&self) -> Vec<&str> {
match self {
Command::Line(ref s) => vec![s.as_str()],
Command::Args(ref a) => a.iter().map(|s| s.as_str()).collect(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, clap::ValueEnum)]
#[serde(rename_all = "kebab-case")]
#[value(rename_all = "kebab-case")]
#[derive(Default)]
pub enum DependentVersion {
/// Always upgrade dependents
#[default] // This is the safest option as its hard to test `Fix`
Upgrade,
/// Upgrade when the old version requirement no longer applies
Fix,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, clap::ValueEnum)]
#[serde(rename_all = "kebab-case")]
#[value(rename_all = "kebab-case")]
#[derive(Default)]
pub enum CertsSource {
/// Use certs from Mozilla's root certificate store.
#[default]
Webpki,
/// Use certs from the system root certificate store.
Native,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, clap::ValueEnum)]
#[serde(rename_all = "kebab-case")]
#[value(rename_all = "kebab-case")]
#[derive(Default)]
pub enum MetadataPolicy {
/// Apply when set, clear when not
#[default]
Optional,
/// Error if not set
Required,
/// Never apply the set metadata
Ignore,
/// Keep the prior metadata if not set
Persistent,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
#[serde(rename_all = "kebab-case")]
pub enum SharedVersion {
Enabled(bool),
Name(String),
}
impl SharedVersion {
pub const WORKSPACE: &'static str = "workspace";
pub fn as_name(&self) -> Option<&str> {
match self {
SharedVersion::Enabled(true) => Some("default"),
SharedVersion::Enabled(false) => None,
SharedVersion::Name(name) => Some(name.as_str()),
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
struct CargoManifest {
workspace: Option<CargoWorkspace>,
package: Option<CargoPackage>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
struct CargoWorkspace {
package: Option<CargoWorkspacePackage>,
metadata: Option<CargoMetadata>,
}
impl CargoWorkspace {
fn into_config(self) -> Option<Config> {
self.metadata?.release
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
struct CargoWorkspacePackage {
publish: Option<CargoPublishField>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
struct CargoPackage {
publish: Option<MaybeWorkspace<CargoPublishField>>,
version: Option<MaybeWorkspace<String>>,
metadata: Option<CargoMetadata>,
}
impl CargoPackage {
fn into_config(self) -> Option<Config> {
self.metadata?.release
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum CargoPublishField {
Bool(bool),
Registries(Vec<String>),
}
impl CargoPublishField {
fn publishable(&self) -> bool {
match self {
Self::Bool(b) => *b,
Self::Registries(r) => !r.is_empty(),
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum MaybeWorkspace<T> {
Workspace(TomlWorkspaceField),
Defined(T),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TomlWorkspaceField {
workspace: bool,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
struct CargoMetadata {
release: Option<Config>,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct RateLimit {
#[serde(default)]
pub new_packages: Option<usize>,
#[serde(default)]
pub existing_packages: Option<usize>,
}
impl RateLimit {
pub fn new() -> Self {
Default::default()
}
pub fn from_defaults() -> Self {
Self {
new_packages: Some(5),
existing_packages: Some(30),
}
}
pub fn update(&mut self, source: &RateLimit) {
if source.new_packages.is_some() {
self.new_packages = source.new_packages;
}
if source.existing_packages.is_some() {
self.existing_packages = source.existing_packages;
}
}
pub fn new_packages(&self) -> usize {
self.new_packages.unwrap_or(5)
}
pub fn existing_packages(&self) -> usize {
self.existing_packages.unwrap_or(30)
}
}
pub fn load_workspace_config(
args: &ConfigArgs,
ws_meta: &cargo_metadata::Metadata,
) -> CargoResult<Config> {
let mut release_config = Config {
is_workspace: 1 < ws_meta.workspace_members.len(),
..Default::default()
};
if !args.isolated {
let is_workspace = 1 < ws_meta.workspace_members.len();
let cfg = if is_workspace {
resolve_workspace_config(ws_meta.workspace_root.as_std_path())?
} else {
// Outside of workspaces, go ahead and treat package config as workspace config so
// users don't have to specially configure workspace-specific fields
let pkg = ws_meta
.packages
.iter()
.find(|p| ws_meta.workspace_members.iter().any(|m| *m == p.id))
.unwrap();
resolve_config(
ws_meta.workspace_root.as_std_path(),
pkg.manifest_path.as_std_path(),
)?
};
release_config.update(&cfg);
}
if let Some(custom_config_path) = args.custom_config.as_ref() {
// when calling with -c option
let cfg = resolve_custom_config(custom_config_path.as_ref())?.unwrap_or_default();
release_config.update(&cfg);
}
release_config.update(&args.to_config());
Ok(release_config)
}
pub fn load_package_config(
args: &ConfigArgs,
ws_meta: &cargo_metadata::Metadata,
pkg: &cargo_metadata::Package,
) -> CargoResult<Config> {
let manifest_path = pkg.manifest_path.as_std_path();
let is_workspace = 1 < ws_meta.workspace_members.len();
let mut release_config = Config {
is_workspace,
..Default::default()
};
if !args.isolated {
let cfg = resolve_config(ws_meta.workspace_root.as_std_path(), manifest_path)?;
release_config.update(&cfg);
}
if let Some(custom_config_path) = args.custom_config.as_ref() {
// when calling with -c option
let cfg = resolve_custom_config(Path::new(custom_config_path))?.unwrap_or_default();
release_config.update(&cfg);
}
release_config.update(&args.to_config());
let overrides = resolve_overrides(ws_meta.workspace_root.as_std_path(), manifest_path)?;
release_config.update(&overrides);
Ok(release_config)
}
#[derive(Clone, Default, Debug, clap::Args)]
pub struct ConfigArgs {
/// Custom config file
#[arg(short, long = "config", value_name = "PATH")]
pub custom_config: Option<PathBuf>,
/// Ignore implicit configuration files.
#[arg(long)]
pub isolated: bool,
/// Unstable options
#[arg(short = 'Z', value_name = "FEATURE")]
pub z: Vec<UnstableValues>,
/// Sign both git commit and tag
#[arg(long, overrides_with("no_sign"))]
pub sign: bool,
#[arg(long, overrides_with("sign"), hide(true))]
pub no_sign: bool,
/// Specify how workspace dependencies on this crate should be handed.
#[arg(long, value_name = "ACTION", value_enum)]
pub dependent_version: Option<DependentVersion>,
/// Comma-separated globs of branch names a release can happen from
#[arg(long, value_delimiter = ',', value_name = "GLOB[,...]")]
pub allow_branch: Option<Vec<String>>,
/// Indicate what certificate store to use for web requests.
#[arg(long)]
pub certs_source: Option<CertsSource>,
#[command(flatten)]
pub commit: CommitArgs,
#[command(flatten)]
pub publish: PublishArgs,
#[command(flatten)]
pub tag: TagArgs,
#[command(flatten)]
pub push: PushArgs,
}
impl ConfigArgs {
pub fn to_config(&self) -> Config {
let mut config = Config {
unstable: Unstable::from(self.z.clone()),
allow_branch: self.allow_branch.clone(),
sign_commit: self.sign(),
sign_tag: self.sign(),
dependent_version: self.dependent_version,
certs_source: self.certs_source,
..Default::default()
};
config.update(&self.commit.to_config());
config.update(&self.publish.to_config());
config.update(&self.tag.to_config());
config.update(&self.push.to_config());
config
}
fn sign(&self) -> Option<bool> {
resolve_bool_arg(self.sign, self.no_sign)
}
}
#[derive(Clone, Debug)]
pub enum UnstableValues {
WorkspacePublish(bool),
}
impl std::str::FromStr for UnstableValues {
type Err = anyhow::Error;
fn from_str(value: &str) -> Result<Self, Self::Err> {
let (name, mut value) = value.split_once('=').unwrap_or((value, ""));
match name {
"workspace-publish" => {
if value.is_empty() {
value = "true";
}
let value = match value {
"true" => true,
"false" => false,
_ => anyhow::bail!(
"unsupported value `{name}={value}`, expected one of `true`, `false`"
),
};
Ok(UnstableValues::WorkspacePublish(value))
}
_ => {
anyhow::bail!("unsupported unstable feature name `{name}` (value `{value}`)");
}
}
}
}
impl std::fmt::Display for UnstableValues {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
Self::WorkspacePublish(true) => "workspace-publish".fmt(fmt),
Self::WorkspacePublish(false) => "".fmt(fmt),
}
}
}
#[derive(Clone, Default, Debug, clap::Args)]
#[command(next_help_heading = "Commit")]
pub struct CommitArgs {
/// Sign git commit
#[arg(long, overrides_with("no_sign_commit"))]
pub sign_commit: bool,
#[arg(long, overrides_with("sign_commit"), hide(true))]
pub no_sign_commit: bool,
}
impl CommitArgs {
pub fn to_config(&self) -> Config {
Config {
sign_commit: resolve_bool_arg(self.sign_commit, self.no_sign_commit),
..Default::default()
}
}
}
#[derive(Clone, Default, Debug, clap::Args)]
#[command(next_help_heading = "Publish")]
pub struct PublishArgs {
#[arg(long, overrides_with("no_publish"), hide(true))]
publish: bool,
/// Do not run cargo publish on release
#[arg(long, overrides_with("publish"))]
no_publish: bool,
/// Cargo registry to upload to
#[arg(long, value_name = "NAME")]
registry: Option<String>,
#[arg(long, overrides_with("no_verify"), hide(true))]
verify: bool,
/// Don't verify the contents by building them
#[arg(long, overrides_with("verify"))]
no_verify: bool,
/// Provide a set of features that need to be enabled
#[arg(long)]
features: Vec<String>,
/// Enable all features via `all-features`. Overrides `features`
#[arg(long)]
all_features: bool,
/// Build for the target triple
#[arg(long, value_name = "TRIPLE")]
target: Option<String>,
}
impl PublishArgs {
pub fn to_config(&self) -> Config {
Config {
publish: resolve_bool_arg(self.publish, self.no_publish),
registry: self.registry.clone(),
verify: resolve_bool_arg(self.verify, self.no_verify),
enable_features: (!self.features.is_empty()).then(|| self.features.clone()),
enable_all_features: self.all_features.then_some(true),
target: self.target.clone(),
..Default::default()
}
}
}
#[derive(Clone, Default, Debug, clap::Args)]
#[command(next_help_heading = "Tag")]
pub struct TagArgs {
#[arg(long, overrides_with("no_tag"), hide(true))]
tag: bool,
/// Do not create git tag
#[arg(long, overrides_with("tag"))]
no_tag: bool,
/// Sign git tag
#[arg(long, overrides_with("no_sign_tag"))]
sign_tag: bool,
#[arg(long, overrides_with("sign_tag"), hide(true))]
no_sign_tag: bool,
/// Prefix of git tag, note that this will override default prefix based on sub-directory
#[arg(long, value_name = "PREFIX")]
tag_prefix: Option<String>,
/// The name of the git tag.
#[arg(long, value_name = "NAME")]
tag_name: Option<String>,
}
impl TagArgs {
pub fn to_config(&self) -> Config {
Config {
tag: resolve_bool_arg(self.tag, self.no_tag),
sign_tag: resolve_bool_arg(self.sign_tag, self.no_sign_tag),
tag_prefix: self.tag_prefix.clone(),
tag_name: self.tag_name.clone(),
..Default::default()
}
}
}
#[derive(Clone, Default, Debug, clap::Args)]
#[command(next_help_heading = "Push")]
pub struct PushArgs {
#[arg(long, overrides_with("no_push"), hide(true))]
push: bool,
/// Do not run git push in the last step
#[arg(long, overrides_with("push"))]
no_push: bool,
/// Git remote to push
#[arg(long, value_name = "NAME")]
push_remote: Option<String>,
}
impl PushArgs {
pub fn to_config(&self) -> Config {
Config {
push: resolve_bool_arg(self.push, self.no_push),
push_remote: self.push_remote.clone(),
..Default::default()
}
}
}
fn get_pkg_config_from_manifest(manifest_path: &Path) -> CargoResult<Option<Config>> {
if manifest_path.exists() {
let m = std::fs::read_to_string(manifest_path)?;
let c: CargoManifest = toml::from_str(&m)
.with_context(|| format!("Failed to parse `{}`", manifest_path.display()))?;
Ok(c.package.and_then(|p| p.into_config()))
} else {
Ok(None)
}
}
fn get_ws_config_from_manifest(manifest_path: &Path) -> CargoResult<Option<Config>> {
if manifest_path.exists() {
let m = std::fs::read_to_string(manifest_path)?;
let c: CargoManifest = toml::from_str(&m)
.with_context(|| format!("Failed to parse `{}`", manifest_path.display()))?;
Ok(c.workspace.and_then(|p| p.into_config()))
} else {
Ok(None)
}
}
fn get_config_from_file(file_path: &Path) -> CargoResult<Option<Config>> {
if file_path.exists() {
let c = std::fs::read_to_string(file_path)?;
let config = toml::from_str(&c)
.with_context(|| format!("Failed to parse `{}`", file_path.display()))?;
Ok(Some(config))
} else {
Ok(None)
}
}
pub fn resolve_custom_config(file_path: &Path) -> CargoResult<Option<Config>> {
get_config_from_file(file_path)
}
/// Try to resolve workspace configuration source.
///
/// This tries the following sources in order, merging the results:
/// 1. $HOME/.release.toml
/// 2. $HOME/.config/cargo-release/release.toml
/// 3. $(workspace)/release.toml
/// 3. $(workspace)/Cargo.toml
pub fn resolve_workspace_config(workspace_root: &Path) -> CargoResult<Config> {
let mut config = Config::default();
// User-local configuration from home directory.
let home_dir = dirs_next::home_dir();
if let Some(mut home) = home_dir {
home.push(".release.toml");
if let Some(cfg) = get_config_from_file(&home)? {
config.update(&cfg);
}
};
let config_dir = dirs_next::config_dir();
if let Some(mut config_path) = config_dir {
config_path.push("cargo-release/release.toml");
if let Some(cfg) = get_config_from_file(&config_path)? {
config.update(&cfg);
}
};
// Workspace config
let default_config = workspace_root.join("release.toml");
let current_dir_config = get_config_from_file(&default_config)?;
if let Some(cfg) = current_dir_config {
config.update(&cfg);
};
let manifest_path = workspace_root.join("Cargo.toml");
let current_dir_config = get_ws_config_from_manifest(&manifest_path)?;
if let Some(cfg) = current_dir_config {
config.update(&cfg);
};
Ok(config)
}
/// Try to resolve configuration source.
///
/// This tries the following sources in order, merging the results:
/// 1. $HOME/.release.toml
/// 2. $HOME/.config/cargo-release/release.toml
/// 3. $(workspace)/release.toml
/// 3. $(workspace)/Cargo.toml `workspace.metadata.release`
/// 4. $(crate)/release.toml
/// 5. $(crate)/Cargo.toml `package.metadata.release`
///
/// `$(crate)/Cargo.toml` is a way to differentiate configuration for the root crate and the
/// workspace.
pub fn resolve_config(workspace_root: &Path, manifest_path: &Path) -> CargoResult<Config> {
let mut config = resolve_workspace_config(workspace_root)?;
// Crate config
let crate_root = manifest_path.parent().unwrap_or_else(|| Path::new("."));
let default_config = crate_root.join("release.toml");
let current_dir_config = get_config_from_file(&default_config)?;
if let Some(cfg) = current_dir_config {
config.update(&cfg);
};
let current_dir_config = get_pkg_config_from_manifest(manifest_path)?;
if let Some(cfg) = current_dir_config {
config.update(&cfg);
};
Ok(config)
}
pub fn resolve_overrides(workspace_root: &Path, manifest_path: &Path) -> CargoResult<Config> {
fn load_workspace<'m, 'c: 'm>(
workspace_root: &Path,
workspace_cache: &'c mut Option<CargoManifest>,
) -> CargoResult<&'m CargoManifest> {
if workspace_cache.is_none() {
let workspace_path = workspace_root.join("Cargo.toml");
let toml = std::fs::read_to_string(&workspace_path)?;
let manifest: CargoManifest = toml::from_str(&toml)
.with_context(|| format!("Failed to parse `{}`", workspace_path.display()))?;
*workspace_cache = Some(manifest);
}
Ok(workspace_cache.as_ref().unwrap())
}
let mut release_config = Config::default();
let mut workspace_cache = None;
// the publish flag in cargo file
let manifest = std::fs::read_to_string(manifest_path)?;
let manifest: CargoManifest = toml::from_str(&manifest)