Пример #1
0
 public override void WriteIntegration(Action <string> writer, VersionVariables variables)
 {
     base.WriteIntegration(writer, variables);
     writer($"Outputting variables to '{_file}' ... ");
     WriteVariablesFile(variables);
 }
Пример #2
0
        private static bool RunExecCommandIfNeeded(Arguments args, string workingDirectory, VersionVariables variables)
        {
            if (string.IsNullOrEmpty(args.Exec))
            {
                return(false);
            }

            Logger.WriteInfo($"Launching {args.Exec} {args.ExecArgs}");
            var results = ProcessHelper.Run(
                Logger.WriteInfo, Logger.WriteError,
                null, args.Exec, args.ExecArgs, workingDirectory,
                GetEnvironmentalVariables(variables));

            if (results != 0)
            {
                throw new WarningException($"Execution of {args.Exec} failed, non-zero return code");
            }

            return(true);
        }
Пример #3
0
        public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion, EffectiveConfiguration config, bool isCurrentCommitTagged)
        {
            if (config.VersioningMode == VersioningMode.ContinuousDeployment && !isCurrentCommitTagged)
            {
                semanticVersion = new SemanticVersion(semanticVersion);
                // Continuous Deployment always requires a pre-release tag unless the commit is tagged
                if (!semanticVersion.PreReleaseTag.HasTag())
                {
                    semanticVersion.PreReleaseTag.Name = NextVersionCalculator.GetBranchSpecificTag(config, semanticVersion.BuildMetaData.Branch, null);
                    if (string.IsNullOrEmpty(semanticVersion.PreReleaseTag.Name))
                    {
                        semanticVersion.PreReleaseTag.Name = config.ContinuousDeploymentFallbackTag;
                    }
                }

                // Evaluate tag number pattern and append to prerelease tag, preserving build metadata
                if (!string.IsNullOrEmpty(config.TagNumberPattern))
                {
                    var match       = Regex.Match(semanticVersion.BuildMetaData.Branch, config.TagNumberPattern);
                    var numberGroup = match.Groups["number"];
                    if (numberGroup.Success)
                    {
                        semanticVersion.PreReleaseTag.Name += numberGroup.Value.PadLeft(config.BuildMetaDataPadding, '0');
                    }
                }

                // For continuous deployment the commits since tag gets promoted to the pre-release number
                semanticVersion.PreReleaseTag.Number = semanticVersion.BuildMetaData.CommitsSinceTag;
                semanticVersion.BuildMetaData.CommitsSinceVersionSource = semanticVersion.BuildMetaData.CommitsSinceTag ?? 0;
                semanticVersion.BuildMetaData.CommitsSinceTag           = null;
            }

            var semverFormatValues = new SemanticVersionFormatValues(semanticVersion, config);

            string informationalVersion;

            if (string.IsNullOrEmpty(config.AssemblyInformationalFormat))
            {
                informationalVersion = semverFormatValues.DefaultInformationalVersion;
            }
            else
            {
                try
                {
                    informationalVersion = config.AssemblyInformationalFormat.FormatWith(semverFormatValues);
                }
                catch (FormatException formex)
                {
                    throw new WarningException(string.Format("Unable to format AssemblyInformationalVersion.  Check your format string: {0}", formex.Message));
                }
            }

            var variables = new VersionVariables(
                semverFormatValues.Major,
                semverFormatValues.Minor,
                semverFormatValues.Patch,
                semverFormatValues.BuildMetaData,
                semverFormatValues.BuildMetaDataPadded,
                semverFormatValues.FullBuildMetaData,
                semverFormatValues.BranchName,
                semverFormatValues.Sha,
                semverFormatValues.MajorMinorPatch,
                semverFormatValues.SemVer,
                semverFormatValues.LegacySemVer,
                semverFormatValues.LegacySemVerPadded,
                semverFormatValues.FullSemVer,
                semverFormatValues.AssemblySemVer,
                semverFormatValues.PreReleaseTag,
                semverFormatValues.PreReleaseTagWithDash,
                semverFormatValues.PreReleaseLabel,
                semverFormatValues.PreReleaseNumber,
                informationalVersion,
                semverFormatValues.CommitDate,
                semverFormatValues.NuGetVersion,
                semverFormatValues.NuGetVersionV2,
                semverFormatValues.CommitsSinceVersionSource,
                semverFormatValues.CommitsSinceVersionSourcePadded);

            return(variables);
        }
Пример #4
0
        private static bool RunMsBuildIfNeeded(Arguments args, string workingDirectory, VersionVariables variables, ILog log)
        {
#pragma warning disable CS0612 // Type or member is obsolete
            if (string.IsNullOrEmpty(args.Proj))
            {
                return(false);
            }

            args.Exec     = "dotnet";
            args.ExecArgs = $"msbuild \"{args.Proj}\" {args.ProjArgs}";
#pragma warning restore CS0612 // Type or member is obsolete

            return(RunExecCommandIfNeeded(args, workingDirectory, variables, log));
        }
Пример #5
0
        public void WriteVariablesToDiskCache(GitPreparer gitPreparer, GitVersionCacheKey cacheKey, VersionVariables variablesFromCache)
        {
            var cacheDir      = PrepareCacheDirectory(gitPreparer);
            var cacheFileName = GetCacheFileName(cacheKey, cacheDir);

            variablesFromCache.FileName = cacheFileName;

            Dictionary <string, string> dictionary;

            using (Logger.IndentLog("Creating dictionary"))
            {
                dictionary = variablesFromCache.ToDictionary(x => x.Key, x => x.Value);
            }

            Action writeCacheOperation = () =>
            {
                using (var stream = fileSystem.OpenWrite(cacheFileName))
                {
                    using (var sw = new StreamWriter(stream))
                    {
                        using (Logger.IndentLog("Storing version variables to cache file " + cacheFileName))
                        {
                            var serializer = new Serializer();
                            serializer.Serialize(sw, dictionary);
                        }
                    }
                }
            };

            var retryOperation = new OperationWithExponentialBackoff <IOException>(new ThreadSleep(), writeCacheOperation, maxRetries: 6);

            retryOperation.ExecuteAsync().Wait();
        }
Пример #6
0
 public override void WriteIntegration(Action <string> writer, VersionVariables variables)
 {
     base.WriteIntegration(writer, variables);
     writer(string.Format("Outputting variables to '{0}' ... ", _file));
     WriteVariablesFile(variables);
 }
Пример #7
0
 public abstract string GenerateSetVersionMessage(VersionVariables variables);
Пример #8
0
        public AssemblyInfoFileUpdater(ISet <string> assemblyInfoFileNames, string workingDirectory, VersionVariables variables, IFileSystem fileSystem, bool ensureAssemblyInfo)
        {
            this.assemblyInfoFileNames = assemblyInfoFileNames;
            this.workingDirectory      = workingDirectory;
            this.variables             = variables;
            this.fileSystem            = fileSystem;
            this.ensureAssemblyInfo    = ensureAssemblyInfo;

            templateManager = new TemplateManager(TemplateType.VersionAssemblyInfoResources);
        }
Пример #9
0
 public WixVersionFileUpdater(string workingDirectory, VersionVariables variables, IFileSystem fileSystem)
 {
     this.variables      = variables;
     this.fileSystem     = fileSystem;
     this.WixVersionFile = Path.Combine(workingDirectory, WIX_VERSION_FILE);
 }
Пример #10
0
 public override string GenerateSetVersionMessage(VersionVariables variables)
 {
     return(null);
 }
Пример #11
0
 public AssemblyInfoFileUpdater(string assemblyInfoFileName, string workingDirectory, VersionVariables variables, IFileSystem fileSystem, bool ensureAssemblyInfo) :
     this(new HashSet <string> {
     assemblyInfoFileName
 }, workingDirectory, variables, fileSystem, ensureAssemblyInfo)
 {
 }
Пример #12
0
        public AssemblyInfoFileUpdate(Arguments args, string workingDirectory, VersionVariables variables, IFileSystem fileSystem)
        {
            if (!args.UpdateAssemblyInfo)
            {
                return;
            }

            if (args.Output != OutputType.Json)
            {
                Logger.WriteInfo("Updating assembly info files");
            }

            var assemblyInfoFiles = GetAssemblyInfoFiles(workingDirectory, args, fileSystem).ToList();

            Logger.WriteInfo($"Found {assemblyInfoFiles.Count} files");

            var assemblyVersion           = variables.AssemblySemVer;
            var assemblyVersionRegex      = new Regex(@"AssemblyVersion(Attribute)?\s*\(.*\)\s*");
            var assemblyVersionString     = !string.IsNullOrWhiteSpace(assemblyVersion) ? $"AssemblyVersion(\"{assemblyVersion}\")" : null;
            var assemblyInfoVersion       = variables.InformationalVersion;
            var assemblyInfoVersionRegex  = new Regex(@"AssemblyInformationalVersion(Attribute)?\s*\(.*\)\s*");
            var assemblyInfoVersionString = $"AssemblyInformationalVersion(\"{assemblyInfoVersion}\")";
            var assemblyFileVersion       = variables.AssemblySemFileVer;
            var assemblyFileVersionRegex  = new Regex(@"AssemblyFileVersion(Attribute)?\s*\(.*\)\s*");
            var assemblyFileVersionString = !string.IsNullOrWhiteSpace(assemblyFileVersion) ? $"AssemblyFileVersion(\"{assemblyFileVersion}\")" : null;

            foreach (var assemblyInfoFile in assemblyInfoFiles)
            {
                var backupAssemblyInfo = assemblyInfoFile.FullName + ".bak";
                var localAssemblyInfo  = assemblyInfoFile.FullName;
                fileSystem.Copy(assemblyInfoFile.FullName, backupAssemblyInfo, true);
                restoreBackupTasks.Add(() =>
                {
                    if (fileSystem.Exists(localAssemblyInfo))
                    {
                        fileSystem.Delete(localAssemblyInfo);
                    }
                    fileSystem.Move(backupAssemblyInfo, localAssemblyInfo);
                });
                cleanupBackupTasks.Add(() => fileSystem.Delete(backupAssemblyInfo));

                var originalFileContents = fileSystem.ReadAllText(assemblyInfoFile.FullName);
                var fileContents         = originalFileContents;
                var appendedAttributes   = false;
                if (!string.IsNullOrWhiteSpace(assemblyVersion))
                {
                    fileContents = ReplaceOrAppend(assemblyVersionRegex, fileContents, assemblyVersionString, assemblyInfoFile.Extension, ref appendedAttributes);
                }
                if (!string.IsNullOrWhiteSpace(assemblyFileVersion))
                {
                    fileContents = ReplaceOrAppend(assemblyFileVersionRegex, fileContents, assemblyFileVersionString, assemblyInfoFile.Extension, ref appendedAttributes);
                }
                fileContents = ReplaceOrAppend(assemblyInfoVersionRegex, fileContents, assemblyInfoVersionString, assemblyInfoFile.Extension, ref appendedAttributes);

                if (appendedAttributes)
                {
                    // If we appended any attributes, put a new line after them
                    fileContents += Environment.NewLine;
                }
                if (originalFileContents != fileContents)
                {
                    fileSystem.WriteAllText(assemblyInfoFile.FullName, fileContents);
                }
            }
        }
Пример #13
0
 public void GenerateGitVersionInformation(VersionVariables variables, FileWriteInfo fileWriteInfo)
 {
     gitVersionInformationGenerator.Generate(variables, fileWriteInfo);
 }
Пример #14
0
        public static IEnumerable <string> GenerateBuildLogOutput(IBuildServer buildServer, VersionVariables variables)
        {
            var output = new List <string>();

            foreach (var variable in variables)
            {
                output.AddRange(buildServer.GenerateSetParameterMessage(variable.Key, variable.Value));
            }

            return(output);
        }
Пример #15
0
 public WixVersionFileUpdater(string workingDirectory, VersionVariables variables, IFileSystem fileSystem)
 {
     this.workingDirectory = workingDirectory;
     this.variables        = variables;
     this.fileSystem       = fileSystem;
 }
Пример #16
0
        private static bool RunMsBuildIfNeeded(Arguments args, string workingDirectory, VersionVariables variables, ILog log)
        {
            if (string.IsNullOrEmpty(args.Proj))
            {
                return(false);
            }

            args.Exec     = "dotnet";
            args.ExecArgs = $"msbuild \"{args.Proj}\" {args.ProjArgs}";

            return(RunExecCommandIfNeeded(args, workingDirectory, variables, log));
        }
Пример #17
0
 public override string GenerateSetVersionMessage(VersionVariables variables)
 {
     return(variables.FullSemVer);
 }
Пример #18
0
        public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion, EffectiveConfiguration config, bool isCurrentCommitTagged)
        {
            var isContinuousDeploymentMode = config.VersioningMode == VersioningMode.ContinuousDeployment && !isCurrentCommitTagged;

            if (isContinuousDeploymentMode)
            {
                semanticVersion = new SemanticVersion(semanticVersion);
                // Continuous Deployment always requires a pre-release tag unless the commit is tagged
                if (!semanticVersion.PreReleaseTag.HasTag())
                {
                    semanticVersion.PreReleaseTag.Name = NextVersionCalculator.GetBranchSpecificTag(config, semanticVersion.BuildMetaData.Branch, null);
                    if (string.IsNullOrEmpty(semanticVersion.PreReleaseTag.Name))
                    {
                        semanticVersion.PreReleaseTag.Name = config.ContinuousDeploymentFallbackTag;
                    }
                }
            }

            // Evaluate tag number pattern and append to prerelease tag, preserving build metadata
            var appendTagNumberPattern = !string.IsNullOrEmpty(config.TagNumberPattern) && semanticVersion.PreReleaseTag.HasTag();

            if (appendTagNumberPattern)
            {
                var match       = Regex.Match(semanticVersion.BuildMetaData.Branch, config.TagNumberPattern);
                var numberGroup = match.Groups["number"];
                if (numberGroup.Success)
                {
                    semanticVersion.PreReleaseTag.Name += numberGroup.Value.PadLeft(config.BuildMetaDataPadding, '0');
                }
            }

            if (isContinuousDeploymentMode || appendTagNumberPattern || config.VersioningMode == VersioningMode.Mainline)
            {
                PromoteNumberOfCommitsToTagNumber(semanticVersion);
            }

            var semverFormatValues = new SemanticVersionFormatValues(semanticVersion, config);

            string informationalVersion = CheckAndFormatString(config.AssemblyInformationalFormat, semverFormatValues,
                                                               semverFormatValues.DefaultInformationalVersion, "AssemblyInformationalVersion");

            string assemblyFileSemVer = CheckAndFormatString(config.AssemblyFileVersioningFormat, semverFormatValues,
                                                             semverFormatValues.AssemblyFileSemVer, "AssemblyFileVersioningFormat");

            string assemblySemVer = CheckAndFormatString(config.AssemblyVersioningFormat, semverFormatValues,
                                                         semverFormatValues.AssemblySemVer, "AssemblyVersioningFormat");

            var variables = new VersionVariables(
                semverFormatValues.Major,
                semverFormatValues.Minor,
                semverFormatValues.Patch,
                semverFormatValues.BuildMetaData,
                semverFormatValues.BuildMetaDataPadded,
                semverFormatValues.FullBuildMetaData,
                semverFormatValues.BranchName,
                semverFormatValues.Sha,
                semverFormatValues.ShortSha,
                semverFormatValues.MajorMinorPatch,
                semverFormatValues.SemVer,
                semverFormatValues.LegacySemVer,
                semverFormatValues.LegacySemVerPadded,
                semverFormatValues.FullSemVer,
                assemblySemVer,
                assemblyFileSemVer,
                semverFormatValues.PreReleaseTag,
                semverFormatValues.PreReleaseTagWithDash,
                semverFormatValues.PreReleaseLabel,
                semverFormatValues.PreReleaseNumber,
                informationalVersion,
                semverFormatValues.CommitDate,
                semverFormatValues.NuGetVersion,
                semverFormatValues.NuGetVersionV2,
                semverFormatValues.NuGetPreReleaseTag,
                semverFormatValues.NuGetPreReleaseTagV2,
                semverFormatValues.CommitsSinceVersionSource,
                semverFormatValues.CommitsSinceVersionSourcePadded);

            return(variables);
        }
Пример #19
0
 void WriteVariablesFile(VersionVariables variables)
 {
     File.WriteAllLines(_file, BuildOutputFormatter.GenerateBuildLogOutput(this, variables));
 }
Пример #20
0
        private static bool RunMsBuildIfNeeded(Arguments args, string workingDirectory, VersionVariables variables, ILog log)
        {
            if (string.IsNullOrEmpty(args.Proj))
            {
                return(false);
            }

            log.Info($"Launching build tool {BuildTool} \"{args.Proj}\" {args.ProjArgs}");
            var results = ProcessHelper.Run(
                m => log.Info(m), m => log.Error(m),
                null, BuildTool, $"\"{args.Proj}\" {args.ProjArgs}", workingDirectory,
                GetEnvironmentalVariables(variables));

            if (results != 0)
            {
                throw new WarningException("MSBuild execution failed, non-zero return code");
            }

            return(true);
        }
Пример #21
0
 public override string GenerateSetVersionMessage(VersionVariables variables)
 {
     return(string.Format("##teamcity[buildNumber '{0}']", ServiceMessageEscapeHelper.EscapeValue(variables.FullSemVer)));
 }
Пример #22
0
        public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion, EffectiveConfiguration config, bool isCurrentCommitTagged)
        {
            if (config.VersioningMode == VersioningMode.ContinuousDeployment && !isCurrentCommitTagged)
            {
                semanticVersion = new SemanticVersion(semanticVersion);
                // Continuous Deployment always requires a pre-release tag unless the commit is tagged
                if (!semanticVersion.PreReleaseTag.HasTag())
                {
                    semanticVersion.PreReleaseTag.Name = config.ContinuousDeploymentFallbackTag;
                }

                // For continuous deployment the commits since tag gets promoted to the pre-release number
                semanticVersion.PreReleaseTag.Number = semanticVersion.BuildMetaData.CommitsSinceTag;
                semanticVersion.BuildMetaData.CommitsSinceVersionSource = semanticVersion.BuildMetaData.CommitsSinceTag ?? 0;
                semanticVersion.BuildMetaData.CommitsSinceTag           = null;
            }

            var semverFormatValues = new SemanticVersionFormatValues(semanticVersion, config);

            string informationalVersion;

            if (string.IsNullOrEmpty(config.AssemblyInformationalFormat))
            {
                informationalVersion = semverFormatValues.DefaultInformationalVersion;
            }
            else
            {
                try
                {
                    informationalVersion = config.AssemblyInformationalFormat.FormatWith(semverFormatValues);
                }
                catch (FormatException formex)
                {
                    throw new WarningException(string.Format("Unable to format AssemblyInformationalVersion.  Check your format string: {0}", formex.Message));
                }
            }

            var variables = new VersionVariables(
                semverFormatValues.Major,
                semverFormatValues.Minor,
                semverFormatValues.Patch,
                semverFormatValues.BuildMetaData,
                semverFormatValues.BuildMetaDataPadded,
                semverFormatValues.FullBuildMetaData,
                semverFormatValues.BranchName,
                semverFormatValues.Sha,
                semverFormatValues.MajorMinorPatch,
                semverFormatValues.SemVer,
                semverFormatValues.LegacySemVer,
                semverFormatValues.LegacySemVerPadded,
                semverFormatValues.FullSemVer,
                semverFormatValues.AssemblySemVer,
                semverFormatValues.PreReleaseTag,
                semverFormatValues.PreReleaseTagWithDash,
                semverFormatValues.PreReleaseLabel,
                semverFormatValues.PreReleaseNumber,
                informationalVersion,
                semverFormatValues.CommitDate,
                semverFormatValues.NuGetVersion,
                semverFormatValues.NuGetVersionV2,
                semverFormatValues.CommitsSinceVersionSource,
                semverFormatValues.CommitsSinceVersionSourcePadded);

            return(variables);
        }
Пример #23
0
        private static bool RunExecCommandIfNeeded(Arguments args, string workingDirectory, VersionVariables variables, ILog log)
        {
#pragma warning disable CS0612 // Type or member is obsolete
            if (string.IsNullOrEmpty(args.Exec))
            {
                return(false);
            }

            log.Info($"Launching {args.Exec} {args.ExecArgs}");
            var results = ProcessHelper.Run(
                m => log.Info(m), m => log.Error(m),
                null, args.Exec, args.ExecArgs, workingDirectory,
                GetEnvironmentalVariables(variables));

            if (results != 0)
            {
                throw new WarningException($"Execution of {args.Exec} failed, non-zero return code");
            }
#pragma warning restore CS0612 // Type or member is obsolete

            return(true);
        }
Пример #24
0
 public override string GenerateSetVersionMessage(VersionVariables variables)
 {
     return(string.Format("@@continua[setBuildVersion value='{0}']", variables.FullSemVer));
 }
Пример #25
0
 static KeyValuePair <string, string>[] GetEnvironmentalVariables(VersionVariables variables)
 {
     return(variables
            .Select(v => new KeyValuePair <string, string>("GitVersion_" + v.Key, v.Value))
            .ToArray());
 }
Пример #26
0
 public override string GenerateSetVersionMessage(VersionVariables variables)
 {
     return($"@@continua[setBuildVersion value='{variables.FullSemVer}']");
 }