0

I work on a Blazor Wasm PWA with AOT. I created a PowerShell script, that I want to execute before the publish build. So, I added

<Target Name="PrePublishScript" BeforeTargets="PrepareForPublish">
    <Exec Command="powershell -ExecutionPolicy Bypass -File script.ps1" />
</Target>

in the project file. This code works, but my script is executed twice:

  1. Initialize build process.
  2. Run script.
  3. "Optimizing assemblies for size. This process might take a while."
  4. Run script again (not wanted).
  5. brotli some files
  6. AOTing

I created a log file via msbuild WRCT.csproj /t:Publish /p:Configuration=Release /v:diag > build.log. Its content contains PrepareForPublish twice. Could this be the reason for the doubled execution?

_CorePublishTargets =
PrepareForPublish;
ComputeAndCopyFilesToPublishDirectory;
;
PublishItemsOutputGroup;

and

_PublishNoBuildAlternativeDependsOn = 
BuildOnlySettings;
_PreventProjectReferencesFromBuilding;
ResolveReferences;
PrepareResourceNames;
ComputeIntermediateSatelliteAssemblies;
ComputeEmbeddedApphostPaths;
;
PrepareForPublish;
ComputeAndCopyFilesToPublishDirectory;
;
PublishItemsOutputGroup;

Maybe, I could find a BeforeTargets="Whatever" in the log file, but its size is 315 Mb...

How can I avoid, executing the script twice?
Or is there is a other way for executing the script automatically before the publish build?

Edit

In my log file, I found the target `_GatherBlazorFilesToPublish` and used it as `BeforeTargets="_GatherBlazorFilesToPublish">`. This works and now my script is executed only once, but to be honest, I have no idea what I am doing. Is it okay to use this target?
1

2 Answers 2

0

The edit in the question offers a possible solution, but it has a downside. Targets with leading underscore (_) are internal targets, that could be changed. So, today _GatherBlazorFilesToPublish works, but it could be renamed. Here's a more reliable possibility:

<Target Name="ExecuteScriptCondition" BeforeTargets="Build">
    <PropertyGroup>
        <ExecuteScript>true</ExecuteScript>
    </PropertyGroup>
</Target>

<Target Name="PrePublishScript" BeforeTargets="BeforePublish" Condition="'$(ExecuteScript)' == 'true'">
    <PropertyGroup>
        <ExecuteScript>false</ExecuteScript>
    </PropertyGroup>
    <Exec Command="powershell -ExecutionPolicy Bypass -File script.ps1" />
</Target>

The build process is the first thing that happens. Above first target now defines the variable ExecuteScript and sets it to "true". When it is time for BeforePublish ExecuteScript is set to false and the script is executed. On the second time for BeforePublish Condition="'$(ExecuteScript)' == 'true'" evaluates to false and the script is not executed.

Sign up to request clarification or add additional context in comments.

Comments

0

The first answer I posted, definitly works, so I kept it below. But I found a smaller solution:

<Target Name="PrePublishScript" BeforeTargets="BeforeBuild" Condition="'$(IsPublishing)' == 'true'">
    <Exec Command="powershell -ExecutionPolicy Bypass -File script.ps1" />
</Target>

Now, the script is executed BeforeBuild but only when the variable IsPublishing is "true". BeforeBuild also makes sure, that it is the first thing, that happens. In my case, this was important, because the script changed some JS files and when the app was used in offline mode, an integrity check failed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.