Created
May 12, 2015 16:23
-
-
Save neremin/b3c2fe92d04388dcdde2 to your computer and use it in GitHub Desktop.
Provide means for invoking inline PS scripts from MSBuild
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
<?xml version="1.0" encoding="utf-8"?> | |
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<!-- USAGE EXAMPLE | |
<PropertyGroup> | |
<MyScript><![CDATA[ | |
function Foo() | |
{ | |
$targetFileName = args[0] | |
$targetPdbName = args[1] | |
$targetPath = args[2] | |
} | |
]]> | |
</MyScript> | |
</PropertyGroup> | |
<Target Name="AfterBuild"> | |
<InvokeScript Script="$(MyScript)" Function="Foo" Parameters="$(TargetFileName);$(TargetName).pdb;$(TargetPath)" /> | |
</Target> | |
--> | |
<!-- Based on http://bartdesmet.net/blogs/bart/archive/2008/02/16/invoking-powershell-scripts-from-msbuild.aspx --> | |
<UsingTask TaskName="InvokeScript" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll"> | |
<ParameterGroup> | |
<Script ParameterType="Microsoft.Build.Framework.ITaskItem" Required="true" /> | |
<Function ParameterType="System.String" Required="true" /> | |
<Parameters ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="false" /> | |
</ParameterGroup> | |
<Task> | |
<Reference Include="System.Management.Automation" /> | |
<Using Namespace="System.Text" /> | |
<Using Namespace="System.Management.Automation" /> | |
<Using Namespace="System.Management.Automation.Runspaces" /> | |
<Code Type="Fragment" Language="cs"> | |
<![CDATA[ | |
var runspaceConfig = RunspaceConfiguration.Create(); | |
using (var runspace = RunspaceFactory.CreateRunspace(runspaceConfig)) | |
{ | |
runspace.Open(); | |
var commandLine = new StringBuilder(); | |
commandLine.Append(Function + " "); | |
foreach (var parameter in Parameters) | |
{ | |
commandLine.AppendFormat("\"{0}\" ", parameter.ItemSpec); | |
} | |
using (var scriptInvoker = new RunspaceInvoke(runspace)) | |
{ | |
scriptInvoker.Invoke(Script.ItemSpec); | |
scriptInvoker.Invoke(commandLine.ToString()); | |
} | |
} | |
return true; | |
]]> | |
</Code> | |
</Task> | |
</UsingTask> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment