Skip to content

Commit f548ff0

Browse files
committed
Add migration checks
1 parent 44a820e commit f548ff0

File tree

2 files changed

+283
-0
lines changed

2 files changed

+283
-0
lines changed

src/ElectronNET/build/ElectronNET.Core.targets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@
2323
<AssemblyMetadata Include="IsAspNet" Value="$(_IsMsAspNetProject)" />
2424
</ItemGroup>
2525

26+
<!-- Import migration validation checks -->
27+
<Import Project="$(MSBuildThisFileDirectory)ElectronNET.MigrationChecks.targets" />
28+
2629
</Project>
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
4+
<PropertyGroup>
5+
<ElectronMigrationChecksDependsOn>
6+
ElectronCheckNoPackageJson;
7+
ElectronCheckNoManifestJson;
8+
ElectronCheckElectronBuilderJson;
9+
ElectronCheckNoParentPaths;
10+
ElectronCheckPubxmlFiles
11+
</ElectronMigrationChecksDependsOn>
12+
</PropertyGroup>
13+
14+
<!-- Main migration checks target that runs before build -->
15+
<Target Name="ElectronMigrationChecks"
16+
BeforeTargets="BeforeBuild"
17+
DependsOnTargets="$(ElectronMigrationChecksDependsOn)"
18+
Condition="'$(ElectronSkipMigrationChecks)' != 'true'">
19+
</Target>
20+
21+
<!--
22+
Check 1: No package.json must be present in the project (except ElectronHostHook folder)
23+
-->
24+
<Target Name="ElectronCheckNoPackageJson">
25+
26+
<!-- Find all package.json files, excluding ElectronHostHook folder and output directories -->
27+
<ItemGroup>
28+
<_InvalidPackageJson Include="$(MSBuildProjectDirectory)\**\package.json"
29+
Exclude="$(MSBuildProjectDirectory)\ElectronHostHook\**\package.json;
30+
$(MSBuildProjectDirectory)\bin\**\package.json;
31+
$(MSBuildProjectDirectory)\obj\**\package.json;
32+
$(MSBuildProjectDirectory)\publish\**\package.json;
33+
$(MSBuildProjectDirectory)\node_modules\**\package.json" />
34+
<_InvalidPackageLockJson Include="$(MSBuildProjectDirectory)\**\package-lock.json"
35+
Exclude="$(MSBuildProjectDirectory)\ElectronHostHook\**\package-lock.json;
36+
$(MSBuildProjectDirectory)\bin\**\package-lock.json;
37+
$(MSBuildProjectDirectory)\obj\**\package-lock.json;
38+
$(MSBuildProjectDirectory)\publish\**\package-lock.json;
39+
$(MSBuildProjectDirectory)\node_modules\**\package-lock.json" />
40+
</ItemGroup>
41+
42+
<PropertyGroup>
43+
<_HasInvalidPackageJson>false</_HasInvalidPackageJson>
44+
<_HasInvalidPackageJson Condition="@(_InvalidPackageJson->Count()) > 0 OR @(_InvalidPackageLockJson->Count()) > 0">true</_HasInvalidPackageJson>
45+
</PropertyGroup>
46+
47+
<Warning Condition="'$(_HasInvalidPackageJson)' == 'true'"
48+
Code="ELECTRON001"
49+
Text="Found package.json or package-lock.json file(s) in the project root or subdirectories. These files are no longer supported in this location.
50+
51+
Files found:
52+
@(_InvalidPackageJson, '%0A')@(_InvalidPackageLockJson, '%0A')
53+
54+
MIGRATION REQUIRED:
55+
All properties from an existing package.json file must now be specified as MSBuild properties in the project file.
56+
57+
For more information, see: https://github.com/ElectronNET/Electron.NET/wiki/Migration-Checks#1-packagejson-not-allowed
58+
59+
EXCEPTION: package.json and package-lock.json files ARE allowed in the 'ElectronHostHook' folder for custom host hook implementations." />
60+
61+
</Target>
62+
63+
<!--
64+
Check 2: No electron-manifest.json must be present in the project
65+
-->
66+
<Target Name="ElectronCheckNoManifestJson">
67+
68+
<ItemGroup>
69+
<_InvalidManifestJson Include="$(MSBuildProjectDirectory)\**\electron.manifest.json;$(MSBuildProjectDirectory)\**\electron-manifest.json"
70+
Exclude="$(MSBuildProjectDirectory)\bin\**\*;
71+
$(MSBuildProjectDirectory)\obj\**\*;
72+
$(MSBuildProjectDirectory)\publish\**\*;
73+
$(MSBuildProjectDirectory)\node_modules\**\*" />
74+
</ItemGroup>
75+
76+
<PropertyGroup>
77+
<_HasInvalidManifestJson>false</_HasInvalidManifestJson>
78+
<_HasInvalidManifestJson Condition="@(_InvalidManifestJson->Count()) > 0">true</_HasInvalidManifestJson>
79+
</PropertyGroup>
80+
81+
<Warning Condition="'$(_HasInvalidManifestJson)' == 'true'"
82+
Code="ELECTRON002"
83+
Text="Found electron-manifest.json file(s) in the project. This file format is no longer supported.
84+
85+
Files found:
86+
@(_InvalidManifestJson, '%0A')
87+
88+
MIGRATION REQUIRED:
89+
1. All properties from an existing electron-manifest.json file must now be specified as MSBuild properties in the project file.
90+
For more information, see: https://github.com/ElectronNET/Electron.NET/wiki/Migration-Checks#2-electron-manifestjson-not-allowed
91+
92+
2. The subtree from the 'build' property must be moved to the electron-builder.json file inside the './Properties' folder.
93+
For more information, see: https://github.com/ElectronNET/Electron.NET/wiki/Migration-Checks#3-electron-builderjson-location" />
94+
95+
</Target>
96+
97+
<!--
98+
Check 3: Single electron-builder.json must exist in ./Properties folder
99+
-->
100+
<Target Name="ElectronCheckElectronBuilderJson">
101+
102+
<!-- Check for electron-builder.json in the Properties folder -->
103+
<ItemGroup>
104+
<_ElectronBuilderJsonInProperties Include="$(MSBuildProjectDirectory)\Properties\electron-builder.json" />
105+
</ItemGroup>
106+
107+
<PropertyGroup>
108+
<_HasElectronBuilderJsonInProperties>false</_HasElectronBuilderJsonInProperties>
109+
<_HasElectronBuilderJsonInProperties Condition="Exists('$(MSBuildProjectDirectory)\Properties\electron-builder.json')">true</_HasElectronBuilderJsonInProperties>
110+
</PropertyGroup>
111+
112+
<!-- Check for electron-builder.json in wrong locations -->
113+
<ItemGroup>
114+
<_ElectronBuilderJsonWrongLocation Include="$(MSBuildProjectDirectory)\**\electron-builder.json"
115+
Exclude="$(MSBuildProjectDirectory)\Properties\electron-builder.json;
116+
$(MSBuildProjectDirectory)\bin\**\*;
117+
$(MSBuildProjectDirectory)\obj\**\*;
118+
$(MSBuildProjectDirectory)\publish\**\*;
119+
$(MSBuildProjectDirectory)\node_modules\**\*" />
120+
</ItemGroup>
121+
122+
<PropertyGroup>
123+
<_HasElectronBuilderJsonWrongLocation>false</_HasElectronBuilderJsonWrongLocation>
124+
<_HasElectronBuilderJsonWrongLocation Condition="@(_ElectronBuilderJsonWrongLocation->Count()) > 0">true</_HasElectronBuilderJsonWrongLocation>
125+
</PropertyGroup>
126+
127+
<Warning Condition="'$(_HasElectronBuilderJsonInProperties)' != 'true'"
128+
Code="ELECTRON003"
129+
Text="The project must contain an electron-builder.json file inside the './Properties' folder.
130+
131+
Expected location: $(MSBuildProjectDirectory)\Properties\electron-builder.json
132+
133+
REQUIRED ACTION:
134+
Create an electron-builder.json file in the Properties folder with your electron-builder configuration.
135+
For more information, see: https://github.com/ElectronNET/Electron.NET/wiki/Migration-Checks#3-electron-builderjson-location" />
136+
137+
<Warning Condition="'$(_HasElectronBuilderJsonWrongLocation)' == 'true'"
138+
Code="ELECTRON004"
139+
Text="Found electron-builder.json file(s) in incorrect location(s). The file must be located only in the './Properties' folder.
140+
141+
Files found in wrong locations:
142+
@(_ElectronBuilderJsonWrongLocation, '%0A')
143+
144+
REQUIRED ACTION:
145+
Move the electron-builder.json file to: $(MSBuildProjectDirectory)\Properties\electron-builder.json" />
146+
147+
</Target>
148+
149+
<!--
150+
Check 4: No parent-path references (..) in electron-builder.json
151+
-->
152+
<Target Name="ElectronCheckNoParentPaths"
153+
Condition="Exists('$(MSBuildProjectDirectory)\Properties\electron-builder.json')">
154+
155+
<PropertyGroup>
156+
<_ElectronBuilderJsonPath>$(MSBuildProjectDirectory)\Properties\electron-builder.json</_ElectronBuilderJsonPath>
157+
</PropertyGroup>
158+
159+
<!-- Read the file content -->
160+
<ReadLinesFromFile File="$(_ElectronBuilderJsonPath)">
161+
<Output TaskParameter="Lines" ItemName="_ElectronBuilderJsonLines" />
162+
</ReadLinesFromFile>
163+
164+
<!-- Check if any line contains parent path references -->
165+
<PropertyGroup>
166+
<_ElectronBuilderJsonContent>@(_ElectronBuilderJsonLines, ' ')</_ElectronBuilderJsonContent>
167+
<_HasParentPathReference>false</_HasParentPathReference>
168+
<_HasParentPathReference Condition="$(_ElectronBuilderJsonContent.Contains('../')) OR $(_ElectronBuilderJsonContent.Contains('..\\'))" >true</_HasParentPathReference>
169+
</PropertyGroup>
170+
171+
<Warning Condition="'$(_HasParentPathReference)' == 'true'"
172+
Code="ELECTRON005"
173+
Text="The electron-builder.json file contains parent-path references ('..').
174+
175+
File: $(_ElectronBuilderJsonPath)
176+
177+
MIGRATION REQUIRED:
178+
Parent-path references are not allowed in electron-builder.json because the file is copied to the build output directory during publishing.
179+
180+
HOW TO FIX:
181+
1. Place any resource files (icons, installers, etc.) inside your project folder structure
182+
2. Configure those files to be copied to the output directory using the project file:
183+
184+
&lt;ItemGroup&gt;
185+
&lt;Content Include=&quot;Assets\**\*&quot;&gt;
186+
&lt;CopyToOutputDirectory&gt;PreserveNewest&lt;/CopyToOutputDirectory&gt;
187+
&lt;/Content&gt;
188+
&lt;/ItemGroup&gt;
189+
190+
3. Reference files using relative paths from the output directory (e.g., 'Assets/myicon.ico' instead of '../Assets/myicon.ico')
191+
192+
For more information, see: https://github.com/ElectronNET/Electron.NET/wiki/Migration-Checks#4-parent-paths-not-allowed-in-electron-builderjson" />
193+
194+
</Target>
195+
196+
<!--
197+
Check 5: Pubxml files match the project type
198+
199+
This check validates that publish profiles are appropriate for the project type:
200+
- ASP.NET projects (using Microsoft.NET.Sdk.Web) have WebPublishMethod in their pubxml files
201+
- Console/other projects PublishProtocol instead of WebPublishMethod
202+
-->
203+
<Target Name="ElectronCheckPubxmlFiles">
204+
205+
<!-- Find all pubxml files -->
206+
<ItemGroup>
207+
<_PubxmlFiles Include="$(MSBuildProjectDirectory)\Properties\PublishProfiles\*.pubxml" />
208+
</ItemGroup>
209+
210+
<!-- Determine if this is an ASP.NET project (UsingMicrosoftNETSdkWeb is set by the Web SDK) -->
211+
<PropertyGroup>
212+
<_IsAspNetProject>false</_IsAspNetProject>
213+
<_IsAspNetProject Condition="'$(UsingMicrosoftNETSdkWeb)' == 'true'">true</_IsAspNetProject>
214+
<_HasPubxmlFiles>false</_HasPubxmlFiles>
215+
<_HasPubxmlFiles Condition="@(_PubxmlFiles->Count()) > 0">true</_HasPubxmlFiles>
216+
</PropertyGroup>
217+
218+
<!-- Build a combined content string for each file and check (only if files exist) -->
219+
<ItemGroup Condition="'$(_HasPubxmlFiles)' == 'true'">
220+
<_PubxmlFileInfo Include="@(_PubxmlFiles)" Condition="'%(Identity)' != ''">
221+
<FileContent>$([System.IO.File]::ReadAllText('%(Identity)'))</FileContent>
222+
</_PubxmlFileInfo>
223+
</ItemGroup>
224+
225+
<!-- Check each file for WebPublishMethod presence -->
226+
<ItemGroup Condition="'$(_HasPubxmlFiles)' == 'true'">
227+
<_PubxmlFileInfoWithFlags Include="@(_PubxmlFileInfo)" Condition="'%(Identity)' != ''">
228+
<HasWebPublishMethod>$([System.Text.RegularExpressions.Regex]::IsMatch('%(FileContent)', '&lt;WebPublishMethod&gt;'))</HasWebPublishMethod>
229+
</_PubxmlFileInfoWithFlags>
230+
</ItemGroup>
231+
232+
<!-- For ASP.NET projects: find pubxml files MISSING WebPublishMethod -->
233+
<ItemGroup>
234+
<_AspNetMissingWebPublishMethod Include="@(_PubxmlFileInfoWithFlags)"
235+
Condition="'$(_IsAspNetProject)' == 'true' AND '%(HasWebPublishMethod)' == 'False'" />
236+
</ItemGroup>
237+
238+
<!-- For Console/Non-ASP.NET projects: find pubxml files that HAVE WebPublishMethod -->
239+
<ItemGroup>
240+
<_ConsolePubxmlWithAspNetProperties Include="@(_PubxmlFileInfoWithFlags)"
241+
Condition="'$(_IsAspNetProject)' != 'true' AND '%(HasWebPublishMethod)' == 'True'" />
242+
</ItemGroup>
243+
244+
<!-- Warning for ASP.NET projects with incorrect pubxml files -->
245+
<Warning
246+
Condition="'@(_AspNetMissingWebPublishMethod)' != ''"
247+
Code="ELECTRON006"
248+
Text="The publish profile '%(_AspNetMissingWebPublishMethod.Identity)' appears to be configured for a console application (missing WebPublishMethod property), but this is an ASP.NET project.
249+
250+
RECOMMENDED ACTION:
251+
1. Delete the existing publish profiles
252+
2. Use the Visual Studio Publishing Wizard to create a new profile:
253+
- Right-click on the project in Solution Explorer
254+
- Select 'Publish...'
255+
- Follow the wizard to create a Folder publish profile for ASP.NET
256+
257+
Note: ASP.NET Electron.NET projects require publish profiles with WebPublishMethod set to FileSystem.
258+
259+
For more information, see: https://github.com/ElectronNET/Electron.NET/wiki/Migration-Checks#5-publish-profile-validation" />
260+
261+
<!-- Warning for Console projects with incorrect pubxml files -->
262+
<Warning
263+
Condition="'@(_ConsolePubxmlWithAspNetProperties)' != ''"
264+
Code="ELECTRON007"
265+
Text="The publish profile '%(_ConsolePubxmlWithAspNetProperties.Identity)' appears to be configured for ASP.NET publishing (containing the WebPublishMethod property), but this is a console application project.
266+
267+
RECOMMENDED ACTION:
268+
1. Delete the existing publish profiles
269+
2. Use the Visual Studio Publishing Wizard to create a new profile:
270+
- Right-click on the project in Solution Explorer
271+
- Select 'Publish...'
272+
- Follow the wizard to create a Folder publish profile for console applications
273+
274+
Note: Console Electron.NET projects should not use ASP.NET-style publish profiles.
275+
276+
For more information, see: https://github.com/ElectronNET/Electron.NET/wiki/Migration-Checks#5-publish-profile-validation" />
277+
278+
</Target>
279+
280+
</Project>

0 commit comments

Comments
 (0)