Skip to content

Commit 3efff90

Browse files
committed
Adding NDF
1 parent a0800b8 commit 3efff90

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using CodeFactory.WinVs;
2+
using CodeFactory.WinVs.Commands;
3+
using CodeFactory.WinVs.Commands.SolutionExplorer;
4+
using CodeFactory.WinVs.Logging;
5+
using CodeFactory.WinVs.Models.CSharp;
6+
using CodeFactory.WinVs.Models.CSharp.Builder;
7+
using CodeFactory.WinVs.Models.ProjectSystem;
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
using System.Text;
12+
using System.Threading.Tasks;
13+
using CodeFactory.Automation.Standard.Logic;
14+
15+
namespace CodeFactory.Automation.Standard
16+
{
17+
/// <summary>
18+
/// Code factory command for automation of a C# document when selected from a project in solution explorer.
19+
/// </summary>
20+
public class AddMissingMembers : CSharpSourceCommandBase
21+
{
22+
private static readonly string commandTitle = "Add Missing Interface Members";
23+
private static readonly string commandDescription = "Adds interface members that are missing from the implementation of the class.";
24+
25+
/// <summary>
26+
/// Name of the Microsoft logging library.
27+
/// </summary>
28+
private const string MicrosoftLoggingNamespace = "Microsoft.Extensions.Logging";
29+
30+
#pragma warning disable CS1998
31+
32+
/// <inheritdoc />
33+
public AddMissingMembers(ILogger logger, IVsActions vsActions) : base(logger, vsActions, commandTitle, commandDescription)
34+
{
35+
//Intentionally blank
36+
}
37+
38+
#region External Configuration
39+
40+
/// <summary>
41+
/// The fully qualified name of the command to be used with configuration.
42+
/// </summary>
43+
public static string Type = typeof(AddMissingMembers).FullName;
44+
45+
/// <summary>
46+
/// Loads the external configuration definition for this command.
47+
/// </summary>
48+
/// <returns>Will return the command configuration or null if this command does not support external configurations.</returns>
49+
public override ConfigCommand LoadExternalConfigDefinition()
50+
{
51+
return null;
52+
}
53+
#endregion
54+
55+
#region Overrides of VsCommandBase<IVsCSharpDocument>
56+
57+
/// <summary>
58+
/// Validation logic that will determine if this command should be enabled for execution.
59+
/// </summary>
60+
/// <param name="result">The target model data that will be used to determine if this command should be enabled.</param>
61+
/// <returns>Boolean flag that will tell code factory to enable this command or disable it.</returns>
62+
public override async Task<bool> EnableCommandAsync(VsCSharpSource result)
63+
{
64+
//Result that determines if the command is enabled and visible in the context menu for execution.
65+
bool isEnabled = false;
66+
67+
try
68+
{
69+
//Getting the first class in the source code file.
70+
var sourceClass = result.SourceCode?.Classes?.FirstOrDefault();
71+
72+
//enable if only a class was found.
73+
isEnabled = sourceClass != null;
74+
75+
//If enabled if no interface members are missing then do not show.
76+
if (isEnabled) isEnabled = sourceClass.GetMissingInterfaceMembers().Any();
77+
78+
}
79+
catch (Exception unhandledError)
80+
{
81+
_logger.Error($"The following unhandled error occurred while checking if the solution explorer C# document command {commandTitle} is enabled. ",
82+
unhandledError);
83+
isEnabled = false;
84+
}
85+
86+
return isEnabled;
87+
}
88+
89+
/// <summary>
90+
/// Code factory framework calls this method when the command has been executed.
91+
/// </summary>
92+
/// <param name="result">The code factory model that has generated and provided to the command to process.</param>
93+
public override async Task ExecuteCommandAsync(VsCSharpSource result)
94+
{
95+
try
96+
{
97+
//Getting the hosting project
98+
var project = await result.GetHostingProjectAsync()
99+
?? throw new CodeFactoryException($"Cannot load the project information for C# source code file '{result.Name}' cannot add members.");
100+
101+
var references = await project.GetProjectReferencesAsync();
102+
103+
var hasLogging = references.Any(r => r.Name.StartsWith(MicrosoftLoggingNamespace));
104+
105+
var sourceClass = result?.SourceCode?.Classes?.FirstOrDefault()
106+
?? throw new CodeFactoryException(
107+
"The class could not be loaded cannot add members.");
108+
109+
var updatedClass = await VisualStudioActions.AddMissingMembersStandardAsync(result.SourceCode, sourceClass, hasLogging);
110+
111+
}
112+
catch (Exception unhandledError)
113+
{
114+
_logger.Error($"The following unhandled error occurred while executing the solution explorer C# document command {commandTitle}. ",
115+
unhandledError);
116+
117+
}
118+
119+
}
120+
121+
#endregion
122+
}
123+
}

0 commit comments

Comments
 (0)