Skip to content
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
49 changes: 49 additions & 0 deletions spkl/SparkleXrm.Tasks.Tests/CodeParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SparkleXrm.Tasks;
using System.Text;

namespace SparkleXrm.Tasks.Tests
{
[TestClass]
public class CodeParserTests
{
private CrmPluginRegistrationAttribute _crmPluginRegistrationAttr = new CrmPluginRegistrationAttribute(
"Create",
"opportunity",
StageEnum.PostOperation,
ExecutionModeEnum.Synchronous,
"",
"PostOpportunityCreate",
1,
IsolationModeEnum.Sandbox);

[TestMethod]
[TestCategory("Unit Tests")]
public void AddAttributeTest_SingleLine()
{
var codeParser = new CodeParser(string.Format(TestCode.SingleLinePluginDefinitionTemplate, ""));
codeParser.AddAttribute(_crmPluginRegistrationAttr, "MyCompany.Plugins.OpportunityPlugin");

var expectedPluginDeifintionSB = string.Format(
TestCode.SingleLinePluginDefinitionTemplate,
_crmPluginRegistrationAttr.GetAttributeCode("\r\n") + "\r\n");

Assert.AreEqual(codeParser.Code, expectedPluginDeifintionSB.ToString());
}

[TestMethod]
[TestCategory("Unit Tests")]
public void AddAttributeTest_MultiLine()
{
var codeParser = new CodeParser(string.Format(TestCode.MultiLinePluginDefinitionTemplate, ""));
codeParser.AddAttribute(_crmPluginRegistrationAttr, "MyCompany.Plugins.OpportunityPlugin");

var expectedPluginDeifintionSB = string.Format(
TestCode.MultiLinePluginDefinitionTemplate,
_crmPluginRegistrationAttr.GetAttributeCode("\r\n "));

Assert.AreEqual(codeParser.Code, expectedPluginDeifintionSB.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace MyCompany.Plugins {{{0}
public class OpportunityPlugin : Plugin {{
}}
}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
namespace MyCompany.Plugins{{{0}public class OpportunityPlugin : Plugin {{ }} }}
7 changes: 7 additions & 0 deletions spkl/SparkleXrm.Tasks.Tests/SparkleXrm.Tasks.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="CodeParserTests.cs" />
<Compile Include="DeployPluginTests.cs" />
<Compile Include="DownloadWebresourceFiles.cs" />
<Compile Include="EarlyBoundTypes.cs" />
Expand Down Expand Up @@ -159,6 +160,12 @@
<ItemGroup>
<None Include="Resources\CustomBaseClassPlugin.txt" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\MultiLinePluginDefinitionTemplate.txt" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\SingleLinePluginDefinitionTemplate.txt" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
Expand Down
23 changes: 22 additions & 1 deletion spkl/SparkleXrm.Tasks.Tests/TestCode.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions spkl/SparkleXrm.Tasks.Tests/TestCode.resx
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,10 @@
<data name="DecoratedPlugin" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\DecoratedPlugin.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="MultiLinePluginDefinitionTemplate" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\MultiLinePluginDefinitionTemplate.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="SingleLinePluginDefinitionTemplate" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\SingleLinePluginDefinitionTemplate.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
</root>
34 changes: 25 additions & 9 deletions spkl/SparkleXrm.Tasks/CodeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public int RemoveExistingAttributes()
return count;
}



public void AddAttribute(CrmPluginRegistrationAttribute attribute, string className)
{
Expand All @@ -169,20 +169,36 @@ public void AddAttribute(CrmPluginRegistrationAttribute attribute, string classN
if (classLocation == null)
throw new Exception(String.Format("Cannot find class {0}", className));

var pos = _code.IndexOf(classLocation.Value);
// start index of "public class OpportunityPluign"
var classStartIndex = _code.IndexOf(classLocation.Value);

// start index of "{ public class OpportunityPluign"
var openBraceBeforeClassIndex = _code.LastIndexOf("{", classStartIndex - 1);

// Find previouse line break
var lineBreak = _code.LastIndexOf("\r\n", pos - 1);
// start index of "<EOL> { public class OpportunityPluign"
var eolBeforeClassIndex = _code.LastIndexOf("\r\n", classStartIndex - 1, classStartIndex - 1 - openBraceBeforeClassIndex);

// Indentation
var indentation = _code.Substring(lineBreak, pos - lineBreak);
// discover indentation between class and EOL (inclusive of EOL character)
var indentation = "\r\n";
if (eolBeforeClassIndex != -1)
{
indentation = _code.Substring(eolBeforeClassIndex, classStartIndex - eolBeforeClassIndex);
}

// Add the attribute
// generate the attribut code
var attributeCode = attribute.GetAttributeCode(indentation);

// Insert
_code = _code.Insert(lineBreak, attributeCode);
if (eolBeforeClassIndex == -1)
{
// insert attribute code at class start
eolBeforeClassIndex = classStartIndex;

// add EOL between attribute code and class
attributeCode += "\r\n";
}

// insert attribute code
_code = _code.Insert(eolBeforeClassIndex, attributeCode);
}
#endregion
}
Expand Down