Skip to content

Commit 765130f

Browse files
authored
feat: Fail build on inconsistent xml namespaces values (#18)
* test: Add app generation validation * fix: Don't run merge task during design time builds * feat: Fail build on inconsistent xml namespaces values * docs: Update readme * fix: Avoid double inclusion of generated xaml file * ci: Adjust mergify configuration
1 parent ddaeb6b commit 765130f

File tree

12 files changed

+124
-79
lines changed

12 files changed

+124
-79
lines changed

.mergify.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ pull_request_rules:
2727
actions:
2828
merge:
2929
method: merge
30-
# https://doc.mergify.io/strict-workflow.html
31-
# https://doc.mergify.io/actions.html#label
32-
strict: smart
3330

3431
- name: automatic merge for allcontributors pull requests
3532
conditions:

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,8 @@ Otherwise, the generated file can be referenced as follows:
5353
<ResourceDictionary Source="ms-appx:///REPLACE_ME/Generated/mergedpages.xaml" />
5454
</ResourceDictionary.MergedDictionaries>
5555
</ResourceDictionary>
56-
```
56+
```
57+
58+
## Troubleshooting
59+
- Make sure that all namespaces definitions across files are of the same values. (e.g. `xlmns:ns1="http://site1"` and `xlmns:ns1="http://site2"` in two different files will fail)
60+
- If you include the same resource dictionary (e.g. Colors.xaml) file in multiple merged files (generally used to have a file for custom brushes and colors), you can remove those multiple inclusions.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<ResourceDictionary
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
4+
5+
</ResourceDictionary>
Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,62 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<PropertyGroup>
4-
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
5-
<HasSharedItems>true</HasSharedItems>
6-
<SharedGUID>6279c845-92f8-4333-ab99-3d213163593c</SharedGUID>
7-
</PropertyGroup>
8-
<PropertyGroup Label="Configuration">
9-
<Import_RootNamespace>XamlMergeUWPTest</Import_RootNamespace>
10-
</PropertyGroup>
11-
<ItemGroup>
12-
<ApplicationDefinition Include="$(MSBuildThisFileDirectory)App.xaml">
13-
<SubType>Designer</SubType>
14-
<Generator>MSBuild:Compile</Generator>
15-
</ApplicationDefinition>
16-
</ItemGroup>
17-
<ItemGroup>
18-
<Compile Include="$(MSBuildThisFileDirectory)App.xaml.cs">
19-
<DependentUpon>App.xaml</DependentUpon>
20-
</Compile>
21-
<Compile Include="$(MSBuildThisFileDirectory)MainPage.xaml.cs">
22-
<DependentUpon>MainPage.xaml</DependentUpon>
23-
</Compile>
24-
</ItemGroup>
25-
<ItemGroup>
26-
<Page Include="$(MSBuildThisFileDirectory)MainPage.xaml">
27-
<SubType>Designer</SubType>
28-
<Generator>MSBuild:Compile</Generator>
29-
</Page>
30-
</ItemGroup>
31-
<ItemGroup>
32-
<None Include="$(MSBuildThisFileDirectory)Assets\SharedAssets.md" />
33-
</ItemGroup>
34-
<ItemGroup>
35-
<PRIResource Include="$(MSBuildThisFileDirectory)Strings\en\Resources.resw" />
36-
</ItemGroup>
37-
3+
<PropertyGroup>
4+
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
5+
<HasSharedItems>true</HasSharedItems>
6+
<SharedGUID>6279c845-92f8-4333-ab99-3d213163593c</SharedGUID>
7+
</PropertyGroup>
8+
<PropertyGroup Label="Configuration">
9+
<Import_RootNamespace>XamlMergeUWPTest</Import_RootNamespace>
10+
</PropertyGroup>
3811
<ItemGroup>
39-
<!--
12+
<ApplicationDefinition Include="$(MSBuildThisFileDirectory)App.xaml">
13+
<SubType>Designer</SubType>
14+
<Generator>MSBuild:Compile</Generator>
15+
</ApplicationDefinition>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<Compile Include="$(MSBuildThisFileDirectory)App.xaml.cs">
19+
<DependentUpon>App.xaml</DependentUpon>
20+
</Compile>
21+
<Compile Include="$(MSBuildThisFileDirectory)MainPage.xaml.cs">
22+
<DependentUpon>MainPage.xaml</DependentUpon>
23+
</Compile>
24+
</ItemGroup>
25+
<ItemGroup>
26+
<Page Include="$(MSBuildThisFileDirectory)MainPage.xaml">
27+
<SubType>Designer</SubType>
28+
<Generator>MSBuild:Compile</Generator>
29+
</Page>
30+
<Page Include="$(MSBuildThisFileDirectory)Styles\Dictionary01.xaml">
31+
<SubType>Designer</SubType>
32+
<Generator>MSBuild:Compile</Generator>
33+
</Page>
34+
</ItemGroup>
35+
<ItemGroup>
36+
<None Include="$(MSBuildThisFileDirectory)Assets\SharedAssets.md" />
37+
</ItemGroup>
38+
<ItemGroup>
39+
<PRIResource Include="$(MSBuildThisFileDirectory)Strings\en\Resources.resw" />
40+
</ItemGroup>
41+
<ItemGroup>
42+
<!--
4043
Add files present in the shared project folder, excluding the ones
4144
explicitly included in the projitem, so files can be added from vscode
4245
without manipulating the projitem file.
4346
-->
44-
<_Globbled_Page Include="$(MSBuildThisFileDirectory)**/*.xaml" Exclude="@(Page);@(ApplicationDefinition)">
45-
<SubType>Designer</SubType>
46-
<Generator>MSBuild:Compile</Generator>
47-
</_Globbled_Page>
48-
<Page Include="@(_Globbled_Page)" />
49-
50-
<_Globbed_Compile Include="$(MSBuildThisFileDirectory)**/*.xaml.cs" Exclude="@(Compile)">
51-
<DependentUpon>%(Filename)</DependentUpon>
52-
</_Globbed_Compile>
53-
<_Globbed_Compile Include="$(MSBuildThisFileDirectory)**/*.cs" Exclude="@(Compile);@(_Globbed_Compile)" />
54-
<Compile Include="@(_Globbed_Compile)" />
55-
56-
<_Globbed_PRIResource Include="$(MSBuildThisFileDirectory)**/*.resw" Exclude="@(PRIResource)" />
57-
<PRIResource Include="@(_Globbed_PRIResource)" />
58-
59-
<_Globbed_Content Include="$(MSBuildThisFileDirectory)Assets/**/*.*" Exclude="@(Content)" />
60-
<Content Include="@(_Globbed_Content)" />
47+
<_Globbled_Page Include="$(MSBuildThisFileDirectory)**/*.xaml" Exclude="@(Page);@(ApplicationDefinition)">
48+
<SubType>Designer</SubType>
49+
<Generator>MSBuild:Compile</Generator>
50+
</_Globbled_Page>
51+
<Page Include="@(_Globbled_Page)" />
52+
<_Globbed_Compile Include="$(MSBuildThisFileDirectory)**/*.xaml.cs" Exclude="@(Compile)">
53+
<DependentUpon>%(Filename)</DependentUpon>
54+
</_Globbed_Compile>
55+
<_Globbed_Compile Include="$(MSBuildThisFileDirectory)**/*.cs" Exclude="@(Compile);@(_Globbed_Compile)" />
56+
<Compile Include="@(_Globbed_Compile)" />
57+
<_Globbed_PRIResource Include="$(MSBuildThisFileDirectory)**/*.resw" Exclude="@(PRIResource)" />
58+
<PRIResource Include="@(_Globbed_PRIResource)" />
59+
<_Globbed_Content Include="$(MSBuildThisFileDirectory)Assets/**/*.*" Exclude="@(Content)" />
60+
<Content Include="@(_Globbed_Content)" />
6161
</ItemGroup>
62-
63-
</Project>
62+
</Project>
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<PropertyGroup Label="Globals">
4-
<ProjectGuid>6279c845-92f8-4333-ab99-3d213163593c</ProjectGuid>
5-
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
6-
</PropertyGroup>
7-
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
8-
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
9-
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
10-
<PropertyGroup />
11-
<Import Project="XamlMergeUWPTest.Shared.projitems" Label="Shared" />
12-
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
13-
</Project>
3+
<PropertyGroup Label="Globals">
4+
<ProjectGuid>6279c845-92f8-4333-ab99-3d213163593c</ProjectGuid>
5+
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
6+
</PropertyGroup>
7+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
8+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
9+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
10+
<PropertyGroup />
11+
<Import Project="XamlMergeUWPTest.Shared.projitems" Label="Shared" />
12+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
13+
<ItemGroup>
14+
<_Globbled_Page Remove="Styles\Dictionary01.xaml" />
15+
</ItemGroup>
16+
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<!--origin: ..\XamlMergeUWPTest.Shared\Styles\Dictionary01.xaml-->
3+
</ResourceDictionary>

src/UWP/XamlMergeUWPTest/XamlMergeUWPTest.UWP/XamlMergeUWPTest.UWP.csproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<PackageCertificateKeyFile>XamlMergeUWPTest.UWP_TemporaryKey.pfx</PackageCertificateKeyFile>
3535
<PackageCertificateThumbprint>76F0655907F6860CA663D3E1FED3FECF3EC25480</PackageCertificateThumbprint>
3636
<AppxPackageSigningEnabled>False</AppxPackageSigningEnabled>
37-
<AppxPackageValidationEnabled>False</AppxPackageValidationEnabled>
37+
<AppxPackageValidationEnabled>False</AppxPackageValidationEnabled>
3838
</PropertyGroup>
3939
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
4040
<DebugSymbols>true</DebugSymbols>
@@ -143,6 +143,7 @@
143143
<AppxManifest Include="Package.appxmanifest">
144144
<SubType>Designer</SubType>
145145
</AppxManifest>
146+
<None Include="xamlmerge.targets" />
146147
<None Include="XamlMergeUWPTest.UWP_TemporaryKey.pfx" />
147148
</ItemGroup>
148149
<ItemGroup>
@@ -154,6 +155,12 @@
154155
<Content Include="Assets\Wide310x150Logo.scale-200.png" />
155156
<Content Include="Properties\Default.rd.xml" />
156157
</ItemGroup>
158+
<ItemGroup>
159+
<Page Include="Generated\mergedpages.xaml">
160+
<Generator>MSBuild:Compile</Generator>
161+
<SubType>Designer</SubType>
162+
</Page>
163+
</ItemGroup>
157164
<Import Project="..\XamlMergeUWPTest.Shared\XamlMergeUWPTest.Shared.projitems" Label="Shared" />
158165
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
159166
<VisualStudioVersion>14.0</VisualStudioVersion>
@@ -166,4 +173,5 @@
166173
<Target Name="AfterBuild">
167174
</Target>
168175
-->
176+
<Import Project="XamlMerge.targets" />
169177
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Project>
3+
4+
<ItemGroup>
5+
<XamlMergeInput Include="..\XamlMergeUWPTest.Shared\Styles\**\*.xaml" />
6+
</ItemGroup>
7+
8+
<Import Project="..\..\..\Uno.XamlMerge.Task\build\Uno.XamlMerge.Task.targets" />
9+
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<!--origin: ..\XamlMergeUWPTest.Shared\Styles\Dictionary01.xaml-->
3+
</ResourceDictionary>

src/UWP/XamlMergeUWPTest/XamlMergeUWPTest.Wasm/XamlMergeUWPTest.Wasm.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
44
<TargetFramework>net5.0</TargetFramework>
@@ -54,4 +54,5 @@
5454
<ProjectReference Include="..\..\TestLibrary\TestLibrary.csproj" />
5555
</ItemGroup>
5656
<Import Project="..\XamlMergeUWPTest.Shared\XamlMergeUWPTest.Shared.projitems" Label="Shared" Condition="Exists('..\XamlMergeUWPTest.Shared\XamlMergeUWPTest.Shared.projitems')" />
57+
<Import Project="..\XamlMergeUWPTest.UWP\XamlMerge.targets"/>
5758
</Project>

src/Uno.XamlMerge.Task/MergedDictionary.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private MergedDictionary(XmlDocument document, MergedDictionary parentDictionary
144144
nodeListNodesToIgnore = new List<int>();
145145
nodeKeyToNodeListIndexDictionary = new Dictionary<string, int>();
146146
mergedThemeDictionaryByKeyDictionary = new Dictionary<string, MergedDictionary>();
147-
namespaceList = new List<string>();
147+
knownNamespaces = new();
148148
this.parentDictionary = parentDictionary;
149149
}
150150

@@ -167,11 +167,20 @@ private void UpdateIgnorables(string ignorables)
167167

168168
private void AddNamespace(string xmlnsString, string namespaceString)
169169
{
170-
if (!namespaceList.Contains(xmlnsString))
170+
if (!knownNamespaces.TryGetValue(xmlnsString, out var existingNamespaceString))
171171
{
172172
xmlElement.SetAttribute("xmlns:" + xmlnsString, namespaceString);
173-
namespaceList.Add(xmlnsString);
173+
knownNamespaces.Add(xmlnsString, namespaceString);
174174
}
175+
else
176+
{
177+
if(existingNamespaceString != namespaceString)
178+
{
179+
throw new InvalidOperationException(
180+
$"The XML namespace [{xmlnsString}] with the value [{namespaceString}] is different than the already defined value [{existingNamespaceString}]. " +
181+
$"Make sure to align all namespace defintions to the same values across merged files.");
182+
}
183+
}
175184
}
176185

177186
private void AddNode(XmlNode node, Dictionary<string, string> xmlnsReplacementDictionary)
@@ -440,7 +449,7 @@ private void RemoveAncestorNodesWithKey(string key)
440449

441450
private Dictionary<string, int> nodeKeyToNodeListIndexDictionary;
442451
private Dictionary<string, MergedDictionary> mergedThemeDictionaryByKeyDictionary;
443-
private List<string> namespaceList;
452+
private Dictionary<string, string> knownNamespaces;
444453
private List<XmlElement> mergedDictionaries = new();
445454
private MergedDictionary parentDictionary;
446455
private XmlAttribute _ignorablesAttribute;

src/Uno.XamlMerge.Task/build/Uno.XamlMerge.Task.targets

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<Target Name="GenerateMergedXaml"
12-
BeforeTargets="_ComputeTargetFrameworkItems;BeforeBuild" Condition="'$(TargetFramework)' == '' or '$(TargetFrameworks)'==''">
12+
BeforeTargets="_ComputeTargetFrameworkItems;BeforeBuild" Condition="('$(TargetFramework)' == '' or '$(TargetFrameworks)'=='') and ('$(BuildingProject)' == 'true' or '$(DesignTimeBuild)' != 'true')">
1313
<ItemGroup>
1414
<_FilteredXamlMergeInput
1515
Include="@(XamlMergeInput)"
@@ -37,11 +37,15 @@
3737
This can happen when building for the first time.
3838
-->
3939
<_additionalPages Include="@(Page)"
40-
Condition="'%(Page.FullPath)'!='$(XamlMergeOutputFile)'"
40+
Condition="'%(Page.FullPath)'!=$([System.IO.Path]::GetFullPath('$(XamlMergeOutputFile)'))"
4141
KeepDuplicates="False" />
42+
43+
<_mergeOutputInluded Include="@(Page)"
44+
Condition="'%(Page.FullPath)'==$([System.IO.Path]::GetFullPath('$(XamlMergeOutputFile)'))" />
4245

4346
<Page Include="$([MSBuild]::MakeRelative('$(MSBuildProjectDirectory)', '$(XamlMergeOutputFile)'))"
44-
KeepDuplicates="False" />
47+
KeepDuplicates="False"
48+
Condition="'@(_mergeOutputInluded)'==''"/>
4549
<Page Include="@(_additionalPages)"
4650
KeepDuplicates="False" />
4751
</ItemGroup>

0 commit comments

Comments
 (0)