Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions ILSpy/Commands/CreateDiagramContextMenuEntry.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions ILSpy/Docking/DockWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ public Task<T> RunWithCancellation<T>(Func<CancellationToken, Task<T>> taskCreat
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));
Expand Down
18 changes: 18 additions & 0 deletions ILSpy/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions ILSpy/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
4 changes: 2 additions & 2 deletions ILSpy/TextView/DecompilerTextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,14 +595,14 @@ public void Report(DecompilationProgress value)
/// 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;
Expand Down
Loading