diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2009-10-10 10:31:26 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2009-10-10 10:31:26 -0700 |
commit | 525c175898c8b98381b217f8d6ef49594da41dcc (patch) | |
tree | 49d5fac30b2257d012433caa07c7dd8c3e80bba0 | |
parent | 075bee88cbb51fc95fdfd27c7a1f8133d2d91890 (diff) | |
download | DotNetOpenAuth-525c175898c8b98381b217f8d6ef49594da41dcc.zip DotNetOpenAuth-525c175898c8b98381b217f8d6ef49594da41dcc.tar.gz DotNetOpenAuth-525c175898c8b98381b217f8d6ef49594da41dcc.tar.bz2 |
Added attempt at a copy and transform task to do variable substitution.
-rw-r--r-- | build.proj | 14 | ||||
-rw-r--r-- | lib/DotNetOpenAuth.BuildTasks.targets | 1 | ||||
-rw-r--r-- | src/DotNetOpenAuth.BuildTasks/CopyWithTokenSubstitution.cs | 91 | ||||
-rw-r--r-- | src/DotNetOpenAuth.BuildTasks/DotNetOpenAuth.BuildTasks.csproj | 1 |
4 files changed, 107 insertions, 0 deletions
@@ -155,7 +155,20 @@ $(ProjectRoot)\projecttemplates\**\*.ldf; $(ProjectRoot)\projecttemplates\**\*.mdf; "/> + <ProjectTemplatesTransformSource Include="@(ProjectTemplatesSource)" Condition=" + '%(Extension)' == 'cs' or + or '%(Extension)' == 'csproj' + or '%(Extension)' == 'config' + or '%(Extension)' == 'Master' + or '%(Extension)' == 'aspx' + "> + <BeforeTokens>%(RecursiveDir)</BeforeTokens> + <AfterTokens>$safeprojectname$</AfterTokens> + </ProjectTemplatesTransformSource> + <ProjectTemplatesSource Remove="@(ProjectTemplatesTransformSource)" /> + <ProjectTemplatesLayout Include="@(ProjectTemplatesSource->'$(ProjectTemplatesLayoutPath)%(RecursiveDir)%(FileName)%(Extension)')"/> + <ProjectTemplatesTransformLayout Include="@(ProjectTemplatesTransformSource->'$(ProjectTemplatesLayoutPath)%(RecursiveDir)%(FileName)%(Extension)')"/> <!-- Include the template icon --> <ProjectTemplatesSource Include="@(ProjectTemplates->'$(ProjectRoot)\doc\logo\dotnetopenid.ico')" /> @@ -172,6 +185,7 @@ <MSBuild Projects="@(ProjectTemplates)" /> <RemoveDir Directories="$(ProjectTemplatesLayoutPath)" Condition="'$(NoClean)' != 'true'" /> <Copy SourceFiles="@(ProjectTemplatesSource)" DestinationFiles="@(ProjectTemplatesLayout)" SkipUnchangedFiles="true" /> + <CopyWithTokenSubstitution SourceFiles="@(ProjectTemplatesTransformSource)" DestinationFiles="@(ProjectTemplatesTransformLayout)" SkipUnchangedFiles="true" /> </Target> <Target Name="BuildProjectTemplates" DependsOnTargets="BuildProjectTemplatesLayout"> diff --git a/lib/DotNetOpenAuth.BuildTasks.targets b/lib/DotNetOpenAuth.BuildTasks.targets index 3a7f935..cfc9e18 100644 --- a/lib/DotNetOpenAuth.BuildTasks.targets +++ b/lib/DotNetOpenAuth.BuildTasks.targets @@ -16,5 +16,6 @@ <UsingTask AssemblyFile="$(ProjectRoot)\lib\DotNetOpenAuth.BuildTasks.dll" TaskName="Trim" /> <UsingTask AssemblyFile="$(ProjectRoot)\lib\DotNetOpenAuth.BuildTasks.dll" TaskName="FilterItems" /> <UsingTask AssemblyFile="$(ProjectRoot)\lib\DotNetOpenAuth.BuildTasks.dll" TaskName="JsPack" /> + <UsingTask AssemblyFile="$(ProjectRoot)\lib\DotNetOpenAuth.BuildTasks.dll" TaskName="CopyWithTokenSubstitution" /> </Project> diff --git a/src/DotNetOpenAuth.BuildTasks/CopyWithTokenSubstitution.cs b/src/DotNetOpenAuth.BuildTasks/CopyWithTokenSubstitution.cs new file mode 100644 index 0000000..0d50254 --- /dev/null +++ b/src/DotNetOpenAuth.BuildTasks/CopyWithTokenSubstitution.cs @@ -0,0 +1,91 @@ +//----------------------------------------------------------------------- +// <copyright file="CopyWithTokenSubstitution.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.BuildTasks { + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using Microsoft.Build.Framework; + using Microsoft.Build.Utilities; + + /// <summary> + /// Copies files and performs a search and replace for given tokens in their contents during the process. + /// </summary> + public class CopyWithTokenSubstitution : Task { + /// <summary> + /// Gets or sets a value indicating whether the task should + /// skip the copying of files that are unchanged between the source and destination. + /// </summary> + /// <value> + /// <c>true</c> to skip copying files where the destination files are newer than the source files; otherwise, <c>false</c> to copy all files. + /// </value> + public bool SkipUnchangedFiles { get; set; } + + /// <summary> + /// Gets or sets the files to copy. + /// </summary> + /// <value>The files to copy.</value> + [Required] + public ITaskItem[] SourceFiles { get; set; } + + /// <summary> + /// Gets or sets a list of files to copy the source files to. + /// </summary> + /// <value>The list of files to copy the source files to.</value> + [Required] + public ITaskItem[] DestinationFiles { get; set; } + + /// <summary> + /// Executes this instance. + /// </summary> + /// <returns><c>true</c> if the operation was successful.</returns> + public override bool Execute() { + if (this.SourceFiles.Length != this.DestinationFiles.Length) { + Log.LogError("{0} inputs and {1} outputs given.", this.SourceFiles.Length, this.DestinationFiles.Length); + return false; + } + + for (int i = 0; i < this.SourceFiles.Length; i++) { + string sourcePath = this.SourceFiles[i].ItemSpec; + string destPath = this.DestinationFiles[i].ItemSpec; + + if (this.SkipUnchangedFiles && File.GetLastWriteTimeUtc(sourcePath) < File.GetLastWriteTimeUtc(destPath)) { + Log.LogMessage(MessageImportance.Low, "Skipping \"{0}\" -> \"{1}\" because the destination is up to date.", sourcePath, destPath); + continue; + } + + Log.LogMessage(MessageImportance.Normal, "Transforming \"{0}\" -> \"{1}\"", sourcePath, destPath); + + string[] beforeTokens = this.SourceFiles[i].GetMetadata("BeforeTokens").Split(';'); + string[] afterTokens = this.SourceFiles[i].GetMetadata("AfterTokens").Split(';'); + if (beforeTokens.Length != afterTokens.Length) { + Log.LogError("Unequal number of before and after tokens. Before: \"{0}\". After \"{1}\".", beforeTokens, afterTokens); + return false; + } + + using (StreamReader sr = File.OpenText(sourcePath)) { + using (StreamWriter sw = File.CreateText(destPath)) { + StringBuilder line = new StringBuilder(); + while (line.Append(sr.ReadLine()) != null) { + for (int j = 0; j < beforeTokens.Length; j++) { + line.Replace(beforeTokens[j], afterTokens[j]); + } + + sw.WriteLine(line); + + // Clear out the line buffer for the next input. + line.Length = 0; + } + } + } + } + + return true; + } + } +} diff --git a/src/DotNetOpenAuth.BuildTasks/DotNetOpenAuth.BuildTasks.csproj b/src/DotNetOpenAuth.BuildTasks/DotNetOpenAuth.BuildTasks.csproj index 7105bdf..3b22fff 100644 --- a/src/DotNetOpenAuth.BuildTasks/DotNetOpenAuth.BuildTasks.csproj +++ b/src/DotNetOpenAuth.BuildTasks/DotNetOpenAuth.BuildTasks.csproj @@ -51,6 +51,7 @@ <Compile Include="ChangeProjectReferenceToAssemblyReference.cs" /> <Compile Include="CompareFiles.cs" /> <Compile Include="ChangeAssemblyReference.cs" /> + <Compile Include="CopyWithTokenSubstitution.cs" /> <Compile Include="CreateWebApplication.cs" /> <Compile Include="DeleteWebApplication.cs" /> <Compile Include="ECMAScriptPacker.cs" /> |