Skip to content
This repository has been archived by the owner on Mar 20, 2019. It is now read-only.

Commit

Permalink
Backporting BuildTasks source
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Aug 7, 2010
1 parent e6e7bcb commit 8c93cbf
Show file tree
Hide file tree
Showing 30 changed files with 2,627 additions and 0 deletions.
Binary file modified lib/DotNetOpenAuth.BuildTasks.dll
Binary file not shown.
Binary file modified lib/DotNetOpenAuth.BuildTasks.pdb
Binary file not shown.
62 changes: 62 additions & 0 deletions src/DotNetOpenAuth.BuildTasks/AddProjectItems.cs
@@ -0,0 +1,62 @@
//-----------------------------------------------------------------------
// <copyright file="AddProjectItems.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.BuildEngine;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Collections;

public class AddProjectItems : Task {
/// <summary>
/// Gets or sets the projects to add items to.
/// </summary>
/// <value>The projects.</value>
[Required]
public ITaskItem[] Projects { get; set; }

/// <summary>
/// Gets or sets the items to add to each project.
/// </summary>
/// <value>The items.</value>
/// <remarks>
/// Use the metadata "ItemType" on each item to specify the item type to use for the new
/// project item. If the metadata is absent, "None" is used as the item type.
/// </remarks>
[Required]
public ITaskItem[] Items { get; set; }

/// <summary>
/// Executes this instance.
/// </summary>
public override bool Execute() {
foreach (var projectTaskItem in this.Projects) {
var project = new Project();
project.Load(projectTaskItem.ItemSpec);

foreach (var projectItem in this.Items) {
string itemType = projectItem.GetMetadata("ItemType");
if (string.IsNullOrEmpty(itemType)) {
itemType = "None";
}
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);
}
}

project.Save(projectTaskItem.ItemSpec);
}

return !this.Log.HasLoggedErrors;
}
}
}
47 changes: 47 additions & 0 deletions src/DotNetOpenAuth.BuildTasks/ChangeAssemblyReference.cs
@@ -0,0 +1,47 @@
using System;
using System.Linq;
using System.IO;
using System.Xml;

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Build.BuildEngine;

namespace DotNetOpenAuth.BuildTasks {
/// <summary>
/// Replaces Reference items HintPaths in a set of projects.
/// </summary>
public class ChangeAssemblyReference : Task {
/// <summary>
/// The projects to alter.
/// </summary>
[Required]
public ITaskItem[] Projects { get; set; }
/// <summary>
/// The project reference to remove.
/// </summary>
[Required]
public string OldReference { get; set; }
/// <summary>
/// The assembly reference to add.
/// </summary>
[Required]
public string NewReference { get; set; }

const string msbuildNamespace = "http://schemas.microsoft.com/developer/msbuild/2003";
public override bool Execute() {
foreach (var project in Projects) {
Project doc = new Project();
doc.Load(project.ItemSpec);

var reference = doc.GetEvaluatedItemsByName("Reference").OfType<BuildItem>().
Where(item => string.Equals(item.GetMetadata("HintPath"), OldReference, StringComparison.OrdinalIgnoreCase)).Single();
reference.SetMetadata("HintPath", NewReference);

doc.Save(project.ItemSpec);
}

return true;
}
}
}
@@ -0,0 +1,52 @@
using System;
using System.Linq;
using System.IO;
using System.Xml;

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 {
/// <summary>
/// The projects to alter.
/// </summary>
[Required]
public ITaskItem[] Projects { get; set; }
/// <summary>
/// The project reference to remove.
/// </summary>
[Required]
public string ProjectReference { get; set; }
/// <summary>
/// The assembly reference to add.
/// </summary>
[Required]
public string Reference { 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);

Project doc = new Project();
doc.Load(project.ItemSpec, ProjectLoadSettings.IgnoreMissingImports);

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);

doc.Save(project.ItemSpec);
}

return true;
}
}
}
30 changes: 30 additions & 0 deletions src/DotNetOpenAuth.BuildTasks/CheckAdminRights.cs
@@ -0,0 +1,30 @@
//-----------------------------------------------------------------------
// <copyright file="CheckAdminRights.cs" company="Andrew Arnott">
// Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.BuildTasks {
using System.Security.Principal;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

public class CheckAdminRights : Task {
/// <summary>
/// Gets or sets a value indicating whether this process has elevated permissions.
/// </summary>
[Output]
public bool IsElevated { get; set; }

/// <summary>
/// Executes this instance.
/// </summary>
public override bool Execute() {
WindowsIdentity id = WindowsIdentity.GetCurrent();
WindowsPrincipal p = new WindowsPrincipal(id);
this.IsElevated = p.IsInRole(WindowsBuiltInRole.Administrator);

return true;
}
}
}
112 changes: 112 additions & 0 deletions src/DotNetOpenAuth.BuildTasks/CompareFiles.cs
@@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System.IO;

namespace DotNetOpenAuth.BuildTasks {
public class CompareFiles : Task {
/// <summary>
/// One set of items to compare.
/// </summary>
[Required]
public ITaskItem[] OriginalItems { get; set; }

/// <summary>
/// The other set of items to compare.
/// </summary>
[Required]
public ITaskItem[] NewItems { get; set; }

/// <summary>
/// Gets whether the items lists contain items that are identical going down the list.
/// </summary>
[Output]
public bool AreSame { get; private set; }

/// <summary>
/// Same as <see cref="AreSame"/>, but opposite.
/// </summary>
[Output]
public bool AreChanged { get { return !AreSame; } }

public override bool Execute() {
AreSame = AreFilesIdentical();
return true;
}

private bool AreFilesIdentical() {
if (OriginalItems.Length != NewItems.Length) {
return false;
}

for (int i = 0; i < OriginalItems.Length; i++) {
if (!IsContentOfFilesTheSame(OriginalItems[i].ItemSpec, NewItems[i].ItemSpec)) {
return false;
}
}

return true;
}

private bool IsContentOfFilesTheSame(string file1, string file2) {
// If exactly one file is missing, that's different.
if (File.Exists(file1) ^ File.Exists(file2)) return false;
// If both are missing, that's the same.
if (!File.Exists(file1)) return true;
// If both are present, we need to do a content comparison.
using (FileStream fileStream1 = File.OpenRead(file1)) {
using (FileStream fileStream2 = File.OpenRead(file2)) {
if (fileStream1.Length != fileStream2.Length) return false;
byte[] buffer1 = new byte[4096];
byte[] buffer2 = new byte[buffer1.Length];
int bytesRead;
do {
bytesRead = fileStream1.Read(buffer1, 0, buffer1.Length);
if (fileStream2.Read(buffer2, 0, buffer2.Length) != bytesRead) {
// We should never get here since we compared file lengths, but
// this is a sanity check.
return false;
}
for (int i = 0; i < bytesRead; i++) {
if (buffer1[i] != buffer2[i]) {
return false;
}
}
} while (bytesRead == buffer1.Length);
}
}

return true;
}

/// <summary>
/// Tests whether a file is up to date with respect to another,
/// based on existence, last write time and file size.
/// </summary>
/// <param name="sourcePath">The source path.</param>
/// <param name="destPath">The dest path.</param>
/// <returns><c>true</c> if the files are the same; <c>false</c> if the files are different</returns>
internal static bool FastFileEqualityCheck(string sourcePath, string destPath) {
FileInfo sourceInfo = new FileInfo(sourcePath);
FileInfo destInfo = new FileInfo(destPath);

if (sourceInfo.Exists ^ destInfo.Exists) {
// Either the source file or the destination file is missing.
return false;
}

if (!sourceInfo.Exists) {
// Neither file exists.
return true;
}

// We'll say the files are the same if their modification date and length are the same.
return
sourceInfo.LastWriteTimeUtc == destInfo.LastWriteTimeUtc &&
sourceInfo.Length == destInfo.Length;
}
}
}

0 comments on commit 8c93cbf

Please sign in to comment.