diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f78b7db9..c440f252 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,12 +24,6 @@ jobs: tests: framework: net5.0 sdk: "5.0.301" - - job: - os: ubuntu-20.04 - build: ./build.sh - tests: - framework: netcoreapp2.1 - sdk: "2.1.816" - job: os: ubuntu-20.04 build: ./build.sh diff --git a/MinVer.Lib/Git.cs b/MinVer.Lib/Git.cs index ec2cef80..26be2f9f 100644 --- a/MinVer.Lib/Git.cs +++ b/MinVer.Lib/Git.cs @@ -33,7 +33,7 @@ public static IEnumerable GetTagsOrEmpty(string directory, ILogger log) => ? output .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) .Select(line => line.Split(new[] { ' ' }, 2)) - .Select(tokens => new Tag(tokens[1].Substring(10).RemoveFromEnd("^{}"), tokens[0])) + .Select(tokens => new Tag(tokens[1][10..].RemoveFromEnd("^{}"), tokens[0])) : Enumerable.Empty(); private static string RemoveFromEnd(this string text, string value) => diff --git a/MinVer.Lib/GitCommand.cs b/MinVer.Lib/GitCommand.cs index fb2bf6e4..2621542e 100644 --- a/MinVer.Lib/GitCommand.cs +++ b/MinVer.Lib/GitCommand.cs @@ -9,49 +9,48 @@ internal static class GitCommand { public static bool TryRun(string args, string workingDirectory, ILogger log, out string output) { - using (var process = new Process()) + using var process = new Process(); + + process.StartInfo = new ProcessStartInfo + { + FileName = "git", + Arguments = args, + WorkingDirectory = workingDirectory, + UseShellExecute = false, + RedirectStandardError = true, + RedirectStandardOutput = true, + }; + + var tcs = new TaskCompletionSource(); + process.Exited += (s, e) => tcs.SetResult(default); + process.EnableRaisingEvents = true; + + log.Trace($"Running Git: {process.StartInfo.FileName} {process.StartInfo.Arguments}"); + + try + { + _ = process.Start(); + } + catch (Win32Exception ex) { - process.StartInfo = new ProcessStartInfo - { - FileName = "git", - Arguments = args, - WorkingDirectory = workingDirectory, - UseShellExecute = false, - RedirectStandardError = true, - RedirectStandardOutput = true, - }; - - var tcs = new TaskCompletionSource(); - process.Exited += (s, e) => tcs.SetResult(default); - process.EnableRaisingEvents = true; - - log.Trace($"Running Git: {process.StartInfo.FileName} {process.StartInfo.Arguments}"); - - try - { - _ = process.Start(); - } - catch (Win32Exception ex) - { - throw new InvalidOperationException("\"git\" is not present in PATH.", ex); - } - - var runProcess = tcs.Task; - var readOutput = process.StandardOutput.ReadToEndAsync(); - var readError = process.StandardError.ReadToEndAsync(); - - Task.WaitAll(runProcess, readOutput, readError); - - var exitCode = process.ExitCode; - output = readOutput.Result; - var error = readError.Result; - - log.Trace($"Git exit code: {exitCode}"); - log.Trace($"Git stdout:{Environment.NewLine}{output}"); - log.Trace($"Git stderr:{Environment.NewLine}{error}"); - - return exitCode == 0; + throw new InvalidOperationException("\"git\" is not present in PATH.", ex); } + + var runProcess = tcs.Task; + var readOutput = process.StandardOutput.ReadToEndAsync(); + var readError = process.StandardError.ReadToEndAsync(); + + Task.WaitAll(runProcess, readOutput, readError); + + var exitCode = process.ExitCode; + output = readOutput.Result; + var error = readError.Result; + + log.Trace($"Git exit code: {exitCode}"); + log.Trace($"Git stdout:{Environment.NewLine}{output}"); + log.Trace($"Git stderr:{Environment.NewLine}{error}"); + + return exitCode == 0; } } } diff --git a/MinVer.Lib/MinVer.Lib.csproj b/MinVer.Lib/MinVer.Lib.csproj index 1c0a4acb..59928ba0 100644 --- a/MinVer.Lib/MinVer.Lib.csproj +++ b/MinVer.Lib/MinVer.Lib.csproj @@ -1,7 +1,7 @@ - netstandard2.0 + netcoreapp3.1 diff --git a/MinVer.Lib/Version.cs b/MinVer.Lib/Version.cs index aa061c8f..b63141f9 100644 --- a/MinVer.Lib/Version.cs +++ b/MinVer.Lib/Version.cs @@ -103,25 +103,16 @@ public Version Satisfying(MajorMinor minMajorMinor, string defaultPreReleasePhas ? this : new Version(minMajorMinor.Major, minMajorMinor.Minor, 0, new[] { defaultPreReleasePhase, "0" }, this.height, this.buildMetadata); - public Version WithHeight(int height, VersionPart autoIncrement, string defaultPreReleasePhase) - { - if (this.preReleaseIdentifiers.Count == 0 && height > 0) - { - switch (autoIncrement) + public Version WithHeight(int height, VersionPart autoIncrement, string defaultPreReleasePhase) => + this.preReleaseIdentifiers.Count == 0 && height > 0 + ? autoIncrement switch { - case VersionPart.Major: - return new Version(this.major + 1, 0, 0, new[] { defaultPreReleasePhase, "0" }, height, null); - case VersionPart.Minor: - return new Version(this.major, this.minor + 1, 0, new[] { defaultPreReleasePhase, "0" }, height, null); - case VersionPart.Patch: - return new Version(this.major, this.minor, this.patch + 1, new[] { defaultPreReleasePhase, "0" }, height, null); - default: - throw new ArgumentOutOfRangeException(nameof(autoIncrement)); + VersionPart.Major => new Version(this.major + 1, 0, 0, new[] { defaultPreReleasePhase, "0" }, height, null), + VersionPart.Minor => new Version(this.major, this.minor + 1, 0, new[] { defaultPreReleasePhase, "0" }, height, null), + VersionPart.Patch => new Version(this.major, this.minor, this.patch + 1, new[] { defaultPreReleasePhase, "0" }, height, null), + _ => throw new ArgumentOutOfRangeException(nameof(autoIncrement)), } - } - - return new Version(this.major, this.minor, this.patch, this.preReleaseIdentifiers, height, height == 0 ? this.buildMetadata : null); - } + : new Version(this.major, this.minor, this.patch, this.preReleaseIdentifiers, height, height == 0 ? this.buildMetadata : null); public Version AddBuildMetadata(string buildMetadata) { @@ -132,7 +123,7 @@ public Version AddBuildMetadata(string buildMetadata) public static bool TryParse(string text, out Version version) => (version = ParseOrDefault(text, null)) != null; public static Version ParseOrDefault(string text, string prefix) => - text == null || !text.StartsWith(prefix ?? "", StringComparison.OrdinalIgnoreCase) ? null : ParseOrDefault(text.Substring(prefix?.Length ?? 0)); + text == null || !text.StartsWith(prefix ?? "", StringComparison.OrdinalIgnoreCase) ? null : ParseOrDefault(text[(prefix?.Length ?? 0)..]); private static Version ParseOrDefault(string text) { @@ -168,7 +159,7 @@ public override int GetHashCode() code = (code * 23) + this.patch.GetHashCode(); code = (code * 23) + this.preReleaseIdentifiers.GetHashCode(); code = (code * 23) + this.height.GetHashCode(); - code = (code * 23) + this.buildMetadata.GetHashCode(); + code = (code * 23) + this.buildMetadata.GetHashCode(StringComparison.Ordinal); return code; } diff --git a/MinVer.Lib/Versioner.cs b/MinVer.Lib/Versioner.cs index ab8978d2..7573de02 100644 --- a/MinVer.Lib/Versioner.cs +++ b/MinVer.Lib/Versioner.cs @@ -4,7 +4,7 @@ public static class Versioner { public static Version GetVersion(string workDir, string tagPrefix, MajorMinor minMajorMinor, string buildMeta, VersionPart autoIncrement, string defaultPreReleasePhase, ILogger log) { - log = log ?? new NullLogger(); + log ??= new NullLogger(); defaultPreReleasePhase = string.IsNullOrEmpty(defaultPreReleasePhase) ? "alpha" diff --git a/MinVer/MinVer.csproj b/MinVer/MinVer.csproj index c37bdb01..a7befd75 100644 --- a/MinVer/MinVer.csproj +++ b/MinVer/MinVer.csproj @@ -18,7 +18,7 @@ true major true - netcoreapp2.1 + netcoreapp3.1 diff --git a/MinVer/VerbosityMap.cs b/MinVer/VerbosityMap.cs index e49897af..6d2fb065 100644 --- a/MinVer/VerbosityMap.cs +++ b/MinVer/VerbosityMap.cs @@ -15,7 +15,7 @@ static VerbosityMap() Add(Verbosity.Detailed, 1); Add(Verbosity.Diagnostic, 4); - void Add(Verbosity verbosity, int shortLength) + static void Add(Verbosity verbosity, int shortLength) { map.Add(verbosity.ToString(), verbosity); map.Add(verbosity.ToString().Substring(0, shortLength), verbosity); diff --git a/MinVerTests.Infra/FileSystem.cs b/MinVerTests.Infra/FileSystem.cs index e6791b04..c56fcc6f 100644 --- a/MinVerTests.Infra/FileSystem.cs +++ b/MinVerTests.Infra/FileSystem.cs @@ -25,11 +25,7 @@ private static void DeleteDirectory(string path) // Directory.Delete fails if anything in the tree has the read-only attribute set. ¯\_(ツ)_/¯ ResetAttributes(new DirectoryInfo(path)); -#if NET static void ResetAttributes(DirectoryInfo directory) -#else - void ResetAttributes(DirectoryInfo directory) -#endif { foreach (var childDirectory in directory.GetDirectories()) { diff --git a/MinVerTests.Infra/MinVerCli.cs b/MinVerTests.Infra/MinVerCli.cs index 0606bbed..b9b231e8 100644 --- a/MinVerTests.Infra/MinVerCli.cs +++ b/MinVerTests.Infra/MinVerCli.cs @@ -20,6 +20,6 @@ public static class MinVerCli } public static string GetPath(string configuration) => - Solution.GetFullPath($"minver-cli/bin/{configuration}/netcoreapp2.1/minver-cli.dll"); + Solution.GetFullPath($"minver-cli/bin/{configuration}/netcoreapp3.1/minver-cli.dll"); } } diff --git a/MinVerTests.Infra/MinVerTests.Infra.csproj b/MinVerTests.Infra/MinVerTests.Infra.csproj index 34644364..cb30dc6c 100644 --- a/MinVerTests.Infra/MinVerTests.Infra.csproj +++ b/MinVerTests.Infra/MinVerTests.Infra.csproj @@ -1,7 +1,7 @@ - netcoreapp2.1;net5.0 + netcoreapp3.1;net5.0 diff --git a/MinVerTests.Lib/MinVerTests.Lib.csproj b/MinVerTests.Lib/MinVerTests.Lib.csproj index 2fc59156..c0589c83 100644 --- a/MinVerTests.Lib/MinVerTests.Lib.csproj +++ b/MinVerTests.Lib/MinVerTests.Lib.csproj @@ -2,11 +2,7 @@ major - netcoreapp2.1;netcoreapp3.1;net5.0 - - - - true + netcoreapp3.1;net5.0 diff --git a/MinVerTests.Packages/MultipleProjects.cs b/MinVerTests.Packages/MultipleProjects.cs index 8e55d573..0976e1a9 100644 --- a/MinVerTests.Packages/MultipleProjects.cs +++ b/MinVerTests.Packages/MultipleProjects.cs @@ -15,7 +15,7 @@ public class MultipleProjects public MultipleProjects(ITestOutputHelper output) => this.output = output; - // for some reason, when using SDK 2.1 or 3.1, + // for some reason, when using SDK 3.1, // there is a 15 minute delay after the `dotnet build` command, // so we only run this test on SDK 5.0 and later [Net5PlusFact] diff --git a/README.md b/README.md index 68c46743..843dff9f 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Also available as a [command line tool](#can-i-use-minver-to-version-software-wh ## Prerequisites -- [.NET Core SDK 2.1 or later](https://www.microsoft.com/net/download) +- [.NET Core SDK 3.1 or later](https://www.microsoft.com/net/download) - [Git](https://git-scm.com/) ## Quick start diff --git a/minver-cli/Program.cs b/minver-cli/Program.cs index d55c5cee..5f6cb8e9 100644 --- a/minver-cli/Program.cs +++ b/minver-cli/Program.cs @@ -13,102 +13,101 @@ internal static class Program private static int Main(string[] args) { - using (var app = new CommandLineApplication { Name = "minver", FullName = $"MinVer CLI {informationalVersion}" }) - { - app.HelpOption(); + using var app = new CommandLineApplication { Name = "minver", FullName = $"MinVer CLI {informationalVersion}" }; + + app.HelpOption(); - var workDirArg = app.Argument("workingDirectory", "Working directory (optional)"); + var workDirArg = app.Argument("workingDirectory", "Working directory (optional)"); - var autoIncrementOption = app.Option("-a|--auto-increment ", VersionPartExtensions.ValidValues, CommandOptionType.SingleValue); - var buildMetaOption = app.Option("-b|--build-metadata ", "", CommandOptionType.SingleValue); - var defaultPreReleasePhaseOption = app.Option("-d|--default-pre-release-phase ", "alpha (default), preview, etc.", CommandOptionType.SingleValue); - var minMajorMinorOption = app.Option("-m|--minimum-major-minor ", MajorMinor.ValidValues, CommandOptionType.SingleValue); + var autoIncrementOption = app.Option("-a|--auto-increment ", VersionPartExtensions.ValidValues, CommandOptionType.SingleValue); + var buildMetaOption = app.Option("-b|--build-metadata ", "", CommandOptionType.SingleValue); + var defaultPreReleasePhaseOption = app.Option("-d|--default-pre-release-phase ", "alpha (default), preview, etc.", CommandOptionType.SingleValue); + var minMajorMinorOption = app.Option("-m|--minimum-major-minor ", MajorMinor.ValidValues, CommandOptionType.SingleValue); #if MINVER_CLI - var workDirOption = app.Option("-r|--repo ", "DEPRECATED — use the workingDirectory argument instead", CommandOptionType.SingleValue); + var workDirOption = app.Option("-r|--repo ", "DEPRECATED — use the workingDirectory argument instead", CommandOptionType.SingleValue); #endif - var tagPrefixOption = app.Option("-t|--tag-prefix ", "", CommandOptionType.SingleValue); - var verbosityOption = app.Option("-v|--verbosity ", VerbosityMap.ValidValues, CommandOptionType.SingleValue); + var tagPrefixOption = app.Option("-t|--tag-prefix ", "", CommandOptionType.SingleValue); + var verbosityOption = app.Option("-v|--verbosity ", VerbosityMap.ValidValues, CommandOptionType.SingleValue); #if MINVER - var versionOverrideOption = app.Option("-o|--version-override ", "", CommandOptionType.SingleValue); + var versionOverrideOption = app.Option("-o|--version-override ", "", CommandOptionType.SingleValue); #endif - app.OnExecute(() => - { - // optional argument — /~https://github.com/adamralph/minver/issues/436 - var workDir = "."; + app.OnExecute(() => + { + // optional argument — /~https://github.com/adamralph/minver/issues/436 + var workDir = "."; #if MINVER_CLI - if (!string.IsNullOrEmpty(workDirOption.Value())) - { - Logger.Warn("-r|--repo is DEPRECATED — use the workingDirectory argument instead"); - } + if (!string.IsNullOrEmpty(workDirOption.Value())) + { + Logger.Warn("-r|--repo is DEPRECATED — use the workingDirectory argument instead"); + } #endif - if (!string.IsNullOrEmpty(workDirArg.Value)) - { - if (!Directory.Exists(workDir = workDirArg.Value)) - { - Logger.ErrorWorkDirDoesNotExist(workDirArg.Value); - return 2; - } - } -#if MINVER_CLI - else if (!string.IsNullOrEmpty(workDirOption.Value()) && !Directory.Exists(workDir = workDirOption.Value())) + if (!string.IsNullOrEmpty(workDirArg.Value)) + { + if (!Directory.Exists(workDir = workDirArg.Value)) { - Logger.ErrorWorkDirDoesNotExist(workDirOption.Value()); + Logger.ErrorWorkDirDoesNotExist(workDirArg.Value); return 2; } + } +#if MINVER_CLI + else if (!string.IsNullOrEmpty(workDirOption.Value()) && !Directory.Exists(workDir = workDirOption.Value())) + { + Logger.ErrorWorkDirDoesNotExist(workDirOption.Value()); + return 2; + } #endif - if (!Options.TryParse( - autoIncrementOption.Value(), - buildMetaOption.Value(), - defaultPreReleasePhaseOption.Value(), - minMajorMinorOption.Value(), - tagPrefixOption.Value(), - verbosityOption.Value(), + if (!Options.TryParse( + autoIncrementOption.Value(), + buildMetaOption.Value(), + defaultPreReleasePhaseOption.Value(), + minMajorMinorOption.Value(), + tagPrefixOption.Value(), + verbosityOption.Value(), #if MINVER - versionOverrideOption.Value(), + versionOverrideOption.Value(), #endif - out var options)) - { - return 2; - } + out var options)) + { + return 2; + } #if MINVER_CLI - if (!Options.TryParseEnvVars(out var envOptions)) - { - return 2; - } + if (!Options.TryParseEnvVars(out var envOptions)) + { + return 2; + } - options = options.Mask(envOptions); + options = options.Mask(envOptions); #endif - var log = new Logger(options.Verbosity); + var log = new Logger(options.Verbosity); - if (log.IsDebugEnabled) - { - log.Debug($"MinVer {informationalVersion}."); - } + if (log.IsDebugEnabled) + { + log.Debug($"MinVer {informationalVersion}."); + } - if (options.VersionOverride != null) - { - log.Info($"Using version override {options.VersionOverride}."); + if (options.VersionOverride != null) + { + log.Info($"Using version override {options.VersionOverride}."); - Console.Out.WriteLine(options.VersionOverride); + Console.Out.WriteLine(options.VersionOverride); - return 0; - } + return 0; + } - var version = Versioner.GetVersion(workDir, options.TagPrefix, options.MinMajorMinor, options.BuildMeta, options.AutoIncrement, options.DefaultPreReleasePhase, log); + var version = Versioner.GetVersion(workDir, options.TagPrefix, options.MinMajorMinor, options.BuildMeta, options.AutoIncrement, options.DefaultPreReleasePhase, log); - Console.Out.WriteLine(version); + Console.Out.WriteLine(version); - return 0; - }); + return 0; + }); - return app.Execute(args); - } + return app.Execute(args); } } } diff --git a/minver-cli/VerbosityMap.cs b/minver-cli/VerbosityMap.cs index 298908d4..1588b8a4 100644 --- a/minver-cli/VerbosityMap.cs +++ b/minver-cli/VerbosityMap.cs @@ -15,7 +15,7 @@ static VerbosityMap() Add(Verbosity.Debug); Add(Verbosity.Trace); - void Add(Verbosity verbosity) + static void Add(Verbosity verbosity) { map.Add(verbosity.ToString(), verbosity); map.Add(verbosity.ToString().Substring(0, 1), verbosity); diff --git a/minver-cli/minver-cli.csproj b/minver-cli/minver-cli.csproj index 4ad9293e..0aa29ebe 100644 --- a/minver-cli/minver-cli.csproj +++ b/minver-cli/minver-cli.csproj @@ -13,7 +13,7 @@ /~https://github.com/adamralph/minver/blob/main/CHANGELOG.md true major - netcoreapp2.1 + netcoreapp3.1 minver