Skip to content

Commit 019e2b9

Browse files
authored
Merge pull request #4 from CodeFactoryLLC/StandardAutomation
Standard automation - 2.23158.0.1-PreRelease
2 parents b8d066c + 4838177 commit 019e2b9

File tree

9 files changed

+523
-0
lines changed

9 files changed

+523
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using CodeFactory.WinVs;
7+
using CodeFactory.WinVs.Models.CSharp;
8+
using CodeFactory.WinVs.Models.CSharp.Builder;
9+
10+
namespace CodeFactory.Automation.Standard.Logic
11+
{
12+
/// <summary>
13+
/// Logic for adding missing members.
14+
/// </summary>
15+
public static class AddMissingMembers
16+
{
17+
/// <summary>
18+
/// Name of the Microsoft logging library.
19+
/// </summary>
20+
public const string MicrosoftLoggingNamespace = "Microsoft.Extensions.Logging";
21+
22+
/// <summary>
23+
/// Add missing interface members
24+
/// </summary>
25+
/// <param name="source">The CodeFactory automation for Visual Studio Windows</param>
26+
/// <param name="sourceCode">Source code model to be updated with add members in the target class.</param>
27+
/// <param name="updateClass">Class model to add missing members to.</param>
28+
/// <param name="supportsLogging">Flag that determines if logging is enabled.</param>
29+
/// <param name="loggerFieldName">Optional, the name of the field to use for logging.</param>
30+
/// <returns></returns>
31+
public static async Task<CsClass> AddMissingMembersStandardAsync(this IVsActions source, CsSource sourceCode,
32+
CsClass updateClass, bool supportsLogging, string loggerFieldName = "_logger")
33+
{
34+
//Bounds checks to make sure all data needed is provided.
35+
if (sourceCode == null)
36+
throw new CodeFactoryException(
37+
"Visual Studio automation for CodeFactory was not provided cannot add missing members.");
38+
39+
if (sourceCode == null)
40+
throw new CodeFactoryException("No source code was provided, cannot add the missing members.");
41+
42+
if (updateClass == null)
43+
throw new CodeFactoryException(
44+
"No target class to add missing members was provided, cannot add the missing members.");
45+
46+
//Get the missing members to be added
47+
var missingMembers = updateClass.GetMissingInterfaceMembers();
48+
49+
//If no missing members are found just return the current class.
50+
if (!missingMembers.Any()) return updateClass;
51+
52+
//Creating the source code manager for the class.
53+
var manager = new SourceClassManager(sourceCode, updateClass, source);
54+
manager.LoadNamespaceManager();
55+
56+
57+
//Creating the blocks to be used for code generation
58+
ILoggerBlock loggerBlock = supportsLogging ? new LoggerBlockMicrosoft(loggerFieldName) : null;
59+
60+
var boundsChecks = new IBoundsCheckBlock[]
61+
{
62+
new BoundsCheckBlockString(true, loggerBlock),
63+
new BoundsCheckBlockNull(true, loggerBlock)
64+
};
65+
66+
var catchBlocks = new ICatchBlock[]
67+
{
68+
new CatchBlockStandard(loggerBlock)
69+
};
70+
71+
ITryBlock tryBlock = new TryBlockStandard(loggerBlock, catchBlocks);
72+
73+
if(supportsLogging) await manager.UsingStatementAddAsync(MicrosoftLoggingNamespace);
74+
75+
//Creating the builders to generate code by member type.
76+
IMethodBuilder methodBuilder = new MethodBuilderStandard(loggerBlock, boundsChecks, tryBlock);
77+
IPropertyBuilder propertyBuilder = new PropertyBuilderStandard();
78+
IEventBuilder eventBuilder = new EventBuilderStandard();
79+
80+
//Process all missing properties.
81+
var missingProperties = missingMembers.Where(m => m.MemberType == CsMemberType.Property).Cast<CsProperty>()
82+
.ToList();
83+
84+
foreach (var missingProperty in missingProperties)
85+
{
86+
var propertySyntax = await propertyBuilder.BuildPropertyAsync(missingProperty, manager, 2);
87+
88+
if(propertySyntax == null) continue;
89+
90+
await manager.PropertiesAddAfterAsync(propertySyntax);
91+
92+
}
93+
94+
//Process all missing methods.
95+
var missingMethods = missingMembers.Where(m => m.MemberType == CsMemberType.Method).Cast<CsMethod>()
96+
.ToList();
97+
98+
foreach (var missingMethod in missingMethods)
99+
{
100+
var methodSyntax = await methodBuilder.BuildMethodAsync(missingMethod, manager, 2);
101+
102+
if(methodSyntax == null) continue;
103+
104+
await manager.MethodsAddAfterAsync(methodSyntax);
105+
}
106+
107+
//Process all missing events.
108+
var missingEvents = missingMembers.Where(m => m.MemberType == CsMemberType.Event).Cast<CsEvent>()
109+
.ToList();
110+
111+
foreach (var missingEvent in missingEvents)
112+
{
113+
var eventSyntax = await eventBuilder.BuildEventAsync(missingEvent, manager, 2);
114+
115+
if(eventSyntax == null) continue;
116+
117+
await manager.EventsAddAfterAsync(eventSyntax);
118+
}
119+
120+
return manager.Container;
121+
}
122+
}
123+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProductVersion>8.0.30703</ProductVersion>
8+
<SchemaVersion>2.0</SchemaVersion>
9+
<ProjectGuid>{4A7D7959-1E8F-41CF-A606-EF326737CD78}</ProjectGuid>
10+
<OutputType>Library</OutputType>
11+
<AppDesignerFolder>Properties</AppDesignerFolder>
12+
<RootNamespace>CodeFactory.Automation.Standard.Logic</RootNamespace>
13+
<AssemblyName>CodeFactory.Automation.Standard.Logic</AssemblyName>
14+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
15+
<FileAlignment>512</FileAlignment>
16+
<StartAction>Program</StartAction>
17+
<StartProgram Condition="'$(DevEnvDir)' != ''">$(DevEnvDir)devenv.exe</StartProgram>
18+
</PropertyGroup>
19+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
20+
<DebugSymbols>true</DebugSymbols>
21+
<DebugType>full</DebugType>
22+
<Optimize>false</Optimize>
23+
<OutputPath>bin\Debug\</OutputPath>
24+
<DefineConstants>DEBUG;TRACE</DefineConstants>
25+
<ErrorReport>prompt</ErrorReport>
26+
<WarningLevel>4</WarningLevel>
27+
<DocumentationFile>bin\Debug\CodeFactory.Automation.Standard.Logic.xml</DocumentationFile>
28+
</PropertyGroup>
29+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<PackageReference Include="CodeFactory.WinVs.SDK" Version="2.23158.1-PreRelease" />
39+
<Reference Include="System" />
40+
<Reference Include="System.Core" />
41+
<Reference Include="System.Xml.Linq" />
42+
<Reference Include="System.Data.DataSetExtensions" />
43+
<Reference Include="Microsoft.CSharp" />
44+
<Reference Include="System.Data" />
45+
<Reference Include="System.Xml" />
46+
<Reference Include="PresentationCore" />
47+
<Reference Include="PresentationFramework" />
48+
<Reference Include="System.Xaml" />
49+
<Reference Include="WindowsBase" />
50+
</ItemGroup>
51+
<ItemGroup>
52+
<Compile Include="AddMissingMembers.cs" />
53+
<Compile Include="Properties\AssemblyInfo.cs" />
54+
</ItemGroup>
55+
<ItemGroup>
56+
<None Include="CodeFactory.Automation.Standard.nuspec" />
57+
</ItemGroup>
58+
<ItemGroup />
59+
<ItemGroup>
60+
<None Include="Images\CFLogo128.png" />
61+
</ItemGroup>
62+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
63+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
64+
Other similar extension points exist, see Microsoft.Common.targets.
65+
<Target Name="BeforeBuild">
66+
</Target>
67+
<Target Name="AfterBuild">
68+
</Target> -->
69+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd" >
3+
<metadata>
4+
<id>CodeFactory.Automation.Standard.Logic</id>
5+
<version>2.23158.1-PreRelease</version>
6+
<title>Standard automation logic to be used with CodeFactory Commands.</title>
7+
<authors>CodeFactory, LLC.</authors>
8+
<owners>CodeFactory, LLC.</owners>
9+
<license type="expression">MIT</license>
10+
<requireLicenseAcceptance>true</requireLicenseAcceptance>
11+
<description>Standard automation logic to be used with CodeFactory commands built with Visual Studio - Windows.</description>
12+
<releaseNotes>
13+
Notes
14+
Initial Release
15+
16+
Automation Support:
17+
- Add Missing interface members.
18+
</releaseNotes>
19+
<copyright>Copyright © 2023 CodeFactory, LLC.</copyright>
20+
<tags>Factory Automation</tags>
21+
<dependencies>
22+
<dependency id="CodeFactory.WinVs.SDK" version="2.23158.1-PreRelease"/>
23+
</dependencies>
24+
<icon>CFLogo128.png</icon>
25+
</metadata>
26+
<files>
27+
<file src="Images\CFLogo128.png" target=""/>
28+
<file src="bin\Debug\CodeFactory.Automation.Standard.Logic.dll" target="lib/net48/"/>
29+
<file src="bin\Debug\CodeFactory.Automation.Standard.Logic.xml" target="lib/net48/"/>
30+
<file src="bin\Debug\CodeFactory.Automation.Standard.Logic.pdb" target="lib/net48/"/>
31+
</files>
32+
</package>
Loading
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using CodeFactory.WinVs;
2+
using System.Reflection;
3+
using System.Runtime.CompilerServices;
4+
using System.Runtime.InteropServices;
5+
6+
// General Information about an assembly is controlled through the following
7+
// set of attributes. Change these attribute values to modify the information
8+
// associated with an assembly.
9+
[assembly: AssemblyTitle("CodeFactory.Automation.Standard.Logic")]
10+
[assembly: AssemblyDescription("")]
11+
[assembly: AssemblyConfiguration("")]
12+
[assembly: AssemblyCompany("")]
13+
[assembly: AssemblyProduct("CodeFactory.Automation.Standard.Logic")]
14+
[assembly: AssemblyCopyright("Copyright © 2023")]
15+
[assembly: AssemblyTrademark("")]
16+
[assembly: AssemblyCulture("")]
17+
18+
// Setting ComVisible to false makes the types in this assembly not visible
19+
// to COM components. If you need to access a type in this assembly from
20+
// COM, set the ComVisible attribute to true on that type.
21+
[assembly: ComVisible(false)]
22+
23+
// The following GUID is for the ID of the typelib if this project is exposed to COM
24+
[assembly: Guid("4a7d7959-1e8f-41cf-a606-ef326737cd78")]
25+
26+
// Version information for an assembly consists of the following four values:
27+
//
28+
// Major Version
29+
// Minor Version
30+
// Build Number
31+
// Revision
32+
//
33+
// You can specify all the values or you can default the Build and Revision Numbers
34+
// by using the '*' as shown below:
35+
// [assembly: AssemblyVersion("1.0.*")]
36+
[assembly: AssemblyVersion("1.0.0.0")]
37+
[assembly: AssemblyFileVersion("2.23158.0.1")]
38+
[assembly: AssemblyCFEnvironment("CFVSW")]
39+
[assembly: AssemblyCFSdkVersion("2.23158.0.1")]
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)