Update project alteration task to support references from "lib"

In the Orchard.Web.csproj file for WebPI, we need to reference all
assemblies coming from our "lib" folder to point to the "bin" folder
of the deployed app.

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-10-22 11:44:34 -07:00
parent 5803922b49
commit 8548de19d7

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Xml.Linq; using System.Xml.Linq;
@@ -17,11 +18,14 @@ namespace MSBuild.Orchard.Tasks {
public override bool Execute() { public override bool Execute() {
//System.Diagnostics.Debugger.Break();
Log.LogMessage("Altering \"{0}\"", ProjectFileName); Log.LogMessage("Altering \"{0}\"", ProjectFileName);
var context = new Context(this); var context = new Context(this);
if (context.LoadProject() && if (context.LoadProject() &&
context.ChangeProjectReferencesToFileReferences() && context.ChangeProjectReferencesToFileReferences() &&
context.ChangeLibraryReferencesToFileReferences() &&
context.FindExtraFiles() && context.FindExtraFiles() &&
context.AddContentFiles() && context.AddContentFiles() &&
context.SaveProject()) { context.SaveProject()) {
@@ -88,12 +92,17 @@ namespace MSBuild.Orchard.Tasks {
} }
foreach (var projectReferenceName in projectReferences.Elements(Name)) { foreach (var projectReferenceName in projectReferences.Elements(Name)) {
string oldHintPath = (projectReferenceName.Parent.Element(HintPath) ?? new XElement(HintPath)).Value;
string newHintPath = string.Format("bin\\{0}.dll", (string)projectReferenceName);
var reference = new XElement( var reference = new XElement(
Reference, Reference,
new XAttribute(Include, (string)projectReferenceName), new XAttribute(Include, (string)projectReferenceName),
new XElement(SpecificVersion, "False"), new XElement(SpecificVersion, "False"),
new XElement(HintPath, string.Format("bin\\{0}.dll", (string)projectReferenceName))); new XElement(HintPath, newHintPath));
referenceItemGroup.Add(reference); referenceItemGroup.Add(reference);
_task.Log.LogMessage("Project reference \"{0}\": HintPath changed from \"{1}\" to \"{2}\"",
(string)projectReferenceName, oldHintPath, newHintPath);
} }
foreach (var projectReference in projectReferences.ToArray()) { foreach (var projectReference in projectReferences.ToArray()) {
@@ -103,6 +112,54 @@ namespace MSBuild.Orchard.Tasks {
return true; return true;
} }
public bool ChangeLibraryReferencesToFileReferences() {
var libraryReferences = _document
.Elements(Project)
.Elements(ItemGroup)
.Elements(Reference);
var referenceItemGroup = _document
.Elements(Project)
.Elements(ItemGroup)
.FirstOrDefault(elt => elt.Elements(Reference).Any());
if (referenceItemGroup == null) {
referenceItemGroup = new XElement(ItemGroup);
_document.Root.Add(referenceItemGroup);
}
List<XElement> elementsToRemove = new List<XElement>();
foreach (var hintPathElement in libraryReferences.Elements(HintPath)) {
string oldHintPath = hintPathElement.Value;
if (!oldHintPath.StartsWith("..\\..\\lib\\"))
continue;
elementsToRemove.Add(hintPathElement.Parent);
// Need to change the hint path from
// ..\\..\\lib\\<libraryfolder>\\<AssemblyName>.dll
// to
// bin\\<AssemblyName>.dll
string assemblyFileName = Path.GetFileName(oldHintPath);
string newHintPath = Path.Combine("bin", assemblyFileName);
var reference = new XElement(
Reference,
new XAttribute(Include, hintPathElement.Parent.Attribute(Include).Value),
new XElement(SpecificVersion, "False"),
new XElement(HintPath, newHintPath));
referenceItemGroup.Add(reference);
_task.Log.LogMessage("Assembly (library) Reference \"{0}\": HintPath changed from \"{1}\" to \"{2}\"",
hintPathElement.Parent.Attribute(Include).Value, oldHintPath, newHintPath);
}
foreach (var reference in elementsToRemove) {
reference.Remove();
}
return true;
}
public bool FindExtraFiles() { public bool FindExtraFiles() {
var extraFiles = _document var extraFiles = _document
.Elements(Project) .Elements(Project)