Skip to content

Instantly share code, notes, and snippets.

@chilversc
Last active January 21, 2016 12:28
Show Gist options
  • Save chilversc/36669492c61b91b52b3b to your computer and use it in GitHub Desktop.
Save chilversc/36669492c61b91b52b3b to your computer and use it in GitHub Desktop.
Target to remove old typescript output files after msbuild. This will remove all javascript files found in the typescript output directory that were not generated by typescript.
<Target Name="TypeScriptCleanOldOutput" DependsOnTargets="PreComputeCompileTypeScript" AfterTargets="Clean;Build" Condition=" '$(TypeScriptOutDir)' != '' ">
<!--
Assumes the TypeScriptOutDir only contains generated code.
If a TypeScript file is deleted from the project the old js file
will remain in the output folder.
Find JS files in the output folder that were not generated by
TypeScript and delete them.
-->
<ConvertToAbsolutePath Paths="$(TypeScriptOutDir)">
<Output TaskParameter="AbsolutePaths" PropertyName="TypeScriptOutDirAbsolute"/>
</ConvertToAbsolutePath>
<PropertyGroup>
<TypeScriptOutDirAbsolute Condition="!HasTrailingSlash('$(TypeScriptOutDirAbsolute)')">$(TypeScriptOutDirAbsolute)\</TypeScriptOutDirAbsolute>
</PropertyGroup>
<ItemGroup>
<OldGeneratedJavascript Include="$(TypeScriptOutDirAbsolute)**\*.js" Exclude="@(GeneratedJavascript)" />
<OldGeneratedJavascript Include="$(TypeScriptOutDirAbsolute)**\*.js.map" Exclude="@(GeneratedJavascript)" />
</ItemGroup>
<Delete Files="@(OldGeneratedJavascript)" />
</Target>
@chilversc
Copy link
Author

[System.IO.Path]::GetFullPath can error in VS2015 because the current working directory in msbuild was C:\Windows\System32. GetFullPath resolves relative to the working directory rather than the project directory. Using ConvertToAbsolutePath instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment