示例#1
0
        public static void OctoVersion(out OctoVersionInfo versionInfo, Action <LoggerConfiguration> additionLogs)
        {
            var(appSettings, configuration) = ConfigurationBootstrapper.Bootstrap <AppSettings>();
            var runner = new OctoVersionRunner(appSettings, configuration, additionLogs);

            runner.Run(out versionInfo);
        }
示例#2
0
        public OctoVersionInfo Create(SimpleVersion version)
        {
            var preReleaseTag = DerivePreReleaseTag();

            var major = version.Major;

            if (_overriddenMajorVersion.HasValue)
            {
                _logger.Debug("Overriding derived major version {DerivedMajorVersion} with {OverriddenMajorVersion}",
                              version.Major,
                              _overriddenMajorVersion.Value);
                major = _overriddenMajorVersion.Value;
            }

            var minor = version.Minor;

            if (_overriddenMinorVersion.HasValue)
            {
                _logger.Debug("Overriding derived minor version {DerivedMinorVersion} with {OverriddenMinorVersion}",
                              version.Minor,
                              _overriddenMinorVersion.Value);
                minor = _overriddenMinorVersion.Value;
            }

            var patch = version.Patch;

            if (_overriddenPatchVersion.HasValue)
            {
                _logger.Debug("Overriding derived patch version {DerivedPatchVersion} with {OverriddenPatchVersion}",
                              version.Patch,
                              _overriddenPatchVersion.Value);
                patch = _overriddenPatchVersion.Value;
            }

            var buildMetadata = DeriveBuildMetadata();

            if (!string.IsNullOrEmpty(_overriddenBuildMetadata))
            {
                var sanitizedBuildMetadata = _sanitizer.Sanitize(_overriddenBuildMetadata);
                _logger.Debug("Overriding derived build metadata {DerivedBuildMetadata} with {OverriddenBuildMetadata}",
                              buildMetadata,
                              sanitizedBuildMetadata);
                buildMetadata = sanitizedBuildMetadata;
            }

            var result = new OctoVersionInfo(major,
                                             minor,
                                             patch,
                                             preReleaseTag,
                                             buildMetadata);

            return(result);
        }
        public void Run(out OctoVersionInfo versionInfo)
        {
            var outputFormatters = new OutputFormattersProvider().GetFormatters(appSettings.OutputFormats);

            LogBootstrapper.Bootstrap(configuration,
                                      lc =>
            {
                foreach (var outputFormatter in outputFormatters)
                {
                    lc.WriteTo.Sink(outputFormatter.LogSink);
                }

                // Special case: if we're writing to the console then use LiterateConsole
                if (outputFormatters.OfType <ConsoleOutputFormatter>().Any())
                {
                    lc.WriteTo.LiterateConsole();
                }
                additionalLogConfiguration(lc);
            });
            Log.Debug("Running OctoVersion {OctoVersionVersion} with {@AppSettings}", ApplicationVersion, appSettings);

            var currentDirectory = Directory.GetCurrentDirectory();

            Log.Debug("Executing in {Directory}", currentDirectory);

            foreach (var outputFormatter in outputFormatters)
            {
                Log.Debug("Writing build output using {OutputFormatter}", outputFormatter.GetType().Name);
            }

            if (!string.IsNullOrWhiteSpace(appSettings.FullSemVer))
            {
                Log.Information("Adopting previously-provided version information {FullSemVer}. Not calculating a new version number.", appSettings.FullSemVer);
                var semanticVersion = SemanticVersion.TryParse(appSettings.FullSemVer);
                if (semanticVersion == null)
                {
                    throw new Exception("Failed to parse semantic version string");
                }

                versionInfo = new OctoVersionInfo(semanticVersion);
            }
            else
            {
                using (Log.Logger.BeginTimedOperation("Calculating version"))
                {
                    var repositorySearchPath = string.IsNullOrWhiteSpace(appSettings.RepositoryPath)
                        ? currentDirectory
                        : appSettings.RepositoryPath;

                    var versionCalculatorFactory = new VersionCalculatorFactory(repositorySearchPath);
                    var calculator = versionCalculatorFactory.Create();
                    var version    = calculator.GetVersion();
                    var currentSha = calculator.CurrentCommitHash;
                    versionInfo = new StructuredOutputFactory(appSettings.NonPreReleaseTags,
                                                              appSettings.NonPreReleaseTagsRegex,
                                                              appSettings.Major,
                                                              appSettings.Minor,
                                                              appSettings.Patch,
                                                              appSettings.CurrentBranch,
                                                              currentSha,
                                                              appSettings.BuildMetadata)
                                  .Create(version);
                }
            }

            Log.Information("Version is {FullSemVer}", versionInfo.FullSemVer);

            foreach (var outputFormatter in outputFormatters)
            {
                outputFormatter.Write(versionInfo);
            }
        }