All Projects → yonaskolb → Beak

yonaskolb / Beak

Licence: mit
A command line interface for your Swift scripts

Programming Languages

swift
15916 projects
script
160 projects

Projects that are alternatives of or similar to Beak

Mask
🎭 A CLI task runner defined by a simple markdown file
Stars: ✭ 495 (-7.82%)
Mutual labels:  make, cli
Hatch
A modern project, package, and virtual env manager for Python
Stars: ✭ 2,268 (+322.35%)
Mutual labels:  cli, package-manager
Ni
💡 Use the right package manager
Stars: ✭ 179 (-66.67%)
Mutual labels:  cli, package-manager
Wapm Cli
📦 WebAssembly Package Manager (CLI)
Stars: ✭ 236 (-56.05%)
Mutual labels:  cli, package-manager
Pizza Cli
🍕 Order a pizza in a CLI app (just for fun!)
Stars: ✭ 58 (-89.2%)
Mutual labels:  make, cli
Npx
execute npm package binaries (moved)
Stars: ✭ 2,634 (+390.5%)
Mutual labels:  cli, package-manager
Stow
GNU Stow - mirror of savannah git repository occasionally with more bleeding-edge branches
Stars: ✭ 207 (-61.45%)
Mutual labels:  cli, package-manager
Foy
A simple, light-weight and modern task runner for general purpose.
Stars: ✭ 157 (-70.76%)
Mutual labels:  make, cli
Nve
Run any command on specific Node.js versions
Stars: ✭ 531 (-1.12%)
Mutual labels:  cli, package-manager
Awless
A Mighty CLI for AWS
Stars: ✭ 4,821 (+797.77%)
Mutual labels:  cli
Ttyplot
a realtime plotting utility for terminal/console with data input from stdin
Stars: ✭ 532 (-0.93%)
Mutual labels:  cli
Saws
A supercharged AWS command line interface (CLI).
Stars: ✭ 4,886 (+809.87%)
Mutual labels:  cli
Table
Formats data into a string table.
Stars: ✭ 524 (-2.42%)
Mutual labels:  cli
Git Labelmaker
🎏 Manage your GitHub labels from the command line!
Stars: ✭ 534 (-0.56%)
Mutual labels:  cli
Patch Package
Fix broken node modules instantly 🏃🏽‍♀️💨
Stars: ✭ 6,062 (+1028.86%)
Mutual labels:  package-manager
Tget
tget is wget for torrents
Stars: ✭ 532 (-0.93%)
Mutual labels:  cli
Jrnl
Collect your thoughts and notes without leaving the command line.
Stars: ✭ 5,126 (+854.56%)
Mutual labels:  cli
Unix
Mirror of the Restoration of 1st Edition UNIX kernel sources from pdf document.
Stars: ✭ 524 (-2.42%)
Mutual labels:  make
Standard Version
🏆 Automate versioning and CHANGELOG generation, with semver.org and conventionalcommits.org
Stars: ✭ 5,806 (+981.19%)
Mutual labels:  cli
Grim
Grab images from a Wayland compositor
Stars: ✭ 533 (-0.74%)
Mutual labels:  cli

Beak 🐦

SPM Linux Git Version Build Status license

Peck into your Swift files from the command line

Beak can take a standard Swift file and then list and run any public global functions in it via a command line interface.

This is useful for scripting and for make-like files written in Swift. You can replace make or rake files with code written in Swift!

An example Swift script:

// This links https://github.com/kylef/PathKit as a dependency
// beak: kylef/PathKit @ 1.0.0

import PathKit // from the dependency listed above
import Foundation

/// Releases the product
/// - Parameters:
///   - version: the version to release
public func release(version: String) throws {
    // implementation here
    print("version \(version) released!")
}

/// Installs the product
public func install() throws {
    // implementation here
    print("installed")
}
$ beak list
    release: Releases the product
    install: Installs the product
$ beak run release --version 1.2.0
  version 1.2.0 released!

How does it work?

Beak analyzes your Swift file via SourceKit and finds all public and global functions. It uses information about the function and parameter names, types and default values to build up a command line interface. It also uses standard comment docs to build up descriptive help.

Beak can parse special comments at the top of your script so it can pull in dependencies via the Swift Package Manager.

By default Beak looks for a file called beak.swift in your current directory, otherwise you can pass a path to a different swift file with --path. This repo itself has a Beak file for running build scripts.

Installing

Make sure Xcode 10.2+ is installed first.

Mint 🌱

$ mint install yonaskolb/beak

Homebrew

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

Swift PM and Beak 🐦

This uses Swift PM to build and run beak from this repo, which then runs the install function inside the Beak.swift file also incuded in this repo. So meta!

$ git clone https://github.com/yonaskolb/Beak.git
$ cd Beak
$ swift run beak run install

Usage

List functions:

This shows all the functions that can be run

$ beak list

  release: Releases the product
  install: Installs the product

Run a function:

This runs a specific function with parameters. Note that any top level expressions in the swift file will also be run before this function.

$ beak run release --version 1.2.0
version 1.2.0 released

Run the swift file

It's also possible to just run the whole script instead of a specific function

$ beak run

Edit the swift file

This generates and opens an Xcode project with all dependencies linked, which is useful for code completion if you have defined any dependencies. The command line will prompt you to type c to commit any changes you made in Xcode back to the original file

$ beak edit
generating project

You can always use --help to get more information about a command or a function. This will use information from the doc comments.

Functions

For function to be accessible they must be global and declared public. Any non-public functions can be as helper functions, but won't be seen by Beak.

Functions can be throwing, with any errors thrown printed using CustomStringConvertible. This makes it easy to fail your tasks. For now you must include an import Foundation in your script to have throwing functions

Parameters

Any parameters without default values will be required.

Param types of Int, Bool, and String are natively supported. All other types will be passed exactly as they are as raw values, so if it compiles you can pass in anything, for example an enum value--buildType .debug.

Function parameters without labels will can be used with positional arguments:

public func release(_ version: String) { }
beak run release 1.2.0

Dependencies

Sometimes it's useful to be able to pull in other Swift packages as dependencies to use in your script. This can be done by adding some special comments at the top of your file. It must take the form:

// beak: {repo} {library} {library} ... @ {version}`

where items in {} are:

  • repo: is the git repo where a Swift package resides. This can take a short form of user/repo or an extended form https://github.com/user/repo.git
  • library: a space delimited list of libraries to include from this package. This defaults to the repo name, which is usually what you want.
  • version: the version of this package to include. This can either be a simple version string, or any of the types allowed by the Swift Package Manager Requirement static members eg
    • branch:develop or .branch("develop")
    • revision:ab794ebb or .revision("ab794ebb")
    • exact:1.2.0 or .exact("1.2.0")
    • .upToNextMajor(from: "1.2.0")
    • .upToNextMinor(from: "1.2.3")

Some examples:

// beak: JohnSundell/ShellOut @ 2.0.0
// beak: kylef/PathKit @ upToNextMajor:0.9.0
// beak: apple/swift-package-manager Utility @ branch:master

import Foundation
import Pathkit
import ShellOut
import Utility

You can use beak edit to get code completion for these imported dependencies.

Shebang

If you put a beak shebang at the top of your swift file and then run chmod a+x beak.swift on it to make it executable, you will be able to execute it directly without calling beak.

tasks.swift

#!/usr/bin/env beak --path

public func foo() {
    print("hello foo")
}

public func bar() {
    print("hello bar")
}
$ chmod a+x tasks.swift
$ ./tasks.swift run foo
  hello foo

If you then place this file into usr/local/bin you could run this file from anywhere:

$ cp tasks.swift /usr/local/bin/tasks
$ tasks run bar
  hello bar

To automatically insert the run option, you can change your shebang to #!/usr/bin/env beak run --path.

Alternatives

License

Beak is licensed under the MIT license. See LICENSE for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].