• Stars
    star
    223
  • Rank 178,458 (Top 4 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Run shell commands from Kotlin scripts, apps or Gradle tasks with ease.

Turtle

Pure Kotlin Latest release Gradle build status Twitter: @lordcodes


Run shell commands from Kotlin scripts, apps or Gradle tasks with ease.

Turtle simplifies the process of running external commands and processes from your Kotlin (or Java) code. It comes bundled with a selection of built-in functions, such as opening MacOS applications and dealing with Git. Running shell commands easily is particularly useful from within Kotlin scripts, command line applications and Gradle tasks.

 

FeaturesInstallUsageContributing

Features

▶︎ Run shell commands with minimal boilerplate

Simply specify the comamnd and its arguments to easily run and retrieve output.

▶︎ Call any of the built-in shell commands

Various commands are provided, such as creating a Git commit and opening files.

▶︎ Use the function syntax to run a series of commands

Specify a sequence of commands to run within a function block.

▶︎ Capture error exit code and output

When a command produces an error, the exit code and error output is thrown as an exception.

Install

Turtle is provided as a Gradle/Maven dependency.

  • v0.5.0 onwards are available via Maven Central.
  • v0.3.0 and v0.4.0 had issues, so please use v0.5.0 or later.
  • Earlier releases were available via Bintray/JCenter.

▶︎ Gradle Kotlin DSL

dependencies {
  implementation("com.lordcodes.turtle:turtle:0.8.0")
}

▶︎ Gradle Groovy DSL

dependencies {
  implementation 'com.lordcodes.turtle:turtle:0.8.0'
}

Usage

Note: Turtle is developed with MacOS in mind and so some commands or features may not exist or work as expected on Linux or Windows. There is an open issue to consider multiplatform support more carefully.


To run a single custom command, just call shellRun() and provide the command and arguments.

val output = shellRun("git", listOf("rev-parse", "--abbrev-ref", "HEAD"))
println(output) // Current branch name, e.g. master

The working directory can be provided, to run the command in a particular location. ShellLocation provides easy access to some useful locations, such as the user's home directory.

val turtleProject = ShellLocation.HOME.resolve("projects/turtle")
val output = shellRun("git", listOf("rev-parse", "--abbrev-ref", "HEAD"), turtleProject)
println(output) // Current branch name, e.g. master

To run a series of commands or use the built-in commands, just call shellRun {}.

shellRun {
  command("mkdir tortoise")

  changeWorkingDirectory("tortoise")

  git.commit("Initial commit")
  git.addTag("v1.2", "Release v1.2")

  files.openApplication("Spotify")
}

The initial working directory can be specified.

val turtleProject = ShellLocation.HOME.resolve("projects/turtle")
shellRun(turtleProject) {
  …
}

Built-in commands

▶︎ Git

shellRun {
  git.init()
  git.status()
  git.commit("Commit message")
  git.commitAllChanges("Commit message")
  git.push("origin", "master")
  git.pull()
  git.checkout("release")
  git.clone("https://github.com/lordcodes/turtle.git")
  git.addTag("v1.1", "Release v1.1")
  git.pushTag("v1.1")
  git.currentBranch()
}

▶︎ Files

shellRun {
  files.openFile("script.kts")
  files.openApplication("Mail")
  files.createSymlink("target", "link")
  files.readSymlink("link")
}

▶︎ More

Extra commands can easily be added by either calling command or by extending ShellScript. If you have created a command that you think should be built in, please feel free to open a PR.

Contributing or Help

If you notice any bugs or have a new feature to suggest, please check out the contributing guide. If you want to make changes, please make sure to discuss anything big before putting in the effort of creating the PR.

To reach out, please contact @lordcodes on Twitter.

More Repositories

1

SnackbarBuilder

[ARCHIVED] Builder pattern for support library Snackbars, that makes them easier to customise and use
Java
69
star
2

lordcodes-samples

Code samples for my Lord Codes blog 💻️
Kotlin
41
star
3

materialandroid

[ARCHIVED] Material Design resources such as colours, text sizes and dimensions through a simple dependency.
Java
36
star
4

android-resize-drawable-script

[ARCHIVED] A script to resize an Android drawable image into lower density versions
Shell
13
star
5

unity-powerprefs

Read and write values of various types to Unity PlayerPrefs with way more options than before!
C#
7
star
6

imagecram

A command line tool to quickly and easily compress images
Swift
7
star
7

template-kotlin-jvm-library

[ARCHIVED] A Kotlin JVM library template using Gradle
Kotlin
4
star
8

cloak-swift

Encrypt and obfuscate secrets, then pass them into Swift applications
Swift
4
star
9

android-studio-templates

Templates for use within Android Studio or IntelliJ
Kotlin
3
star
10

dotfiles

🛠 Dotfiles, config and MacOS setup
Shell
2
star
11

resonance-game

[ARCHIVED] A co-operative multi-player game developed for the Xbox 360 (and Windows) using the XNA game studio.
C#
2
star
12

nanolog-swift

[ARCHIVED] Easy-to-use, pretty, lightweight and powerful logging framework for Swift.
Swift
2
star
13

swifthooks

Share Git hooks between contributors and install using Tuist
Swift
2
star
14

lordcodes-syntax-theme

Lordcodes Syntax - A code colour theme 🖍 ⌨️
SCSS
2
star
15

swiftformat-tuist

A Tuist plugin that can format the project using SwiftFormat.
Swift
1
star
16

lordcodes

My public GitHub profile README
1
star
17

lordcodes-dev-tips

[ARCHIVED] Development tips, mainly in Kotlin and Swift. No longer updated, moved to my website.
1
star