Skip to content

Commit

Permalink
Latest version check added (disabled). (Temp) Fixed DirectoryExists f…
Browse files Browse the repository at this point in the history
…or ReferencePaths (see natemcmaster/CommandLineUtils#536)
  • Loading branch information
christophwille committed Jul 12, 2023
1 parent 8e3c1af commit bf91f46
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
51 changes: 51 additions & 0 deletions ICSharpCode.ILSpyCmd/DotNetToolUpdateChecker.cs
Original file line number Diff line number Diff line change
@@ -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<AssemblyInformationalVersionAttribute>()!
.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<FindPackageByIdResource>().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.
}
}
}
1 change: 1 addition & 0 deletions ICSharpCode.ILSpyCmd/ICSharpCode.ILSpyCmd.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<ItemGroup>
<PackageReference Include=" McMaster.Extensions.Hosting.CommandLine" Version="4.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="NuGet.Protocol" Version="6.6.1" />
</ItemGroup>

<Target Name="ILSpyUpdateAssemblyInfo" AfterTargets="ResolveProjectReferences">
Expand Down
12 changes: 8 additions & 4 deletions ICSharpCode.ILSpyCmd/IlspyCmdProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> Main(string[] args) => new HostBuilder().RunCommandLineApplicationAsync<ILSpyCmdProgram>(args);

[FilesExist]
Expand Down Expand Up @@ -95,15 +97,15 @@ class ILSpyCmdProgram

[DirectoryExists]
[Option("-r|--referencepath <path>", "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; }

[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)]
Expand All @@ -117,6 +119,8 @@ public ILSpyCmdProgram(IHostEnvironment env)

private Task<int> OnExecuteAsync(CommandLineApplication app)
{
// await DotNetToolUpdateChecker.CheckForPackageUpdateAsync("ilspycmd");

TextWriter output = System.Console.Out;
string outputDirectory = ResolveOutputDirectory(OutputDirectory);

Expand Down Expand Up @@ -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<string>()))
{
resolver.AddSearchDirectory(path);
}
Expand Down Expand Up @@ -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<string>()))
{
resolver.AddSearchDirectory(path);
}
Expand Down

0 comments on commit bf91f46

Please sign in to comment.