Skip to content

Commit

Permalink
Merge pull request #2 from stavroskasidis/feature/remove-publish-issue
Browse files Browse the repository at this point in the history
Feature/remove publish issue
  • Loading branch information
stavroskasidis authored Feb 8, 2022
2 parents 642674e + 6a55d00 commit 9f10c62
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 30 deletions.
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,10 @@ dotnet add package BlazorWasmAntivirusProtection

2. Publish your app in Release mode and test it!
```
#Perform a clean first, see "Known Issue" below
dotnet clean BlazorHostedSampleApp.sln -c Release
dotnet publish Server\BlazorHostedSampleApp.Server.csproj -c Release
```
*Nuget package page can be found [here](https://www.nuget.org/packages/BlazorDialog).*

## ⚠️ Known issue ⚠️
If you try to publish a project after you have already published you have to clean your solution or the publish will fail with the following exception:

`error MSB4018: The "PInvokeTableGenerator" task failed unexpectedly.`

This happens because the il linker cannot load the existing modified dlls in the obj folder.
To prevent this error you should perform a clean on your solution before every publish, as displayed above.

## Configuration
The following options allow you to customize the tasks executed by this package.
### **Custom dll rename extension**
Expand Down Expand Up @@ -73,7 +62,12 @@ This work was inspired by the post in https://github.com/dotnet/aspnetcore/issue

## Release Notes

<details open="open"><summary>1.0</summary>
<details open="open"><summary>1.2</summary>

>- Fixed sequential publishing issue.
</details>
<details><summary>1.0</summary>

>- Added customization options.
</details>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.1" PrivateAssets="all" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="1.0.0-beta" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="1.2.0-beta" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.1" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="1.0.0-beta" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="1.2.0-beta" />
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions src/BlazorWasmAntivirusProtection.Tasks/ChangeDllHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ public override bool Execute()

if (DisableChangingDllHeaders)
{
Log.LogMessage(MessageImportance.High, $"Skipping changing .dll headers");
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Skipping changing .dll headers");
return true;
}


Log.LogMessage(MessageImportance.High, "Changing .dll headers from MZ to BZ");
Log.LogMessage(MessageImportance.High, "BlazorWasmAntivirusProtection: Changing .dll headers from MZ to BZ");
foreach (var asset in PublishBlazorBootStaticWebAsset)
{
var name = Path.GetFileName(asset.GetMetadata("RelativePath"));
if (Path.GetExtension(name) != ".dll") continue;

ChangeDllHeaderToBz(asset.ItemSpec);
Log.LogMessage(MessageImportance.High, $"Changed header of {asset.ItemSpec}");
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Changed header of {asset.ItemSpec}");
}
Log.LogMessage(MessageImportance.High, $"Changing .dll headers from MZ to BZ finished");
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Changing .dll headers from MZ to BZ finished");

return true;
}
Expand Down
45 changes: 45 additions & 0 deletions src/BlazorWasmAntivirusProtection.Tasks/CleanOldDlls.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace BlazorWasmAntivirusProtection.Tasks
{
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Text;

public class CleanOldDlls : Task
{
[Required]
public string IntermediateLinkDir { get; set; }

public override bool Execute()
{
if (!Directory.Exists(IntermediateLinkDir)) return true;

var linkSemaphore = Path.Combine(IntermediateLinkDir, "Link.semaphore");
var existingDll = Directory.GetFiles(IntermediateLinkDir, "*.dll").FirstOrDefault();
if (File.Exists(linkSemaphore) && existingDll != null && IsDllHeaderBz(existingDll))
{
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Cleaning old .dll files");
//We delete the Link.semaphore file to force a regeneration of objs in the IntermediateLinkDir
//This is to remove remnants of a previous publish
File.Delete(linkSemaphore);
}

return true;
}

bool IsDllHeaderBz(string fn)
{
using var fs = File.Open(fn, FileMode.Open, FileAccess.ReadWrite);
using var reader = new BinaryReader(fs);
var buffer = new byte[2];
reader.Read(buffer, 0 , buffer.Length);

var bz = Encoding.ASCII.GetBytes("BZ");
return bz[0] == buffer[0] && bz[1] == buffer[1];
}
}
}
16 changes: 8 additions & 8 deletions src/BlazorWasmAntivirusProtection.Tasks/RenameDlls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public override bool Execute()
{
if (DisableRenamingDlls)
{
Log.LogMessage(MessageImportance.High, $"Skipping renaming .dll files");
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Skipping renaming .dll files");
return true;
}
Log.LogMessage(MessageImportance.High,$"Renaming .dll files to .{RenameDllsTo}");
Log.LogMessage(MessageImportance.High,$"BlazorWasmAntivirusProtection: Renaming .dll files to .{RenameDllsTo}");
var frameworkDir = Directory.GetDirectories(PublishDir, "_framework", SearchOption.AllDirectories).First();
foreach(var file in Directory.GetFiles(frameworkDir,"*.*", SearchOption.AllDirectories))
{
if (file.EndsWith(".dll") || file.EndsWith(".dll.gz") || file.EndsWith(".dll.br"))
{
var newName = file.Replace(".dll", $".{RenameDllsTo}");
Log.LogMessage(MessageImportance.High, $"Renaming \"{file}\" to \"{newName}\"");
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Renaming \"{file}\" to \"{newName}\"");
if (File.Exists(newName)) File.Delete(newName);
File.Move(file, newName);
}
Expand All @@ -43,30 +43,30 @@ public override bool Execute()
var serviceWorkerPath = Path.Combine(frameworkDir, "service-worker-assets.js");


Log.LogMessage(MessageImportance.High, $"Updating \"{bootJsonPath}\"");
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Updating \"{bootJsonPath}\"");
var bootJson = File.ReadAllText(bootJsonPath);
bootJson = bootJson.Replace(".dll", $".{RenameDllsTo}");
File.WriteAllText(bootJsonPath, bootJson);

if (File.Exists(serviceWorkerPath))
{
Log.LogMessage(MessageImportance.High, $"Updating \"{serviceWorkerPath}\"");
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Updating \"{serviceWorkerPath}\"");
var serviceWorker = File.ReadAllText(serviceWorkerPath);
serviceWorker = serviceWorker.Replace(".dll", $".{RenameDllsTo}");
File.WriteAllText(serviceWorkerPath, serviceWorker);
}

if (File.Exists(bootJsonGzPath))
{
Log.LogMessage(MessageImportance.High, $"Deleting \"{bootJsonGzPath}\"");
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Deleting \"{bootJsonGzPath}\"");
File.Delete(bootJsonGzPath);
}
if (File.Exists(bootJsonBrPath))
{
Log.LogMessage(MessageImportance.High, $"Deleting \"{bootJsonBrPath}\"");
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Deleting \"{bootJsonBrPath}\"");
File.Delete(bootJsonBrPath);
}
Log.LogMessage(MessageImportance.High, $"Renaming .dll files to .{RenameDllsTo} finished");
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Renaming .dll files to .{RenameDllsTo} finished");

return true;
}
Expand Down
28 changes: 28 additions & 0 deletions src/BlazorWasmAntivirusProtection.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorWasmAntivirusProtecti
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorWasmAntivirusProtection.Tasks", "BlazorWasmAntivirusProtection.Tasks\BlazorWasmAntivirusProtection.Tasks.csproj", "{9B4ECABA-1FF3-4B4A-9223-DBBE1D1B1745}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SampleApps", "SampleApps", "{B6087F3A-2586-4098-BD69-449619BEA560}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BlazorHostedSampleApp", "BlazorHostedSampleApp", "{B0BE31C9-49A3-4DD5-9A92-218F35C2AF35}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorHostedSampleApp.Shared", "..\sampleapps\BlazorHostedSampleApp\Shared\BlazorHostedSampleApp.Shared.csproj", "{D4618F64-B8E4-4EF4-98B2-936421947434}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorHostedSampleApp.Server", "..\sampleapps\BlazorHostedSampleApp\Server\BlazorHostedSampleApp.Server.csproj", "{F2A0C954-4F13-4F30-8CAB-97D5E6800474}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorHostedSampleApp.Client", "..\sampleapps\BlazorHostedSampleApp\Client\BlazorHostedSampleApp.Client.csproj", "{5869E488-3490-4286-A632-3924847C9245}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,10 +31,28 @@ Global
{9B4ECABA-1FF3-4B4A-9223-DBBE1D1B1745}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B4ECABA-1FF3-4B4A-9223-DBBE1D1B1745}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B4ECABA-1FF3-4B4A-9223-DBBE1D1B1745}.Release|Any CPU.Build.0 = Release|Any CPU
{D4618F64-B8E4-4EF4-98B2-936421947434}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D4618F64-B8E4-4EF4-98B2-936421947434}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D4618F64-B8E4-4EF4-98B2-936421947434}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D4618F64-B8E4-4EF4-98B2-936421947434}.Release|Any CPU.Build.0 = Release|Any CPU
{F2A0C954-4F13-4F30-8CAB-97D5E6800474}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F2A0C954-4F13-4F30-8CAB-97D5E6800474}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F2A0C954-4F13-4F30-8CAB-97D5E6800474}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F2A0C954-4F13-4F30-8CAB-97D5E6800474}.Release|Any CPU.Build.0 = Release|Any CPU
{5869E488-3490-4286-A632-3924847C9245}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5869E488-3490-4286-A632-3924847C9245}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5869E488-3490-4286-A632-3924847C9245}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5869E488-3490-4286-A632-3924847C9245}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{B0BE31C9-49A3-4DD5-9A92-218F35C2AF35} = {B6087F3A-2586-4098-BD69-449619BEA560}
{D4618F64-B8E4-4EF4-98B2-936421947434} = {B0BE31C9-49A3-4DD5-9A92-218F35C2AF35}
{F2A0C954-4F13-4F30-8CAB-97D5E6800474} = {B0BE31C9-49A3-4DD5-9A92-218F35C2AF35}
{5869E488-3490-4286-A632-3924847C9245} = {B0BE31C9-49A3-4DD5-9A92-218F35C2AF35}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8E715717-C14A-4A09-BE07-93EF8245C66C}
EndGlobalSection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageProjectUrl>https://github.com/stavroskasidis/BlazorWasmAntivirusProtection</PackageProjectUrl>
<Description>This package attempts to guard against false positives from antiviruses that flag Blazor Wasm as malware</Description>
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>
</PropertyGroup>

Expand All @@ -21,11 +21,11 @@
</ItemGroup>

<Target Name="GetTasksOutputDlls" BeforeTargets="CoreCompile">
<MSBuild Projects="..\BlazorWasmAntivirusProtection.Tasks\BlazorWasmAntivirusProtection.Tasks.csproj" Targets="Publish;PublishItemsOutputGroup" Properties="Configuration=Release">
<MSBuild Projects="..\BlazorWasmAntivirusProtection.Tasks\BlazorWasmAntivirusProtection.Tasks.csproj" Targets="Publish;PublishItemsOutputGroup" Properties="Configuration=$(Configuration)">
<Output TaskParameter="TargetOutputs" ItemName="_TasksProjectOutputs" />
</MSBuild>
<ItemGroup>
<Content Include="@(_TasksProjectOutputs)" Condition="'%(_TasksProjectOutputs.Extension)' == '.dll'" Pack="true" PackagePath="tasks\%(_TasksProjectOutputs.TargetPath)" KeepMetadata="Pack;PackagePath" />
<Content Include="@(_TasksProjectOutputs)" Condition="('$(Configuration)' == 'Release' And '%(_TasksProjectOutputs.Extension)' == '.dll') Or &#xD;&#xA; '$(Configuration)' == 'Debug' And ('%(_TasksProjectOutputs.Extension)' == '.dll' Or '%(_TasksProjectOutputs.Extension)' == '.pdb')" Pack="true" PackagePath="tasks\%(_TasksProjectOutputs.TargetPath)" KeepMetadata="Pack;PackagePath" />
</ItemGroup>
</Target>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
AssemblyFile="$(MSBuildThisProjectFileDirectory)..\..\tasks\BlazorWasmAntivirusProtection.Tasks.dll" />
<UsingTask TaskName="BlazorWasmAntivirusProtection.Tasks.RenameDlls"
AssemblyFile="$(MSBuildThisProjectFileDirectory)..\..\tasks\BlazorWasmAntivirusProtection.Tasks.dll" />
<UsingTask TaskName="BlazorWasmAntivirusProtection.Tasks.CleanOldDlls"
AssemblyFile="$(MSBuildThisProjectFileDirectory)..\..\tasks\BlazorWasmAntivirusProtection.Tasks.dll" />

<PropertyGroup>
<ComputeBlazorExtensionsDependsOn>$(ComputeBlazorExtensionsDependsOn);_ChangeDllHeaders</ComputeBlazorExtensionsDependsOn>
</PropertyGroup>


<Target Name="_CleanOldDlls" BeforeTargets="PrepareForILLink">
<CleanOldDlls IntermediateLinkDir="$(IntermediateLinkDir)"></CleanOldDlls>
</Target>

<Target Name="_ChangeDllHeaders">
<ChangeDllHeaders PublishBlazorBootStaticWebAsset="@(PublishBlazorBootStaticWebAsset)" DisableChangingDllHeaders="$(DisableChangingDllHeaders)"></ChangeDllHeaders>
</Target>
Expand Down

0 comments on commit 9f10c62

Please sign in to comment.