Skip to content

Refactoring #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ target
*.bloop
*.metals
.ammonite
.bash_history
.bash_history
teleport-scala
teleport-scala.jar
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
5 changes: 3 additions & 2 deletions build-native.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 0 additions & 19 deletions ci/debug.sh

This file was deleted.

2 changes: 1 addition & 1 deletion smoke-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 7 additions & 17 deletions src/main/scala/pl/msitko/teleport/Commands.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/pl/msitko/teleport/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
25 changes: 25 additions & 0 deletions teleport.sh
Original file line number Diff line number Diff line change
@@ -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)