예제 #1
0
        public static int Process(Options options, TextWriter output)
        {
            var versionSet =
                !string.IsNullOrWhiteSpace(options.AssemblyFileVersion + options.AssemblyVersion +
                                           options.AssemblyInformationalVersion);

            var incRequested = options.IncrementPart != IncrementablePart.None;

            if (versionSet && incRequested)
            {
                output.WriteLine("Versions cannot be incremented at the same time as version numbers are being assigned.");
                return -1;
            }

            var allInfo = new InfoFinder(options.ProjectFolder);
            var versionData = allInfo.Infos
                .Select(i => new FileVersion { Path = i, Versions = Scanner.Scan(i).ToList() })
                .ToList();

            if (versionSet)
                return SetVersions(options, output, versionData);

            if (options.IncrementTarget != null)
                return IncrementVersion(options, output, versionData);

            if (options.Report)
                return ReportVersions(output, versionData);

            if (options.Get != null)
                return ReportVersion(options, output, versionData);
            return 0;
        }
예제 #2
0
 public static bool Validator(Options options, IList<string> errorList)
 {
     return ValidateReportSetting(options, errorList)
         && ValidateGetSetting(options, errorList)
         && ValidateIncSetting(options, errorList)
         && ValidateThatSomeActionIsSpecified(options, errorList)
         && ValidateEnvSetting(options, errorList)
         && ValidateVersionNumberFormats(options, errorList);
 }
예제 #3
0
        private static int ReportVersion(Options options, TextWriter output, List<FileVersion> versionData)
        {
            var requiredVersion = TargetToName(options.Get);
            var version = versionData.SelectMany(f => f.Versions).FirstOrDefault(v => v.Description == requiredVersion);
            if (version == null)
            {
                output.WriteLine("No {0} found.", requiredVersion);
                return -1;
            }

            var value = version.Original;
            output.WriteLine("{0} = {1}", requiredVersion, value);

            if (options.EnvironmentVariableName != null)
                Environment.SetEnvironmentVariable(options.EnvironmentVariableName, value, EnvironmentVariableTarget.User);
            return 0;
        }
예제 #4
0
        private static bool ValidateVersionNumberFormats(Options options, IList<string> errorList)
        {
            string detail;
            if (!IsValidVersionNumber(options.AssemblyFileVersion, out detail))
            {
                errorList.Add(MakeVersionErrorMessage("Invalid assembly file version.", detail));
                return false;
            }

            if (!IsValidVersionNumber(options.AssemblyVersion, out detail))
            {
                errorList.Add(MakeVersionErrorMessage("Invalid assembly version.", detail));
                return false;
            }

            return true;
        }
예제 #5
0
        private static bool ValidateReportSetting(Options options, IList<string> errorList)
        {
            if (options.Report && (IsVersionSet(options) || IsIncSet(options)))
            {
                errorList.Add("Report cannot be combined with other options.");
                return false;
            }

            return true;
        }
예제 #6
0
        private static bool ValidateThatSomeActionIsSpecified(Options options, IList<string> errorList)
        {
            if (!UpdateOperationSelected(options) && !ReadOperationSelected(options))
            {
                errorList.Add("A version update, report or get must be selected.");
                return false;
            }

            return true;
        }
예제 #7
0
        private static bool ValidateGetSetting(Options options, IList<string> errorList)
        {
            if (!IsGetSet(options)) return true;

            if (IsVersionSet(options) || IsIncSet(options) || options.Report)
            {
                errorList.Add("Get cannot be combined with other options.");
                return false;
            }

            if (!IsValidVersionName(options.Get, true))
            {
                errorList.Add("Invalid version specified for Get. Specify FV, AV, or IV only.");
                return false;
            }

            return true;
        }
예제 #8
0
 private static bool IsGetSet(Options options)
 {
     return options.Get != null;
 }
예제 #9
0
 private static bool UpdateOperationSelected(Options options)
 {
     return IsVersionSet(options) || IsIncSet(options);
 }
예제 #10
0
        private static bool ValidateEnvSetting(Options options, IList<string> errorList)
        {
            if (options.EnvironmentVariableName != null && !UpdateOperationSelected(options) && !IsGetSet(options))
            {
                errorList.Add("An environment variable can only be set if a version update or get is selected.");
                return false;
            }

            if (options.EnvironmentVariableName != null)
            {
                if (GetNumVersionsSet(options) > 1)
                {
                    errorList.Add(@"The /env option is not valid when setting multiple version numbers.");
                    return false;
                }
            }

            return true;
        }
예제 #11
0
 private static bool ReadOperationSelected(Options options)
 {
     return options.Report || IsGetSet(options);
 }
예제 #12
0
 private static bool IsVersionSet(Options options)
 {
     return GetNumVersionsSet(options) > 0;
 }
예제 #13
0
 private static bool IsIncSet(Options options)
 {
     return options.IncrementTarget != null;
 }
예제 #14
0
        private static int SetVersions(Options options, TextWriter output, List<FileVersion> versionData)
        {
            var replacements = new List<VersionUpdate>();
            if (options.AssemblyFileVersion != null)
                replacements.Add(new VersionUpdate("AssemblyFileVersion", options.AssemblyFileVersion));
            if (options.AssemblyVersion != null)
                replacements.Add(new VersionUpdate("AssemblyVersion", options.AssemblyVersion));
            if (options.AssemblyInformationalVersion != null)
                replacements.Add(new VersionUpdate("AssemblyInformationalVersion", options.AssemblyInformationalVersion));

            foreach (var fileVersion in versionData)
            {
                var rc = Editor.Edit(replacements, fileVersion.Path, output, options.DryRun);
                if (rc != 0)
                    return rc;
            }

            if (options.EnvironmentVariableName != null)
            {
                var firstReplacement = replacements.FirstOrDefault();
                if (firstReplacement != null)
                    Environment.SetEnvironmentVariable(options.EnvironmentVariableName, firstReplacement.Version, EnvironmentVariableTarget.User);
            }

            return 0;
        }
예제 #15
0
        private static bool ValidateIncSetting(Options options, IList<string> errorList)
        {
            if (!IsIncSet(options)) return true;

            if (options.IncrementPart == IncrementablePart.None)
            {
                errorList.Add("Invalid increment part.");
                return false;
            }

            if (!IsValidVersionName(options.IncrementTarget, false))
            {
                errorList.Add("Invalid version specified for Inc. Specify FV or AV only.");
                return false;
            }

            return true;
        }
예제 #16
0
        private static int IncrementVersion(Options options, TextWriter output, List<FileVersion> versionData)
        {
            if (options.IncrementPart == IncrementablePart.None)
            {
                output.WriteLine("Increment can only change build (b) or revision number (r).");
                return -1;
            }

            if (!Validator.Check(versionData, output))
            {
                output.WriteLine();
                output.WriteLine("All versions must be made consistent before they can be edited.");
                return -1;
            }

            var incrementName = TargetToName(options.IncrementTarget);
            if (incrementName == null)
            {
                output.WriteLine("Increment target not recognised.");
                return -1;
            }

            var canon = versionData.SelectMany(f => f.Versions).FirstOrDefault(v => v.Description == incrementName);
            if (canon == null)
            {
                output.WriteLine("No version found.");
                return -1;
            }

            string textVersion = options.IncrementPart == IncrementablePart.Revision
                ? canon.Revision
                : canon.Build;

            int newValue;
            if (!int.TryParse(textVersion, out newValue))
            {
                output.WriteLine("Unable to decode version: {0}", textVersion);
                return -1;
            }

            ++newValue;
            if (options.IncrementPart == IncrementablePart.Revision)
            {
                canon.Revision = newValue.ToString();
            }
            else
            {
                canon.Build = newValue.ToString();
                canon.Revision = "0";
            }

            var versionUpdate = new VersionUpdate(incrementName, canon);
            var versionUpdates = new[] { versionUpdate };
            foreach (var fileVersion in versionData)
            {
                var rc = Editor.Edit(versionUpdates, fileVersion.Path, output, options.DryRun);
                if (rc != 0)
                    return rc;
            }

            if (options.EnvironmentVariableName != null)
            {
                Environment.SetEnvironmentVariable(options.EnvironmentVariableName, versionUpdate.Version, EnvironmentVariableTarget.User);

            }

            return 0;
        }
예제 #17
0
 private static int GetNumVersionsSet(Options options)
 {
     var numVersionsSet = 0;
     if (options.AssemblyVersion != null) ++numVersionsSet;
     if (options.AssemblyFileVersion != null) ++numVersionsSet;
     if (options.AssemblyInformationalVersion != null) ++numVersionsSet;
     return numVersionsSet;
 }