|
| 1 | +namespace Nerdbank.GitVersioning |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.IO; |
| 6 | + using System.Linq; |
| 7 | + using Validation; |
| 8 | + |
| 9 | + /// <summary> |
| 10 | + /// Implementation of the "nbgv cloud" command that updates the build environments variables with version variables |
| 11 | + /// </summary> |
| 12 | + public class CloudCommand |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Defines the possible errors of the "cloud" command |
| 16 | + /// </summary> |
| 17 | + public enum CloudCommandError |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// Teh specified CI system was not found |
| 21 | + /// </summary> |
| 22 | + NoCloudBuildProviderMatch, |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// A cloud variable was defined multiple times |
| 26 | + /// </summary> |
| 27 | + DuplicateCloudVariable, |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// No supported cloud build environment could be detected |
| 31 | + /// </summary> |
| 32 | + NoCloudBuildEnvDetected |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Exception indicating an error while setting build variables |
| 37 | + /// </summary> |
| 38 | + public class CloudCommandException : Exception |
| 39 | + { |
| 40 | + /// <summary> |
| 41 | + /// Gets the error that occurred. |
| 42 | + /// </summary> |
| 43 | + public CloudCommandError Error { get; } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Initializes a new instance of <see cref="CloudCommandException"/> |
| 47 | + /// </summary> |
| 48 | + /// <param name="message">The message that describes the error.</param> |
| 49 | + /// <param name="error">The error that occurred.</param> |
| 50 | + public CloudCommandException(string message, CloudCommandError error) : base(message) => this.Error = error; |
| 51 | + } |
| 52 | + |
| 53 | + private readonly TextWriter stdout; |
| 54 | + private readonly TextWriter stderr; |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Initializes a new instance of <see cref="CloudCommand"/>. |
| 58 | + /// </summary> |
| 59 | + /// <param name="outputWriter">The <see cref="TextWriter"/> to write output to (e.g. <see cref="Console.Out" />).</param> |
| 60 | + /// <param name="errorWriter">The <see cref="TextWriter"/> to write error messages to (e.g. <see cref="Console.Error" />).</param> |
| 61 | + public CloudCommand(TextWriter outputWriter = null, TextWriter errorWriter = null) |
| 62 | + { |
| 63 | + this.stdout = outputWriter ?? TextWriter.Null; |
| 64 | + this.stderr = errorWriter ?? TextWriter.Null; |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Adds version variables to the the current cloud build environment. |
| 70 | + /// </summary> |
| 71 | + /// <exception cref="CloudCommandException">Thrown when the build environment could not be updated.</exception> |
| 72 | + /// <param name="projectDirectory"> |
| 73 | + /// The path to the directory which may (or its ancestors may) define the version file. |
| 74 | + /// </param> |
| 75 | + /// <param name="metadata"> |
| 76 | + /// Optionally adds an identifier to the build metadata part of a semantic version. |
| 77 | + /// </param> |
| 78 | + /// <param name="version"> |
| 79 | + /// The string to use for the cloud build number. If not specified, the computed version will be used. |
| 80 | + /// </param> |
| 81 | + /// <param name="ciSystem"> |
| 82 | + /// The CI system to activate. If not specified, auto-detection will be used. |
| 83 | + /// </param> |
| 84 | + /// <param name="allVars"> |
| 85 | + /// Controls whether to define all version variables as cloud build variables. |
| 86 | + /// </param> |
| 87 | + /// <param name="commonVars"> |
| 88 | + /// Controls whether to define common version variables as cloud build variables. |
| 89 | + /// </param> |
| 90 | + /// <param name="additionalVariables"> |
| 91 | + /// Additional cloud build variables to define. |
| 92 | + /// </param> |
| 93 | + /// <param name="alwaysUseLibGit2"> |
| 94 | + /// Force usage of LibGit2 for accessing the git repository. |
| 95 | + /// </param> |
| 96 | + public void SetBuildVariables(string projectDirectory, IEnumerable<string> metadata, string version, string ciSystem, bool allVars, bool commonVars, IEnumerable<KeyValuePair<string, string>> additionalVariables, bool alwaysUseLibGit2) |
| 97 | + { |
| 98 | + Requires.NotNull(projectDirectory, nameof(projectDirectory)); |
| 99 | + Requires.NotNull(additionalVariables, nameof(additionalVariables)); |
| 100 | + |
| 101 | + ICloudBuild activeCloudBuild = CloudBuild.Active; |
| 102 | + if (!string.IsNullOrEmpty(ciSystem)) |
| 103 | + { |
| 104 | + int matchingIndex = Array.FindIndex(CloudProviderNames, m => string.Equals(m, ciSystem, StringComparison.OrdinalIgnoreCase)); |
| 105 | + if (matchingIndex == -1) |
| 106 | + { |
| 107 | + throw new CloudCommandException( |
| 108 | + $"No cloud provider found by the name: \"{ciSystem}\"", |
| 109 | + CloudCommandError.NoCloudBuildProviderMatch); |
| 110 | + } |
| 111 | + |
| 112 | + activeCloudBuild = CloudBuild.SupportedCloudBuilds[matchingIndex]; |
| 113 | + } |
| 114 | + |
| 115 | + using var context = GitContext.Create(projectDirectory, writable: alwaysUseLibGit2); |
| 116 | + var oracle = new VersionOracle(context, cloudBuild: activeCloudBuild); |
| 117 | + if (metadata is not null) |
| 118 | + { |
| 119 | + oracle.BuildMetadata.AddRange(metadata); |
| 120 | + } |
| 121 | + |
| 122 | + var variables = new Dictionary<string, string>(); |
| 123 | + if (allVars) |
| 124 | + { |
| 125 | + foreach (var pair in oracle.CloudBuildAllVars) |
| 126 | + { |
| 127 | + variables.Add(pair.Key, pair.Value); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (commonVars) |
| 132 | + { |
| 133 | + foreach (var pair in oracle.CloudBuildVersionVars) |
| 134 | + { |
| 135 | + variables.Add(pair.Key, pair.Value); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + foreach (var kvp in additionalVariables) |
| 140 | + { |
| 141 | + if (variables.ContainsKey(kvp.Key)) |
| 142 | + { |
| 143 | + throw new CloudCommandException( |
| 144 | + $"Cloud build variable \"{kvp.Key}\" specified more than once.", |
| 145 | + CloudCommandError.DuplicateCloudVariable); |
| 146 | + } |
| 147 | + |
| 148 | + variables[kvp.Key] = kvp.Value; |
| 149 | + } |
| 150 | + |
| 151 | + if (activeCloudBuild is not null) |
| 152 | + { |
| 153 | + if (string.IsNullOrEmpty(version)) |
| 154 | + { |
| 155 | + version = oracle.CloudBuildNumber; |
| 156 | + } |
| 157 | + |
| 158 | + activeCloudBuild.SetCloudBuildNumber(version, this.stdout, this.stderr); |
| 159 | + |
| 160 | + foreach (var pair in variables) |
| 161 | + { |
| 162 | + activeCloudBuild.SetCloudBuildVariable(pair.Key, pair.Value, this.stdout, this.stderr); |
| 163 | + } |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + throw new CloudCommandException( |
| 168 | + "No cloud build detected.", |
| 169 | + CloudCommandError.NoCloudBuildEnvDetected); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + |
| 174 | + private static string[] CloudProviderNames => CloudBuild.SupportedCloudBuilds.Select(cb => cb.GetType().Name).ToArray(); |
| 175 | + } |
| 176 | +} |
0 commit comments