/
VersionOptions.cs
1304 lines (1119 loc) · 53.1 KB
/
VersionOptions.cs
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
namespace Nerdbank.GitVersioning
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Validation;
/// <summary>
/// Describes the various versions and options required for the build.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class VersionOptions : IEquatable<VersionOptions>
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string gitCommitIdPrefix;
/// <summary>
/// Default value for <see cref="VersionPrecision"/>.
/// </summary>
public const VersionPrecision DefaultVersionPrecision = VersionPrecision.Minor;
/// <summary>
/// The placeholder that may appear in the <see cref="Version"/> property's <see cref="SemanticVersion.Prerelease"/>
/// to specify where the version height should appear in a computed semantic version.
/// </summary>
/// <remarks>
/// When this macro does not appear in the string, the version height is set as the first unspecified integer of the 4-integer version.
/// If all 4 integers in a version are specified, and the macro does not appear, the version height isn't inserted anywhere.
/// </remarks>
public const string VersionHeightPlaceholder = "{height}";
/// <summary>
/// The default value for the <see cref="SemVer1NumericIdentifierPaddingOrDefault"/> property.
/// </summary>
private const int DefaultSemVer1NumericIdentifierPadding = 4;
/// <summary>
/// The default value for the <see cref="GitCommitIdShortFixedLength"/> property.
/// </summary>
public const int DefaultGitCommitIdShortFixedLength = 10;
/// <summary>
/// The $schema field that should be serialized when writing
/// </summary>
[JsonProperty(PropertyName = "$schema")]
public string Schema => "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json";
/// <summary>
/// Gets or sets the default version to use.
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public SemanticVersion Version { get; set; }
/// <summary>
/// Gets or sets the version to use particularly for the <see cref="AssemblyVersionAttribute"/>
/// instead of the default <see cref="Version"/>.
/// </summary>
/// <value>An instance of <see cref="System.Version"/> or <c>null</c> to simply use the default <see cref="Version"/>.</value>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public AssemblyVersionOptions AssemblyVersion { get; set; }
/// <summary>
/// Gets or sets the prefix for git commit id in version.
/// Because of semver rules the prefix must lead with a [A-z_] character (not a number) and it cannot be the empty string.
/// If <c>null</c> 'g' will be used.
/// </summary>
/// <value>A prefix for git commit id.</value>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public string GitCommitIdPrefix
{
get => gitCommitIdPrefix;
set
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentNullException(nameof(value), $"{nameof(this.GitCommitIdPrefix)} can't be empty");
}
char first = value[0];
if (first < 'A' || (first > 'Z' && first < 'a' && first != '_') || first > 'z')
{
throw new ArgumentException(nameof(value), $"{nameof(this.GitCommitIdPrefix)} must lead with a [A-z_] character (not a number)");
}
this.gitCommitIdPrefix = value;
}
}
/// <summary>
/// Gets the version to use particularly for the <see cref="AssemblyVersionAttribute"/>
/// instead of the default <see cref="Version"/>.
/// </summary>
/// <value>An instance of <see cref="System.Version"/> or <c>null</c> to simply use the default <see cref="Version"/>.</value>
[JsonIgnore]
public AssemblyVersionOptions AssemblyVersionOrDefault => this.AssemblyVersion ?? AssemblyVersionOptions.DefaultInstance;
/// <summary>
/// Gets or sets a number to add to the git height when calculating the version height,
/// which typically is used in the <see cref="Version.Build"/> portion of the computed version.
/// </summary>
/// <value>Any integer (0, positive, or negative).</value>
/// <remarks>
/// An error will result if this value is negative with such a magnitude as to exceed the git height,
/// resulting in a negative build number.
/// </remarks>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
[Obsolete("Use " + nameof(VersionHeightOffset) + " instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public int? BuildNumberOffset { get; set; }
/// <summary>
/// Gets or sets a number to add to the git height when calculating the <see cref="Version.Build"/> number.
/// </summary>
/// <value>Any integer (0, positive, or negative).</value>
/// <remarks>
/// An error will result if this value is negative with such a magnitude as to exceed the git height,
/// resulting in a negative build number.
/// </remarks>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public int? VersionHeightOffset
{
#pragma warning disable CS0618
get => this.BuildNumberOffset;
set => this.BuildNumberOffset = value;
#pragma warning restore CS0618
}
/// <summary>
/// Gets a number to add to the git height when calculating the <see cref="Version.Build"/> number.
/// </summary>
/// <value>Any integer (0, positive, or negative).</value>
/// <remarks>
/// An error will result if this value is negative with such a magnitude as to exceed the git height,
/// resulting in a negative build number.
/// </remarks>
[JsonIgnore]
[Obsolete("Use " + nameof(VersionHeightOffsetOrDefault) + " instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public int BuildNumberOffsetOrDefault => this.BuildNumberOffset ?? 0;
/// <summary>
/// Gets a number to add to the git height when calculating the <see cref="Version.Build"/> number.
/// </summary>
/// <value>Any integer (0, positive, or negative).</value>
/// <remarks>
/// An error will result if this value is negative with such a magnitude as to exceed the git height,
/// resulting in a negative build number.
/// </remarks>
[JsonIgnore]
public int VersionHeightOffsetOrDefault
{
#pragma warning disable CS0618
get => this.BuildNumberOffsetOrDefault;
#pragma warning restore CS0618
}
/// <summary>
/// Gets or sets the minimum number of digits to use for numeric identifiers in SemVer 1.
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public int? SemVer1NumericIdentifierPadding { get; set; }
/// <summary>
/// Gets the minimum number of digits to use for numeric identifiers in SemVer 1.
/// </summary>
[JsonIgnore]
public int SemVer1NumericIdentifierPaddingOrDefault => this.SemVer1NumericIdentifierPadding ?? DefaultSemVer1NumericIdentifierPadding;
/// <summary>
/// Gets or sets the abbreviated git commit hash length.
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public int? GitCommitIdShortFixedLength { get; set; }
/// <summary>
/// Gets or sets the abbreviated git commit hash length minimum value.
/// The git repository provides the value.
/// If set to 0 or a git repository is not available, <see cref="GitCommitIdShortFixedLength"/> is used.
/// The value is 0 by default.
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public int? GitCommitIdShortAutoMinimum { get; set; }
/// <summary>
/// Gets or sets the options around NuGet version strings
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public NuGetPackageVersionOptions NuGetPackageVersion { get; set; }
/// <summary>
/// Gets the options around NuGet version strings
/// </summary>
[JsonIgnore]
public NuGetPackageVersionOptions NuGetPackageVersionOrDefault => this.NuGetPackageVersion ?? NuGetPackageVersionOptions.DefaultInstance;
/// <summary>
/// Gets or sets an array of regular expressions that describes branch or tag names that should
/// be built with PublicRelease=true as the default value on build servers.
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public string[] PublicReleaseRefSpec { get; set; }
/// <summary>
/// Gets an array of regular expressions that describes branch or tag names that should
/// be built with PublicRelease=true as the default value on build servers.
/// </summary>
[JsonIgnore]
public string[] PublicReleaseRefSpecOrDefault => this.PublicReleaseRefSpec ?? new string[0];
/// <summary>
/// Gets or sets the options around cloud build.
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public CloudBuildOptions CloudBuild { get; set; }
/// <summary>
/// Gets the options around cloud build.
/// </summary>
[JsonIgnore]
public CloudBuildOptions CloudBuildOrDefault => this.CloudBuild ?? CloudBuildOptions.DefaultInstance;
/// <summary>
/// Gets or sets the options for the prepare-release command
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public ReleaseOptions Release { get; set; }
/// <summary>
/// Gets the options for the prepare-release command
/// </summary>
[JsonIgnore]
public ReleaseOptions ReleaseOrDefault => this.Release ?? ReleaseOptions.DefaultInstance;
/// <summary>
/// Gets or sets a list of paths to use to filter commits when calculating version height.
/// If a given commit does not affect any paths in this filter, it is ignored for version height calculations.
/// Paths should be relative to the root of the repository.
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public string[] PathFilters { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this options object should inherit from an ancestor any settings that are not explicitly set in this one.
/// </summary>
/// <remarks>
/// When this is <c>true</c>, this object may not completely describe the options to be applied.
/// </remarks>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool Inherit { get; set; }
/// <summary>
/// Gets the position in a computed version that the version height should appear.
/// </summary>
[JsonIgnore]
internal SemanticVersion.Position? VersionHeightPosition
{
get
{
if (this.Version?.Prerelease?.Contains(VersionHeightPlaceholder) ?? false)
{
return SemanticVersion.Position.Prerelease;
}
else if (this.Version?.Version.Build == -1)
{
return SemanticVersion.Position.Build;
}
else if (this.Version?.Version.Revision == -1)
{
return SemanticVersion.Position.Revision;
}
else
{
return null;
}
}
}
/// <summary>
/// Gets the position in a computed version that the first 16 bits of a git commit ID should appear, if any.
/// </summary>
[JsonIgnore]
internal SemanticVersion.Position? GitCommitIdPosition
{
get
{
// We can only store the git commit ID info after there was a place to put the version height.
// We don't want to store the commit ID (which is effectively a random integer) in the revision slot
// if the version height does not appear, or only appears later (in the -prerelease tag) since that
// would mess up version ordering.
if (this.VersionHeightPosition == SemanticVersion.Position.Build)
{
return SemanticVersion.Position.Revision;
}
else
{
return null;
}
}
}
/// <summary>
/// Gets the debugger display for this instance.
/// </summary>
private string DebuggerDisplay => this.Version?.ToString() ?? (this.Inherit ? "Inheriting version info" : "(missing version)");
/// <summary>
/// Initializes a new instance of the <see cref="VersionOptions"/> class
/// with <see cref="Version"/> initialized with the specified parameters.
/// </summary>
/// <param name="version">The version number.</param>
/// <param name="unstableTag">The prerelease tag, if any.</param>
/// <returns>The new instance of <see cref="VersionOptions"/>.</returns>
public static VersionOptions FromVersion(Version version, string unstableTag = null)
{
return new VersionOptions
{
Version = new SemanticVersion(version, unstableTag),
};
}
/// <summary>
/// Gets the <see cref="JsonSerializerSettings"/> to use based on certain requirements.
/// The $schema property is not serialized when using this overload.
/// </summary>
/// <param name="includeDefaults">A value indicating whether default values should be serialized.</param>
/// <returns>The serializer settings to use.</returns>
public static JsonSerializerSettings GetJsonSettings(bool includeDefaults) => GetJsonSettings(includeDefaults, includeSchemaProperty: false);
/// <summary>
/// Gets the <see cref="JsonSerializerSettings"/> to use based on certain requirements.
/// </summary>
/// <param name="includeDefaults">A value indicating whether default values should be serialized.</param>
/// <param name="includeSchemaProperty">A value indicating whether the $schema property should be serialized.</param>
/// <returns>The serializer settings to use.</returns>
public static JsonSerializerSettings GetJsonSettings(bool includeDefaults = false, bool includeSchemaProperty = false)
{
return new JsonSerializerSettings
{
Converters = new JsonConverter[] {
new VersionConverter(),
new SemanticVersionJsonConverter(),
new AssemblyVersionOptionsConverter(includeDefaults),
new StringEnumConverter() { CamelCaseText = true },
},
ContractResolver = new VersionOptionsContractResolver
{
IncludeDefaults = includeDefaults,
IncludeSchemaProperty = includeSchemaProperty,
},
Formatting = Formatting.Indented,
};
}
/// <summary>
/// Checks equality against another object.
/// </summary>
/// <param name="obj">The other instance.</param>
/// <returns><c>true</c> if the instances have equal values; <c>false</c> otherwise.</returns>
public override bool Equals(object obj)
{
return this.Equals(obj as VersionOptions);
}
/// <summary>
/// Gets a hash code for this instance.
/// </summary>
/// <returns>The hash code.</returns>
public override int GetHashCode() => EqualWithDefaultsComparer.Singleton.GetHashCode(this);
/// <summary>
/// Checks equality against another instance of this class.
/// </summary>
/// <param name="other">The other instance.</param>
/// <returns><c>true</c> if the instances have equal values; <c>false</c> otherwise.</returns>
public bool Equals(VersionOptions other) => EqualWithDefaultsComparer.Singleton.Equals(this, other);
/// <summary>
/// Gets a value indicating whether <see cref="Version"/> is
/// set and the only property on this class that is set.
/// </summary>
internal bool IsDefaultVersionTheOnlyPropertySet
{
get
{
return this.Version != null
&& this.AssemblyVersion == null
&& this.CloudBuild.IsDefault
&& this.VersionHeightOffset == 0
&& !this.SemVer1NumericIdentifierPadding.HasValue
&& !this.Inherit;
}
}
/// <summary>
/// The class that contains settings for the <see cref="NuGetPackageVersion" /> property.
/// </summary>
public class NuGetPackageVersionOptions : IEquatable<NuGetPackageVersionOptions>
{
/// <summary>
/// The default (uninitialized) instance.
/// </summary>
internal static readonly NuGetPackageVersionOptions DefaultInstance = new NuGetPackageVersionOptions(isReadOnly: true)
{
semVer = 1.0f,
};
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly bool isReadOnly;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private float? semVer;
/// <summary>
/// Initializes a new instance of the <see cref="NuGetPackageVersionOptions" /> class.
/// </summary>
public NuGetPackageVersionOptions()
: this(isReadOnly: false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="NuGetPackageVersionOptions" /> class.
/// </summary>
protected NuGetPackageVersionOptions(bool isReadOnly)
{
this.isReadOnly = isReadOnly;
}
/// <summary>
/// Gets or sets the version of SemVer (e.g. 1 or 2) that should be used when generating the package version.
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public float? SemVer
{
get => this.semVer;
set => this.SetIfNotReadOnly(ref this.semVer, value);
}
/// <summary>
/// Gets the version of SemVer (e.g. 1 or 2) that should be used when generating the package version.
/// </summary>
[JsonIgnore]
public float? SemVerOrDefault => this.SemVer ?? DefaultInstance.SemVer;
/// <inheritdoc />
public override bool Equals(object obj) => this.Equals(obj as NuGetPackageVersionOptions);
/// <inheritdoc />
public bool Equals(NuGetPackageVersionOptions other) => EqualWithDefaultsComparer.Singleton.Equals(this, other);
/// <inheritdoc />
public override int GetHashCode() => EqualWithDefaultsComparer.Singleton.GetHashCode(this);
/// <summary>
/// Gets a value indicating whether this instance is equivalent to the default instance.
/// </summary>
internal bool IsDefault => this.Equals(DefaultInstance);
/// <summary>
/// Sets the value of a field if this instance is not marked as read only.
/// </summary>
/// <typeparam name="T">The type of the value stored by the field.</typeparam>
/// <param name="field">The field to change.</param>
/// <param name="value">The value to set.</param>
private void SetIfNotReadOnly<T>(ref T field, T value)
{
Verify.Operation(!this.isReadOnly, "This instance is read only.");
field = value;
}
internal class EqualWithDefaultsComparer : IEqualityComparer<NuGetPackageVersionOptions>
{
internal static readonly EqualWithDefaultsComparer Singleton = new EqualWithDefaultsComparer();
private EqualWithDefaultsComparer() { }
/// <inheritdoc />
public bool Equals(NuGetPackageVersionOptions x, NuGetPackageVersionOptions y)
{
if (x == null ^ y == null)
{
return false;
}
if (x == null)
{
return true;
}
return x.SemVerOrDefault == y.SemVerOrDefault;
}
/// <inheritdoc />
public int GetHashCode(NuGetPackageVersionOptions obj)
{
return obj.SemVerOrDefault.GetHashCode();
}
}
}
/// <summary>
/// Describes the details of how the AssemblyVersion value will be calculated.
/// </summary>
public class AssemblyVersionOptions : IEquatable<AssemblyVersionOptions>
{
/// <summary>
/// The default (uninitialized) instance.
/// </summary>
internal static readonly AssemblyVersionOptions DefaultInstance = new AssemblyVersionOptions(isReadOnly: true)
{
precision = DefaultVersionPrecision,
};
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly bool isReadOnly;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private Version version;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private VersionPrecision? precision;
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyVersionOptions"/> class.
/// </summary>
public AssemblyVersionOptions()
: this(isReadOnly: false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyVersionOptions"/> class.
/// </summary>
/// <param name="version">The assembly version (with major.minor components).</param>
/// <param name="precision">The additional version precision to add toward matching the AssemblyFileVersion.</param>
public AssemblyVersionOptions(Version version, VersionPrecision? precision = null)
: this(isReadOnly: false)
{
this.Version = version;
this.Precision = precision;
}
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyVersionOptions"/> class.
/// </summary>
protected AssemblyVersionOptions(bool isReadOnly)
{
this.isReadOnly = isReadOnly;
}
/// <summary>
/// Gets or sets the major.minor components of the assembly version.
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public Version Version
{
get => this.version;
set => this.SetIfNotReadOnly(ref this.version, value);
}
/// <summary>
/// Gets or sets the additional version precision to add toward matching the AssemblyFileVersion.
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public VersionPrecision? Precision
{
get => this.precision;
set => this.SetIfNotReadOnly(ref this.precision, value);
}
/// <summary>
/// Gets the additional version precision to add toward matching the AssemblyFileVersion.
/// </summary>
[JsonIgnore]
public VersionPrecision PrecisionOrDefault => this.Precision ?? DefaultVersionPrecision;
/// <inheritdoc />
public override bool Equals(object obj) => this.Equals(obj as AssemblyVersionOptions);
/// <inheritdoc />
public bool Equals(AssemblyVersionOptions other) => EqualWithDefaultsComparer.Singleton.Equals(this, other);
/// <inheritdoc />
public override int GetHashCode() => EqualWithDefaultsComparer.Singleton.GetHashCode(this);
/// <summary>
/// Gets a value indicating whether this instance is equivalent to the default instance.
/// </summary>
internal bool IsDefault => this.Equals(DefaultInstance);
/// <summary>
/// Sets the value of a field if this instance is not marked as read only.
/// </summary>
/// <typeparam name="T">The type of the value stored by the field.</typeparam>
/// <param name="field">The field to change.</param>
/// <param name="value">The value to set.</param>
private void SetIfNotReadOnly<T>(ref T field, T value)
{
Verify.Operation(!this.isReadOnly, "This instance is read only.");
field = value;
}
internal class EqualWithDefaultsComparer : IEqualityComparer<AssemblyVersionOptions>
{
internal static readonly EqualWithDefaultsComparer Singleton = new EqualWithDefaultsComparer();
private EqualWithDefaultsComparer() { }
/// <inheritdoc />
public bool Equals(AssemblyVersionOptions x, AssemblyVersionOptions y)
{
if (x == null ^ y == null)
{
return false;
}
if (x == null)
{
return true;
}
return EqualityComparer<Version>.Default.Equals(x.Version, y.Version)
&& x.PrecisionOrDefault == y.PrecisionOrDefault;
}
/// <inheritdoc />
public int GetHashCode(AssemblyVersionOptions obj)
{
return (obj.Version?.GetHashCode() ?? 0) + (int)obj.PrecisionOrDefault;
}
}
}
/// <summary>
/// Options that are applicable specifically to cloud builds (e.g. VSTS, AppVeyor, TeamCity)
/// </summary>
public class CloudBuildOptions : IEquatable<CloudBuildOptions>
{
/// <summary>
/// The default (uninitialized) instance.
/// </summary>
internal static readonly CloudBuildOptions DefaultInstance = new CloudBuildOptions(isReadOnly: true)
{
setAllVariables = false,
setVersionVariables = true,
};
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly bool isReadOnly;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private bool? setAllVariables;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private bool? setVersionVariables;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private CloudBuildNumberOptions buildNumber;
/// <summary>
/// Initializes a new instance of the <see cref="CloudBuildOptions"/> class.
/// </summary>
public CloudBuildOptions()
: this(false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CloudBuildOptions"/> class.
/// </summary>
protected CloudBuildOptions(bool isReadOnly)
{
this.isReadOnly = isReadOnly;
}
/// <summary>
/// Gets or sets a value indicating whether to elevate all build properties to cloud build variables prefaced with "NBGV_".
/// </summary>
public bool? SetAllVariables
{
get => this.setAllVariables;
set => this.SetIfNotReadOnly(ref this.setAllVariables, value);
}
/// <summary>
/// Gets or sets a value indicating whether to elevate certain calculated version build properties to cloud build variables.
/// </summary>
public bool? SetVersionVariables
{
get => this.setVersionVariables;
set => this.SetIfNotReadOnly(ref this.setVersionVariables, value);
}
/// <summary>
/// Gets a value indicating whether to elevate all build properties to cloud build variables prefaced with "NBGV_".
/// </summary>
[JsonIgnore]
public bool SetAllVariablesOrDefault => this.SetAllVariables ?? DefaultInstance.SetAllVariables.Value;
/// <summary>
/// Gets a value indicating whether to elevate certain calculated version build properties to cloud build variables.
/// </summary>
[JsonIgnore]
public bool SetVersionVariablesOrDefault => this.SetVersionVariables ?? DefaultInstance.SetVersionVariables.Value;
/// <summary>
/// Gets or sets options around how and whether to set the build number preset by the cloud build with one enriched with version information.
/// </summary>
public CloudBuildNumberOptions BuildNumber
{
get => this.buildNumber;
set => this.SetIfNotReadOnly(ref this.buildNumber, value);
}
/// <summary>
/// Gets options around how and whether to set the build number preset by the cloud build with one enriched with version information.
/// </summary>
[JsonIgnore]
public CloudBuildNumberOptions BuildNumberOrDefault => this.BuildNumber ?? CloudBuildNumberOptions.DefaultInstance;
/// <inheritdoc />
public override bool Equals(object obj) => this.Equals(obj as CloudBuildOptions);
/// <inheritdoc />
public bool Equals(CloudBuildOptions other) => EqualWithDefaultsComparer.Singleton.Equals(this, other);
/// <inheritdoc />
public override int GetHashCode() => EqualWithDefaultsComparer.Singleton.GetHashCode(this);
/// <summary>
/// Gets a value indicating whether this instance is equivalent to the default instance.
/// </summary>
internal bool IsDefault => this.Equals(DefaultInstance);
/// <summary>
/// Sets the value of a field if this instance is not marked as read only.
/// </summary>
/// <typeparam name="T">The type of the value stored by the field.</typeparam>
/// <param name="field">The field to change.</param>
/// <param name="value">The value to set.</param>
private void SetIfNotReadOnly<T>(ref T field, T value)
{
Verify.Operation(!this.isReadOnly, "This instance is read only.");
field = value;
}
internal class EqualWithDefaultsComparer : IEqualityComparer<CloudBuildOptions>
{
internal static readonly EqualWithDefaultsComparer Singleton = new EqualWithDefaultsComparer();
private EqualWithDefaultsComparer() { }
/// <inheritdoc />
public bool Equals(CloudBuildOptions x, CloudBuildOptions y)
{
if (x == null ^ y == null)
{
return false;
}
if (x == null)
{
return true;
}
return x.SetVersionVariablesOrDefault == y.SetVersionVariablesOrDefault
&& x.SetAllVariablesOrDefault == y.SetAllVariablesOrDefault
&& CloudBuildNumberOptions.EqualWithDefaultsComparer.Singleton.Equals(x.BuildNumberOrDefault, y.BuildNumberOrDefault);
}
/// <inheritdoc />
public int GetHashCode(CloudBuildOptions obj)
{
return (obj.SetVersionVariablesOrDefault ? 1 : 0)
+ (obj.SetAllVariablesOrDefault ? 1 : 0)
+ obj.BuildNumberOrDefault.GetHashCode();
}
}
}
/// <summary>
/// Override the build number preset by the cloud build with one enriched with version information.
/// </summary>
public class CloudBuildNumberOptions : IEquatable<CloudBuildNumberOptions>
{
/// <summary>
/// The default (uninitialized) instance.
/// </summary>
internal static readonly CloudBuildNumberOptions DefaultInstance = new CloudBuildNumberOptions(isReadOnly: true)
{
enabled = false,
};
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly bool isReadOnly;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private bool? enabled;
/// <summary>
/// Initializes a new instance of the <see cref="CloudBuildNumberOptions"/> class.
/// </summary>
public CloudBuildNumberOptions()
: this(isReadOnly: false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CloudBuildNumberOptions"/> class.
/// </summary>
protected CloudBuildNumberOptions(bool isReadOnly)
{
this.isReadOnly = isReadOnly;
}
/// <summary>
/// Gets or sets a value indicating whether to override the build number preset by the cloud build.
/// </summary>
public bool? Enabled
{
get => this.enabled;
set => this.SetIfNotReadOnly(ref this.enabled, value);
}
/// <summary>
/// Gets a value indicating whether to override the build number preset by the cloud build.
/// </summary>
[JsonIgnore]
public bool EnabledOrDefault => this.Enabled ?? DefaultInstance.Enabled.Value;
/// <summary>
/// Gets or sets when and where to include information about the git commit being built.
/// </summary>
public CloudBuildNumberCommitIdOptions IncludeCommitId { get; set; }
/// <summary>
/// Gets when and where to include information about the git commit being built.
/// </summary>
[JsonIgnore]
public CloudBuildNumberCommitIdOptions IncludeCommitIdOrDefault => this.IncludeCommitId ?? CloudBuildNumberCommitIdOptions.DefaultInstance;
/// <inheritdoc />
public override bool Equals(object obj) => this.Equals(obj as CloudBuildNumberOptions);
/// <inheritdoc />
public bool Equals(CloudBuildNumberOptions other) => EqualWithDefaultsComparer.Singleton.Equals(this, other);
/// <inheritdoc />
public override int GetHashCode() => EqualWithDefaultsComparer.Singleton.GetHashCode(this);
/// <summary>
/// Gets a value indicating whether this instance is equivalent to the default instance.
/// </summary>
internal bool IsDefault => this.Equals(DefaultInstance);
/// <summary>
/// Sets the value of a field if this instance is not marked as read only.
/// </summary>
/// <typeparam name="T">The type of the value stored by the field.</typeparam>
/// <param name="field">The field to change.</param>
/// <param name="value">The value to set.</param>
private void SetIfNotReadOnly<T>(ref T field, T value)
{
Verify.Operation(!this.isReadOnly, "This instance is read only.");
field = value;
}
internal class EqualWithDefaultsComparer : IEqualityComparer<CloudBuildNumberOptions>
{
internal static readonly EqualWithDefaultsComparer Singleton = new EqualWithDefaultsComparer();
private EqualWithDefaultsComparer() { }
/// <inheritdoc />
public bool Equals(CloudBuildNumberOptions x, CloudBuildNumberOptions y)
{
if (x == null ^ y == null)
{
return false;
}
if (x == null)
{
return true;
}
return x.EnabledOrDefault == y.EnabledOrDefault
&& CloudBuildNumberCommitIdOptions.EqualWithDefaultsComparer.Singleton.Equals(x.IncludeCommitIdOrDefault, y.IncludeCommitIdOrDefault);
}
/// <inheritdoc />
public int GetHashCode(CloudBuildNumberOptions obj)
{
return obj.EnabledOrDefault ? 1 : 0
+ obj.IncludeCommitIdOrDefault.GetHashCode();
}
}
}
/// <summary>
/// Describes when and where to include information about the git commit being built.
/// </summary>
public class CloudBuildNumberCommitIdOptions : IEquatable<CloudBuildNumberCommitIdOptions>
{
/// <summary>
/// The default (uninitialized) instance.
/// </summary>
internal static readonly CloudBuildNumberCommitIdOptions DefaultInstance = new CloudBuildNumberCommitIdOptions(isReadOnly: true)
{
when = CloudBuildNumberCommitWhen.NonPublicReleaseOnly,
where = CloudBuildNumberCommitWhere.BuildMetadata,
};
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly bool isReadOnly;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private CloudBuildNumberCommitWhen? when;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private CloudBuildNumberCommitWhere? where;
/// <summary>
/// Initializes a new instance of the <see cref="CloudBuildNumberCommitIdOptions"/> class.
/// </summary>
public CloudBuildNumberCommitIdOptions()
: this(isReadOnly: false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CloudBuildNumberCommitIdOptions"/> class.
/// </summary>
protected CloudBuildNumberCommitIdOptions(bool isReadOnly)
{
this.isReadOnly = isReadOnly;
}
/// <summary>
/// Gets or sets the conditions when the commit ID is included in the build number.
/// </summary>
public CloudBuildNumberCommitWhen? When
{
get => this.when;
set => this.SetIfNotReadOnly(ref this.when, value);
}
/// <summary>
/// Gets the conditions when the commit ID is included in the build number.
/// </summary>
[JsonIgnore]
public CloudBuildNumberCommitWhen WhenOrDefault => this.When ?? DefaultInstance.When.Value;
/// <summary>
/// Gets or sets the position to include the commit ID information.
/// </summary>
public CloudBuildNumberCommitWhere? Where
{
get => this.where;
set => this.SetIfNotReadOnly(ref this.where, value);
}
/// <summary>
/// Gets the position to include the commit ID information.
/// </summary>
[JsonIgnore]
public CloudBuildNumberCommitWhere WhereOrDefault => this.Where ?? DefaultInstance.Where.Value;
/// <inheritdoc />
public override bool Equals(object obj) => this.Equals(obj as CloudBuildNumberCommitIdOptions);
/// <inheritdoc />
public bool Equals(CloudBuildNumberCommitIdOptions other) => EqualWithDefaultsComparer.Singleton.Equals(this, other);
/// <inheritdoc />
public override int GetHashCode() => EqualWithDefaultsComparer.Singleton.GetHashCode(this);
/// <summary>
/// Gets a value indicating whether this instance is equivalent to the default instance.
/// </summary>
internal bool IsDefault => this.Equals(DefaultInstance);
/// <summary>
/// Sets the value of a field if this instance is not marked as read only.
/// </summary>
/// <typeparam name="T">The type of the value stored by the field.</typeparam>
/// <param name="field">The field to change.</param>
/// <param name="value">The value to set.</param>
private void SetIfNotReadOnly<T>(ref T field, T value)
{
Verify.Operation(!this.isReadOnly, "This instance is read only.");
field = value;
}
internal class EqualWithDefaultsComparer : IEqualityComparer<CloudBuildNumberCommitIdOptions>
{
internal static readonly EqualWithDefaultsComparer Singleton = new EqualWithDefaultsComparer();