最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

blazor - Why is my pre-publish PowerShell script executed twice? - Stack Overflow

programmeradmin0浏览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?

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? Share edited Mar 5 at 6:54 Henk Holterman 274k32 gold badges351 silver badges533 bronze badges asked Mar 4 at 11:47 FlippowitschFlippowitsch 5471 gold badge8 silver badges24 bronze badges 1
  • 1 There is a set of "public" targets that are safe to reference - see "Table of predefined targets". – Jonathan Dodds Commented Mar 5 at 12:16
Add a comment  | 

2 Answers 2

Reset to default 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.

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.

发布评论

评论列表(0)

  1. 暂无评论