Created
December 1, 2013 21:16
-
-
Save iskeld/7740896 to your computer and use it in GitHub Desktop.
A simple MSBuild Task to modify the project file properties.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Linq; | |
using Microsoft.Build.Construction; | |
using Microsoft.Build.Framework; | |
using Microsoft.Build.Utilities; | |
namespace EldSharp.Scripts.MsBuild | |
{ | |
public sealed class ProjectPropertyWriterTask : Task | |
{ | |
[Required] | |
public string ProjectFile { get; set; } | |
[Required] | |
public string Property { get; set; } | |
[Required] | |
public string Value { get; set; } | |
public override bool Execute() | |
{ | |
if (!File.Exists(ProjectFile)) | |
{ | |
Log.LogError("Project file {0} does not exist", ProjectFile); | |
return false; | |
} | |
ProjectRootElement root = ProjectRootElement.Open(ProjectFile); | |
if (root == null) | |
{ | |
Log.LogError("Invalid project file: {0}", ProjectFile); | |
return false; | |
} | |
var foundProperties = root.Properties.Where(p => p.Name == Property); | |
foreach (ProjectPropertyElement property in foundProperties) | |
{ | |
property.Value = Value; | |
} | |
root.Save(); | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment