Skip to content

Instantly share code, notes, and snippets.

@kg
Created April 3, 2022 10:43
Show Gist options
  • Save kg/832ec01b6adeb00d551191fcc1fa86d4 to your computer and use it in GitHub Desktop.
Save kg/832ec01b6adeb00d551191fcc1fa86d4 to your computer and use it in GitHub Desktop.
Automatically compile game scripts as part of visual studio build
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<ProjectGuid>{29214583-65ED-44D0-8DC6-F57539C6BC8E}</ProjectGuid>
<OutputType>Library</OutputType>
<AssemblyName>HeavenLens.Scripts</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<LangVersion>latest</LangVersion>
<Deterministic>true</Deterministic>
<OutputPath>bin\</OutputPath>
<IntermediateOutputPath>obj\</IntermediateOutputPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<!-- if a script changes this will invalidate our output and trigger a build -->
<Content Include="..\Client\Scripts\**\*.hl;..\Client\Scripts\**\*.hlh" />
<!-- this file is used to track whether a previous build failed, so we will build again -->
<Content Include="obj\BuildOk.txt" />
<!-- when the client is built this file will change, so that scripts are rebuilt -->
<Content Include="..\Client\obj\BuildSentinel.txt" />
</ItemGroup>
<!-- we would normally ProjectReference the script compiler here, but that would force it to rebuild any time we want to build scripts. Project dependencies in the sln do this too. So we just... rely on the project order in the sln to get this right. oh well -->
<Import Project="..\PathAttributes.targets" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="CompileScripts" BeforeTargets="Build">
<Message Importance="High" Text="Compiling scripts in configuration $(Configuration)" />
<Exec Command="$(SolutionDir)\Client\bin\$(Configuration)\HeavenLens.exe --compilescripts" />
<OnError ExecuteTargets="OnCompileFailed" />
</Target>
<Target Name="OnCompileFailed">
<!-- modifying BuildOk.txt will ensure that a project dependency has always changed, so it gets built again next time -->
<WriteLinesToFile File="obj\BuildOk.txt" Lines="build previously failed" Overwrite="true" />
</Target>
<Target Name="OnCompileOk" AfterTargets="CompileScripts">
<!-- because we have a project dependency on BuildOk.txt, if it is unchanged the project will not rebuild. the Overwrite + WriteOnlyWhenDifferent combo means that the file will not be modified if it's already there, so the project will not re-build the next time unless its other dependencies (scripts) have changed -->
<!-- unfortunately this will still produce a spurious rebuild the first time after a successful build, but that's not a big deal -->
<WriteLinesToFile File="obj\BuildOk.txt" Lines="hey msbuild: project is up to date" WriteOnlyWhenDifferent="true" Overwrite="true" />
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment