Skip to content

Commit

Permalink
Fix <ApplicationTitle> encoding in maui templates (#22084)
Browse files Browse the repository at this point in the history
* Makes <ApplicationTitle> use a custom symbol instead of the default name parameter to ensure the name is XML encoded where necessaru
* Adds new tests for various encodings and special characters
---------

Co-authored-by: Eilon Lipton <Eilon@users.noreply.github.com>
  • Loading branch information
MiYanni and Eilon committed Jun 21, 2024
1 parent 95680e9 commit 8181190
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 22 deletions.
9 changes: 6 additions & 3 deletions src/Templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ Add the local artifacts to the NuGet.config:
```

```dotnetcli
# uninstall, build, and install the templates
dotnet new uninstall Microsoft.Maui.Templates.net8
dotnet pack Microsoft.Maui.sln
dotnet new -i artifacts\Microsoft.Maui.Templates.*.nupkg
dotnet new install artifacts\packages\Release\Shipping\Microsoft.Maui.Templates.*.nupkg
# then just in the maui folder, so you get a NuGet.config
mkdir foo
cd foo
mkdir myproject
cd myproject
dotnet new maui
```
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
]
}
},
"XmlEncodedAppNameParam": {
"type": "derived",
"valueSource": "name",
"valueTransform": "encode",
"replaces": "XmlEncodedAppName"
},
"defaultAppId":{
"type": "generated",
"generator": "join",
Expand All @@ -128,5 +134,10 @@
"replaces": "com.companyname.mauiapp"
}
},
"forms": {
"encode": {
"identifier": "xmlEncode"
}
},
"defaultName": "MauiApp1"
}
2 changes: 1 addition & 1 deletion src/Templates/src/templates/maui-blazor/MauiApp.1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<Nullable>enable</Nullable>

<!-- Display name -->
<ApplicationTitle>MauiApp.1</ApplicationTitle>
<ApplicationTitle>XmlEncodedAppName</ApplicationTitle>

<!-- App Identifier -->
<ApplicationId>com.companyname.mauiapp</ApplicationId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="maui-application-id-placeholder" version="0.0.0" api-version="8" xmlns="http://tizen.org/ns/packages">
<profile name="common" />
<ui-application appid="maui-application-id-placeholder" exec="MauiApp.1.dll" multiple="false" nodisplay="false" taskmanage="true" type="dotnet" launch_mode="single">
<ui-application appid="maui-application-id-placeholder" exec="XmlEncodedAppName.dll" multiple="false" nodisplay="false" taskmanage="true" type="dotnet" launch_mode="single">
<label>maui-application-title-placeholder</label>
<icon>maui-appicon-placeholder</icon>
<metadata key="http://tizen.org/metadata/prefer_dotnet_aot" value="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@
]
}
},
"XmlEncodedAppNameParam": {
"type": "derived",
"valueSource": "name",
"valueTransform": "encode",
"replaces": "XmlEncodedAppName"
},
"defaultAppId":{
"type": "generated",
"generator": "join",
Expand All @@ -132,5 +138,10 @@
"replaces": "com.companyname.mauiapp"
}
},
"forms": {
"encode": {
"identifier": "xmlEncode"
}
},
"defaultName": "MauiApp1"
}
2 changes: 1 addition & 1 deletion src/Templates/src/templates/maui-mobile/MauiApp.1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<Nullable>enable</Nullable>

<!-- Display name -->
<ApplicationTitle>MauiApp.1</ApplicationTitle>
<ApplicationTitle>XmlEncodedAppName</ApplicationTitle>

<!-- App Identifier -->
<ApplicationId>com.companyname.mauiapp</ApplicationId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="maui-application-id-placeholder" version="0.0.0" api-version="8" xmlns="http://tizen.org/ns/packages">
<profile name="common" />
<ui-application appid="maui-application-id-placeholder" exec="MauiApp.1.dll" multiple="false" nodisplay="false" taskmanage="true" type="dotnet" launch_mode="single">
<ui-application appid="maui-application-id-placeholder" exec="XmlEncodedAppName.dll" multiple="false" nodisplay="false" taskmanage="true" type="dotnet" launch_mode="single">
<label>maui-application-title-placeholder</label>
<icon>maui-appicon-placeholder</icon>
<metadata key="http://tizen.org/metadata/prefer_dotnet_aot" value="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@
]
}
},
"XmlEncodedAppNameParam": {
"type": "derived",
"valueSource": "name",
"valueTransform": "encode",
"replaces": "XmlEncodedAppName"
},
"defaultAppId":{
"type": "generated",
"generator": "join",
Expand All @@ -217,5 +223,10 @@
"replaces": "com.companyname.mauiapp"
}
},
"forms": {
"encode": {
"identifier": "xmlEncode"
}
},
"defaultName": "MauiApp1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>DOTNET_TFM-android</TargetFramework>
<SupportedOSPlatformVersion>21.0</SupportedOSPlatformVersion>
<OutputType>Exe</OutputType>
<RootNamespace>MauiApp._1</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseMaui>true</UseMaui>
Expand All @@ -16,7 +17,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MauiApp.1\MauiApp.1.csproj" />
<ProjectReference Include="..\XmlEncodedAppName\XmlEncodedAppName.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<resources>
<string name="app_name">MauiApp.1</string>
<string name="app_name">XmlEncodedAppName</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>DOTNET_TFM-maccatalyst</TargetFramework>
<SupportedOSPlatformVersion>13.1</SupportedOSPlatformVersion>
<OutputType>Exe</OutputType>
<RootNamespace>MauiApp._1</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseMaui>true</UseMaui>
Expand All @@ -16,7 +17,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MauiApp.1\MauiApp.1.csproj" />
<ProjectReference Include="..\XmlEncodedAppName\XmlEncodedAppName.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>WinExe</OutputType>
<TargetFramework>DOTNET_TFM-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RootNamespace>MauiApp.1.WinUI</RootNamespace>
<RootNamespace>MauiApp._1.WinUI</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;ARM64</Platforms>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
Expand All @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MauiApp.1\MauiApp.1.csproj" />
<ProjectReference Include="..\XmlEncodedAppName\XmlEncodedAppName.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>DOTNET_TFM-ios</TargetFramework>
<SupportedOSPlatformVersion>11.0</SupportedOSPlatformVersion>
<OutputType>Exe</OutputType>
<RootNamespace>MauiApp._1</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseMaui>true</UseMaui>
Expand All @@ -16,7 +17,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MauiApp.1\MauiApp.1.csproj" />
<ProjectReference Include="..\XmlEncodedAppName\XmlEncodedAppName.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>DOTNET_TFM</TargetFramework>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>MauiApp._1</RootNamespace>
<UseMaui>true</UseMaui>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
30 changes: 21 additions & 9 deletions src/TestUtils/src/Microsoft.Maui.IntegrationTests/TemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ public void Build(string id, string framework, string config, bool shouldPack)
}

[Test]
[TestCase("Debug")]
[TestCase("Release")]
public void BuildMultiProject(string config)
[TestCase("Debug", "simplemulti")]
[TestCase("Release", "simplemulti")]
[TestCase("Debug", "MultiProject@Symbol & More")]
[TestCase("Release", "MultiProject@Symbol & More")]
public void BuildMultiProject(string config, string projectName)
{
var projectDir = TestDirectory;
var projectDir = Path.Combine(TestDirectory, projectName);
var name = Path.GetFileName(projectDir);
var solutionFile = Path.Combine(projectDir, $"{name}.sln");

Expand All @@ -62,7 +64,7 @@ public void BuildMultiProject(string config)

if (!TestEnvironment.IsWindows)
{
Assert.IsTrue(DotnetInternal.Run("sln", $"{solutionFile} remove {projectDir}/{name}.WinUI/{name}.WinUI.csproj"),
Assert.IsTrue(DotnetInternal.Run("sln", $"\"{solutionFile}\" remove \"{projectDir}/{name}.WinUI/{name}.WinUI.csproj\""),
$"Unable to remove WinUI project from solution. Check test output for errors.");
}

Expand Down Expand Up @@ -99,10 +101,10 @@ public void BuildMultiProjectSinglePlatform(string config, string platformArg)
[TestCase("maui", "Project Space", "projectspace")]
[TestCase("maui-blazor", "Project Space", "projectspace")]
[TestCase("mauilib", "Project Space", "projectspace")]
// with invalid characters
[TestCase("maui", "Project@Symbol", "projectsymbol")]
[TestCase("maui-blazor", "Project@Symbol", "projectsymbol")]
[TestCase("mauilib", "Project@Symbol", "projectsymbol")]
// with lots tricky characters (the '&' requires XML escaping too)
[TestCase("maui", "Project@Symbol & More", "projectsymbolmore")]
[TestCase("maui-blazor", "Project@Symbol & More", "projectsymbolmore")]
[TestCase("mauilib", "Project@Symbol & More", "projectsymbolmore")]
public void BuildsWithSpecialCharacters(string id, string projectName, string expectedId)
{
var projectDir = Path.Combine(TestDirectory, projectName);
Expand All @@ -117,12 +119,22 @@ public void BuildsWithSpecialCharacters(string id, string projectName, string ex
if (id != "mauilib")
{
var doc = XDocument.Load(projectFile);

// Check the app ID got invalid characters removed
var appId = doc.Root!
.Elements("PropertyGroup")
.Elements("ApplicationId")
.Single()
.Value;
Assert.AreEqual($"com.companyname.{expectedId}", appId);

// Check the app title matches the project name exactly (it might have been XML-encoded, but loading the document decodes that)
var appTitle = doc.Root!
.Elements("PropertyGroup")
.Elements("ApplicationTitle")
.Single()
.Value;
Assert.AreEqual(projectName, appTitle);
}

Assert.IsTrue(DotnetInternal.Build(projectFile, "Debug", properties: BuildProps, msbuildWarningsAsErrors: true),
Expand Down

0 comments on commit 8181190

Please sign in to comment.