Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/try to separate post build events #11

Merged
merged 4 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions Common/Common.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<PropertyGroup Label="Vcpkg">
<VcpkgEnableManifest>true</VcpkgEnableManifest>
</PropertyGroup>
<Import Project="$(SolutionDir)PostBuild.targets" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
Expand All @@ -81,13 +82,7 @@
</Command>
</PreBuildEvent>
<PostBuildEvent>
<Command>call "$(DevEnvDir)..\Tools\vsdevcmd.bat" -no_logo
set PostBuildTool="$(SolutionDir)PostBuildTool\bin\x86\$(Configuration)\net48\PostBuildTool"
cl PreparationScript.h /P /EP /u /nologo
if %errorlevel% neq 0 exit /b %errorlevel%
copy PreparationScript.i PreparationScript.lua /Y
if %errorlevel% neq 0 exit /b %errorlevel%
%PostBuildTool% FormatLuaScript PreparationScript.lua</Command>
<Command>PostBuildScript.bat</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand Down Expand Up @@ -118,13 +113,7 @@ copy PreparationScript.i PreparationScript.lua /Y
</Command>
</PreBuildEvent>
<PostBuildEvent>
<Command>call "$(DevEnvDir)..\Tools\vsdevcmd.bat" -no_logo
set PostBuildTool="$(SolutionDir)PostBuildTool\bin\x86\$(Configuration)\net48\PostBuildTool"
cl PreparationScript.h /P /EP /u /nologo
if %errorlevel% neq 0 exit /b %errorlevel%
copy PreparationScript.i PreparationScript.lua /Y
if %errorlevel% neq 0 exit /b %errorlevel%
%PostBuildTool% FormatLuaScript PreparationScript.lua</Command>
<Command>PostBuildScript.bat</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down Expand Up @@ -167,6 +156,7 @@ copy PreparationScript.i PreparationScript.lua /Y
<ClCompile Include="Variables.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="PostBuildScript.bat" />
<None Include="PreparationScript.lua" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
1 change: 1 addition & 0 deletions Common/Common.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,6 @@
<None Include="PreparationScript.lua">
<Filter>Header Files\LuaScript</Filter>
</None>
<None Include="PostBuildScript.bat" />
</ItemGroup>
</Project>
9 changes: 9 additions & 0 deletions Common/PostBuildScript.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
call "%DevEnvDir%..\Tools\vsdevcmd.bat" -no_logo
set PostBuildTool="%SolutionDir%PostBuildTool\bin\x86\%Configuration%\net48\PostBuildTool"
rem Process the header file into a Lua script file
cl PreparationScript.h /P /EP /u /nologo
if %errorlevel% neq 0 exit /b %errorlevel%
rem Format and commit only if there is any content-change
copy PreparationScript.i _PreparationScript.lua /Y
if %errorlevel% neq 0 exit /b %errorlevel%
%PostBuildTool% FormatLuaScript _PreparationScript.lua PreparationScript.lua
28 changes: 28 additions & 0 deletions PostBuild.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project>
<UsingTask
TaskName="SetEnvironmentVariable"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<Name ParameterType="System.String" Required="true" />
<Value ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Code Type="Fragment" Language="cs">
<![CDATA[
Environment.SetEnvironmentVariable(Name, Value);
]]>
</Code>
</Task>
</UsingTask>

<Target Name="PostBuildPreparation" BeforeTargets="PostBuildEvent">
<SetEnvironmentVariable Name="DevEnvDir" Value="$(DevEnvDir)" />
<SetEnvironmentVariable Name="SolutionDir" Value="$(SolutionDir)" />
<SetEnvironmentVariable Name="Configuration" Value="$(Configuration)" />
<SetEnvironmentVariable Name="ProjectDir" Value="$(ProjectDir)" />
<SetEnvironmentVariable Name="OutDir" Value="$(OutDir)" />
</Target>

</Project>
18 changes: 15 additions & 3 deletions PostBuildTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ static void FormatLuaScript(string[] args)
Console.WriteLine("This cmdline relies on StyLua which doesn't have a 32-bit executable file, skip Lua script formatting.");
return;
}
var targetPath = args.ElementAtOrDefault(0);
var inputPath = args.ElementAtOrDefault(0);
var outputPath = args.ElementAtOrDefault(1);
var styluaDirPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var styluaPath = Path.Combine(styluaDirPath, "stylua.exe");
if (!File.Exists(styluaPath))
Expand All @@ -133,9 +134,20 @@ static void FormatLuaScript(string[] args)
using var tmpZipStream = new ZipArchive(tmpStream);
tmpZipStream.ExtractToDirectory(styluaDirPath);
}
Console.WriteLine($"Format lua script: {targetPath}");
Process.Start(new ProcessStartInfo(styluaPath, $"--column-width 999 --indent-type Spaces \"{targetPath}\"") {
Console.WriteLine($"Format lua script: {outputPath}");
Process.Start(new ProcessStartInfo(styluaPath, $"--column-width 999 --indent-type Spaces \"{inputPath}\"") {
UseShellExecute = false,
}).WaitForExit();
if (File.Exists(outputPath))
{
var newContent = File.ReadAllText(inputPath);
var oldContent = File.ReadAllText(outputPath);
if (newContent == oldContent)
{
Console.WriteLine($"{outputPath} has no change, keep modified date.");
return;
}
}
File.Copy(inputPath, outputPath, true);
}
}
1 change: 1 addition & 0 deletions ThMouseX.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ThMouseXGUI", "ThMouseXGUI\
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B337E9DD-BB1B-4851-B933-05C75CB5D9AC}"
ProjectSection(SolutionItems) = preProject
PostBuild.targets = PostBuild.targets
vcpkg.json = vcpkg.json
EndProjectSection
EndProject
Expand Down
5 changes: 5 additions & 0 deletions ThMouseX/PostBuildScript.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
call "%DevEnvDir%..\Tools\vsdevcmd.bat" -no_logo
set ClientManifestPath="%SolutionDir%ThMouseXServer\client.manifest"
set ComClientPath="%OutDir%ThMouseX.dll;#2"
rem Embed the manifest file
mt -nologo -manifest %ClientManifestPath% -outputresource:%ComClientPath%
16 changes: 7 additions & 9 deletions ThMouseX/ThMouseX.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
</PropertyGroup>
<PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<VcpkgUseStatic>true</VcpkgUseStatic>
</PropertyGroup>
</PropertyGroup>
<Import Project="$(SolutionDir)PostBuild.targets" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
Expand All @@ -79,10 +80,7 @@
</ModuleDefinitionFile>
</Link>
<PostBuildEvent>
<Command>call "$(DevEnvDir)..\Tools\vsdevcmd.bat" -no_logo
set ClientManifestPath="$(SolutionDir)ThMouseXServer\client.manifest"
set ComClientPath="$(OutDir)ThMouseX.dll;#2"
mt -nologo -manifest %ClientManifestPath% -outputresource:%ComClientPath%</Command>
<Command>PostBuildScript.bat</Command>
</PostBuildEvent>
<PreBuildEvent>
<Command>
Expand Down Expand Up @@ -118,10 +116,7 @@ mt -nologo -manifest %ClientManifestPath% -outputresource:%ComClientPath%</Comma
</ModuleDefinitionFile>
</Link>
<PostBuildEvent>
<Command>call "$(DevEnvDir)..\Tools\vsdevcmd.bat" -no_logo
set ClientManifestPath="$(SolutionDir)ThMouseXServer\client.manifest"
set ComClientPath="$(OutDir)ThMouseX.dll;#2"
mt -nologo -manifest %ClientManifestPath% -outputresource:%ComClientPath%</Command>
<Command>PostBuildScript.bat</Command>
</PostBuildEvent>
<PreBuildEvent>
<Command>
Expand Down Expand Up @@ -236,6 +231,9 @@ mt -nologo -manifest %ClientManifestPath% -outputresource:%ComClientPath%</Comma
<ItemGroup>
<ResourceCompile Include="ThMouseX.rc" />
</ItemGroup>
<ItemGroup>
<None Include="PostBuildScript.bat" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
3 changes: 3 additions & 0 deletions ThMouseX/ThMouseX.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,7 @@
<ItemGroup>
<ResourceCompile Include="ThMouseX.rc" />
</ItemGroup>
<ItemGroup>
<None Include="PostBuildScript.bat" />
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions ThMouseXGUI/PostBuildScript.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
call "%DevEnvDir%..\Tools\vsdevcmd.bat" -no_logo
rem Copy all the C++ output files into this
xcopy "%SolutionDir%%Configuration%\*" "%ProjectDir%%OutDir%" /y /s /d
if %errorlevel% neq 0 exit /b %errorlevel%
rem Copy all the files of NeoLuaBootstrap into this
xcopy "%SolutionDir%NeoLuaBootstrap\bin\x86\%Configuration%\net48\*" "%ProjectDir%%OutDir%" /y /s /d
if %errorlevel% neq 0 exit /b %errorlevel%
set ServerManifestPath="%SolutionDir%ThMouseXServer\server.manifest"
set ComServerPath="%ProjectDir%%OutDir%ThMouseXGUI.exe;#1"
rem Embed the manifest file
mt -nologo -manifest %ServerManifestPath% -outputresource:%ComServerPath%
46 changes: 21 additions & 25 deletions ThMouseXGUI/ThMouseXGUI.csproj
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net48</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>12</LangVersion>
<ApplicationIcon>ThMouseX.ico</ApplicationIcon>
<Prefer32Bit>true</Prefer32Bit>
<DisableFastUpToDateCheck>True</DisableFastUpToDateCheck>
<Version>3.0.0</Version>
<Platforms>x86</Platforms>
</PropertyGroup>
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net48</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>12</LangVersion>
<ApplicationIcon>ThMouseX.ico</ApplicationIcon>
<Prefer32Bit>true</Prefer32Bit>
<DisableFastUpToDateCheck>True</DisableFastUpToDateCheck>
<Version>3.0.0</Version>
<Platforms>x86</Platforms>
</PropertyGroup>

<ItemGroup>
<None Remove=".gitignore" />
</ItemGroup>
<ItemGroup>
<None Remove=".gitignore" />
<Content Include="ThMouseX.ico" />
<ProjectReference Include="..\ThMouseXServer\ThMouseXServer.csproj" />
</ItemGroup>

<ItemGroup>
<Content Include="ThMouseX.ico" />
</ItemGroup>
<Import Project="$(SolutionDir)PostBuild.targets" />

<ItemGroup>
<ProjectReference Include="..\ThMouseXServer\ThMouseXServer.csproj" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="call &quot;$(DevEnvDir)..\Tools\vsdevcmd.bat&quot; -no_logo&#xD;&#xA;xcopy &quot;$(SolutionDir)$(Configuration)\*&quot; &quot;$(ProjectDir)$(OutDir)&quot; /y /s /d&#xD;&#xA; if %25errorlevel%25 neq 0 exit /b %25errorlevel%25&#xD;&#xA;xcopy &quot;$(SolutionDir)NeoLuaBootstrap\bin\x86\$(Configuration)\net48\*&quot; &quot;$(ProjectDir)$(OutDir)&quot; /y /s /d&#xD;&#xA; if %25errorlevel%25 neq 0 exit /b %25errorlevel%25&#xD;&#xA;set ServerManifestPath=&quot;$(SolutionDir)ThMouseXServer\server.manifest&quot;&#xD;&#xA;set ComServerPath=&quot;$(ProjectDir)$(OutDir)ThMouseXGUI.exe;#1&quot;&#xD;&#xA;mt -nologo -manifest %25ServerManifestPath%25 -outputresource:%25ComServerPath%25" />
</Target>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="PostBuildScript.bat" />
</Target>

</Project>
44 changes: 44 additions & 0 deletions ThMouseXServer/PostBuildScript.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
call "%DevEnvDir%..\Tools\vsdevcmd.bat" -no_logo
set PostBuildTool="%SolutionDir%PostBuildTool\bin\x86\%Configuration%\net48\PostBuildTool"
set ServerName=ThMouseXServer
set ClientName=ThMouseX
set TypeLibName=ThMouseXServer.dll
set Architecture=*
set ServerPath="%ProjectDir%%OutDir%ThMouseXServer.dll"
set ServerManifestPath=server.manifest
set ClientManifestPath=client.manifest
set TlbPath=server.tlb
rem Make a manifest file for the Server
%PostBuildTool% GenerateComServerManifest %ServerName% %TypeLibName% %Architecture% %ServerPath% %ServerManifestPath%
if %errorlevel% neq 0 exit /b %errorlevel%
rem Make a manifest file for the client
%PostBuildTool% GenerateComServerManifest %ClientName% %TypeLibName% %Architecture% %ServerPath% %ClientManifestPath%
if %errorlevel% neq 0 exit /b %errorlevel%
rem Generate a TypeLib file
tlbexp /nologo /win32 %ServerPath% /out:%TlbPath%
if %errorlevel% neq 0 exit /b %errorlevel%
rem Embed the Typelib file into the Server dll file
%PostBuildTool% ImportResource %ServerPath% %TlbPath% typelib #1
if %errorlevel% neq 0 exit /b %errorlevel%
if not exist AutoGenerated mkdir AutoGenerated
cd AutoGenerated
rem Update the tlh and tli only if their content changes
ren server.tlh old_server.tlh > nul 2> nul
ren server.tli old_server.tli > nul 2> nul
cl -nologo "%ProjectDir%GenerateComHeaders.cpp"
fc server.tlh old_server.tlh > nul 2> nul
if %errorlevel% neq 0 (
rem There are changes
del old_server.tlh > nul 2> nul
del old_server.tli > nul 2> nul
) else (
rem No change
del server.tlh
del server.tli
ren old_server.tlh server.tlh > nul
ren old_server.tli server.tli > nul
)
rem Remove trash
del GenerateComHeaders.exe
del GenerateComHeaders.obj
cd ..
55 changes: 27 additions & 28 deletions ThMouseXServer/ThMouseXServer.csproj
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>x86</Platforms>
</PropertyGroup>

<ItemGroup>
<None Remove=".gitignore" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="NativeMethods.json" />
<AdditionalFiles Include="NativeMethods.txt" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>x86</Platforms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>
<ItemGroup>
<None Remove=".gitignore" />
<AdditionalFiles Include="NativeMethods.json" />
<AdditionalFiles Include="NativeMethods.txt" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="call &quot;$(DevEnvDir)..\Tools\vsdevcmd.bat&quot; -no_logo&#xD;&#xA;set PostBuildTool=&quot;$(SolutionDir)PostBuildTool\bin\x86\$(Configuration)\net48\PostBuildTool&quot;&#xD;&#xA;set ServerName=ThMouseXServer&#xD;&#xA;set ClientName=ThMouseX&#xD;&#xA;set TypeLibName=ThMouseXServer.dll&#xD;&#xA;set Architecture=*&#xD;&#xA;set ServerPath=&quot;$(ProjectDir)$(OutDir)ThMouseXServer.dll&quot;&#xD;&#xA;set ServerManifestPath=server.manifest&#xD;&#xA;set ClientManifestPath=client.manifest&#xD;&#xA;set TlbPath=server.tlb&#xD;&#xA;%25PostBuildTool%25 GenerateComServerManifest %25ServerName%25 %25TypeLibName%25 %25Architecture%25 %25ServerPath%25 %25ServerManifestPath%25&#xD;&#xA; if %25errorlevel%25 neq 0 exit /b %25errorlevel%25&#xD;&#xA;%25PostBuildTool%25 GenerateComServerManifest %25ClientName%25 %25TypeLibName%25 %25Architecture%25 %25ServerPath%25 %25ClientManifestPath%25&#xD;&#xA; if %25errorlevel%25 neq 0 exit /b %25errorlevel%25&#xD;&#xA;tlbexp /nologo /win32 %25ServerPath%25 /out:%25TlbPath%25&#xD;&#xA; if %25errorlevel%25 neq 0 exit /b %25errorlevel%25&#xD;&#xA;%25PostBuildTool%25 ImportResource %25ServerPath%25 %25TlbPath%25 typelib #1&#xD;&#xA; if %25errorlevel%25 neq 0 exit /b %25errorlevel%25&#xD;&#xA;if not exist AutoGenerated mkdir AutoGenerated&#xD;&#xA;cd AutoGenerated&#xD;&#xA;cl -nologo ../GenerateComHeaders.cpp&#xD;&#xA;cd .." />
</Target>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>

<Import Project="$(SolutionDir)PostBuild.targets" />

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="PostBuildScript.bat" />
</Target>

</Project>