Permalink
Browse files

Adding TransformText parameter to TransformXml. Closed #1

  • Loading branch information...
1 parent 96df6b0 commit b6d2fba258a1fbe06f69a49130ad76cfd6d1071c @sayedihashimi committed Jul 3, 2014
View
@@ -2,7 +2,7 @@
<package >
<metadata>
<id>SlowCheetah.Xdt</id>
- <version>1.1.1</version>
+ <version>1.1.2</version>
<authors>Sayed Ibrahim Hashimi</authors>
<owners>Sayed Ibrahim Hashimi</owners>
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0</licenseUrl>
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+
+ <IISExpressAnonymousAuthentication />
+ <IISExpressWindowsAuthentication />
+ <IISExpressUseClassicPipelineMode />
+ </PropertyGroup>
+
+ <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
+ <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
+ <ProjectExtensions>
+ <VisualStudio>
+ <FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
+ <WebProjectProperties>
+ <UseIIS>True</UseIIS>
+ <AutoAssignPort>True</AutoAssignPort>
+ <DevelopmentServerPort>2909</DevelopmentServerPort>
+ <DevelopmentServerVPath>/</DevelopmentServerVPath>
+ <IISUrl>http://localhost:2909/</IISUrl>
+ <NTLMAuthentication>False</NTLMAuthentication>
+ <UseCustomServer>False</UseCustomServer>
+ <CustomServerUrl>
+ </CustomServerUrl>
+ <SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
+ </WebProjectProperties>
+ </FlavorProperties>
+ </VisualStudio>
+ </ProjectExtensions>
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
+
+ <appSettings>
+ <add key="inserted" value="from-transform" xdt:Transform="Insert"/>
+ </appSettings>
+
+</configuration>
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Demo">
+
+ <!--
+ This file should be invoked from the output folder not the source folder.
+ -->
+
+ <PropertyGroup>
+
+ <XdtTaskPath Condition=" '$(XdtTaskPath)'=='' ">$(MSBuildThisFileDirectory)..\SlowCheetah.Xdt.dll</XdtTaskPath>
+
+ <SourceFile Condition=" '$(SourceFile)'=='' ">$(MSBuildThisFileDirectory)sample.config</SourceFile>
+ <TransformFile Condition=" '$(TransformFile)'=='' ">$(MSBuildThisFileDirectory)sample.Insert.config</TransformFile>
+ <DestFile Condition=" '$(DestFile)'=='' ">$(MSBuildThisFileDirectory)transform-from-file-result.xml</DestFile>
+ </PropertyGroup>
+
+ <UsingTask TaskName="TransformXml" AssemblyFile="$([System.IO.Path]::GetFullPath($(XdtTaskPath)))"/>
+
+ <Target Name="Demo">
+
+ <Message Text="Transform parameters
+Source=[$(SourceFile)]
+TransformFile=[$(TransformFile)]
+Destination=[$(DestFile)]
+" Importance="high" />
+ <TransformXml Source="$(SourceFile)"
+ Transform="$(TransformFile)"
+ Destination="$(DestFile)"/>
+
+ <Message Text="Transform completed, open the result from: [$(DestFile)]" Importance="high" />
+
+ <!-- Read the result and print it out -->
+ <!--<Exec Command="start $()" />-->
+ </Target>
+
+</Project>
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Demo">
+
+ <!--
+ This file should be invoked from the output folder not the source folder.
+ -->
+
+ <PropertyGroup>
+
+ <XdtTaskPath Condition=" '$(XdtTaskPath)'=='' ">$(MSBuildThisFileDirectory)..\SlowCheetah.Xdt.dll</XdtTaskPath>
+
+ <SourceFile Condition=" '$(SourceFile)'=='' ">$(MSBuildThisFileDirectory)WebApplication1.proj</SourceFile>
+ <SampleTransform Condition=" '$(SampleTransform)'=='' ">
+ <![CDATA[<?xml version="1.0" encoding="utf-8" ?>
+<Project xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
+ xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+
+ <ProjectExtensions xdt:Transform="Remove" />
+
+</Project>
+ ]]></SampleTransform>
+ <DestFile Condition=" '$(DestFile)'=='' ">$(MSBuildThisFileDirectory)transform-from-string-result.xml</DestFile>
+ </PropertyGroup>
+
+ <UsingTask TaskName="TransformXml" AssemblyFile="$([System.IO.Path]::GetFullPath($(XdtTaskPath)))"/>
+
+ <Target Name="Demo">
+
+ <Message Text="Transform parameters
+Source=[$(SourceFile)]
+TransformText=[$(SampleTransform)]
+Destination=[$(DestFile)]
+" Importance="high" />
+ <TransformXml Source="$(SourceFile)"
+ TransformText="$(SampleTransform)"
+ Destination="$(DestFile)"/>
+
+ <Message Text="Transform completed, open the result from: [$(DestFile)]" Importance="high" />
+
+ <!-- Read the result and print it out -->
+ <!--<Exec Command="start $()" />-->
+ </Target>
+
+</Project>
@@ -67,14 +67,28 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
+ <None Include="Samples\sample.Insert.config">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ <None Include="Samples\transform-from-file.proj">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ <None Include="Samples\transform-from-string.proj">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
<None Include="Samples\transform.AttributeRegexReplace.config">
<DependentUpon>sample.config</DependentUpon>
<IsTransformFile>True</IsTransformFile>
<SubType>Designer</SubType>
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Samples\sample.config">
<SubType>Designer</SubType>
<TransformOnBuild>true</TransformOnBuild>
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ <None Include="Samples\WebApplication1.proj">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
@@ -19,13 +19,16 @@ public class TransformXml : Task {
set;
}
-
- [Required]
public ITaskItem Transform {
get;
set;
}
+ /// <summary>
+ /// Either this is passed in or Transform is passed in. Not both.
+ /// </summary>
+ public string TransformText { get; set; }
+
public string TransformRootPath {
get {
if (string.IsNullOrEmpty(_transformRootPath)) {
@@ -38,6 +41,12 @@ public class TransformXml : Task {
set { _transformRootPath = value; }
}
+ /// <summary>
+ /// If this is set to true then the value for
+ /// Transform will be passed to document.LoadXml() instead of
+ /// document.Load()
+ /// </summary>
+ // public bool TransformProvidedAsText { get; set; }
[Required]
public ITaskItem Destination {
@@ -59,12 +68,27 @@ public class TransformXml : Task {
XmlTransformation transformation = null;
XmlTransformableDocument document = null;
+ // validate that both Transform and TransformString are not passed in
+ if (Transform != null && !string.IsNullOrEmpty(TransformText)) {
+ Log.LogError("Either Transform or TransformString should be passed in, not both.");
+ return false;
+ }
+
+ bool loadTransformFromFile = string.IsNullOrEmpty(TransformText);
+
try {
Log.LogMessage(MessageImportance.Low, "Transfroming source file: {0}", Source);
+
document = OpenSourceFile(Source.GetMetadata("FullPath"));
- Log.LogMessage(MessageImportance.Low, "Applying Transform File: {0}", Transform);
- transformation = OpenTransformFile(Transform.GetMetadata("FullPath"), null);
+ if (loadTransformFromFile) {
+ Log.LogMessage(MessageImportance.Low, "Applying Transform File: {0}", Transform);
+ transformation = OpenTransformFile(Transform.GetMetadata("FullPath"), null);
+ }
+ else {
+ Log.LogMessage(MessageImportance.Low, "Applying Transform from TransformText");
+ transformation = OpenTransformText(TransformText, null);
+ }
succeeded = transformation.Apply(document);
@@ -110,13 +134,13 @@ public class TransformXml : Task {
throw new Exception(string.Format("Could not write Destination file: {0}", ex.Message), ex);
}
}
-
- private XmlTransformableDocument OpenSourceFile(string sourceFile) {
+
+ private XmlTransformableDocument OpenSourceFile(string transform) {
try {
XmlTransformableDocument document = new XmlTransformableDocument();
document.PreserveWhitespace = true;
- document.Load(sourceFile);
+ document.Load(transform);
return document;
}
@@ -145,5 +169,20 @@ public class TransformXml : Task {
ex);
}
}
+
+ private XmlTransformation OpenTransformText(string transforText, IXmlTransformationLogger logger) {
+ try {
+ return new XmlTransformation(TransformText, false, logger);
+ }
+ catch (System.Xml.XmlException) {
+ throw;
+ }
+ catch (Exception ex) {
+ throw new Exception(
+ string.Format(System.Globalization.CultureInfo.CurrentCulture,
+ "Could not open Transform file: {0}", ex.Message),
+ ex);
+ }
+ }
}
}

0 comments on commit b6d2fba

Please sign in to comment.