GetVersion(string projectDir, string requiredVcs, string revisionFormat, string tagMatch, bool removeTagV, string copyright, ILogger logger, bool warnOnMissing) { // Analyse working directory RevisionData data = ProcessDirectory(projectDir, requiredVcs, tagMatch, logger); if (!string.IsNullOrEmpty(requiredVcs) && data.VcsProvider == null) { logger.Error($@"The required version control system ""{requiredVcs}"" is not available or not used in the project directory."); return(false, null, null, null); } if (string.IsNullOrEmpty(revisionFormat)) { revisionFormat = GetRevisionFormat(projectDir, logger, warnOnMissing); } if (string.IsNullOrEmpty(revisionFormat)) { revisionFormat = data.GetDefaultRevisionFormat(logger); } var rf = new RevisionFormatter { RevisionData = data, RemoveTagV = removeTagV }; try { return(true, rf.ResolveShort(revisionFormat), rf.Resolve(revisionFormat), rf.Resolve(copyright)); } catch (FormatException ex) { logger.Error(ex.Message); return(false, null, null, null); } }
/// <summary> /// Processes the specified directory with a suitable VCS provider. /// </summary> /// <param name="path">The directory to process.</param> /// <param name="requiredVcs">The required VCS name, or null if any VCS is acceptable.</param> /// <param name="tagMatch">The pattern of tag names to match. If empty, all tags are accepted.</param> /// <param name="logger">A logger.</param> /// <returns>Data about the revision. If no provider was able to process the directory, /// dummy data is returned.</returns> public static RevisionData ProcessDirectory(string path, string requiredVcs, string tagMatch, ILogger logger) { RevisionData data = null; // Try to process the directory with all available VCS providers logger.Trace("Processing directory..."); foreach (var provider in GetVcsProviders(logger)) { logger.Trace("Found VCS provider: " + provider); if (!string.IsNullOrEmpty(requiredVcs) && !provider.Name.Equals(requiredVcs, StringComparison.OrdinalIgnoreCase)) { logger.Trace("Provider is not what is required, skipping."); continue; } if (provider.CheckEnvironment()) { logger.Success("Provider can be executed in this environment."); if (provider.CheckDirectory(path, out string rootPath)) { logger.Success("Provider can process this directory."); data = provider.ProcessDirectory(path, tagMatch); break; } } } if (data == null) { // No provider could process the directory, return dummy data logger.Warning("No VCS provider used, using dummy revision data."); data = new RevisionData { CommitHash = "0000000000000000000000000000000000000000", CommitTime = DateTimeOffset.Now, IsModified = false, RevisionNumber = 0 }; } data.Normalize(); data.DumpData(logger); return(data); }
/// <summary> /// Determines if a modified repository that matches the given pattern triggers a build error. /// </summary> /// <param name="data">The data about the current revision of the source directory.</param> /// <param name="cfgNamePattern">The match pattern that shall trigger the error.</param> /// <param name="cfgName">The name of the build configuration.</param> /// <returns>True if an error shall be triggered, false otherwise.</returns> public static bool TriggerErrorIfRepoModified(ILogger logger, RevisionData data, string cfgNameMatchPattern, string cfgName) { if (!string.IsNullOrEmpty(cfgNameMatchPattern) && !string.IsNullOrEmpty(cfgName)) { if (data.IsModified) { var match = Regex.Match(cfgName, $@"^{cfgNameMatchPattern}$", RegexOptions.IgnoreCase); if (match.Success) { logger.Error($@"The ""{cfgName}"" configuration does not allow builds with a modified {data.VcsProvider.Name} repository."); return(true); } } } return(false); }