diff options
Diffstat (limited to 'src/DotNetOpenAuth.BuildTasks')
16 files changed, 895 insertions, 86 deletions
diff --git a/src/DotNetOpenAuth.BuildTasks/AddProjectItems.cs b/src/DotNetOpenAuth.BuildTasks/AddProjectItems.cs index 30fa284..0b84398 100644 --- a/src/DotNetOpenAuth.BuildTasks/AddProjectItems.cs +++ b/src/DotNetOpenAuth.BuildTasks/AddProjectItems.cs @@ -6,13 +6,13 @@ namespace DotNetOpenAuth.BuildTasks { using System; + using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Build.BuildEngine; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; - using System.Collections; public class AddProjectItems : Task { /// <summary> @@ -49,7 +49,10 @@ namespace DotNetOpenAuth.BuildTasks { BuildItem newItem = project.AddNewItem(itemType, projectItem.ItemSpec, false); var customMetadata = projectItem.CloneCustomMetadata(); foreach (DictionaryEntry entry in customMetadata) { - newItem.SetMetadata((string)entry.Key, (string)entry.Value); + string value = (string)entry.Value; + if (value.Length > 0) { + newItem.SetMetadata((string)entry.Key, value); + } } } diff --git a/src/DotNetOpenAuth.BuildTasks/ChangeProjectReferenceToAssemblyReference.cs b/src/DotNetOpenAuth.BuildTasks/ChangeProjectReferenceToAssemblyReference.cs index f940a72..503e168 100644 --- a/src/DotNetOpenAuth.BuildTasks/ChangeProjectReferenceToAssemblyReference.cs +++ b/src/DotNetOpenAuth.BuildTasks/ChangeProjectReferenceToAssemblyReference.cs @@ -1,47 +1,59 @@ -using System; -using System.Linq; -using System.IO; -using System.Xml; +namespace DotNetOpenAuth.BuildTasks { + using System; + using System.IO; + using System.Linq; + using System.Xml; + using Microsoft.Build.BuildEngine; + using Microsoft.Build.Framework; + using Microsoft.Build.Utilities; -using Microsoft.Build.Framework; -using Microsoft.Build.Utilities; -using Microsoft.Build.BuildEngine; - -namespace DotNetOpenAuth.BuildTasks { /// <summary> /// Replaces ProjectReference items in a set of projects with Reference items. /// </summary> public class ChangeProjectReferenceToAssemblyReference : Task { + private const string msbuildNamespace = "http://schemas.microsoft.com/developer/msbuild/2003"; + /// <summary> /// The projects to alter. /// </summary> [Required] public ITaskItem[] Projects { get; set; } + /// <summary> - /// The project reference to remove. + /// The project references to remove. /// </summary> [Required] - public string ProjectReference { get; set; } + public ITaskItem[] ProjectReferences { get; set; } + /// <summary> - /// The assembly reference to add. + /// The assembly references to add. /// </summary> [Required] - public string Reference { get; set; } + public ITaskItem[] References { get; set; } - const string msbuildNamespace = "http://schemas.microsoft.com/developer/msbuild/2003"; public override bool Execute() { - foreach (var project in Projects) { - Log.LogMessage(MessageImportance.Normal, "Changing P2P references to assembly references in \"{0}\".", project.ItemSpec); + if (this.ProjectReferences.Length != this.References.Length) { + this.Log.LogError("ProjectReferences and References arrays do not have matching lengths."); + } + foreach (var project in Projects) { Project doc = new Project(); doc.Load(project.ItemSpec); - - var projectReference = doc.EvaluatedItems.OfType<BuildItem>().Where( - item => item.Name == "ProjectReference" && item.Include == ProjectReference).Single(); - doc.RemoveItem(projectReference); - var newReference = doc.AddNewItem("Reference", Path.GetFileNameWithoutExtension(Reference), true); - newReference.SetMetadata("HintPath", Reference); + var projectReferences = doc.EvaluatedItems.OfType<BuildItem>().Where(item => item.Name == "ProjectReference"); + var matchingReferences = from reference in projectReferences + join refToRemove in this.ProjectReferences on reference.Include equals refToRemove.ItemSpec + let addIndex = Array.IndexOf(this.ProjectReferences, refToRemove) + select new { Remove = reference, Add = this.References[addIndex] }; + foreach (var matchingReference in matchingReferences) { + this.Log.LogMessage("Removing project reference to \"{0}\" from \"{1}\".", matchingReference.Remove.Include, project.ItemSpec); + doc.RemoveItem(matchingReference.Remove); + if (matchingReference.Add.ItemSpec != "REMOVE") { + this.Log.LogMessage("Adding assembly reference to \"{0}\" to \"{1}\".", matchingReference.Add.ItemSpec, project.ItemSpec); + var newReference = doc.AddNewItem("Reference", Path.GetFileNameWithoutExtension(matchingReference.Add.ItemSpec), true); + newReference.SetMetadata("HintPath", matchingReference.Add.ItemSpec); + } + } doc.Save(project.ItemSpec); } diff --git a/src/DotNetOpenAuth.BuildTasks/CopyWithTokenSubstitution.cs b/src/DotNetOpenAuth.BuildTasks/CopyWithTokenSubstitution.cs index e17d8f2..5b097ab 100644 --- a/src/DotNetOpenAuth.BuildTasks/CopyWithTokenSubstitution.cs +++ b/src/DotNetOpenAuth.BuildTasks/CopyWithTokenSubstitution.cs @@ -56,38 +56,51 @@ namespace DotNetOpenAuth.BuildTasks { for (int i = 0; i < this.SourceFiles.Length; i++) { string sourcePath = this.SourceFiles[i].ItemSpec; string destPath = this.DestinationFiles[i].ItemSpec; - bool skipUnchangedFiles = bool.Parse(this.SourceFiles[i].GetMetadata("SkipUnchangedFiles")); + bool skipUnchangedFiles; + bool.TryParse(this.SourceFiles[i].GetMetadata("SkipUnchangedFiles"), out skipUnchangedFiles); - // We deliberably consider newer destination files to be up-to-date rather than - // requiring equality because this task modifies the destination file while copying. - if (skipUnchangedFiles && File.GetLastWriteTimeUtc(sourcePath) < File.GetLastWriteTimeUtc(destPath)) { - Log.LogMessage(MessageImportance.Low, "Skipping \"{0}\" -> \"{1}\" because the destination is up to date.", sourcePath, destPath); - continue; + if (!Directory.Exists(Path.GetDirectoryName(destPath))) { + Directory.CreateDirectory(Path.GetDirectoryName(destPath)); } - Log.LogMessage(MessageImportance.Normal, "Transforming \"{0}\" -> \"{1}\"", sourcePath, destPath); + if (string.IsNullOrEmpty(this.SourceFiles[i].GetMetadata("BeforeTokens"))) { + // this is just a standard copy without token substitution + if (skipUnchangedFiles && File.GetLastWriteTimeUtc(sourcePath) == File.GetLastWriteTimeUtc(destPath)) { + Log.LogMessage(MessageImportance.Low, "Skipping \"{0}\" -> \"{1}\" because the destination is up to date.", sourcePath, destPath); + continue; + } - 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; - } + Log.LogMessage(MessageImportance.Normal, "Copying file from \"{0}\" to \"{1}\"", sourcePath, destPath); + File.Copy(sourcePath, destPath, true); + } else { + // We deliberably consider newer destination files to be up-to-date rather than + // requiring equality because this task modifies the destination file while copying. + if (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); - using (StreamReader sr = File.OpenText(sourcePath)) { - if (!Directory.Exists(Path.GetDirectoryName(destPath))) { - Directory.CreateDirectory(Path.GetDirectoryName(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 (StreamWriter sw = File.CreateText(destPath)) { - StringBuilder line = new StringBuilder(); - while (!sr.EndOfStream) { - line.Length = 0; - line.Append(sr.ReadLine()); - for (int j = 0; j < beforeTokens.Length; j++) { - line.Replace(beforeTokens[j], afterTokens[j]); - } - sw.WriteLine(line); + using (StreamReader sr = File.OpenText(sourcePath)) { + using (StreamWriter sw = File.CreateText(destPath)) { + StringBuilder line = new StringBuilder(); + while (!sr.EndOfStream) { + line.Length = 0; + line.Append(sr.ReadLine()); + for (int j = 0; j < beforeTokens.Length; j++) { + line.Replace(beforeTokens[j], afterTokens[j]); + } + + sw.WriteLine(line); + } } } } diff --git a/src/DotNetOpenAuth.BuildTasks/DiscoverProjectTemplates.cs b/src/DotNetOpenAuth.BuildTasks/DiscoverProjectTemplates.cs index 0162c16..f49c9b1 100644 --- a/src/DotNetOpenAuth.BuildTasks/DiscoverProjectTemplates.cs +++ b/src/DotNetOpenAuth.BuildTasks/DiscoverProjectTemplates.cs @@ -15,6 +15,7 @@ namespace DotNetOpenAuth.BuildTasks { using Microsoft.Build.Utilities; public class DiscoverProjectTemplates : Task { + [Required] public ITaskItem[] TopLevelTemplates { get; set; } [Output] diff --git a/src/DotNetOpenAuth.BuildTasks/DotNetOpenAuth.BuildTasks.csproj b/src/DotNetOpenAuth.BuildTasks/DotNetOpenAuth.BuildTasks.csproj index 5de32e7..365bec5 100644 --- a/src/DotNetOpenAuth.BuildTasks/DotNetOpenAuth.BuildTasks.csproj +++ b/src/DotNetOpenAuth.BuildTasks/DotNetOpenAuth.BuildTasks.csproj @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="utf-8"?> -<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> +<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> @@ -12,6 +12,21 @@ <AssemblyName>DotNetOpenAuth.BuildTasks</AssemblyName> <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> + <PublishUrl>publish\</PublishUrl> + <Install>true</Install> + <InstallFrom>Disk</InstallFrom> + <UpdateEnabled>false</UpdateEnabled> + <UpdateMode>Foreground</UpdateMode> + <UpdateInterval>7</UpdateInterval> + <UpdateIntervalUnits>Days</UpdateIntervalUnits> + <UpdatePeriodically>false</UpdatePeriodically> + <UpdateRequired>false</UpdateRequired> + <MapFileExtensions>true</MapFileExtensions> + <ApplicationRevision>0</ApplicationRevision> + <ApplicationVersion>1.0.0.%2a</ApplicationVersion> + <IsWebBootstrapper>false</IsWebBootstrapper> + <UseApplicationTrust>false</UseApplicationTrust> + <BootstrapperEnabled>true</BootstrapperEnabled> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> @@ -48,6 +63,7 @@ </CodeContractsBaseLineFile> <CodeContractsRuntimeCheckingLevel>Full</CodeContractsRuntimeCheckingLevel> <CodeContractsReferenceAssembly>%28none%29</CodeContractsReferenceAssembly> + <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <DebugType>pdbonly</DebugType> @@ -56,6 +72,7 @@ <DefineConstants>TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> + <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> </PropertyGroup> <ItemGroup> <Reference Include="Microsoft.Build.Engine" /> @@ -89,15 +106,20 @@ <Compile Include="CreateWebApplication.cs" /> <Compile Include="DeleteWebApplication.cs" /> <Compile Include="DiscoverProjectTemplates.cs" /> + <Compile Include="DowngradeProjects.cs" /> <Compile Include="ECMAScriptPacker.cs" /> <Compile Include="FilterItems.cs" /> <Compile Include="FixupReferenceHintPaths.cs" /> <Compile Include="FixupShippingToolSamples.cs" /> + <Compile Include="HardLinkCopy.cs" /> <Compile Include="MergeProjectWithVSTemplate.cs" /> <Compile Include="GetBuildVersion.cs" /> <Compile Include="CheckAdminRights.cs" /> <Compile Include="JsPack.cs" /> + <Compile Include="NativeMethods.cs" /> <Compile Include="ParseMaster.cs" /> + <Compile Include="PathSegment.cs" /> + <Compile Include="Publicize.cs" /> <Compile Include="Purge.cs" /> <Compile Include="ReSignDelaySignedAssemblies.cs" /> <Compile Include="SetEnvironmentVariable.cs" /> @@ -110,6 +132,7 @@ <DependentUpon>TaskStrings.resx</DependentUpon> </Compile> <Compile Include="Trim.cs" /> + <Compile Include="Utilities.cs" /> </ItemGroup> <ItemGroup> <EmbeddedResource Include="TaskStrings.resx"> @@ -117,6 +140,23 @@ <LastGenOutput>TaskStrings.Designer.cs</LastGenOutput> </EmbeddedResource> </ItemGroup> + <ItemGroup> + <BootstrapperPackage Include="Microsoft.Net.Client.3.5"> + <Visible>False</Visible> + <ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName> + <Install>false</Install> + </BootstrapperPackage> + <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1"> + <Visible>False</Visible> + <ProductName>.NET Framework 3.5 SP1</ProductName> + <Install>true</Install> + </BootstrapperPackage> + <BootstrapperPackage Include="Microsoft.Windows.Installer.3.1"> + <Visible>False</Visible> + <ProductName>Windows Installer 3.1</ProductName> + <Install>true</Install> + </BootstrapperPackage> + </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- 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. diff --git a/src/DotNetOpenAuth.BuildTasks/DotNetOpenAuth.BuildTasks.sln b/src/DotNetOpenAuth.BuildTasks/DotNetOpenAuth.BuildTasks.sln index fca41e8..f3e3982 100644 --- a/src/DotNetOpenAuth.BuildTasks/DotNetOpenAuth.BuildTasks.sln +++ b/src/DotNetOpenAuth.BuildTasks/DotNetOpenAuth.BuildTasks.sln @@ -1,16 +1,27 @@ -Microsoft Visual Studio Solution File, Format Version 10.00 -# Visual Studio 2008 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetOpenAuth.BuildTasks", "DotNetOpenAuth.BuildTasks.csproj", "{AC231A51-EF60-437C-A33F-AF8ADEB8EB74}" -EndProject +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{ABBE14A3-0404-4123-9093-E598C3DD3E9B}" ProjectSection(SolutionItems) = preProject ..\..\build.proj = ..\..\build.proj + ..\..\doc\doc.proj = ..\..\doc\doc.proj + ..\..\tools\DotNetOpenAuth.automated.props = ..\..\tools\DotNetOpenAuth.automated.props + ..\..\tools\DotNetOpenAuth.automated.targets = ..\..\tools\DotNetOpenAuth.automated.targets ..\..\lib\DotNetOpenAuth.BuildTasks.targets = ..\..\lib\DotNetOpenAuth.BuildTasks.targets ..\..\tools\DotNetOpenAuth.Common.Settings.targets = ..\..\tools\DotNetOpenAuth.Common.Settings.targets + ..\..\tools\DotNetOpenAuth.props = ..\..\tools\DotNetOpenAuth.props + ..\..\tools\DotNetOpenAuth.targets = ..\..\tools\DotNetOpenAuth.targets ..\..\tools\DotNetOpenAuth.Versioning.targets = ..\..\tools\DotNetOpenAuth.Versioning.targets + ..\..\tools\drop.proj = ..\..\tools\drop.proj + ..\..\projecttemplates\projecttemplates.proj = ..\..\projecttemplates\projecttemplates.proj + ..\..\samples\Samples.proj = ..\..\samples\Samples.proj + ..\..\samples\tools.proj = ..\..\samples\tools.proj + ..\..\vsi\vsi.proj = ..\..\vsi\vsi.proj + ..\..\vsix\vsix.proj = ..\..\vsix\vsix.proj EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetOpenAuth.BuildTasks", "DotNetOpenAuth.BuildTasks.csproj", "{AC231A51-EF60-437C-A33F-AF8ADEB8EB74}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/src/DotNetOpenAuth.BuildTasks/DowngradeProjects.cs b/src/DotNetOpenAuth.BuildTasks/DowngradeProjects.cs new file mode 100644 index 0000000..645522d --- /dev/null +++ b/src/DotNetOpenAuth.BuildTasks/DowngradeProjects.cs @@ -0,0 +1,110 @@ +//----------------------------------------------------------------------- +// <copyright file="DowngradeProjects.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.BuildEngine; + using Microsoft.Build.Framework; + using Microsoft.Build.Utilities; + + /// <summary> + /// Downgrades Visual Studio 2010 solutions and projects so that they load in Visual Studio 2008. + /// </summary> + public class DowngradeProjects : Task { + /// <summary> + /// Gets or sets the projects and solutions to downgrade. + /// </summary> + [Required] + public ITaskItem[] Projects { get; set; } + + /// <summary> + /// Gets or sets a value indicating whether ASP.NET MVC 2 projects are downgraded to MVC 1.0. + /// </summary> + public bool DowngradeMvc2ToMvc1 { get; set; } + + /// <summary> + /// Executes this instance. + /// </summary> + public override bool Execute() { + foreach (ITaskItem taskItem in this.Projects) { + switch (GetClassification(taskItem)) { + case ProjectClassification.VS2010Project: + this.Log.LogMessage(MessageImportance.Low, "Downgrading project \"{0}\".", taskItem.ItemSpec); + Project project = new Project(); + project.Load(taskItem.ItemSpec); + project.DefaultToolsVersion = "3.5"; + + if (this.DowngradeMvc2ToMvc1) { + string projectTypeGuids = project.GetEvaluatedProperty("ProjectTypeGuids"); + if (!string.IsNullOrEmpty(projectTypeGuids)) { + projectTypeGuids = projectTypeGuids.Replace("{F85E285D-A4E0-4152-9332-AB1D724D3325}", "{603c0e0b-db56-11dc-be95-000d561079b0}"); + project.SetProperty("ProjectTypeGuids", projectTypeGuids); + } + } + + // Web projects usually have an import that includes these substrings + foreach (Import import in project.Imports) { + import.ProjectPath = import.ProjectPath + .Replace("$(MSBuildExtensionsPath32)", "$(MSBuildExtensionsPath)") + .Replace("VisualStudio\\v10.0", "VisualStudio\\v9.0"); + } + + // VS2010 won't let you have a System.Core reference, but VS2008 requires it. + BuildItemGroup references = project.GetEvaluatedItemsByName("Reference"); + if (!references.Cast<BuildItem>().Any(item => item.FinalItemSpec.StartsWith("System.Core", StringComparison.OrdinalIgnoreCase))) { + project.AddNewItem("Reference", "System.Core"); + } + + project.Save(taskItem.ItemSpec); + break; + case ProjectClassification.VS2010Solution: + this.Log.LogMessage(MessageImportance.Low, "Downgrading solution \"{0}\".", taskItem.ItemSpec); + string[] contents = File.ReadAllLines(taskItem.ItemSpec); + if (contents[1] != "Microsoft Visual Studio Solution File, Format Version 11.00" || + contents[2] != "# Visual Studio 2010") { + this.Log.LogError("Unrecognized solution file header in \"{0}\".", taskItem.ItemSpec); + break; + } + + contents[1] = "Microsoft Visual Studio Solution File, Format Version 10.00"; + contents[2] = "# Visual Studio 2008"; + + for (int i = 3; i < contents.Length; i++) { + contents[i] = contents[i].Replace("TargetFrameworkMoniker = \".NETFramework,Version%3Dv", "TargetFramework = \""); + } + + File.WriteAllLines(taskItem.ItemSpec, contents); + break; + default: + this.Log.LogWarning("Unrecognized project type for \"{0}\".", taskItem.ItemSpec); + break; + } + } + + return !this.Log.HasLoggedErrors; + } + + private static ProjectClassification GetClassification(ITaskItem taskItem) { + if (Path.GetExtension(taskItem.ItemSpec).EndsWith("proj", StringComparison.OrdinalIgnoreCase)) { + return ProjectClassification.VS2010Project; + } else if (Path.GetExtension(taskItem.ItemSpec).Equals(".sln", StringComparison.OrdinalIgnoreCase)) { + return ProjectClassification.VS2010Solution; + } else { + return ProjectClassification.Unrecognized; + } + } + + private enum ProjectClassification { + VS2010Project, + VS2010Solution, + Unrecognized, + } + } +} diff --git a/src/DotNetOpenAuth.BuildTasks/FixupReferenceHintPaths.cs b/src/DotNetOpenAuth.BuildTasks/FixupReferenceHintPaths.cs index 13a4b8f..babaab3 100644 --- a/src/DotNetOpenAuth.BuildTasks/FixupReferenceHintPaths.cs +++ b/src/DotNetOpenAuth.BuildTasks/FixupReferenceHintPaths.cs @@ -40,7 +40,13 @@ namespace DotNetOpenAuth.BuildTasks { // Figure out what the assembly names are of the references that are available. AssemblyName[] availableReferences = new AssemblyName[this.References.Length]; for (int i = 0; i < this.References.Length; i++) { - availableReferences[i] = AssemblyName.GetAssemblyName(this.References[i].ItemSpec); + if (File.Exists(this.References[i].ItemSpec)) { + availableReferences[i] = AssemblyName.GetAssemblyName(this.References[i].ItemSpec); + } else { + availableReferences[i] = new AssemblyName(Path.GetFileNameWithoutExtension(this.References[i].ItemSpec)) { + CodeBase = this.References[i].GetMetadata("FullPath"), + }; + } } foreach (var projectTaskItem in this.Projects) { diff --git a/src/DotNetOpenAuth.BuildTasks/FixupShippingToolSamples.cs b/src/DotNetOpenAuth.BuildTasks/FixupShippingToolSamples.cs index 92b0235..6c71740 100644 --- a/src/DotNetOpenAuth.BuildTasks/FixupShippingToolSamples.cs +++ b/src/DotNetOpenAuth.BuildTasks/FixupShippingToolSamples.cs @@ -6,13 +6,14 @@ namespace DotNetOpenAuth.BuildTasks { using System; + using System.Collections; using System.Collections.Generic; + using System.IO; using System.Linq; using System.Text; - using Microsoft.Build.Utilities; - using Microsoft.Build.Framework; - using System.IO; using Microsoft.Build.BuildEngine; + using Microsoft.Build.Framework; + using Microsoft.Build.Utilities; /// <summary> /// Removes imports that only apply when a shipping tool sample builds as part of @@ -22,6 +23,10 @@ namespace DotNetOpenAuth.BuildTasks { [Required] public ITaskItem[] Projects { get; set; } + public string[] RemoveImportsStartingWith { get; set; } + + public ITaskItem[] AddReferences { get; set; } + /// <summary> /// Executes this instance. /// </summary> @@ -34,10 +39,21 @@ namespace DotNetOpenAuth.BuildTasks { Uri projectUri = new Uri(projectTaskItem.GetMetadata("FullPath")); project.Load(projectTaskItem.ItemSpec, ProjectLoadSettings.IgnoreMissingImports); - project.Imports.Cast<Import>() - .Where(import => import.ProjectPath.StartsWith(@"..\..\tools\", StringComparison.OrdinalIgnoreCase)) - .ToList() - .ForEach(import => project.Imports.RemoveImport(import)); + if (this.RemoveImportsStartingWith != null && this.RemoveImportsStartingWith.Length > 0) { + project.Imports.Cast<Import>() + .Where(import => this.RemoveImportsStartingWith.Any(start => import.ProjectPath.StartsWith(start, StringComparison.OrdinalIgnoreCase))) + .ToList() + .ForEach(import => project.Imports.RemoveImport(import)); + } + + if (this.AddReferences != null) { + foreach (var reference in this.AddReferences) { + BuildItem item = project.AddNewItem("Reference", reference.ItemSpec); + foreach (DictionaryEntry metadata in reference.CloneCustomMetadata()) { + item.SetMetadata((string)metadata.Key, (string)metadata.Value); + } + } + } project.Save(projectTaskItem.ItemSpec); } diff --git a/src/DotNetOpenAuth.BuildTasks/HardLinkCopy.cs b/src/DotNetOpenAuth.BuildTasks/HardLinkCopy.cs new file mode 100644 index 0000000..af2d1d0 --- /dev/null +++ b/src/DotNetOpenAuth.BuildTasks/HardLinkCopy.cs @@ -0,0 +1,61 @@ +//----------------------------------------------------------------------- +// <copyright file="HardLinkCopy.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.BuildTasks { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using Microsoft.Build.Framework; + using Microsoft.Build.Utilities; + using System.IO; + + public class HardLinkCopy : Task { + [Required] + public ITaskItem[] SourceFiles { get; set; } + + [Required] + public ITaskItem[] DestinationFiles { get; set; } + + /// <summary> + /// Executes this instance. + /// </summary> + public override bool Execute() { + if (this.SourceFiles.Length != this.DestinationFiles.Length) { + this.Log.LogError("SourceFiles has {0} elements and DestinationFiles has {1} elements.", this.SourceFiles.Length, this.DestinationFiles.Length); + return false; + } + + for (int i = 0; i < this.SourceFiles.Length; i++) { + bool hardLink; + bool.TryParse(this.DestinationFiles[i].GetMetadata("HardLink"), out hardLink); + string sourceFile = this.SourceFiles[i].ItemSpec; + string destinationFile = this.DestinationFiles[i].ItemSpec; + this.Log.LogMessage( + MessageImportance.Low, + "Copying {0} -> {1}{2}.", + sourceFile, + destinationFile, + hardLink ? " as hard link" : string.Empty); + + if (!Directory.Exists(Path.GetDirectoryName(destinationFile))) { + Directory.CreateDirectory(Path.GetDirectoryName(destinationFile)); + } + + if (hardLink) { + if (File.Exists(destinationFile)) { + File.Delete(destinationFile); + } + NativeMethods.CreateHardLink(sourceFile, destinationFile); + } else { + File.Copy(sourceFile, destinationFile, true); + } + } + + return !this.Log.HasLoggedErrors; + } + } +} diff --git a/src/DotNetOpenAuth.BuildTasks/MergeProjectWithVSTemplate.cs b/src/DotNetOpenAuth.BuildTasks/MergeProjectWithVSTemplate.cs index 1a8a17d..d162cd6 100644 --- a/src/DotNetOpenAuth.BuildTasks/MergeProjectWithVSTemplate.cs +++ b/src/DotNetOpenAuth.BuildTasks/MergeProjectWithVSTemplate.cs @@ -7,18 +7,27 @@ namespace DotNetOpenAuth.BuildTasks { using System; using System.Collections.Generic; + using System.Collections.ObjectModel; + using System.Diagnostics.Contracts; + using System.Globalization; + using System.IO; using System.Linq; using System.Text; - using Microsoft.Build.Framework; - using Microsoft.Build.Utilities; using System.Xml.Linq; - using System.IO; using Microsoft.Build.BuildEngine; - using System.Diagnostics.Contracts; + using Microsoft.Build.Framework; + using Microsoft.Build.Utilities; public class MergeProjectWithVSTemplate : Task { internal const string VSTemplateNamespace = "http://schemas.microsoft.com/developer/vstemplate/2005"; + internal const string VsixNamespace = "http://schemas.microsoft.com/developer/vsx-schema/2010"; + + /// <summary> + /// A dictionary where the key is the project name and the value is the path contribution. + /// </summary> + private Dictionary<string, string> vsixContributionToPath = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); + [Required] public string[] ProjectItemTypes { get; set; } @@ -26,13 +35,44 @@ namespace DotNetOpenAuth.BuildTasks { public string[] ReplaceParametersExtensions { get; set; } [Required] - public ITaskItem[] Templates { get; set; } + public ITaskItem[] SourceTemplates { get; set; } + + [Required] + public ITaskItem[] SourceProjects { get; set; } + + [Required] + public ITaskItem[] DestinationTemplates { get; set; } + + public ITaskItem[] SourcePathExceptions { get; set; } + + /// <summary> + /// Gets or sets the maximum length a project item's relative path should + /// be limited to, artificially renaming them as necessary. + /// </summary> + /// <value>Use 0 to disable the renaming feature.</value> + public int MaximumRelativePathLength { get; set; } + + /// <summary> + /// Gets or sets the project item paths from the source project to copy to the destination location. + /// </summary> + [Output] + public ITaskItem[] ProjectItems { get; set; } /// <summary> /// Executes this instance. /// </summary> public override bool Execute() { - foreach(ITaskItem sourceTemplateTaskItem in this.Templates) { + if (this.DestinationTemplates.Length != this.SourceTemplates.Length) { + this.Log.LogError("SourceTemplates array has length {0} while DestinationTemplates array has length {1}, but must equal.", this.SourceTemplates.Length, this.DestinationTemplates.Length); + } + if (this.SourceProjects.Length != this.SourceTemplates.Length) { + this.Log.LogError("SourceTemplates array has length {0} while SourceProjects array has length {1}, but must equal.", this.SourceTemplates.Length, this.SourceProjects.Length); + } + + var projectItemsToCopy = new List<ITaskItem>(); + + for (int iTemplate = 0; iTemplate < this.SourceTemplates.Length; iTemplate++) { + ITaskItem sourceTemplateTaskItem = this.SourceTemplates[iTemplate]; var template = XElement.Load(sourceTemplateTaskItem.ItemSpec); var templateContentElement = template.Element(XName.Get("TemplateContent", VSTemplateNamespace)); var projectElement = templateContentElement.Element(XName.Get("Project", VSTemplateNamespace)); @@ -41,37 +81,65 @@ namespace DotNetOpenAuth.BuildTasks { continue; } - var projectPath = Path.Combine(Path.GetDirectoryName(sourceTemplateTaskItem.ItemSpec), projectElement.Attribute("File").Value); + var projectPath = this.SourceProjects[iTemplate].ItemSpec; + var projectDirectory = Path.GetDirectoryName(Path.Combine(Path.GetDirectoryName(sourceTemplateTaskItem.GetMetadata("FullPath")), projectElement.Attribute("File").Value)); Log.LogMessage("Merging project \"{0}\" with \"{1}\".", projectPath, sourceTemplateTaskItem.ItemSpec); var sourceProject = new Project(); sourceProject.Load(projectPath); + var projectItems = sourceProject.EvaluatedItems.Cast<BuildItem>().Where(item => this.ProjectItemTypes.Contains(item.Name)); + + // Figure out where every project item is in source, and where it will go in the destination, + // taking into account a given maximum path length that may require that we shorten the path. + PathSegment root = new PathSegment(); + root.Add(projectItems.Select(item => item.Include)); + root.EnsureSelfAndChildrenNoLongerThan(this.MaximumRelativePathLength); // Collect the project items from the project that are appropriate // to include in the .vstemplate file. - var itemsByFolder = from item in sourceProject.EvaluatedItems.Cast<BuildItem>() - where this.ProjectItemTypes.Contains(item.Name) - orderby item.Include - group item by Path.GetDirectoryName(item.Include); - foreach (var folder in itemsByFolder) { - XElement parentNode = FindOrCreateParent(folder.Key, projectElement); - - foreach (var item in folder) { - bool replaceParameters = this.ReplaceParametersExtensions.Contains(Path.GetExtension(item.Include)); + foreach (var folder in root.SelfAndDescendents.Where(path => !path.IsLeaf && path.LeafChildren.Any())) { + XElement parentNode = projectElement; + parentNode = FindOrCreateParent(folder.CurrentPath, projectElement); + if (folder.NameChanged) { + parentNode.SetAttributeValue("TargetFolderName", folder.OriginalName); + } + + foreach (var item in folder.LeafChildren) { var itemName = XName.Get("ProjectItem", VSTemplateNamespace); - var projectItem = parentNode.Elements(itemName).FirstOrDefault(el => string.Equals(el.Value, Path.GetFileName(item.Include), StringComparison.OrdinalIgnoreCase)); + // The project item MAY be hard-coded in the .vstemplate file, under the original name. + var projectItem = parentNode.Elements(itemName).FirstOrDefault(el => string.Equals(el.Value, Path.GetFileName(item.OriginalName), StringComparison.OrdinalIgnoreCase)); if (projectItem == null) { - projectItem = new XElement(itemName, Path.GetFileName(item.Include)); + projectItem = new XElement(itemName, item.CurrentName); parentNode.Add(projectItem); } - if (replaceParameters) { + if (item.NameChanged) { + projectItem.Value = item.CurrentName; // set Value in case it was a hard-coded item in the .vstemplate file. + projectItem.SetAttributeValue("TargetFileName", item.OriginalName); + } + if (this.ReplaceParametersExtensions.Contains(Path.GetExtension(item.OriginalPath))) { projectItem.SetAttributeValue("ReplaceParameters", "true"); } } } - template.Save(sourceTemplateTaskItem.ItemSpec); + template.Save(this.DestinationTemplates[iTemplate].ItemSpec); + foreach (var pair in root.LeafDescendents) { + TaskItem item = new TaskItem(Path.Combine(Path.GetDirectoryName(this.SourceTemplates[iTemplate].ItemSpec), pair.OriginalPath)); + string apparentSource = Path.Combine(Path.GetDirectoryName(this.SourceTemplates[iTemplate].ItemSpec), pair.OriginalPath); + var sourcePathException = this.SourcePathExceptions.FirstOrDefault(ex => string.Equals(ex.ItemSpec, apparentSource)); + if (sourcePathException != null) { + item.SetMetadata("SourceFullPath", sourcePathException.GetMetadata("ActualSource")); + } else { + item.SetMetadata("SourceFullPath", Path.GetFullPath(apparentSource)); + } + item.SetMetadata("DestinationFullPath", Path.GetFullPath(Path.Combine(Path.GetDirectoryName(this.DestinationTemplates[iTemplate].ItemSpec), pair.CurrentPath))); + item.SetMetadata("RecursiveDir", Path.GetDirectoryName(this.SourceTemplates[iTemplate].ItemSpec)); + item.SetMetadata("Transform", this.ReplaceParametersExtensions.Contains(Path.GetExtension(pair.OriginalName)) ? "true" : "false"); + projectItemsToCopy.Add(item); + } } + this.ProjectItems = projectItemsToCopy.ToArray(); + return !Log.HasLoggedErrors; } diff --git a/src/DotNetOpenAuth.BuildTasks/NativeMethods.cs b/src/DotNetOpenAuth.BuildTasks/NativeMethods.cs new file mode 100644 index 0000000..26de3a4 --- /dev/null +++ b/src/DotNetOpenAuth.BuildTasks/NativeMethods.cs @@ -0,0 +1,18 @@ +namespace DotNetOpenAuth.BuildTasks { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Runtime.InteropServices; + + internal static class NativeMethods { + [DllImport("kernel32", SetLastError = true)] + private static extern bool CreateHardLink(string newFileName, string existingFileName, IntPtr securityAttributes); + + internal static void CreateHardLink(string existingFileName, string newFileName) { + if (!CreateHardLink(newFileName, existingFileName, IntPtr.Zero)) { + Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); + } + } + } +} diff --git a/src/DotNetOpenAuth.BuildTasks/PathSegment.cs b/src/DotNetOpenAuth.BuildTasks/PathSegment.cs new file mode 100644 index 0000000..56655ff --- /dev/null +++ b/src/DotNetOpenAuth.BuildTasks/PathSegment.cs @@ -0,0 +1,321 @@ +//----------------------------------------------------------------------- +// <copyright file="PathSegment.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.BuildTasks { + using System; + using System.Collections.Generic; + using System.Collections.ObjectModel; + using System.Diagnostics; + using System.Diagnostics.Contracts; + using System.IO; + using System.Linq; + using System.Text; + + internal class PathSegment { + private const float ParentChildResizeThreshold = 0.30f; + private readonly PathSegment parent; + private readonly string originalName; + private string currentName; + private bool minimized; + private static readonly string[] ReservedFileNames = "CON PRN AUX CLOCK$ NUL COM0 COM1 COM2 COM3 COM4 COM5 COM6 COM7 COM8 COM9 LPT0 LPT1 LPT2 LPT3 LPT4 LPT5 LPT6 LPT7 LPT8 LPT9".Split(' '); + + internal PathSegment() { + this.currentName = string.Empty; + this.originalName = string.Empty; + this.minimized = true; + this.Children = new Collection<PathSegment>(); + } + + private PathSegment(string originalName, PathSegment parent) + : this() { + Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(originalName)); + Contract.Requires<ArgumentNullException>(parent != null); + this.currentName = this.originalName = originalName; + this.parent = parent; + this.minimized = false; + } + + internal string OriginalPath { + get { + if (this.parent != null) { + return Path.Combine(this.parent.OriginalPath, this.originalName); + } else { + return this.originalName; + } + } + } + + internal string CurrentPath { + get { + if (this.parent != null) { + return Path.Combine(this.parent.CurrentPath, this.currentName); + } else { + return this.currentName; + } + } + } + + internal string CurrentName { + get { return this.currentName; } + } + + internal string OriginalName { + get { return this.originalName; } + } + + private int SegmentCount { + get { + int parents = this.parent != null ? this.parent.SegmentCount : 0; + return parents + 1; + } + } + + internal int FullLength { + get { + if (this.parent != null) { + int parentLength = this.parent.FullLength; + if (parentLength > 0) { + parentLength++; // allow for an in between slash + } + return parentLength + this.currentName.Length; + } else { + return this.currentName.Length; + } + } + } + + internal bool NameChanged { + get { return !string.Equals(this.currentName, this.originalName, StringComparison.OrdinalIgnoreCase); } + } + + internal bool IsLeaf { + get { return this.Children.Count == 0; } + } + + internal IEnumerable<PathSegment> Descendents { + get { + IEnumerable<PathSegment> result = this.Children; + foreach (PathSegment child in this.Children) { + result = result.Concat(child.Descendents); + } + + return result; + } + } + + internal IEnumerable<PathSegment> Ancestors { + get { + PathSegment parent = this.parent; + while (parent != null) { + yield return parent; + parent = parent.parent; + } + } + } + + internal IEnumerable<PathSegment> SelfAndDescendents { + get { + yield return this; + foreach (var child in this.Descendents) { + yield return child; + } + } + } + + internal IEnumerable<PathSegment> SelfAndAncestors { + get { + yield return this; + foreach (var parent in this.Ancestors) { + yield return parent; + } + } + } + + internal IEnumerable<PathSegment> LeafChildren { + get { return this.Children.Where(child => child.IsLeaf); } + } + + internal IEnumerable<PathSegment> LeafDescendents { + get { return this.Descendents.Where(child => child.IsLeaf); } + } + + internal IEnumerable<PathSegment> Siblings { + get { return this.parent != null ? this.parent.Children : Enumerable.Empty<PathSegment>(); } + } + + internal Collection<PathSegment> Children { get; private set; } + + public override string ToString() { + string path; + if (this.NameChanged) { + path = "{" + this.originalName + " => " + this.currentName + "}"; + } else { + path = this.currentName; + } + + if (path.Length > 0 && !this.IsLeaf) { + path += "\\"; + } + + if (this.parent != null) { + path = parent.ToString() + path; + } + + return path; + } + + internal PathSegment Add(string originalPath) { + Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(originalPath)); + Contract.Ensures(Contract.Result<PathSegment>() != null); + string[] segments = originalPath.Split(Path.DirectorySeparatorChar); + return this.Add(segments, 0); + } + + internal void Add(IEnumerable<string> originalPaths) { + foreach (string path in originalPaths) { + this.Add(path); + } + } + + internal int EnsureSelfAndChildrenNoLongerThan(int maxLength) { + Contract.Requires<ArgumentOutOfRangeException>(maxLength > 0, "A path can only have a positive length."); + Contract.Requires<ArgumentOutOfRangeException>(this.parent == null || maxLength > this.parent.FullLength + 1, "A child path cannot possibly be made shorter than its parent."); + Contract.Ensures(Contract.Result<int>() <= maxLength); + const int uniqueBase = 16; + + // Find the items that are too long, and always work on the longest one + var longPaths = this.SelfAndDescendents.Where(path => path.FullLength > maxLength).OrderByDescending(path => path.FullLength); + PathSegment longPath; + while ((longPath = longPaths.FirstOrDefault()) != null) { + // Keep working on this one until it's short enough. + do { + int tooLongBy = longPath.FullLength - maxLength; + var longSegments = longPath.SelfAndAncestors.Where(segment => !segment.minimized).OrderByDescending(segment => segment.CurrentName.Length); + PathSegment longestSegment = longSegments.FirstOrDefault(); + if (longestSegment == null) { + throw new InvalidOperationException("Unable to shrink path length sufficiently."); + } + PathSegment secondLongestSegment = longSegments.Skip(1).FirstOrDefault(); + int shortenByUpTo; + if (secondLongestSegment != null) { + shortenByUpTo = Math.Min(tooLongBy, Math.Max(1, longestSegment.CurrentName.Length - secondLongestSegment.CurrentName.Length)); + } else { + shortenByUpTo = tooLongBy; + } + int minimumGuaranteedUniqueLength = Math.Max(1, RoundUp(Math.Log(longestSegment.Siblings.Count(), uniqueBase))); + int allowableSegmentLength = Math.Max(minimumGuaranteedUniqueLength, longestSegment.CurrentName.Length - shortenByUpTo); + if (allowableSegmentLength >= longestSegment.CurrentName.Length) { + // We can't make this segment any shorter. + longestSegment.minimized = true; + } + longestSegment.currentName = longestSegment.CreateUniqueShortFileName(longestSegment.CurrentName, allowableSegmentLength); + } while (longPath.FullLength > maxLength); + } + + // Return the total length of self or longest child. + return this.SelfAndDescendents.Max(c => c.FullLength); + } + + internal PathSegment FindByOriginalPath(string originalPath) { + Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(originalPath)); + string[] segments = originalPath.Split(Path.DirectorySeparatorChar); + return this.FindByOriginalPath(segments, 0); + } + + private string GetUniqueShortName(string preferredPrefix, string preferredSuffix, int allowableLength) { + Contract.Requires<ArgumentNullException>(preferredPrefix != null); + Contract.Requires<ArgumentNullException>(preferredSuffix != null); + Contract.Requires<ArgumentException>(allowableLength > 0); + Contract.Ensures(!string.IsNullOrEmpty(Contract.Result<string>())); + Contract.Ensures(Contract.Result<string>().Length <= allowableLength); + string candidateName = string.Empty; + int i; + for (i = -1; candidateName.Length == 0 || ReservedFileNames.Contains(candidateName, StringComparer.OrdinalIgnoreCase) || this.Siblings.Any(child => string.Equals(child.CurrentName, candidateName, StringComparison.OrdinalIgnoreCase)); i++) { + string unique = i < 0 ? string.Empty : i.ToString("x"); + if (allowableLength < unique.Length) { + throw new InvalidOperationException("Unable to shorten path sufficiently to fit constraints."); + } + + candidateName = unique; + + // Suffix gets higher priority than the prefix, but only if the entire suffix can be appended. + if (candidateName.Length + preferredSuffix.Length <= allowableLength) { + candidateName += preferredSuffix; + } + + // Now prepend as much of the prefix as fits. + candidateName = preferredPrefix.Substring(0, Math.Min(allowableLength - candidateName.Length, preferredPrefix.Length)) + candidateName; + } + + return candidateName; + } + + private static int RoundUp(double value) { + int roundedValue = (int)value; + if (roundedValue < value) { + roundedValue++; + } + + return roundedValue; + } + + private string CreateUniqueShortFileName(string fileName, int targetLength) { + Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(fileName)); + Contract.Ensures(!string.IsNullOrEmpty(Contract.Result<string>())); + Contract.Ensures(Contract.Result<string>().Length <= targetLength); + + // The filename may already full within the target length. + if (fileName.Length <= targetLength) { + return fileName; + } + + string preferredPrefix = Path.GetFileNameWithoutExtension(fileName); + string preferredSuffix = Path.GetExtension(fileName); + + string shortenedFileName = GetUniqueShortName(preferredPrefix, preferredSuffix, targetLength); + return shortenedFileName; + } + + private void ShortenThis(int targetLength) { + this.currentName = CreateUniqueShortFileName(this.originalName, targetLength); + } + + private PathSegment Add(string[] segments, int segmentIndex) { + Contract.Requires<ArgumentNullException>(segments != null); + Contract.Requires<ArgumentOutOfRangeException>(segmentIndex < segments.Length); + Contract.Ensures(Contract.Result<PathSegment>() != null); + var match = this.Children.SingleOrDefault(child => String.Equals(child.originalName, segments[segmentIndex])); + if (match == null) { + match = new PathSegment(segments[segmentIndex], this); + this.Children.Add(match); + if (segments.Length == segmentIndex + 1) { + return match; + } + } + + return match.Add(segments, segmentIndex + 1); + } + + private PathSegment FindByOriginalPath(string[] segments, int segmentIndex) { + Contract.Requires<ArgumentNullException>(segments != null); + Contract.Requires<ArgumentOutOfRangeException>(segmentIndex < segments.Length); + if (string.Equals(this.originalName, segments[segmentIndex], StringComparison.OrdinalIgnoreCase)) { + if (segmentIndex == segments.Length - 1) { + return this; + } + + foreach (var child in this.Children) { + var match = child.FindByOriginalPath(segments, segmentIndex + 1); + if (match != null) { + return match; + } + } + } + + return null; + } + } +} diff --git a/src/DotNetOpenAuth.BuildTasks/Publicize.cs b/src/DotNetOpenAuth.BuildTasks/Publicize.cs new file mode 100644 index 0000000..f1781a7 --- /dev/null +++ b/src/DotNetOpenAuth.BuildTasks/Publicize.cs @@ -0,0 +1,97 @@ +//----------------------------------------------------------------------- +// <copyright file="Publicize.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.BuildEngine; + using Microsoft.Build.Utilities; + using Microsoft.Build.Framework; + + public class Publicize : ToolTask { + [Required] + public string MSBuildExtensionsPath { get; set; } + + [Required] + public ITaskItem Assembly { get; set; } + + public bool DelaySign { get; set; } + + public string KeyFile { get; set; } + + public bool SkipUnchangedFiles { get; set; } + + [Output] + public ITaskItem AccessorAssembly { get; set; } + + /// <summary> + /// Generates the full path to tool. + /// </summary> + /// <returns>An absolute path.</returns> + protected override string GenerateFullPathToTool() { + string toolPath = Path.Combine(this.MSBuildExtensionsPath, @"Microsoft\VisualStudio\v9.0\TeamTest\Publicize.exe"); + return toolPath; + } + + /// <summary> + /// Gets the name of the tool. + /// </summary> + /// <value>The name of the tool.</value> + protected override string ToolName { + get { return "Publicize.exe"; } + } + + /// <summary> + /// Validates the parameters. + /// </summary> + protected override bool ValidateParameters() { + if (!base.ValidateParameters()) { + return false; + } + + if (this.DelaySign && string.IsNullOrEmpty(this.KeyFile)) { + this.Log.LogError("DelaySign=true, but no KeyFile given."); + return false; + } + + return true; + } + + /// <summary> + /// Generates the command line commands. + /// </summary> + protected override string GenerateCommandLineCommands() { + CommandLineBuilder builder = new CommandLineBuilder(); + + if (this.DelaySign) { + builder.AppendSwitch("/delaysign"); + } + + builder.AppendSwitchIfNotNull("/keyfile:", this.KeyFile); + + builder.AppendFileNameIfNotNull(this.Assembly); + + return builder.ToString(); + } + + public override bool Execute() { + this.AccessorAssembly = new TaskItem(this.Assembly); + this.AccessorAssembly.ItemSpec = Path.Combine( + Path.GetDirectoryName(this.AccessorAssembly.ItemSpec), + Path.GetFileNameWithoutExtension(this.AccessorAssembly.ItemSpec) + "_Accessor") + Path.GetExtension(this.AccessorAssembly.ItemSpec); + + if (this.SkipUnchangedFiles && File.GetLastWriteTimeUtc(this.Assembly.ItemSpec) < File.GetLastWriteTimeUtc(this.AccessorAssembly.ItemSpec)) { + Log.LogMessage(MessageImportance.Low, "Skipping public accessor generation for {0} because {1} is up to date.", this.Assembly.ItemSpec, this.AccessorAssembly.ItemSpec); + return true; + } + + return base.Execute(); + } + } +} diff --git a/src/DotNetOpenAuth.BuildTasks/Purge.cs b/src/DotNetOpenAuth.BuildTasks/Purge.cs index e19e485..cf1a214 100644 --- a/src/DotNetOpenAuth.BuildTasks/Purge.cs +++ b/src/DotNetOpenAuth.BuildTasks/Purge.cs @@ -7,12 +7,12 @@ namespace DotNetOpenAuth.BuildTasks { using System; using System.Collections.Generic; + using System.IO; using System.Linq; using System.Text; - using Microsoft.Build.Utilities; - using Microsoft.Build.Framework; - using System.IO; using System.Text.RegularExpressions; + using Microsoft.Build.Framework; + using Microsoft.Build.Utilities; /// <summary> /// Purges directory trees of all directories and files that are not on a whitelist. @@ -40,6 +40,7 @@ namespace DotNetOpenAuth.BuildTasks { /// <summary> /// Gets or sets the files that should be NOT be purged. /// </summary> + [Required] public ITaskItem[] IntendedFiles { get; set; } /// <summary> @@ -82,7 +83,7 @@ namespace DotNetOpenAuth.BuildTasks { } private static string NormalizePath(string path) { - return Regex.Replace(path, @"\\+", @"\"); + return Path.GetFullPath(Regex.Replace(path, @"\\+", @"\")); } } } diff --git a/src/DotNetOpenAuth.BuildTasks/Utilities.cs b/src/DotNetOpenAuth.BuildTasks/Utilities.cs new file mode 100644 index 0000000..80e1733 --- /dev/null +++ b/src/DotNetOpenAuth.BuildTasks/Utilities.cs @@ -0,0 +1,31 @@ +//----------------------------------------------------------------------- +// <copyright file="Utilities.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.BuildTasks { + using System; + using System.Collections.Generic; + using System.Diagnostics.Contracts; + using System.Linq; + using System.Text; + + internal static class Utilities { + internal static string SuppressCharacters(string fileName, char[] suppress, char replacement) { + Contract.Requires<ArgumentNullException>(fileName != null); + Contract.Requires<ArgumentNullException>(suppress != null); + + if (fileName.IndexOfAny(suppress) < 0) { + return fileName; + } + + StringBuilder builder = new StringBuilder(fileName); + foreach (char ch in suppress) { + builder.Replace(ch, replacement); + } + + return builder.ToString(); + } + } +} |