Swift is a programming language developed by Apple. It provides a fast, safe and modern option for developing iOS apps from scratch without having to learn Objective-C or manually write much code at all. The primary goal of Swift was to make it easier for developers in the enterprise space to use their time coding rather than learning new languages like Java and C#. But there are some great advantages offered by using Swift even if you’re not building an app for iPhones:

You can use the command line to quickly access and manipulate your files, but there are a few things you need to be aware of. This article will show you how easy it is to get started with file management in Terminal on macOS.

The “swift scripting” is a command-line tool that allows users to beak into their Swift files from the command line.

1635761786_751_Beak-Peck-into-your-Swift-files-from-the-command-line

Using the command line, look over your Swift files.

Beak can take a typical Swift file and use a command line interface to list and execute any public global functions in it.

This comes in handy when writing Swift scripts and make-like files. You can use Swift code to replace build or rake files!

An example Swift script is as follows:

/ This is a collection of links As a dependent, use https://github.com/kylef/PathKit / beak: kylef/PathKit @ 1.0.0 import PathKit / import PathKit from the dependency list above. The product is released by the foundation / – Parameters: / – version: the version of the function to be released to the public (version: String) print(“version (version) released!”) throws / implementation here / Installs the product public func install() throws print(“installed”) print(“installed”) print(“installed”) print(“installed”) print(“installed”) print(“installed”) print(“installed

Release of the $ beak list: Installs the product once it has been released: Installs the product using the command $ beak run release —version 1.2.0 Version 1.2.0 is now available!

What is the mechanism behind it?

Beak uses SourceKit to scan your Swift code and locate all public and global functions. It creates a command line interface using information such as function and parameter names, types, and default values. It also builds up detailed assistance using standard comment documentation.

Beak can analyze specific comments at the start of your script and use the Swift Package Manager to bring in dependencies.

Beak searches for a file named beak.swift in your current directory by default, but you may use —path to provide a different swift file. This repository provides a Beak file that may be used to execute build scripts.

Installing

First, make sure you have Xcode 10.2+ installed.

Mint 🌱

yonaskolb/beak yonaskolb/beak yonaskolb/beak yon

Homebrew

yonaskolb/Beak brew tap $ https://github.com/yonaskolb/Beak.git install Beak $ brew

Beak and Swift PM

This uses Swift PM to build and run beak from this repository, which then calls the install method in the Beak.swift file, which is also included in this repository. It’s so meta!

https://github.com/yonaskolb/Beak.git $ git clone cd cd cd cd cd cd $ quick beak beak beak beak beak beak beak beak beak be

Usage

Functions in a list:

This diagram depicts all of the functions that may be used.

Release of the $ beak list: Installs the product once it has been released: Installs the item.

Execute the following command:

This executes a function with arguments. This function will be called before any top-level expressions in the swift file.

$ beak run release —version 1.2.0 version 1.2.0 is now available for download.

Execute the swift file.

It’s also possible to execute the whole script rather than just a single function.

Make changes to the swift file.

If you have declared any dependencies, this builds and starts an Xcode project with all dependencies connected, which is beneficial for code completion. To commit any changes you made in Xcode back to the original file, enter c on the command line.

creating a project with $ beak edit

If you need additional information about a command or a function, you can always use —help. The information from the doc comments will be used in this.

Functions

Functions must be global and made public in order to be accessible. Any non-public functions may be used as helper functions, but Beak will not notice them.

CustomStringConvertible may be used to print any faults thrown by functions. This makes it simple to flunk your assignments. To have tossing functions, you must add an import Foundation in your script for the time being.

Parameters

Any options that don’t have default values will need to be filled up.

Int, Bool, and String are natively available parameter types. All other types will be supplied as raw values, so if it builds, you may send it anything, even an enum value—buildType.debug.

Positional arguments may be used with function parameters that don’t have labels:

public func release( version: String) version: String) version: String) version: String) version: String)

Dependencies

It’s sometimes handy to have the ability to utilize other Swift packages as dependents in your script. This may be accomplished by adding some specific remarks to the file’s top. It has to be in the following format:

/ beak: repo library library library… @ version

where the following components are included:

  • Swift packages are stored in a git repo called repo. This may be as simple as user/repo or as complex as https://github.com/user/repo.git.
  • library: a list of libraries to include from this package, separated by spaces. The repo name is the default, which is typically what you want.
  • version: the package’s version to include. This may be a basic version string or any of the kinds permitted by the Swift Package Manager Requirement static members, for example.
    • .branch or branch:develop (“develop”)
    • .revision:ab794ebb or ab794ebb (“ab794ebb”)
    • .exact or exact:1.2.0 (“1.2.0”)
    • .upToNextMajorMajorMajorMajorMajorMajor (from: “1.2.0”)
    • .upToNextMinor is a function that takes you to the next minor (from: “1.2.3”)

Here are several examples:

/ beak: JohnSundell/ShellOut @ 2.0.0 import beak: kylef/PathKit @ upToNextMajor:0.9.0 import beak: apple/swift-package-manager Utility @ branch:master import beak: apple/swift-package-manager Utility @ branch:master import beak: apple/swift-package-manager Utility @ branch:master import beak: apple/swift-package Pathkit import Foundation import ShellOut is a utility for importing files.

For these imported dependencies, you may use beak edit to receive code completion.

Shebang

You can execute your swift file directly without calling beak if you add a beak shebang at the top of it and then run chmod a+x beak.swift on it to make it executable.

tasks.swift

—path #!/usr/bin/env beak foo public func () if print(“hello foo”) if print(“hello foo”) if print(“hello foo”) print(“hello bar”) public func bar()

tasks.swift $ chmod a+x run foo hello foo with $./tasks.swift

After that, you may execute this file from anywhere if you put it under usr/local/bin:

tasks.swift /usr/local/bin/tasks $ cp tasks.swift /usr/local/bin/tasks $ run bar tasks hi there, bar

Change your shebang to #!/usr/bin/env beak run —path to automatically insert the run option.

Alternatives

License

The MIT license is used to license Beak. For further information, see LICENSE.

GitHub

https://github.com/yonaskolb/Beak

The “swift package command line” is a command-line tool that allows users to perform tasks such as extracting, building, and signing their Swift files.

Related Tags

  • swift shell command
  • swift scripting tutorial
  • swift package manager commands
  • swift package manager command line
  • spm command line
Author

Peter started his tech website because he was motivated by a desire to share his knowledge with the world. He felt that there was a lot of information out there that was either difficult to find or not presented in a way that was easy to understand. His website provides concise, easy-to-understand guides on various topics related to technology. Peter's ultimate goal is to help people become more comfortable and confident with technology. He believes that everyone has the ability to learn and use technology, and his website is designed to provide the tools and information necessary to make that happen.