Skip to content

#378 Integrate EarlyBound Generator API #389

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

Open
wants to merge 6 commits into
base: spkl_dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
<HintPath>..\packages\Microsoft.CrmSdk.CoreAssemblies.9.0.2.21\lib\net462\Microsoft.Crm.Sdk.Proxy.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.5.2.4\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=5.2.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.5.2.8\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CrmSdk.XrmTooling.CoreAssembly.9.1.0.25\lib\net462\Microsoft.Rest.ClientRuntime.dll</HintPath>
Expand Down
2 changes: 1 addition & 1 deletion spkl/CrmSvcUtilFilteringService/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.IdentityModel.Clients.ActiveDirectory" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
<bindingRedirect oldVersion="0.0.0.0-5.2.8.0" newVersion="5.2.8.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Expand Down
2 changes: 1 addition & 1 deletion spkl/CrmSvcUtilFilteringService/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<package id="Microsoft.CrmSdk.Deployment" version="9.0.2.21" targetFramework="net462" />
<package id="Microsoft.CrmSdk.Workflow" version="9.0.2.21" targetFramework="net462" />
<package id="Microsoft.CrmSdk.XrmTooling.CoreAssembly" version="9.1.0.25" targetFramework="net462" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="5.2.4" targetFramework="net462" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="5.2.8" targetFramework="net462" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net462" />
<package id="System.Net.Http" version="4.3.4" targetFramework="net462" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.1" targetFramework="net462" />
Expand Down
10 changes: 3 additions & 7 deletions spkl/SparkleXrm.Tasks.Tests/EarlyBoundTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SparkleXrm.Tasks.Tests
{
Expand Down Expand Up @@ -222,7 +219,7 @@ public void TestNotGeneratingAnyOptionsets()
Directory.Delete(tempFolder, true);
}
}

[TestMethod]
[TestCategory("Integration Tests")]
public void TestNotGeneratingAnyOptionsets_OneTypePerFile()
Expand Down Expand Up @@ -263,7 +260,7 @@ public void TestNotGeneratingAnyOptionsets_OneTypePerFile()
Directory.Delete(tempFolder, true);
}
}

private static void Generate(string tempFolder, ConfigFile config)
{
var connectionString = ConfigurationManager.ConnectionStrings["integration_testing"].ConnectionString;
Expand All @@ -278,13 +275,12 @@ private static void Generate(string tempFolder, ConfigFile config)

// Act
var task = new EarlyBoundClassGeneratorTask(ctx, trace);
task.ConectionString = connectionString;
task.ConnectionString = connectionString;

task.CreateEarlyBoundTypes(ctx, config);
}
}


private static void EnsureClassIsCreatedCorrectly(string classPath, string className)
{
var code = File.ReadAllText(classPath);
Expand Down
152 changes: 152 additions & 0 deletions spkl/SparkleXrm.Tasks.Tests/EarlyBoundTypes_DlaB.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Tooling.Connector;
using SparkleXrm.Tasks.Config;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SparkleXrm.Tasks.Tests
{
[TestClass]
public class EarlyBoundTypesDlaB
{
[TestMethod]
[TestCategory("Integration Tests")]
public void TestEbgGenerateGlobalOptionsets()
{
// Arrange
Guid id = Guid.NewGuid();
var tempFolder = Path.Combine(Path.GetTempPath(), id.ToString());
Directory.CreateDirectory(tempFolder);
try
{
var config = new ConfigFile
{
earlyboundtypes = new List<EarlyBoundTypeConfig>{
new EarlyBoundTypeConfig{
useEarlyBoundGeneratorApi = true,
generateOptionsetEnums = true,
entities ="socialprofile,socialactivity"
}
},
filePath = tempFolder

};
Generate(tempFolder, config);

// Check that there was only a single instance of the global optionsset 'socialprofile_community'
// public enum socialprofile_community

var matches = CountMatches("public enum SocialProfile_Community", tempFolder);
Assert.AreEqual(1, matches, "Global optionset created once only");
}
finally
{
Directory.Delete(tempFolder, true);
}
}

[TestMethod]
[TestCategory("Integration Tests")]
public void TestEbgGenerateGlobalOptionsets_OneTypePerFile()
{
// Arrange
Guid id = Guid.NewGuid();
var tempFolder = Path.Combine(Path.GetTempPath(), id.ToString());
Directory.CreateDirectory(tempFolder);
try
{
var config = new ConfigFile
{
earlyboundtypes = new List<EarlyBoundTypeConfig>{
new EarlyBoundTypeConfig{
generateOptionsetEnums = true,
useEarlyBoundGeneratorApi = true,
entities ="socialprofile,socialactivity",
oneTypePerFile = true
}
},
filePath = tempFolder

};
Generate(tempFolder, config);


Assert.IsFalse(File.Exists(Path.Combine(tempFolder, "entities.cs")));

EnsureClassIsCreatedCorrectly(Path.Combine($"{tempFolder}\\Entities", "SocialProfile.cs"), "SocialProfile");
EnsureClassIsCreatedCorrectly(Path.Combine($"{tempFolder}\\Entities", "SocialActivity.cs"), "SocialActivity");

EnsureOptionSetsIsCreatedCorrectly(Path.Combine($"{tempFolder}\\OptionSets", "socialprofile_community.cs"), "SocialProfile_Community");
EnsureOptionSetsIsCreatedCorrectly(Path.Combine($"{tempFolder}\\OptionSets", "socialactivity_prioritycode.cs"), "SocialActivity_PriorityCode");
}
finally
{
Directory.Delete(tempFolder, true);
}
}
private static void Generate(string tempFolder, ConfigFile config)
{
var connectionString = ConfigurationManager.ConnectionStrings["integration_testing"].ConnectionString;
CrmServiceClient crmSvc = new CrmServiceClient(connectionString);
var userId = crmSvc.GetMyCrmUserId();
var trace = new TraceLogger();


using (var ctx = new OrganizationServiceContext(crmSvc))
{
ctx.MergeOption = MergeOption.NoTracking;

// Act
var task = new EarlyBoundClassGeneratorTask(ctx, trace);
task.ConnectionString = connectionString;

task.CreateEarlyBoundTypes(ctx, config);
}
}

private static void EnsureClassIsCreatedCorrectly(string classPath, string className)
{
var code = File.ReadAllText(classPath);
var matches = Regex.Matches(code, $"public partial class {className}");
Assert.AreEqual(1, matches.Count, $"Class {className} created once only");
}

private static void EnsureOptionSetsIsCreatedCorrectly(string optionSetsPath, string optionSetsName)
{
var code = File.ReadAllText(optionSetsPath);
var matches = Regex.Matches(code, $"public enum {optionSetsName}");
Assert.AreEqual(1, matches.Count, $"Optionset {optionSetsName} created once only");
}

private static int CountMatches(string matchString, string tempFolder)
{
var path = Path.Combine(tempFolder, "entities.cs");
string code = File.ReadAllText(path);
var matches = Regex.Matches(code, matchString);
var count = matches.Count;
path = Path.Combine(tempFolder, "optionsets.cs");
if (File.Exists(path))
{
code = File.ReadAllText(path);
matches = Regex.Matches(code, matchString);
count += matches.Count;
}

path = Path.Combine(tempFolder, "actions.cs");
if (File.Exists(path))
{
code = File.ReadAllText(path);
matches = Regex.Matches(code, matchString);
count += matches.Count;
}
return count;
}
}
}
27 changes: 19 additions & 8 deletions spkl/SparkleXrm.Tasks.Tests/SparkleXrm.Tasks.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,42 @@
<AssemblyOriginatorKeyFile>Plugins.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="DLaB.EarlyBoundGenerator.Api, Version=1.2020.5.5, Culture=neutral, PublicKeyToken=915c5118a4c943b9, processorArchitecture=MSIL">
<HintPath>..\packages\DLaB.Xrm.EarlyBoundGenerator.Api.1.2020.5.5\lib\net462\DLaB.EarlyBoundGenerator.Api.dll</HintPath>
</Reference>
<Reference Include="FakeItEasy, Version=5.0.0.0, Culture=neutral, PublicKeyToken=eff28e2146d5fd2c, processorArchitecture=MSIL">
<HintPath>..\packages\FakeItEasy.5.5.0\lib\net45\FakeItEasy.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Crm.Sdk.Proxy, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CrmSdk.CoreAssemblies.9.0.2.23\lib\net462\Microsoft.Crm.Sdk.Proxy.dll</HintPath>
<HintPath>..\packages\Microsoft.CrmSdk.CoreAssemblies.9.0.2.26\lib\net462\Microsoft.Crm.Sdk.Proxy.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.5.2.4\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=5.2.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.5.2.8\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CrmSdk.XrmTooling.CoreAssembly.9.1.0.38\lib\net462\Microsoft.Rest.ClientRuntime.dll</HintPath>
<HintPath>..\packages\Microsoft.CrmSdk.XrmTooling.CoreAssembly.9.1.0.44\lib\net462\Microsoft.Rest.ClientRuntime.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualBasic" />
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.Xrm.Sdk, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CrmSdk.CoreAssemblies.9.0.2.23\lib\net462\Microsoft.Xrm.Sdk.dll</HintPath>
<HintPath>..\packages\Microsoft.CrmSdk.CoreAssemblies.9.0.2.26\lib\net462\Microsoft.Xrm.Sdk.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Xrm.Sdk.Deployment, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CrmSdk.Deployment.9.0.2.23\lib\net462\Microsoft.Xrm.Sdk.Deployment.dll</HintPath>
<HintPath>..\packages\Microsoft.CrmSdk.Deployment.9.0.2.26\lib\net462\Microsoft.Xrm.Sdk.Deployment.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Xrm.Sdk.Workflow, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CrmSdk.Workflow.9.0.2.23\lib\net462\Microsoft.Xrm.Sdk.Workflow.dll</HintPath>
<HintPath>..\packages\Microsoft.CrmSdk.Workflow.9.0.2.26\lib\net462\Microsoft.Xrm.Sdk.Workflow.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Xrm.Tooling.Connector, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CrmSdk.XrmTooling.CoreAssembly.9.1.0.38\lib\net462\Microsoft.Xrm.Tooling.Connector.dll</HintPath>
<HintPath>..\packages\Microsoft.CrmSdk.XrmTooling.CoreAssembly.9.1.0.44\lib\net462\Microsoft.Xrm.Tooling.Connector.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
Expand Down Expand Up @@ -166,6 +175,7 @@
<Compile Include="DeployPluginTests.cs" />
<Compile Include="DownloadWebresourceFiles.cs" />
<Compile Include="EarlyBoundTypes.cs" />
<Compile Include="EarlyBoundTypes_DlaB.cs" />
<Compile Include="GetWebresources.cs" />
<Compile Include="ImportCodeTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down Expand Up @@ -209,6 +219,7 @@
<ItemGroup>
<None Include="Resources\CustomBaseClassPlugin.txt" />
</ItemGroup>
<ItemGroup />
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions spkl/SparkleXrm.Tasks.Tests/app.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="integration_testing" connectionString="Url=http://dev04/Contoso" />
<add name="integration_testing" connectionString="" />
</connectionStrings>

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

Expand Down Expand Up @@ -73,7 +73,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.IdentityModel.Clients.ActiveDirectory" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
<bindingRedirect oldVersion="0.0.0.0-5.2.8.0" newVersion="5.2.8.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
Expand Down
11 changes: 6 additions & 5 deletions spkl/SparkleXrm.Tasks.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DLaB.Xrm.EarlyBoundGenerator.Api" version="1.2020.5.5" targetFramework="net462" />
<package id="FakeItEasy" version="5.5.0" targetFramework="net462" />
<package id="Microsoft.CrmSdk.CoreAssemblies" version="9.0.2.23" targetFramework="net462" />
<package id="Microsoft.CrmSdk.Deployment" version="9.0.2.23" targetFramework="net462" />
<package id="Microsoft.CrmSdk.Workflow" version="9.0.2.23" targetFramework="net462" />
<package id="Microsoft.CrmSdk.XrmTooling.CoreAssembly" version="9.1.0.38" targetFramework="net462" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="5.2.4" targetFramework="net462" />
<package id="Microsoft.CrmSdk.CoreAssemblies" version="9.0.2.26" targetFramework="net462" />
<package id="Microsoft.CrmSdk.Deployment" version="9.0.2.26" targetFramework="net462" />
<package id="Microsoft.CrmSdk.Workflow" version="9.0.2.26" targetFramework="net462" />
<package id="Microsoft.CrmSdk.XrmTooling.CoreAssembly" version="9.1.0.44" targetFramework="net462" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="5.2.8" targetFramework="net462" />
<package id="Microsoft.Rest.ClientRuntime" version="2.3.20" targetFramework="net462" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net462" />
<package id="System.Buffers" version="4.5.0" targetFramework="net462" />
Expand Down
28 changes: 18 additions & 10 deletions spkl/SparkleXrm.Tasks/Config/ConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,36 @@ public virtual SolutionPackageConfig[] GetSolutionConfig(string profile)
public virtual EarlyBoundTypeConfig[] GetEarlyBoundConfig(string profile)
{
if (earlyboundtypes == null)
return new EarlyBoundTypeConfig[] { new EarlyBoundTypeConfig(){
return new [] { new EarlyBoundTypeConfig{
filename ="Entities.cs",
entities = "account,contact",
classNamespace = "Xrm",
generateOptionsetEnums = true,
generateStateEnums = true
} };

foreach (var type in earlyboundtypes)
{
if (string.IsNullOrEmpty(type.entities) && type.entityCollection?.Length > 0)
{
type.entities = string.Join(",", type.entityCollection);
}

if (string.IsNullOrEmpty(type.actions) && type.actionCollection?.Length > 0)
{
type.actions = string.Join(",", type.actionCollection);
}
}

EarlyBoundTypeConfig[] config = null;
if (profile == "default")
{
profile = null;
}
if (profile != null)
{
config = earlyboundtypes.Where(c => c.profile != null && c.profile.Replace(" ", "").Split(',').Contains(profile)).ToArray();
}
else
{
// Default profile or empty
config = earlyboundtypes.Where(c => c.profile == null || c.profile.Replace(" ", "").Split(',').Contains("default") || String.IsNullOrWhiteSpace(c.profile)).ToArray();
}

config = profile == null
? earlyboundtypes.Where(c => c.profile == null || c.profile.Replace(" ", "").Split(',').Contains("default") || string.IsNullOrWhiteSpace(c.profile)).ToArray()
: earlyboundtypes.Where(c => c.profile != null && c.profile.Replace(" ", "").Split(',').Contains(profile)).ToArray();

return config;
}
Expand Down
Loading