Skip to content

Commit

Permalink
Fix #526: Crash when switching to text template
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-englert committed Feb 3, 2023
1 parent 22cba1d commit ae63156
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
35 changes: 28 additions & 7 deletions src/ResXManager.VSIX.Compatibility.Shared/DteProjectFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Linq;
using System.Runtime.InteropServices;

using EnvDTE;

using ResXManager.Model;
using ResXManager.VSIX.Compatibility;
using ResXManager.VSIX.Compatibility.Properties;
Expand Down Expand Up @@ -116,8 +118,9 @@ public override bool IsWinFormsDesignerResource
// https://microsoft.public.de.german.entwickler.dotnet.vstudio.narkive.com/nL9BqJlj/aus-subtype-form-wird-subtype-component
return subType is @"Form" or @"UserControl" or @"Component";
}
catch (ExternalException)
catch (Exception ex)
{
_solution.Tracer.TraceError(ex.ToString());
}

return false;
Expand Down Expand Up @@ -148,8 +151,9 @@ private CodeGenerator GetCodeGenerator()

return Enum.TryParse(customTool, out CodeGenerator codeGenerator) ? codeGenerator : CodeGenerator.Unknown;
}
catch (ExternalException)
catch (Exception ex)
{
_solution.Tracer.TraceError(ex.ToString());
}

return CodeGenerator.Unknown;
Expand Down Expand Up @@ -188,8 +192,9 @@ private void SetCodeGenerator(CodeGenerator value)
}
}
}
catch (ExternalException)
catch (Exception ex)
{
_solution.Tracer.TraceError(ex.ToString());
}
}

Expand All @@ -216,10 +221,7 @@ private void SetTextTemplateCodeGenerator(EnvDTE.ProjectItem projectItem)
}

// Ensure DataAnnotations is referenced, used by TT generated code.
const string dataAnnotations = "System.ComponentModel.DataAnnotations";

var vsProject = projectItem.ContainingProject?.Object as VSLangProj.VSProject;
vsProject?.References?.Add(dataAnnotations);
ReferenceDataAnnotations(projectItem);

var fileName = Path.ChangeExtension(FilePath, "Designer.tt");

Expand All @@ -234,6 +236,25 @@ private void SetTextTemplateCodeGenerator(EnvDTE.ProjectItem projectItem)
item.RunCustomTool();
}

private static void ReferenceDataAnnotations(ProjectItem projectItem)
{
try
{
const string dataAnnotations = "System.ComponentModel.DataAnnotations";

var vsProject = projectItem.ContainingProject?.Object as VSLangProj.VSProject;
var references = vsProject?.References;
if ((references == null) || (references.Find(dataAnnotations) != null))
return;

references.Add(dataAnnotations);
}
catch
{
// just go without annotations, can be added manually.
}
}

private static void SetCustomToolCodeGenerator(EnvDTE.ProjectItem projectItem, CodeGenerator value)
{
ThrowIfNotOnUIThread();
Expand Down
17 changes: 9 additions & 8 deletions src/ResXManager.VSIX.Compatibility.Shared/DteSolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ internal class DteSolution
{
private const string SolutionItemsFolderName = "Solution Items";

private readonly ITracer _tracer;
private readonly DTE2 _dte;

private IEnumerable<ProjectFile>? _projectFiles;
Expand All @@ -39,10 +38,12 @@ public DteSolution(ITracer tracer)
{
ThrowIfNotOnUIThread();

_tracer = tracer;
Tracer = tracer;
_dte = (DTE2)(ServiceProvider.GlobalProvider.GetService(typeof(DTE)) ?? throw new InvalidOperationException("Can't get DTE service"));
}

public ITracer Tracer { get; }

/// <summary>
/// Gets all files of all project in the solution.
/// </summary>
Expand Down Expand Up @@ -90,13 +91,13 @@ public IEnumerable<ProjectFile> GetProjectFiles(IFileFilter fileFilter)
}
catch (Exception ex)
{
_tracer.TraceWarning("Error loading project {0}[{1}]: {2}", name, index, ex);
Tracer.TraceWarning("Error loading project {0}[{1}]: {2}", name, index, ex);
}
}
}
catch (Exception ex)
{
_tracer.TraceError("Error loading projects: {0}", ex);
Tracer.TraceError("Error loading projects: {0}", ex);
}

var files = items.Values;
Expand Down Expand Up @@ -204,7 +205,7 @@ private IEnumerable<Project> GetProjects()
}
catch
{
_tracer.TraceError("Error loading project #" + i);
Tracer.TraceError("Error loading project #" + i);
continue;
}

Expand Down Expand Up @@ -232,15 +233,15 @@ private void GetProjectFiles(string? projectName, ProjectItems? projectItems, ID
}
catch
{
_tracer.TraceError("Error loading project item #{0} in project {1}.", index, projectName ?? "unknown");
Tracer.TraceError("Error loading project item #{0} in project {1}.", index, projectName ?? "unknown");
}

index += 1;
}
}
catch
{
_tracer.TraceError("Error loading a project item in project {0}.", projectName ?? "unknown");
Tracer.TraceError("Error loading a project item in project {0}.", projectName ?? "unknown");
}
}

Expand Down Expand Up @@ -305,7 +306,7 @@ private void GetProjectFiles(string? projectName, ProjectItem projectItem, IDict
}
catch (ArgumentException)
{
_tracer.TraceWarning("Can't get filename for project item: {0} - {1}", name, projectItem.Kind);
Tracer.TraceWarning("Can't get filename for project item: {0} - {1}", name, projectItem.Kind);
}

return null;
Expand Down

0 comments on commit ae63156

Please sign in to comment.