top of page
Search
dekeberna1983

Run Bash From A Swift Project



If you'd like to use the bash environment for calling commands use the following bash function which uses a fixed up version of Legoless. I had to remove a trailing newline from the shell function's result.


The left column shows a list of files in this project. Select the ContentView.swift file. This file contains the Swift UI code that sets up the interface. You will see the code in the center pane. The relevant part of the code is:




Run Bash From A Swift Project



yes. I made some progress. If I remove the sandbox entitlement completely from the project it works, so it is related to that. But I cannot figure out how to create an exception to the sandbox entitlements for this tool in /usr/local/bin


xcodebuild is a command-line tool that allows you to perform build, query, analyze, test, and archive operations on your Xcode projects and workspaces from the command line. It operates on one or more targets contained in your project, or a scheme contained in your project or workspace. xcodebuild provides several options for performing these operations as seen its man page. xcodebuild saves the output of your commands in the locations defined in the Locations preferences pane of your Xcode application, by default.


When Xcode executes any run script phase, it shares all its build settings through environment variables. Think of environment variables as global variables. You can still read them from your Swift code. In HelloXcode.swift, add the following in main():


The sky is the limit for what you can do in the build phases with Swift. You can download resources used by the project from a remote server, you can validate more things in your project directly from Xcode or you can automate configuration changes or even upload the compiled binary yourself. You can literally program your own CI/CD if you want to. :]


While reading the SPM documentation, i found that many of the Xcode concepts I'm familiar with can be described in the Package.swift manifest (e.g. targets, products, build-settings, build-configurations...).I found nothing about Schemes in the documentation, but I noticed that Xcode automatically creates a hidden .swiftpm directory which can then contain my Schemes .But I still haven't found an easy way to run a Build-Phase script, like in my other Xcode projects.


The first two options (the -L ... ones) tell the native compiler where to find the swift libraries. The native compiler will ignore libraries that don't have the correct architecture, which means that it's possible to pass the location for both simulator libraries and device libraries at the same time, so that it works for both simulator and device builds (these paths are only correct for iOS; for tvOS and watchOS they have to be updated). One downside is that this approach requires that the correct Xcode is in /Application/Xcode.app, if the consumer of the binding library has Xcode in a different location, it won't work. The alternative solution is to add these options in the additional mtouch arguments in the executable project's iOS Build options (--gcc_flags -L... -L...). The third option makes the native linker store the location of the swift libraries in the executable, so that the OS can find them.


Actually, there's really not much to do here! CI is enabled by default on newprojects. If your iOS project has some environment variables you want to keepsecret, but you want to keep the project public on GitLab, you may want to disablePublic builds in Project Settings, under Continuous Integration. This willhide the build results from everyone except members of the project.


The archive_project job runs two scripts: the first cleans and archives yourXcode project, and the second builds an .ipa file; the only setting means thatthis job will only run when we commit to master. Notice that it also definesartifacts; after the ProjectName.ipa application archive is created, thisoption uploads it to GitLab, where you can later download it from the Build page.


The xcodebuild command takes several arguments specifying how to build an Xcode project (or workspace). Here we'll see how to pass in two arguments, for the project path and scheme, from the command line. The rest will be hard-coded. Below the shell function, add the following:


You should now be able to run ./generate-coverage.swift /Documents/Dev/CodableKeychain/CodableKeychain.xcodeproj/ CodableKeychainMobile --verbose in Terminal, replacing the project path and scheme arguments with the values for your project, and observe the output in real-time.


Note that if you wanted you could pass in the other arguments xcodebuild takes from Terminal in order to customize the destination, whether code coverage is enabled, and code signing options. You could also build a workspace by replacing -project with -workspace and providing a workspace path.


As with any process, we have a Standard Output, Standard Error, and Standard Input. The first two are used to output data and the last one to receive data. If we want to capture the Process standard output, we can redirect it to a Pipe and read from the pipe. The same idea like how we do it in bash using the (pipe operator).


When you want to build more complex CLI tools using Swift, have a look at Vapor ConsoleKit. If you've used Vapor before you've probably noticed the very slick tool they built. They used ConsoleKit to create it.Jonas one of the project core contributors suggested (on reddit) to have a look at this Demo.He also said: "The example is from the current master branch which is for the new Vapor 4, which is in Alpha, the 3.3.0 tag is for Vapor 3"


This guide demonstrates how to create a Swift application with Gradle using gradle init.You can follow the guide step-by-step to create a new project from scratch or download the complete sample project using the links above.


It's time to put a copy of your source files on the server. Do this in any way you please. Download the source directly, clone from Git, use scp, whatever you like. I've create an /app directory in my home folder in which to clone the project source files to.


Before beginning, I'd suggest creating a simple bash script named run.sh that Supervisor can run to start your Vapor app. For Petty, I use the one below, and the file is in the root directory of my project.


When you need to interact with an Xcode project from the terminal, xcodebuild is the first place to look in.This command-line tool allows you to perform different actions on an Xcode project or workspace, such as building, archiving, querying information, and of course, testing.


Swift command line tools can be added to our project by simply adding them as a dependency within Package.swift, alongside framework dependencies we may already be using. We will be running the tools directly and so there is no need to add them as dependencies to any of our targets, we just need SPM to download them for us.


For our Uploader Package.swift alongside ArgumentParser which we are using as a dependency on our main target, we can include tools: SwiftLint and SwiftFormat. As they are added just like other dependencies, we can specify the version of the command line tool to use, allowing all developers on the project to use the same version. This is especially important for tools that are linting or formatting code, as it means the same rules will be applied for all developers.


Our task executable is developed just like any other CLI, although it will be a simple one containing very little code. We could do it manually within main.swift, however, as we will need to run different tasks as arguments to the main command we will need to be parsing these arguments and making it easy for users to find the tasks they can run. To make this really easy there is the ArgumentParser framework from Apple.


A really nice feature of ArgumentParser is that it generates a help message from the command structs we have created. This aids discovery of the tasks that can be executed on our project by using swift run task -h or --help.


These patterns are file system glob patterns ('?' is a wildcard for a single character, '' is a wildcard for zero or more characters). For example, suppose you're running in bash on Linux, you've set --detect.detector.search.depth=1, and have a subdirectory named blackduck-common (a gradle project) that you want to exclude from the detector search. Any of the following would exclude it: --detect.detector.search.exclusion.patterns=blackduck-common, --detect.detector.search.exclusion.patterns='blackduck-common', --detect.detector.search.exclusion.patterns='blackduck-'


You should then see a new section added where you can inject your bash script. In fact, if you've been going crazy writing Swift scripts after reading my post on scripting in swift, you can place it in your project's root directory and you can execute it from your new run script!


UPDATE November 2022: I recommend using a different script that is optimized and works well for Xcode projects/workspaces as well as Swift Packages. I share open-iterm-from-xcode as a gist with you.


1 Swift 5.7.3 contains Linux and Windows changes only, Swift 5.7.2 is available as part of Xcode 14.2.2 Swift 5.7.3 Windows 10 toolchain is provided by Saleem Abdulrasool. Saleem is the platform champion for the Windows port of Swift and this is an official build from the Swift project.


Let me show you how to run Swift under linux inside a Docker container. First of all, install Docker (fastest way is brew cask install docker), start the app itself (give it some permissions), and pull the official Swift Docker image from the cloud by using the docker pull swift command. ?


The first thing I'd like to teach you is how to create a custom Docker image & pack all your Swift source code into it. Just create a new Swift project swift package init --type=executable inside a folder and also make a new Dockerfile:


The first option is that you execute a bash docker run -it my-swift-image bash and log in to your container so you'll be able to edit Swift source files inside of it & build the whole package by using swift build or you can run swift test if you'd just like to test your app under Linux. 2ff7e9595c


0 views0 comments

Recent Posts

See All

Comments


bottom of page