Skip to content

Commit 2bde6df

Browse files
authored
Add target to optionally run CodeGen before ReactNativeXaml build (#234)
This PR adds some new MSBuild properties and targets within ReactNativeXaml.vcxproj to support re-running the CodeGen before the build. By setting a property in their ExperimentalFeatures.props file: ```xml <RunReactNativeXamlCodeGenBeforeBuild>true</RunReactNativeXamlCodeGenBeforeBuild> ``` Customers can make sure the CodeGen tool runs before the native build. It's default to `false` since most customers don't need it. However, this will hopefully address microsoft/react-native-windows#10792, which was a hack to get the same effect. Note that customers can also customize how the CodeGen runs, in particular, by specifying a different `ReactNativeXamlTargetWinMD` file if desired.
1 parent 131eea6 commit 2bde6df

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Add target to optionally run CodeGen before ReactNativeXaml build",
4+
"packageName": "react-native-xaml",
5+
"email": "jthysell@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

package/windows/ExperimentalFeatures.props

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@
88
<WinUI2xVersion>2.6.0</WinUI2xVersion>
99
</PropertyGroup>
1010

11+
<PropertyGroup>
12+
<!-- Setting this to true here so we're sure to run the codegen when working within the RNX repo itself. -->
13+
<RunReactNativeXamlCodeGenBeforeBuild>true</RunReactNativeXamlCodeGenBeforeBuild>
14+
</PropertyGroup>
15+
1116
</Project>

package/windows/ReactNativeXaml/ReactNativeXaml.vcxproj

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,29 @@
252252
<Error Condition="!Exists('$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.Uwp.CppLib.props')" Text="$([System.String]::Format('$(ErrorText)', '$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.Uwp.CppLib.props'))" />
253253
<Error Condition="!Exists('$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.Uwp.CppLib.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.Uwp.CppLib.targets'))" />
254254
</Target>
255+
<PropertyGroup Label="ReactNativeXamlCodeGenProps">
256+
<RunReactNativeXamlCodeGenBeforeBuild Condition="$(RunReactNativeXamlCodeGenBeforeBuild)==''">false</RunReactNativeXamlCodeGenBeforeBuild>
257+
<ReactNativeXamlTargetWinMD Condition="$(ReactNativeXamlTargetWinMD)=='' And $(RnwUsesPackageReference)=='true' And Exists('$(NuGetPackageRoot)\$(WinUIPackageName)\$(WinUIPackageVersion)\lib\uap10.0\Microsoft.UI.Xaml.winmd')">$(NuGetPackageRoot)\$(WinUIPackageName)\$(WinUIPackageVersion)\lib\uap10.0\Microsoft.UI.Xaml.winmd</ReactNativeXamlTargetWinMD>
258+
<ReactNativeXamlTargetWinMD Condition="$(ReactNativeXamlTargetWinMD)=='' And $(RnwUsesPackageReference)!='true' And Exists('$(SolutionDir)\packages\$(WinUIPackageName).$(WinUIPackageVersion)\lib\uap10.0\Microsoft.UI.Xaml.winmd')">$(SolutionDir)\packages\$(WinUIPackageName).$(WinUIPackageVersion)\lib\uap10.0\Microsoft.UI.Xaml.winmd</ReactNativeXamlTargetWinMD>
259+
<ReactNativeXamlRootDir Condition="$(ReactNativeXamlRootDir)==''">$([MSBuild]::NormalizePath('$(MSBuildThisFileDirectory)..\..\'))</ReactNativeXamlRootDir>
260+
<ReactNativeXamlCodeGenCommand Condition="$(ReactNativeXamlCodeGenCommand)==''">dotnet run --project Codegen -verbose -winmd $(ReactNativeXamlTargetWinMD)</ReactNativeXamlCodeGenCommand>
261+
<ReactNativeXamlYarnBuildCommand Condition="$(ReactNativeXamlYarnBuildCommand)==''">yarn build</ReactNativeXamlYarnBuildCommand>
262+
</PropertyGroup>
263+
<ItemGroup>
264+
<RnxCodeGenOutput Include="Codegen\EventArgsTypeProperties.g.h" />
265+
<RnxCodeGenOutput Include="Codegen\TypeCreator.g.cpp" />
266+
<RnxCodeGenOutput Include="Codegen\TypeEnums.g.h" />
267+
<RnxCodeGenOutput Include="Codegen\TypeEvents.g.h" />
268+
<RnxCodeGenOutput Include="Codegen\TypeProperties.g.h" />
269+
<RnxCodeGenOutput Include="Codegen\Version.g.h" />
270+
<RnxCodeGenOutput Include="..\..\src\Enums.ts" />
271+
<RnxCodeGenOutput Include="..\..\src\Props.ts" />
272+
<RnxCodeGenOutput Include="..\..\src\Types.tsx" />
273+
</ItemGroup>
274+
<Target Name="RunReactNativeXamlCodeGen" BeforeTargets="PrepareForBuild" Condition="$(RunReactNativeXamlCodeGenBeforeBuild)=='true'" Inputs="$(ReactNativeXamlTargetWinMD)" Outputs="@(RnxCodeGenOutput)">
275+
<Exec Command="$(ReactNativeXamlCodeGenCommand)" WorkingDirectory="$(ReactNativeXamlRootDir)" CustomWarningRegularExpression="Warning: " CustomErrorRegularExpression="Error: " />
276+
<Exec Command="$(ReactNativeXamlYarnBuildCommand)" WorkingDirectory="$(ReactNativeXamlRootDir)" CustomWarningRegularExpression="Warning: " CustomErrorRegularExpression="Error: " />
277+
</Target>
255278
<ImportGroup Label="ExtensionTargets" Condition="$(RnwUsesPackageReference)!='true'">
256279
<Import Project="$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.$(CppWinRTVersion)\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.$(CppWinRTVersion)\build\native\Microsoft.Windows.CppWinRT.targets')" />
257280
<Import Project="$(SolutionDir)\packages\CDebug.0.0.3\build\CDebug.targets" Condition="Exists('$(SolutionDir)\packages\CDebug.0.0.3\build\CDebug.targets')" />

0 commit comments

Comments
 (0)