VanDalkvist
1/28/2016 - 12:41 PM

node.targets

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <NodeFolderPath>.node</NodeFolderPath>
    <NodeExePath Condition=" '$(NodeExePath)' == '' ">$(NodeFolderPath)\Node.exe</NodeExePath>
    <NpmFolderPath Condition=" '$(NpmFolderPath)' == '' ">$(NodeFolderPath)\node_modules\npm</NpmFolderPath>
    <NpmZipPath Condition=" '$(NpmZipPath)' == '' ">$(NodeFolderPath)\npm.zip</NpmZipPath>
    <NpmCommand>.node\npm.cmd</NpmCommand>
    <BowerCommand>.node\node.exe  node_modules\bower\bin\bower</BowerCommand>
    <GulpCommand>.node\node.exe  node_modules\gulp\bin\gulp.js</GulpCommand>
    <Action Condition=" '$(Action)' == '' ">build</Action>
    <App Condition=" '$(App)' == '' ">operator</App>
	<GulpBuildTask>ms-build</GulpBuildTask>
	<GulpServeTask>ms-serve</GulpServeTask>
	<GulpCleanTask>ms-clean</GulpCleanTask>
  </PropertyGroup>
  <!-- define Release mode -->
  <PropertyGroup>
    <GulpTask>mindscan</GulpTask>
  </PropertyGroup>
  <PropertyGroup>
    <DeliveryDir Condition="'$(OutDir)' != '$(OutputPath)'">$(OutputPath)\..\_NgSite\$(MSBuildProjectName)</DeliveryDir>
  </PropertyGroup>
  <PropertyGroup>
    <RestorePackages>
      PrintParameters;
	    EnsureNode;
      RestorNodePackages;
      RestoreBowerPackages;
      ServeGulpTask;
      BuildGulpTask;
	    CleanDeliveryOutputDir;
	    CopyDelivery;
    </RestorePackages>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
    <OutputPath>bin\Debug\</OutputPath>
  </PropertyGroup>
  <Target Name="BuildSite" DependsOnTargets="$(RestorePackages)" BeforeTargets="Build">
  </Target>
  <Target Name="PrintParameters" >
    <Message Text="App: $(App)" />
    <Message Text="Configuration: $(Configuration)" />
    <Message Text="Action: $(Action)" />
    <Message Text="OutDir: $(OutDir)" />
    <Message Text="OutputPath: $(OutputPath)" />
    <Message Text="DeliveryDir: $(DeliveryDir)" />
  </Target>
  <Target Name="EnsureNode">
	<MakeDir Directories="$(NodeFolderPath)" Condition="!Exists('$(NodeFolderPath)')" />
    <Message Text="Downloading node..." Importance="High" Condition="!Exists('$(NodeExePath)')"/>
	<DownloadNode OutputFilename="$(NodeExePath)" Condition="!Exists('$(NodeExePath)')" />
    <DownloadNpm OutputFilename="$(NpmZipPath)" Condition="!Exists('$(NpmFolderPath)')" />
  </Target>
  <Target Name="RestorNodePackages" Inputs="package.json" Outputs="node_modules\touch.info">
    <Message Text="Restoring node packages..." Importance="High" />
    <Exec Command="$(NpmCommand) install --ignore-script" />
	<Message Text="Rebuilding node-sass package..." Importance="High" />
	<Exec Command="$(NpmCommand) rebuild node-sass" />
    <Message Text="Restored node packages." Importance="High" />
    <Touch Files="node_modules\touch.info" AlwaysCreate="True" />
  </Target>
  <Target Name="RestoreBowerPackages" Inputs="bower.json" Outputs="bower_components\touch.info">
    <Message Text="Restoring bower packages..." Importance="High" />
    <Exec Command="$(BowerCommand) prune" />
    <Exec Command="$(BowerCommand) install" />
    <Message Text="Restored bower packages." Importance="High" />
    <Touch Files="bower_components\touch.info" AlwaysCreate="True" />
  </Target>
  <Target Name="ServeGulpTask" Condition="'$(Action)' == 'serve'">
    <Message Text="Running gulp task: '$(Action)'." Importance="High" />
    <Exec Command="$(GulpCommand) $(GulpCleanTask)" />
	<Exec Command="$(GulpCommand) $(GulpServeTask) --environment local" />
    <Message Text="Finished gulp task: '$(Action)'." Importance="High" />
  </Target>
  <Target Name="BuildGulpTask" Condition="'$(Action)' == 'build' ">
    <Message Text="Running gulp task: '$(Action)'." Importance="High" />
    <Exec Command="$(GulpCommand) $(GulpCleanTask)" />
	<Exec Command="$(GulpCommand) $(GulpBuildTask) --environment prod" />
    <Message Text="Finished gulp task: '$(Action)'." Importance="High" />
  </Target>
  <Target Name="CleanDeliveryOutputDir" Condition="'$(Configuration)'=='Release'">
    <RemoveDir Directories="$(DeliveryDir)" Condition="'$(DeliveryDir)'=='$(OutputPath)\$(ProjectName)\' and Exists('$(DeliveryDir)')" />
  </Target>
  <Target Name="CopyDelivery" Condition="'$(OutDir)' != '$(OutputPath)' AND '$(Configuration)'=='Release'">
    <ItemGroup>
      <FilesToDelivery Include="dist\**\*.*" />
    </ItemGroup>
    <Copy SourceFiles="@(FilesToDelivery)" DestinationFolder="$(DeliveryDir)\%(RecursiveDir)" OverwriteReadOnlyFiles="True" Retries="10" RetryDelayMilliseconds="1000" />
  </Target>
  <UsingTask TaskName="DownloadNode" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
      <OutputFilename ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
      <Reference Include="System.Core" />
      <Using Namespace="System" />
      <Using Namespace="System.IO" />
      <Using Namespace="System.Net" />
      <Using Namespace="Microsoft.Build.Framework" />
      <Using Namespace="Microsoft.Build.Utilities" />
      <Code Type="Fragment" Language="cs">
                      <![CDATA[
                try {
                    OutputFilename = Path.GetFullPath(OutputFilename);

                    Log.LogMessage("Downloading 4.2.1 x64 version of Node.exe...");
                    WebClient webClient = new WebClient();
                    webClient.DownloadFile("https://nodejs.org/dist/latest-v5.x/win-x64/node.exe", OutputFilename);

                    return true;
                }
                catch (Exception ex) {
                    Log.LogErrorFromException(ex);
                    return false;
                }
            ]]></Code>
    </Task>
  </UsingTask>
  <UsingTask TaskName="DownloadNpm" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
      <OutputFilename ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
      <Reference Include="System.Core" />
      <Reference Include="System.IO.Compression.FileSystem" />
      <Using Namespace="System" />
      <Using Namespace="System.IO" />
      <Using Namespace="System.IO.Compression" />
      <Using Namespace="System.Net" />
      <Using Namespace="Microsoft.Build.Framework" />
      <Using Namespace="Microsoft.Build.Utilities" />
      <Code Type="Fragment" Language="cs"><![CDATA[
                try {
                    OutputFilename = Path.GetFullPath(OutputFilename);

                    Log.LogMessage("Downloading 1.4.9 version of npm...");
                    WebClient webClient = new WebClient();
                    webClient.DownloadFile("http://nodejs.org/dist/npm/npm-1.4.9.zip", OutputFilename);
                    var dir = Directory.GetParent(OutputFilename).FullName;
                    Log.LogMessage("Extracting npm.zip to " + dir);
                    ZipFile.ExtractToDirectory(OutputFilename, dir);
                    Log.LogMessage("Removing npm.zip to " + dir);
                    File.Delete(OutputFilename);
                    return true;
                }
                catch (Exception ex) {
                    Log.LogErrorFromException(ex);
                    return false;
                }
            ]]></Code>
    </Task>
  </UsingTask>
</Project>