Browse Source

Implement 3330: Generate diagram from UI without advanced options (#3336)

pull/3350/head
Christoph Wille 5 months ago committed by GitHub
parent
commit
e652490cce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 124
      ILSpy/Commands/CreateDiagramContextMenuEntry.cs
  2. 5
      ILSpy/Docking/DockWorkspace.cs
  3. 18
      ILSpy/Properties/Resources.Designer.cs
  4. 6
      ILSpy/Properties/Resources.resx
  5. 4
      ILSpy/TextView/DecompilerTextView.cs

124
ILSpy/Commands/CreateDiagramContextMenuEntry.cs

@ -0,0 +1,124 @@ @@ -0,0 +1,124 @@
// Copyright (c) 2024 Christoph Wille for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Composition;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using ICSharpCode.Decompiler;
using ICSharpCode.ILSpy.Docking;
using ICSharpCode.ILSpy.Properties;
using ICSharpCode.ILSpy.TreeNodes;
using ICSharpCode.ILSpyX.MermaidDiagrammer;
using Microsoft.Win32;
namespace ICSharpCode.ILSpy.TextView
{
[ExportContextMenuEntry(Header = nameof(Resources._CreateDiagram), Category = nameof(Resources.Save), Icon = "Images/Save")]
[Shared]
sealed class CreateDiagramContextMenuEntry(DockWorkspace dockWorkspace) : IContextMenuEntry
{
public void Execute(TextViewContext context)
{
var assembly = (context.SelectedTreeNodes?.FirstOrDefault() as AssemblyTreeNode)?.LoadedAssembly;
if (assembly == null)
return;
var selectedPath = SelectDestinationFolder();
if (string.IsNullOrEmpty(selectedPath))
return;
dockWorkspace.RunWithCancellation(ct => Task<AvalonEditTextOutput>.Factory.StartNew(() => {
AvalonEditTextOutput output = new() {
EnableHyperlinks = true
};
Stopwatch stopwatch = Stopwatch.StartNew();
try
{
var command = new GenerateHtmlDiagrammer {
Assembly = assembly.FileName,
OutputFolder = selectedPath
};
command.Run();
}
catch (OperationCanceledException)
{
output.WriteLine();
output.WriteLine(Resources.GenerationWasCancelled);
throw;
}
stopwatch.Stop();
output.WriteLine(Resources.GenerationCompleteInSeconds, stopwatch.Elapsed.TotalSeconds.ToString("F1"));
output.WriteLine();
output.WriteLine("Learn more: " + "https://github.com/icsharpcode/ILSpy/wiki/Diagramming#tips-for-using-the-html-diagrammer");
output.WriteLine();
var diagramHtml = Path.Combine(selectedPath, "index.html");
output.AddButton(null, Resources.OpenExplorer, delegate { Process.Start("explorer", "/select,\"" + diagramHtml + "\""); });
output.WriteLine();
return output;
}, ct), Properties.Resources.CreatingDiagram).Then(dockWorkspace.ShowText).HandleExceptions();
return;
}
public bool IsEnabled(TextViewContext context) => true;
public bool IsVisible(TextViewContext context)
{
return context.SelectedTreeNodes?.Length == 1
&& context.SelectedTreeNodes?.FirstOrDefault() is AssemblyTreeNode tn
&& tn.LoadedAssembly.IsLoadedAsValidAssembly;
}
static string SelectDestinationFolder()
{
OpenFolderDialog dialog = new();
dialog.Multiselect = false;
dialog.Title = "Select target folder";
if (dialog.ShowDialog() != true)
{
return null;
}
string selectedPath = Path.GetDirectoryName(dialog.FolderName);
bool directoryNotEmpty;
try
{
directoryNotEmpty = Directory.EnumerateFileSystemEntries(selectedPath).Any();
}
catch (Exception e) when (e is IOException || e is UnauthorizedAccessException || e is System.Security.SecurityException)
{
MessageBox.Show(
"The directory cannot be accessed. Please ensure it exists and you have sufficient rights to access it.",
"Target directory not accessible",
MessageBoxButton.OK, MessageBoxImage.Error);
return null;
}
return dialog.FolderName;
}
}
}

5
ILSpy/Docking/DockWorkspace.cs

@ -226,6 +226,11 @@ namespace ICSharpCode.ILSpy.Docking @@ -226,6 +226,11 @@ namespace ICSharpCode.ILSpy.Docking
return ActiveTabPage.ShowTextViewAsync(textView => textView.RunWithCancellation(taskCreation));
}
public Task<T> RunWithCancellation<T>(Func<CancellationToken, Task<T>> taskCreation, string progressTitle)
{
return ActiveTabPage.ShowTextViewAsync(textView => textView.RunWithCancellation(taskCreation, progressTitle));
}
internal void ShowNodes(AvalonEditTextOutput output, TreeNodes.ILSpyTreeNode[] nodes, IHighlightingDefinition highlighting)
{
ActiveTabPage.ShowTextView(textView => textView.ShowNodes(output, nodes, highlighting));

18
ILSpy/Properties/Resources.Designer.cs generated

@ -114,6 +114,15 @@ namespace ICSharpCode.ILSpy.Properties { @@ -114,6 +114,15 @@ namespace ICSharpCode.ILSpy.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Create _Diagram....
/// </summary>
public static string _CreateDiagram {
get {
return ResourceManager.GetString("_CreateDiagram", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to _File.
/// </summary>
@ -567,6 +576,15 @@ namespace ICSharpCode.ILSpy.Properties { @@ -567,6 +576,15 @@ namespace ICSharpCode.ILSpy.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Creating diagram....
/// </summary>
public static string CreatingDiagram {
get {
return ResourceManager.GetString("CreatingDiagram", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Culture.
/// </summary>

6
ILSpy/Properties/Resources.resx

@ -210,6 +210,9 @@ Are you sure you want to continue?</value> @@ -210,6 +210,9 @@ Are you sure you want to continue?</value>
<data name="Create" xml:space="preserve">
<value>Create</value>
</data>
<data name="CreatingDiagram" xml:space="preserve">
<value>Creating diagram...</value>
</data>
<data name="CultureLabel" xml:space="preserve">
<value>Culture</value>
</data>
@ -1036,6 +1039,9 @@ Do you want to continue?</value> @@ -1036,6 +1039,9 @@ Do you want to continue?</value>
<data name="_CollapseTreeNodes" xml:space="preserve">
<value>_Collapse all tree nodes</value>
</data>
<data name="_CreateDiagram" xml:space="preserve">
<value>Create _Diagram...</value>
</data>
<data name="_File" xml:space="preserve">
<value>_File</value>
</data>

4
ILSpy/TextView/DecompilerTextView.cs

@ -595,14 +595,14 @@ namespace ICSharpCode.ILSpy.TextView @@ -595,14 +595,14 @@ namespace ICSharpCode.ILSpy.TextView
/// the task.
/// If another task is started before the previous task finishes running, the previous task is cancelled.
/// </summary>
public Task<T> RunWithCancellation<T>(Func<CancellationToken, Task<T>> taskCreation)
public Task<T> RunWithCancellation<T>(Func<CancellationToken, Task<T>> taskCreation, string? progressTitle = null)
{
if (waitAdorner.Visibility != Visibility.Visible)
{
waitAdorner.Visibility = Visibility.Visible;
// Work around a WPF bug by setting IsIndeterminate only while the progress bar is visible.
// https://github.com/icsharpcode/ILSpy/issues/593
progressTitle.Text = Properties.Resources.Decompiling;
this.progressTitle.Text = progressTitle == null ? Properties.Resources.Decompiling : progressTitle;
progressBar.IsIndeterminate = true;
progressText.Text = null;
progressText.Visibility = Visibility.Collapsed;

Loading…
Cancel
Save