puzzler
puzzler

Reputation: 246

Visual Studio 2022 Community Publish event called too early?

I want to delete appsettings.json from the publish folder after publishing the web application. I've added a bat file to do the job and call it on AfterTargets="Publish". My bat file gets executed fine, but it seems the published files are copied in the folder after the script has been called. How can I achieve my goal (without using time delay in the bat)?

The publish profile looks like this:

<Project>
  <PropertyGroup>
    <DeleteExistingFiles>true</DeleteExistingFiles>
    <ExcludeApp_Data>false</ExcludeApp_Data>
    <LaunchSiteAfterPublish>true</LaunchSiteAfterPublish>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <PublishProvider>FileSystem</PublishProvider>
    <PublishUrl>C:\Publish\MyWebApp</PublishUrl>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <_TargetId>Folder</_TargetId>
    <SiteUrlToLaunchAfterPublish />
    <TargetFramework>net9.0</TargetFramework>
    <ProjectGuid>dd151040-6c68-4080-986d-fc920e06a562</ProjectGuid>
    <SelfContained>false</SelfContained>
  </PropertyGroup>
</Project>

I've set the target in MyWebApp.csproj:

  <Target Name="AfterPublishDelSet" AfterTargets="Publish">
    <Exec Command="dset.bat $(PublishUrl)" />
  </Target>

dset.bat changes the directory and tries to delete appsettings.json:

cd %1

IF EXIST appsettings.json (
    del appsettings.json
) ELSE (
    ECHO "appsettings.json not found"
)

And the output seems to indicate that the files are copied in the publish dir from PubTmp\Out after the script is called:

MyWebApp -> C:\MyWebApp\MyWebApp\obj\Release\net9.0\PubTmp\Out\
clearSettings.bat C:\Publish\MyWebApp
"appsettings.json not found"
Web App was published successfully file:///C:/Publish/MyWebApp

Upvotes: 1

Views: 19

Answers (0)

Related Questions