Can xcodebuild delete the contents of the project's Build Folder?
Asked Answered
R

2

7

Back in Xcode 9, there was a build option called "Clean Build Folder..." (K), which deleted all files in the build folder, only leaving the folder behind with no contents. Since then, this behavior was removed, the menu item's title changed to "Clean Build Folder", and now behaving like the old "Clean" used to.

xcodebuild has a build option called clean which simply does the same thing as Xcode's "Clean Build Folder" (K), which leaves stuff around.

Is there any way to delete all files in the build folder via a scriptable command?


What I've tried so far:

xcodebuild clean -workspace "My Workspace.xcworkspace" -scheme "My Scheme"

This, as I said, doesn't actually clean everything up. For that, I added this bodge to my build script:

export IS_XCODE_CACHE_FOLDER_PRESENT="`ls -la ~/Library/Developer/ | grep -x "Xcode"`"

if [ 0 -ne "$IS_XCODE_CACHE_FOLDER_PRESENT" ]; then
    echo "Xcode cache folder should not be present at build time! Attempting to delete..."
    rm -rf "~/Library/Developer/Xcode"
    RM_RESULT=$?
    if [ 0 -ne "$RM_RESULT" ]; then
        echo "FAILED to remove Xcode cache folder!"
        exit $RM_RESULT
    fi
fi
Reprimand answered 22/3, 2018 at 16:57 Comment(2)
What have you tried so far?Cytosine
@ElisevanLooij Thanks for asking! Question updatedReprimand
A
2

I faced a similar requirement. So after trying for several hours, I resolved to a custom script instead of using Xcode's run script.

So instead of using Xcode to run the app on the simulator I use my script which in turn first cleans the build folder, then builds the project, then installs and finally launches the app in the simulator.

Here is what I am using as a quick script:

 # Delete Build directory
 rm -rf ./build/Build

 # pod install
 pod install

 # Build project
 xcrun xcodebuild -scheme Example -workspace Example.xcworkspace -configuration Debug -destination 'platform=iOS Simulator,name=iPhone 11 Pro Max,OS=13.1' -derivedDataPath build

 # Install App
 xcrun simctl install "iPhone 11 Pro Max" ./build/Build/Products/Debug-iphonesimulator/Example.app/

 # Launch in Simulator
 xcrun simctl launch "iPhone 11 Pro Max" com.ihak.arpatech.Example

Note: See this question I posted to know the issue I was facing.

Akvavit answered 3/12, 2019 at 10:58 Comment(0)
D
-1

You can add clean action.

xcodebuild clean build -workspace "My Workspace.xcworkspace" -scheme "My Scheme"

see more in man xcodebuild

 action ...
           Specify one or more actions to perform. Available actions are:

           build                  Build the target in the build root (SYMROOT).  This is the default action, and is used if no action is given.

           build-for-testing      Build the target and associated tests in the build root (SYMROOT).  This will also produce an xctestrun file in the build root. This requires speci-
                                  fying a scheme.

           analyze                Build and analyze a target or scheme from the build root (SYMROOT).  This requires specifying a scheme.

           archive                Archive a scheme from the build root (SYMROOT).  This requires specifying a scheme.

           test                   Test a scheme from the build root (SYMROOT).  This requires specifying a scheme and optionally a destination.

           test-without-building  Test compiled bundles. If a scheme is provided with -scheme then the command finds bundles in the build root (SRCROOT).  If an xctestrun file is
                                  provided with -xctestrun then the command finds bundles at paths specified in the xctestrun file.

           installsrc             Copy the source of the project to the source root (SRCROOT).

           install                Build the target and install it into the target's installation directory in the distribution root (DSTROOT).

           clean                  Remove build products and intermediate files from the build root (SYMROOT).
Dasteel answered 9/7, 2020 at 9:42 Comment(1)
My question mentions that I've already tried this. Just in case things have changed since then, I just tried this with the app store version of Xcode (currently 11.5), and it left the build folder around: i.imgur.com/UB8dpEd.mp4Reprimand

© 2022 - 2024 — McMap. All rights reserved.