From bf91f463f9f96db6e4a1ceec77a00e78caf7f147 Mon Sep 17 00:00:00 2001 From: Christoph Wille Date: Wed, 12 Jul 2023 14:56:44 +0200 Subject: [PATCH] Latest version check added (disabled). (Temp) Fixed DirectoryExists for ReferencePaths (see https://github.com/natemcmaster/CommandLineUtils/issues/536) --- .../DotNetToolUpdateChecker.cs | 51 +++++++++++++++++++ .../ICSharpCode.ILSpyCmd.csproj | 1 + ICSharpCode.ILSpyCmd/IlspyCmdProgram.cs | 12 +++-- 3 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 ICSharpCode.ILSpyCmd/DotNetToolUpdateChecker.cs diff --git a/ICSharpCode.ILSpyCmd/DotNetToolUpdateChecker.cs b/ICSharpCode.ILSpyCmd/DotNetToolUpdateChecker.cs new file mode 100644 index 0000000000..2f467e8fc9 --- /dev/null +++ b/ICSharpCode.ILSpyCmd/DotNetToolUpdateChecker.cs @@ -0,0 +1,51 @@ +using System; +using System.Linq; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; + +using NuGet.Common; +using NuGet.Protocol; +using NuGet.Protocol.Core.Types; +using NuGet.Versioning; + +namespace ICSharpCode.ILSpyCmd +{ + // Idea from https://github.com/ErikEJ/EFCorePowerTools/blob/master/src/GUI/efcpt/Services/PackageService.cs + internal static class DotNetToolUpdateChecker + { + static NuGetVersion CurrentPackageVersion() + { + return new NuGetVersion(Assembly.GetEntryAssembly()!.GetCustomAttribute()! + .InformationalVersion); + } + + public static async Task CheckForPackageUpdateAsync(string packageId) + { + try + { + using var cache = new SourceCacheContext(); + var repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json"); + var resource = await repository.GetResourceAsync().ConfigureAwait(false); + + var versions = await resource.GetAllVersionsAsync( + packageId, + cache, + new NullLogger(), + CancellationToken.None).ConfigureAwait(false); + + var latestVersion = versions.Where(v => v.Release == "").MaxBy(v => v); + if (latestVersion > CurrentPackageVersion()) + { + Console.WriteLine("You are not using the latest version of the tool, please update."); + Console.WriteLine($"Latest version is '{latestVersion}'"); + } + } +#pragma warning disable RCS1075 // Avoid empty catch clause that catches System.Exception. + catch (Exception) + { + } +#pragma warning restore RCS1075 // Avoid empty catch clause that catches System.Exception. + } + } +} diff --git a/ICSharpCode.ILSpyCmd/ICSharpCode.ILSpyCmd.csproj b/ICSharpCode.ILSpyCmd/ICSharpCode.ILSpyCmd.csproj index bf430983ae..c97d0e6505 100644 --- a/ICSharpCode.ILSpyCmd/ICSharpCode.ILSpyCmd.csproj +++ b/ICSharpCode.ILSpyCmd/ICSharpCode.ILSpyCmd.csproj @@ -51,6 +51,7 @@ + diff --git a/ICSharpCode.ILSpyCmd/IlspyCmdProgram.cs b/ICSharpCode.ILSpyCmd/IlspyCmdProgram.cs index 50770a98f6..17ff152b25 100644 --- a/ICSharpCode.ILSpyCmd/IlspyCmdProgram.cs +++ b/ICSharpCode.ILSpyCmd/IlspyCmdProgram.cs @@ -51,6 +51,8 @@ into nicely nested directories. MemberName = nameof(DecompilerVersion))] class ILSpyCmdProgram { + // https://natemcmaster.github.io/CommandLineUtils/docs/advanced/generic-host.html + // https://github.com/natemcmaster/CommandLineUtils/blob/main/docs/samples/dependency-injection/generic-host/Program.cs public static Task Main(string[] args) => new HostBuilder().RunCommandLineApplicationAsync(args); [FilesExist] @@ -95,7 +97,7 @@ class ILSpyCmdProgram [DirectoryExists] [Option("-r|--referencepath ", "Path to a directory containing dependencies of the assembly that is being decompiled.", CommandOptionType.MultipleValue)] - public string[] ReferencePaths { get; } = new string[0]; + public string[] ReferencePaths { get; } [Option("--no-dead-code", "Remove dead code.", CommandOptionType.NoValue)] public bool RemoveDeadCode { get; } @@ -103,7 +105,7 @@ class ILSpyCmdProgram [Option("--no-dead-stores", "Remove dead stores.", CommandOptionType.NoValue)] public bool RemoveDeadStores { get; } - [Option("-d|--dump-package", "Dump package assembiles into a folder. This requires the output directory option.", CommandOptionType.NoValue)] + [Option("-d|--dump-package", "Dump package assemblies into a folder. This requires the output directory option.", CommandOptionType.NoValue)] public bool DumpPackageFlag { get; } [Option("--nested-directories", "Use nested directories for namespaces.", CommandOptionType.NoValue)] @@ -117,6 +119,8 @@ public ILSpyCmdProgram(IHostEnvironment env) private Task OnExecuteAsync(CommandLineApplication app) { + // await DotNetToolUpdateChecker.CheckForPackageUpdateAsync("ilspycmd"); + TextWriter output = System.Console.Out; string outputDirectory = ResolveOutputDirectory(OutputDirectory); @@ -249,7 +253,7 @@ CSharpDecompiler GetDecompiler(string assemblyFileName) { var module = new PEFile(assemblyFileName); var resolver = new UniversalAssemblyResolver(assemblyFileName, false, module.Metadata.DetectTargetFrameworkId()); - foreach (var path in ReferencePaths) + foreach (var path in (ReferencePaths ?? Array.Empty())) { resolver.AddSearchDirectory(path); } @@ -287,7 +291,7 @@ ProjectId DecompileAsProject(string assemblyFileName, string projectFileName) { var module = new PEFile(assemblyFileName); var resolver = new UniversalAssemblyResolver(assemblyFileName, false, module.Metadata.DetectTargetFrameworkId()); - foreach (var path in ReferencePaths) + foreach (var path in (ReferencePaths ?? Array.Empty())) { resolver.AddSearchDirectory(path); }