diff --git a/.gitignore b/.gitignore index f1acb2c..424383f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ target *.bloop *.metals .ammonite -.bash_history \ No newline at end of file +.bash_history +teleport-scala +teleport-scala.jar \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index c91579f..d046e99 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,12 +19,11 @@ branches: before_install: - if [ $TRAVIS_OS_NAME = windows ]; then choco install zip unzip ; fi - if [ $TRAVIS_OS_NAME = windows ]; then choco install visualstudio2017-workload-vctools ; fi - #- choco uninstall windows-sdk-10.1 - curl -sL https://get.sdkman.io | bash - mkdir -p "$HOME/.sdkman/etc/" - echo sdkman_auto_answer=true > "$HOME/.sdkman/etc/config" - echo sdkman_auto_selfupdate=true >> "$HOME/.sdkman/etc/config" - - "source $HOME/.sdkman/bin/sdkman-init.sh" + - source "$HOME/.sdkman/bin/sdkman-init.sh" install: - sdk install java 20.0.0.r11-grl diff --git a/README.md b/README.md new file mode 100644 index 0000000..be414be --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# teleport-scala + +[teleport](https://github.com/bollu/teleport) rewritten in Scala as a showcase that writing native CLIs in Scala +might not be such a bad idea. + +[![asciicast](https://asciinema.org/a/Zj1ZDAgF02PP3JpD5RNtwBz0M.svg)](https://asciinema.org/a/Zj1ZDAgF02PP3JpD5RNtwBz0M) + +`goto` command cannot be fully implemented in subprocess. `teleport-scala goto point` executable will return +status code 2 after printing the absolute path of the `point` (if `point` is registered teleport point). It's not +possible for the `teleport-scala` to change current directory of the caller process. The solution for that +is to have a bash function sourced. That function can use absolute path returned by `teleport scala`. + +In `.zshrc`/`.bashrc`: + +``` +source /path/to/teleport-scala/teleport.sh +``` diff --git a/build-native.sh b/build-native.sh index 7cbd173..a7d0403 100755 --- a/build-native.sh +++ b/build-native.sh @@ -3,5 +3,6 @@ # Exit on any failure set -e -VERSION=`sbt version | tail -n 1 | awk '{print $2}'` -native-image --verbose --static --no-fallback -jar "target/scala-2.13/teleport-scala-assembly-$VERSION.jar" teleport-scala +sbt assembly +find target -iname "*.jar" -exec cp {} teleport-scala.jar \; +native-image --verbose -H:+ReportExceptionStackTraces --static --no-fallback -jar teleport-scala.jar teleport-scala diff --git a/ci/debug.sh b/ci/debug.sh deleted file mode 100755 index a5b58fc..0000000 --- a/ci/debug.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash - -# Exit on any failure -set -e - -which unzip || echo "not found: unzip" -which curl || echo "not found: curl" -which wget || echo "not found: wget" - - -echo $SDKMAN_PLATFORM -echo $JAVA_HOME -echo $PATH -#ls -al /c/Users/travis/.sdkman/candidates/java/current/bin - -which java || echo "fail0" -which javac || echo "fail1" - - diff --git a/smoke-test.sh b/smoke-test.sh index 237dbed..eaf9f2a 100755 --- a/smoke-test.sh +++ b/smoke-test.sh @@ -4,7 +4,7 @@ set -e if [ "$TRAVIS_OS_NAME" = windows ] || [ "$TRAVIS_OS_NAME" = osx ] ; then - # We skip on both windowss and osx but for different reasons + # We skip on both windows and osx but for different reasons # In case of windows it's about difficulty to programatically mount a volume # In case of macos it's about lack of support for docker on travis ci # The alternative would be to run non-dockerized ammonite but currently tests do some assumptions about paths diff --git a/src/main/scala/pl/msitko/teleport/Commands.scala b/src/main/scala/pl/msitko/teleport/Commands.scala index 6182e7e..299a5e8 100644 --- a/src/main/scala/pl/msitko/teleport/Commands.scala +++ b/src/main/scala/pl/msitko/teleport/Commands.scala @@ -15,7 +15,7 @@ final case object VersionCmdOptions extends object Commands { - val flags = { + val flags: Opts[GlobalFlags] = { val nocolorsOpt = booleanFlag("no-colors", help = "Disable ANSI color codes") val noheadersOpt = booleanFlag("no-headers", help = "Disable printing headers for tabular data") (nocolorsOpt, noheadersOpt).mapN((noColors, noHeaders) => GlobalFlags(!noColors, !noHeaders)) @@ -28,41 +28,31 @@ object Commands { Command( name = "add", header = "add a teleport point" - ) { - (nameOpt, Opts.argument[String]("FOLDERPATH").orNone).mapN(AddCmdOptions) - } + )((nameOpt, Opts.argument[String]("FOLDERPATH").orNone).mapN(AddCmdOptions)) val list = Command( name = "list", header = "list all teleport points" - ) { - Opts.unit.map(_ => ListCmdOptions) - } + )(Opts.unit.map(_ => ListCmdOptions)) val remove = Command( name = "remove", header = "remove a teleport point" - ) { - nameOpt.map(RemoveCmdOptions) - } + )(nameOpt.map(RemoveCmdOptions)) val goto = Command( name = "goto", header = "go to a created teleport point" - ) { - nameOpt.map(GotoCmdOptions) - } + )(nameOpt.map(GotoCmdOptions)) val version = Command( name = "version", header = "display version" - ) { - Opts.unit.map(_ => VersionCmdOptions) - } + )(Opts.unit.map(_ => VersionCmdOptions)) Opts .subcommand(add) @@ -72,7 +62,7 @@ object Commands { .orElse(Opts.subcommand(version)) } - val allSubCommands: Opts[(GlobalFlags, CmdOptions)] = (flags, subcommands).tupled + val appCmd: Opts[(GlobalFlags, CmdOptions)] = (flags, subcommands).tupled private def booleanFlag(long: String, help: String): Opts[Boolean] = Opts.flag(long = long, help = help).map(_ => true).withDefault(false) diff --git a/src/main/scala/pl/msitko/teleport/Main.scala b/src/main/scala/pl/msitko/teleport/Main.scala index 8747ab7..91f5618 100644 --- a/src/main/scala/pl/msitko/teleport/Main.scala +++ b/src/main/scala/pl/msitko/teleport/Main.scala @@ -13,7 +13,7 @@ object Main extends IOApp { val command: Command[(GlobalFlags, CmdOptions)] = Command( name = "teleport-scala", header = "teleport: A tool to quickly switch between directories", - helpFlag = true)(Commands.allSubCommands) + helpFlag = true)(Commands.appCmd) val storage = new Storage(os.home / ".teleport-data") val handler = new Handler(storage) diff --git a/teleport.sh b/teleport.sh new file mode 100644 index 0000000..37533f9 --- /dev/null +++ b/teleport.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Copied from https://unix.stackexchange.com/a/351658: +if test -n "$BASH" ; then script=$BASH_SOURCE +elif test -n "$TMOUT"; then script=${.sh.file} +elif test -n "$ZSH_NAME" ; then script=${(%):-%x} +elif test ${0##*/} = dash; then x=$(lsof -p $$ -Fn0 | tail -1); script=${x#n} +else script=$0 +fi + +TELEPORT_SCALA_DIR=`dirname $script` + +# mostly copied from https://github.com/bollu/teleport +function tp() { + # $@ takes all arguments of the shell script and passes it along to `teleport-scala + # which is our tool + OUTPUT=`${TELEPORT_SCALA_DIR}/teleport-scala $@` + # return code 2 tells the shell script to cd to whatever `teleport` outputs + if [ $? -eq 2 ] + then cd "$OUTPUT" + else echo "$OUTPUT" + fi +} + +fpath=(`pwd` $fpath)