Marathon
[DEPRECATED] Marathon makes it easy to write, run and manage your Swift scripts 🏃
Top Related Projects
Quick Overview
Marathon is a Swift library that simplifies the process of writing command-line tools and scripts. It provides a clean and intuitive API for defining command-line arguments, handling input and output, and executing tasks.
Pros
- Simplicity: Marathon abstracts away the complexities of command-line argument parsing and handling, allowing developers to focus on the core functionality of their tools.
- Flexibility: The library supports a wide range of argument types, including flags, options, and positional arguments, making it suitable for a variety of command-line tools.
- Testability: Marathon's design makes it easy to write unit tests for command-line tools, improving the overall quality and maintainability of the codebase.
- Cross-platform: The library is built on top of Swift, which means it can be used to create cross-platform command-line tools that run on macOS, Linux, and other platforms.
Cons
- Limited Ecosystem: As a relatively niche library, Marathon may not have the same level of community support and third-party integrations as some more popular command-line frameworks.
- Steep Learning Curve: While the library's API is generally straightforward, developers who are new to command-line programming may still need to invest some time in understanding the concepts and patterns used by Marathon.
- Dependency Management: Using Marathon as a dependency in a project may add an extra layer of complexity to the overall dependency management process.
- Performance: For very large or complex command-line tools, Marathon's abstraction layer may introduce some performance overhead compared to lower-level command-line interfaces.
Code Examples
Here are a few examples of how to use Marathon in your Swift projects:
Defining a Command-Line Tool
import Marathon
struct MyTool: ParsableCommand {
@Option(name: .shortAndLong, help: "The name of the person to greet.")
var name: String = "World"
func run() throws {
print("Hello, \(name)!")
}
}
MyTool.main
Handling Positional Arguments
import Marathon
struct FileManager: ParsableCommand {
@Argument(help: "The file to operate on.")
var file: String
@Option(name: .shortAndLong, help: "The operation to perform on the file.")
var operation: String = "copy"
func run() throws {
switch operation {
case "copy":
// Copy the file
case "delete":
// Delete the file
default:
throw MarathonError.invalidArgument(for: "operation")
}
}
}
FileManager.main
Validating Input
import Marathon
struct Calculator: ParsableCommand {
@Argument(help: "The first number to calculate with.")
var firstNumber: Double
@Argument(help: "The second number to calculate with.")
var secondNumber: Double
@Option(name: .shortAndLong, help: "The operation to perform.")
var operation: String = "add"
func validate() throws {
guard secondNumber != 0 else {
throw MarathonError.invalidArgument(for: "secondNumber")
}
}
func run() throws {
var result: Double
switch operation {
case "add":
result = firstNumber + secondNumber
case "subtract":
result = firstNumber - secondNumber
case "multiply":
result = firstNumber * secondNumber
case "divide":
result = firstNumber / secondNumber
default:
throw MarathonError.invalidArgument(for: "operation")
}
print("Result: \(result)")
}
}
Calculator.main
Getting Started
To get started with Marathon, you can add it as a dependency to your Swift project using a package manager like Swift Package Manager or CocoaPods. Here's an example of how to add Marathon to your project using Swift Package Manager:
- Open your Xcode project and navigate to the "Swift Packages" tab in the project settings.
- Click the "+" button to add a new package dependency.
- Enter the URL for the Marathon repository: `https://github.com/JohnSun
Competitor Comparisons
A simple, decentralized dependency manager for Cocoa
Pros of Carthage
- Carthage is a decentralized dependency manager, allowing for more flexibility in managing dependencies.
- Carthage supports a wide range of platforms, including iOS, macOS, tvOS, and watchOS.
- Carthage provides a straightforward and easy-to-use interface for managing dependencies.
Cons of Carthage
- Carthage can be more complex to set up and configure compared to Marathon.
- Carthage may not provide the same level of integration and tooling support as other dependency managers like CocoaPods.
- Carthage's build process can be slower than other dependency managers, especially for larger projects.
Code Comparison
Carthage:
github "Alamofire/Alamofire" ~> 5.2
github "ReactiveX/RxSwift" ~> 6.0
Marathon:
import MarathonCore
let package = Package(
name: "MyProject",
dependencies: [
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.2.0"),
.package(url: "https://github.com/ReactiveX/RxSwift.git", from: "6.0.0")
],
targets: [
.target(
name: "MyProject",
dependencies: [
"Alamofire",
"RxSwift"
]
)
]
)
The Cocoa Dependency Manager.
Pros of CocoaPods
- Extensive library of available pods, providing a wide range of functionality for iOS development
- Mature and well-established ecosystem with a large community of contributors
- Simplified dependency management and integration process for iOS projects
Cons of CocoaPods
- Potential for version conflicts and compatibility issues between different pods
- Increased project complexity due to the need to manage the CocoaPods project file
- Slower build times compared to other dependency management solutions
Code Comparison
CocoaPods (Podfile):
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '13.0'
use_frameworks!
target 'MyApp' do
pod 'AFNetworking', '~> 4.0'
pod 'Alamofire', '~> 5.0'
end
Marathon (Package.swift):
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "MyApp",
dependencies: [
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.0.0"),
.package(url: "https://github.com/AFNetworking/AFNetworking.git", from: "4.0.0")
],
targets: [
.target(
name: "MyApp",
dependencies: [
"Alamofire",
"AFNetworking"
]
)
]
)
🚀 The easiest way to automate building and releasing your iOS and Android apps
Pros of fastlane/fastlane
- Comprehensive suite of tools for automating iOS and Android app deployment and management
- Supports a wide range of platforms and services, including App Store, Google Play, TestFlight, and more
- Extensive plugin ecosystem with hundreds of available plugins
Cons of fastlane/fastlane
- Steeper learning curve compared to Marathon, especially for beginners
- Can be overkill for smaller projects or simple deployment needs
- Requires more configuration and setup compared to Marathon
Code Comparison
fastlane/fastlane:
lane :beta do
increment_build_number
gym
pilot
end
JohnSundell/Marathon:
import Foundation
import Marathon
let script = Script {
task("build") {
run("swift", "build")
}
task("test") {
run("swift", "test")
}
}
try script.run()
The Cocoa Dependency Manager.
Pros of CocoaPods
- Extensive library of available pods, providing a wide range of functionality for iOS development
- Mature and well-established ecosystem with a large community of contributors
- Simplified dependency management and integration process for iOS projects
Cons of CocoaPods
- Potential for version conflicts and compatibility issues between different pods
- Increased project complexity due to the need to manage the CocoaPods project file
- Slower build times compared to other dependency management solutions
Code Comparison
CocoaPods (Podfile):
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '13.0'
use_frameworks!
target 'MyApp' do
pod 'AFNetworking', '~> 4.0'
pod 'Alamofire', '~> 5.0'
end
Marathon (Package.swift):
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "MyApp",
dependencies: [
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.0.0"),
.package(url: "https://github.com/AFNetworking/AFNetworking.git", from: "4.0.0")
],
targets: [
.target(
name: "MyApp",
dependencies: [
"Alamofire",
"AFNetworking"
]
)
]
)
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
â ï¸ DEPRECATED: Marathon is now deprecated in favor of using the Swift Package Manager directly. It's recommended to migrate your scripts as soon as possible, since future Xcode/macOS versions may break compatibility. See this issue for more info.
Welcome to Marathon, a command line tool that makes it easy to write, run and manage your Swift scripts. It's powered by the Swift Package Manager and requires no modification to your existing scripts or dependency packages.
Features
ð£ Create scripts
$ marathon create helloWorld "import Foundation; print(\"Hello world\")"
ðââï¸ Run scripts
$ marathon run helloWorld
> Hello world
ð¦ Hassle free dependency management. Simply add a package...
$ marathon add https://github.com/JohnSundell/Files.git
...and use it without any additional work
import Files
for file in try Folder(path: "MyFolder").files {
print(file.name)
}
ð Update all of your scripting dependencies with a single call
$ marathon update
â Edit, run & debug your scripts using Xcode...
$ marathon edit helloWorld
...or in your favorite text editor
$ marathon edit helloWorld --no-xcode
ð Run remote scripts directly from a Git repository...
$ marathon run https://github.com/johnsundell/testdrive.git
...using only a GitHub username & repository name:
$ marathon run johnsundell/testdrive
ð» Install scripts as binaries and run them independently from anywhere...
$ marathon install helloWorld
$ helloWorld
> Hello world
...you can even install remote scripts (+ their dependencies) from a URL:
$ marathon install https://raw.githubusercontent.com/JohnSundell/Marathon-Examples/master/AddSuffix/addSuffix.swift
$ cd myImages
$ addSuffix "@2x"
> Added suffix "@2x" to 15 files
...or from a GitHub repository:
$ marathon install johnsundell/testdrive
$ testdrive
ðª Share your scripts with your team and automatically install their dependencies...
import Files // marathon:https://github.com/JohnSundell/Files.git
print(Folder.current.path)
...or specify your dependencies using a Marathonfile
:
$ echo "https://github.com/JohnSundell/Files.git" > Marathonfile
Installing
On macOS
Using Make (recommended):
$ git clone https://github.com/JohnSundell/Marathon.git
$ cd Marathon
$ make
Using the Swift Package Manager:
$ git clone https://github.com/JohnSundell/Marathon.git
$ cd Marathon
$ swift build -c release -Xswiftc -static-stdlib
$ cp -f .build/release/Marathon /usr/local/bin/marathon
Using Mint:
$ mint install JohnSundell/Marathon
Using Homebrew (not recommended, due to slow update cycle):
brew install marathon-swift
On Linux
$ git clone https://github.com/JohnSundell/Marathon.git
$ cd Marathon
$ swift build -c release
$ cp -f .build/release/Marathon /usr/local/bin/marathon
If you encounter a permissions failure while installing, you may need to prepend sudo
to the commands.
To update Marathon, simply repeat any of the above two series of commands, except cloning the repo.
Requirements
Marathon requires the following to be installed on your system:
- Swift 4.1 or later (bundled with Xcode 9.3 or later)
- Git
- Xcode (if you want to edit scripts using it)
Examples
Check out this repository for a few example Swift scripts that you can run using Marathon.
Specifying dependencies inline
Scripting usually involves using 3rd party frameworks to get your job done, and Marathon provides an easy way to define such dependencies right when you are importing them in your script, using a simple comment syntax:
import Files // marathon:https://github.com/JohnSundell/Files.git
import Unbox // marathon:https://github.com/JohnSundell/Unbox.git
Specifying your dependencies ensures that they will always be installed by Marathon before your script is run, edited or installed - making it super easy to share scripts with your friends, team or the wider community. All you have to do is share the script file, and Marathon takes care of the rest!
Using a Marathonfile
If you prefer to keep your dependency declarations separate, you can create a Marathonfile
in the same folder as your script. This file is simply a new line separated list of URLs pointing to either:
- The URL to a git repository of a local or remote package to install before running your script.
- The path to another script that should be linked to your script before running it.
Here is an example of a Marathonfile
:
https://github.com/JohnSundell/Files.git
https://github.com/JohnSundell/Unbox.git
https://github.com/JohnSundell/Wrap.git
~/packages/MyPackage
otherScript.swift
Shell autocomplete
Marathon includes autocomplete for the zsh
and fish
shells (PRs adding support for other shells is more than welcome!). To enable it, do the following:
-
zsh
:- Add the line
fpath=(~/.marathon/ShellAutocomplete/zsh $fpath)
to your~/.zshrc
file. - Add the line
autoload -Uz compinit && compinit -i
to your~/.zshrc
file if it doesn't already contain it. - Restart your terminal.
- Add the line
-
fish
:cp -f ~/.marathon/ShellAutocomplete/fish/marathon.fish ~/.config/fish/completions
You can now type marathon r
and have it be autocompleted to marathon run
ð
Help, feedback or suggestions?
- Run
$ marathon help
to display help for the tool itself or for any specific command. - Append
--verbose
to any command to make Marathon output everything it's doing, for debugging purposes. - Open an issue if you need help, if you found a bug, or if you want to discuss a feature request.
- Open a PR if you want to make some change to Marathon.
- Contact @johnsundell on Twitter for discussions, news & announcements about Marathon.
Top Related Projects
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot