Search code examples
powershellandroid-studioautomation

How to fix a PowerShell script to close automatically on success?


i created this script text for Android Studio launch configuration:

(flutter build web) -and (xcopy C:\Users\MyUser\AndroidStudioProjects\MyApp\build\web C:\Web\nginx\data\www\MyApp /E /C /H /R /O /Y)

It builds the web version and copies it to the Nginx folder.

How to make Android Studio close the console in case of success, and not close it in case of an error?


Solution

  • You're probably looking for something like this:

    • Windows PowerShell:
    flutter build web
    if ($0 -eq $LASTEXITCODE) { xcopy C:\Users\MyUser\AndroidStudioProjects\MyApp\build\web C:\Web\nginx\data\www\MyApp /E /C /H /R /O /Y }
    if ($0 -eq $LASTEXITCODE) { exit 0 }
    pause # An error occurred - keep the window open
    exit $LASTEXITCODE
    
    flutter build web && 
      xcopy C:\Users\MyUser\AndroidStudioProjects\MyApp\build\web C:\Web\nginx\data\www\MyApp /E /C /H /R /O /Y &&
      $(exit 0)
    pause # An error occurred - keep the window open
    exit $LASTEXITCODE
    

    Note: I'm not familiar with Android Studio, but in case it opens an interactive, stay-open console to run tasks, you can use [System.Environment]::Exit(0) to exit the session as as whole rather than just your script.