aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Package/MsBuild/ConversionReportViewer.cs
blob: c80ed92aa0a8d02cacc67ad98e09a1cba406900e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/***************************************************************************************************
 Copyright (C) 2024 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Newtonsoft.Json.Linq;
using IServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

namespace QtVsTools.Package.MsBuild
{
    using Core.MsBuild;
    using VisualStudio;
    using static Core.Common.Utils;

    [Guid(GuidString)]
    public partial class ConversionReportViewer : IVsEditorFactory
    {
        private const string GuidString = "57F4A78F-2C57-4903-8A8E-A48FF5D0B2A8";
        public static Guid Guid { get; } = new(GuidString);

        private ConversionReportViewerWindow ViewerWindow { get; set; }

        private void CreateEditorInstance()
        {
            ViewerWindow = new ConversionReportViewerWindow();
        }

        public int Close()
        {
            return VSConstants.S_OK;
        }
    }

    public partial class ConversionReportViewerWindow : WindowPane, IVsPersistDocData
    {
        private RichTextBox TextBox { get; } = new();
        private JObject Metadata { get; set; }
        private Dictionary<string, string> TempBefore { get; } = new();
        private Dictionary<string, string> TempAfter { get; } = new();

        public ConversionReportViewerWindow()
        {
            TextBox.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
            TextBox.IsReadOnly = true;
            TextBox.IsReadOnlyCaretVisible = false;
            Content = TextBox;
        }

        public int LoadDocData(string path)
        {
            if (ConversionReport.Load(path) is not { } report)
                return VSConstants.E_FAIL;
            if (report.Document is not { } document)
                return VSConstants.E_FAIL;
            if (document.Tag is not string { Length: > 0 } metadata)
                return VSConstants.E_FAIL;
            TextBox.Document = document;
            try {
                Metadata = JObject.Parse(metadata);
                if (Metadata["files"] is not JObject files)
                    return VSConstants.E_FAIL;
                foreach (var file in files.Properties()) {
                    File.WriteAllText(TempBefore[file.Name]
                        = $@"{Path.GetTempPath()}\{Path.GetRandomFileName()}.xml",
                        FromZipBase64(file.Value["before"].Value<string>()));
                    File.WriteAllText(TempAfter[file.Name]
                        = $@"{Path.GetTempPath()}\{Path.GetRandomFileName()}.xml",
                        FromZipBase64(file.Value["after"].Value<string>()));
                }
            } catch (Exception) {
                return VSConstants.E_FAIL;
            }

            foreach (var link in FindDocumentHyperlinks(TextBox.Document)) {
                var enableLink = !link.NavigateUri.Query.Contains("current")
                    || File.Exists(link.NavigateUri.LocalPath);
                link.Cursor = enableLink ? Cursors.Hand : Cursors.No;
                link.Foreground = new SolidColorBrush(enableLink ? Colors.Blue : Colors.Gray);
                if (enableLink)
                    link.MouseDown += Link_MouseDown;
            }

            return VSConstants.S_OK;
        }

        private string LinkPath(Hyperlink link, string moniker)
        {
            return moniker switch
            {
                "before" => TempBefore[link.NavigateUri.LocalPath],
                "after" => TempAfter[link.NavigateUri.LocalPath],
                _ => link.NavigateUri.LocalPath,
            };
        }

        private void Link_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            if (sender is not Hyperlink link)
                return;
            if (link.NavigateUri?.Query is not { Length: > 0 } query)
                return;
            var queryArgs = query.Split(new[] { '?', '&' }, StringSplitOptions.RemoveEmptyEntries);
            if (queryArgs.ElementAtOrDefault(0) is not { Length: > 0 } request)
                return;
            switch (request) {
            case "before":
            case "after":
                VsEditor.Open(LinkPath(link, request), VsEditor.OpenWith.CodeEditor);
                break;
            case "diff":
                if (queryArgs.ElementAtOrDefault(1) is not { Length: > 0 } leftMoniker)
                    break;
                if (queryArgs.ElementAtOrDefault(2) is not { Length: > 0 } rightMoniker)
                    break;
                if (LinkPath(link, leftMoniker) is not { Length: > 0 } leftPath)
                    break;
                if (LinkPath(link, rightMoniker) is not { Length: > 0 } rightPath)
                    break;
                if (!File.Exists(leftPath))
                    break;
                if (!File.Exists(rightPath))
                    break;
                VsEditor.Diff(leftPath, rightPath);
                break;
            }
        }

        public int Close()
        {
            foreach (var tempFile in TempBefore.Values.Union(TempAfter.Values).ToList()) {
                try { File.Delete(tempFile); } catch (Exception) { }
            }
            TempBefore.Clear();
            TempAfter.Clear();
            return VSConstants.S_OK;
        }

        private List<Hyperlink> FindDocumentHyperlinks(FlowDocument doc)
        {
            var nodes = new Stack<DependencyObject>();
            var links = new List<Hyperlink>();
            nodes.Push(doc);
            while (nodes.Any()) {
                var node = nodes.Pop();
                foreach (var child in LogicalTreeHelper.GetChildren(node).OfType<DependencyObject>())
                    nodes.Push(child);
                if (node is Hyperlink link)
                    links.Add(link);
            }
            return links;
        }
    }

    #region ### BOILERPLATE ########################################################################

    public partial class ConversionReportViewer : IVsEditorFactory
    {
        public int SetSite(IServiceProvider psp)
        {
            return VSConstants.S_OK;
        }

        [EnvironmentPermission(SecurityAction.Demand, Unrestricted = true)]
        public int CreateEditorInstance(uint grfCreateDoc, string pszMkDocument,
            string pszPhysicalView, IVsHierarchy pvHier, uint itemid, IntPtr punkDocDataExisting,
            out IntPtr ppunkDocView, out IntPtr ppunkDocData, out string pbstrEditorCaption,
            out Guid pguidCmdUI, out int pgrfCDW)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            ppunkDocView = IntPtr.Zero;
            ppunkDocData = IntPtr.Zero;
            pguidCmdUI = Guid;
            pgrfCDW = 0;
            pbstrEditorCaption = null;

            if ((grfCreateDoc & (VSConstants.CEF_OPENFILE | VSConstants.CEF_SILENT)) == 0) {
                return VSConstants.E_INVALIDARG;
            }
            if (punkDocDataExisting != IntPtr.Zero) {
                return VSConstants.VS_E_INCOMPATIBLEDOCDATA;
            }

            CreateEditorInstance();
            ppunkDocView = Marshal.GetIUnknownForObject(ViewerWindow);
            ppunkDocData = Marshal.GetIUnknownForObject(ViewerWindow);
            pbstrEditorCaption = "";
            pgrfCDW = (int)(_VSRDTFLAGS.RDT_CantSave | _VSRDTFLAGS.RDT_DontAutoOpen);

            return VSConstants.S_OK;
        }

        public int MapLogicalView(ref Guid rguidLogicalView, out string pbstrPhysicalView)
        {
            pbstrPhysicalView = null;
            if (VSConstants.LOGVIEWID_Primary == rguidLogicalView)
                return VSConstants.S_OK;
            return VSConstants.E_NOTIMPL;
        }
    }

    public partial class ConversionReportViewerWindow : WindowPane, IVsPersistDocData
    {
        public int GetGuidEditorType(out Guid pClassID)
        {
            pClassID = ConversionReportViewer.Guid;
            return VSConstants.S_OK;
        }

        public int IsDocDataDirty(out int pfDirty)
        {
            pfDirty = 0;
            return VSConstants.S_OK;
        }

        public int SetUntitledDocPath(string pszDocDataPath)
        {
            return VSConstants.S_OK;
        }

        public int SaveDocData(VSSAVEFLAGS dwSave, out string pbstrMkDocumentNew,
            out int pfSaveCanceled)
        {
            pbstrMkDocumentNew = string.Empty;
            pfSaveCanceled = 0;
            return VSConstants.E_NOTIMPL;
        }

        public int OnRegisterDocData(uint docCookie, IVsHierarchy pHierNew, uint itemidNew)
        {
            return VSConstants.S_OK;
        }

        public int RenameDocData(uint grfAttribs, IVsHierarchy pHierNew, uint itemidNew,
            string pszMkDocumentNew)
        {
            return VSConstants.E_NOTIMPL;
        }

        public int IsDocDataReloadable(out int pfReloadable)
        {
            pfReloadable = 0;
            return VSConstants.S_OK;
        }

        public int ReloadDocData(uint grfFlags)
        {
            return VSConstants.E_NOTIMPL;
        }
    }
    #endregion ### BOILERPLATE #####################################################################
}