Skip to content

Commit

Permalink
Fix template build to use correct slashes on all platforms (#4991)
Browse files Browse the repository at this point in the history
* Fix template build to use correct slashes on all platforms

Instead of using ItemGroups, which have platform-specific behavior for slashes, it uses a CodeTask to read/write/modify file contents.
  • Loading branch information
Eilon authored Mar 7, 2022
1 parent bc222ce commit 5d08654
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 40 deletions.
75 changes: 51 additions & 24 deletions src/Templates/src/Microsoft.Maui.Templates.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<UsingTask TaskName="ReplaceFileText" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<InputFilename ParameterType="System.String" Required="true" />
<OutputFilename ParameterType="System.String" Required="true" />
<MatchExpression ParameterType="System.String[]" Required="true" />
<ReplacementText ParameterType="System.String[]" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
if (MatchExpression.Length != ReplacementText.Length) throw new Exception("The number of MatchExpression items must match the number of ReplacementText items.");
for (int i = 0; i < MatchExpression.Length; i++)
{
File.WriteAllText(
OutputFilename,
Regex.Replace(File.ReadAllText(InputFilename), MatchExpression[i], ReplacementText[i])
);
//Console.WriteLine($"Processing file '{InputFilename}' --> '{OutputFilename}', replacing match '{MatchExpression[i]}' with text '{ReplacementText[i]}'.");
}
]]>
</Code>
</Task>
</UsingTask>

<PropertyGroup>
<TargetFramework>$(_MauiDotNetTfm)</TargetFramework>
<PackageType>Template</PackageType>
Expand All @@ -18,38 +46,37 @@
</PropertyGroup>

<ItemGroup>
<Content Include="templates\**\*" Exclude="templates\**\bin\**;templates\**\obj\**" />
<TemplateJsonInput Include="templates\*\.template.config\template.in.json">
<PackageDestination>$([System.String]::Concat(%(RelativeDir), 'template.json'))</PackageDestination>
<IntermediateLocation>$(IntermediateOutputPath)$([System.String]::Concat(%(RelativeDir), 'template.json'))</IntermediateLocation>
</TemplateJsonInput>
<Content Include="templates\**\*" Exclude="templates\**\bin\**;templates\**\obj\**;@(TemplateJsonInput)" />
<Compile Remove="**\*" />
</ItemGroup>

<PropertyGroup>
<BeforePack>_UpdateTemplateVersions</BeforePack>
</PropertyGroup>

<Target Name="_UpdateTemplateVersions" DependsOnTargets="SetVersions">
<ItemGroup>
<_TemplateJsonFile Include="templates\*\.template.config\template.json" />
<_TemplateJsonFileWithContent
Include="@(_TemplateJsonFile)"
Contents="$([System.IO.File]::ReadAllText('%(FullPath)')
.Replace('WINDOWSAPPSDK_VERSION_VALUE', '$(MicrosoftWindowsAppSDKPackageVersion)')
.Replace('WIN2D_VERSION_VALUE', '$(MicrosoftGraphicsWin2DPackageVersion)'))" />
<_TemplateCsprojFile Include="templates\*\*.csproj" />
<_TemplateCsprojFileWithContent
Include="@(_TemplateCsprojFile)"
Contents="$([System.IO.File]::ReadAllText('%(FullPath)')
.Replace('DOTNET_TFM_VALUE', '$(_MauiDotNetTfm)'))" />
<_TemplateReplacedFile Include="@(_TemplateJsonFile);@(_TemplateCsprojFile)" />
<_TemplateReplacedFileWithContent Include="@(_TemplateJsonFileWithContent);@(_TemplateCsprojFileWithContent)" />
</ItemGroup>
<WriteLinesToFile
File="@(_TemplateReplacedFileWithContent -> '$(IntermediateOutputPath)%(Identity)')"
Lines="%(Contents)"
Overwrite="true" />
<Target Name="_UpdateTemplateVersions" DependsOnTargets="SetVersions"
Inputs="@(TemplateJsonInput)"
Outputs="%(TemplateJsonInput.IntermediateLocation)">

<!-- Copy files to their output name -->
<Copy
SourceFiles="%(TemplateJsonInput.Fullpath)"
DestinationFiles="%(TemplateJsonInput.IntermediateLocation)" />

<!-- Replace WinAppSDK, Win2D, .NET TFM versions -->
<ReplaceFileText
InputFilename="%(TemplateJsonInput.IntermediateLocation)"
OutputFilename="%(TemplateJsonInput.IntermediateLocation)"
MatchExpression="WINDOWSAPPSDK_VERSION_VALUE;WIN2D_VERSION_VALUE;DOTNET_TFM_VALUE"
ReplacementText="$(MicrosoftWindowsAppSDKPackageVersion);$(MicrosoftGraphicsWin2DPackageVersion);$(_MauiDotNetTfm)" />

<ItemGroup>
<FileWrites Include="@(_TemplateReplacedFile -> '$(IntermediateOutputPath)%(Identity)')" />
<Content Remove="@(_TemplateReplacedFile)" />
<Content Include="@(_TemplateReplacedFile -> '$(IntermediateOutputPath)%(Identity)')" PackagePath="$(ContentTargetFolders)\%(Identity)" Pack="true" />
<FileWrites Include="%(TemplateJsonInput.IntermediateLocation)" />
<Content Include="%(TemplateJsonInput.IntermediateLocation)" PackagePath="$(ContentTargetFolders)\%(TemplateJsonInput.PackageDestination)" Pack="true" />
</ItemGroup>
</Target>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
"replaces": "WIN2D_VERSION",
"defaultValue": "WIN2D_VERSION_VALUE"
},
"DotNetTfm": {
"type": "parameter",
"dataType": "string",
"replaces": "DOTNET_TFM",
"defaultValue": "DOTNET_TFM_VALUE"
},
"HostIdentifier": {
"type": "bind",
"binding": "HostIdentifier"
Expand Down
10 changes: 5 additions & 5 deletions src/Templates/src/templates/maui-blazor/MauiApp.1.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFrameworks>DOTNET_TFM_VALUE-android;DOTNET_TFM_VALUE-ios;DOTNET_TFM_VALUE-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows')) and '$(MSBuildRuntimeType)' == 'Full'">$(TargetFrameworks);DOTNET_TFM_VALUE-windows10.0.19041</TargetFrameworks>
<TargetFrameworks>DOTNET_TFM-android;DOTNET_TFM-ios;DOTNET_TFM-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows')) and '$(MSBuildRuntimeType)' == 'Full'">$(TargetFrameworks);DOTNET_TFM-windows10.0.19041</TargetFrameworks>
<OutputType>Exe</OutputType>
<RootNamespace>MauiApp._1</RootNamespace>
<UseMaui>true</UseMaui>
Expand All @@ -25,9 +25,9 @@
<!-- Required for C# Hot Reload -->
<UseInterpreter Condition="'$(Configuration)' == 'Debug'">True</UseInterpreter>

<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM_VALUE-ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM_VALUE-maccatalyst'">14.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM_VALUE-android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM-ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM-maccatalyst'">14.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM-android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$(TargetFramework.Contains('-windows'))">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$(TargetFramework.Contains('-windows'))">10.0.17763.0</TargetPlatformMinVersion>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
],
"preferNameDirectory": true,
"symbols": {
"DotNetTfm": {
"type": "parameter",
"dataType": "string",
"replaces": "DOTNET_TFM",
"defaultValue": "DOTNET_TFM_VALUE"
}
},
"defaultName": "MauiLib1"
}
}
10 changes: 5 additions & 5 deletions src/Templates/src/templates/maui-lib/MauiLib1.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>DOTNET_TFM_VALUE;DOTNET_TFM_VALUE-android;DOTNET_TFM_VALUE-ios;DOTNET_TFM_VALUE-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows')) and '$(MSBuildRuntimeType)' == 'Full'">$(TargetFrameworks);DOTNET_TFM_VALUE-windows10.0.19041</TargetFrameworks>
<TargetFrameworks>DOTNET_TFM;DOTNET_TFM-android;DOTNET_TFM-ios;DOTNET_TFM-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows')) and '$(MSBuildRuntimeType)' == 'Full'">$(TargetFrameworks);DOTNET_TFM-windows10.0.19041</TargetFrameworks>
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">MauiLib1</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>

<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM_VALUE-ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM_VALUE-maccatalyst'">14.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM_VALUE-android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM-ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM-maccatalyst'">14.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM-android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$(TargetFramework.Contains('-windows'))">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$(TargetFramework.Contains('-windows'))">10.0.17763.0</TargetPlatformMinVersion>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@
"replaces": "WIN2D_VERSION",
"defaultValue": "WIN2D_VERSION_VALUE"
},
"DotNetTfm": {
"type": "parameter",
"dataType": "string",
"replaces": "DOTNET_TFM",
"defaultValue": "DOTNET_TFM_VALUE"
},
"HostIdentifier": {
"type": "bind",
"binding": "HostIdentifier"
Expand Down
10 changes: 5 additions & 5 deletions src/Templates/src/templates/maui-mobile/MauiApp.1.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>DOTNET_TFM_VALUE-android;DOTNET_TFM_VALUE-ios;DOTNET_TFM_VALUE-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows')) and '$(MSBuildRuntimeType)' == 'Full'">$(TargetFrameworks);DOTNET_TFM_VALUE-windows10.0.19041</TargetFrameworks>
<TargetFrameworks>DOTNET_TFM-android;DOTNET_TFM-ios;DOTNET_TFM-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows')) and '$(MSBuildRuntimeType)' == 'Full'">$(TargetFrameworks);DOTNET_TFM-windows10.0.19041</TargetFrameworks>
<OutputType>Exe</OutputType>
<RootNamespace>MauiApp._1</RootNamespace>
<UseMaui>true</UseMaui>
Expand All @@ -24,9 +24,9 @@
<!-- Required for C# Hot Reload -->
<UseInterpreter Condition="'$(Configuration)' == 'Debug'">True</UseInterpreter>

<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM_VALUE-ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM_VALUE-maccatalyst'">14.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM_VALUE-android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM-ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM-maccatalyst'">14.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'DOTNET_TFM-android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$(TargetFramework.Contains('-windows'))">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$(TargetFramework.Contains('-windows'))">10.0.17763.0</TargetPlatformMinVersion>
</PropertyGroup>
Expand Down

0 comments on commit 5d08654

Please sign in to comment.