Skip to content

Commit d4fa939

Browse files
authored
Merge pull request #6 from note/refactoring
Refactoring
2 parents a58c9fb + 4553ddb commit d4fa939

File tree

9 files changed

+58
-43
lines changed

9 files changed

+58
-43
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ target
44
*.bloop
55
*.metals
66
.ammonite
7-
.bash_history
7+
.bash_history
8+
teleport-scala
9+
teleport-scala.jar

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ branches:
1919
before_install:
2020
- if [ $TRAVIS_OS_NAME = windows ]; then choco install zip unzip ; fi
2121
- if [ $TRAVIS_OS_NAME = windows ]; then choco install visualstudio2017-workload-vctools ; fi
22-
#- choco uninstall windows-sdk-10.1
2322
- curl -sL https://get.sdkman.io | bash
2423
- mkdir -p "$HOME/.sdkman/etc/"
2524
- echo sdkman_auto_answer=true > "$HOME/.sdkman/etc/config"
2625
- echo sdkman_auto_selfupdate=true >> "$HOME/.sdkman/etc/config"
27-
- "source $HOME/.sdkman/bin/sdkman-init.sh"
26+
- source "$HOME/.sdkman/bin/sdkman-init.sh"
2827

2928
install:
3029
- sdk install java 20.0.0.r11-grl

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# teleport-scala
2+
3+
[teleport](https://github.com/bollu/teleport) rewritten in Scala as a showcase that writing native CLIs in Scala
4+
might not be such a bad idea.
5+
6+
[![asciicast](https://asciinema.org/a/Zj1ZDAgF02PP3JpD5RNtwBz0M.svg)](https://asciinema.org/a/Zj1ZDAgF02PP3JpD5RNtwBz0M)
7+
8+
`goto` command cannot be fully implemented in subprocess. `teleport-scala goto point` executable will return
9+
status code 2 after printing the absolute path of the `point` (if `point` is registered teleport point). It's not
10+
possible for the `teleport-scala` to change current directory of the caller process. The solution for that
11+
is to have a bash function sourced. That function can use absolute path returned by `teleport scala`.
12+
13+
In `.zshrc`/`.bashrc`:
14+
15+
```
16+
source /path/to/teleport-scala/teleport.sh
17+
```

build-native.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
# Exit on any failure
44
set -e
55

6-
VERSION=`sbt version | tail -n 1 | awk '{print $2}'`
7-
native-image --verbose --static --no-fallback -jar "target/scala-2.13/teleport-scala-assembly-$VERSION.jar" teleport-scala
6+
sbt assembly
7+
find target -iname "*.jar" -exec cp {} teleport-scala.jar \;
8+
native-image --verbose -H:+ReportExceptionStackTraces --static --no-fallback -jar teleport-scala.jar teleport-scala

ci/debug.sh

Lines changed: 0 additions & 19 deletions
This file was deleted.

smoke-test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
set -e
55

66
if [ "$TRAVIS_OS_NAME" = windows ] || [ "$TRAVIS_OS_NAME" = osx ] ; then
7-
# We skip on both windowss and osx but for different reasons
7+
# We skip on both windows and osx but for different reasons
88
# In case of windows it's about difficulty to programatically mount a volume
99
# In case of macos it's about lack of support for docker on travis ci
1010
# The alternative would be to run non-dockerized ammonite but currently tests do some assumptions about paths

src/main/scala/pl/msitko/teleport/Commands.scala

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final case object VersionCmdOptions extends
1515

1616
object Commands {
1717

18-
val flags = {
18+
val flags: Opts[GlobalFlags] = {
1919
val nocolorsOpt = booleanFlag("no-colors", help = "Disable ANSI color codes")
2020
val noheadersOpt = booleanFlag("no-headers", help = "Disable printing headers for tabular data")
2121
(nocolorsOpt, noheadersOpt).mapN((noColors, noHeaders) => GlobalFlags(!noColors, !noHeaders))
@@ -28,41 +28,31 @@ object Commands {
2828
Command(
2929
name = "add",
3030
header = "add a teleport point"
31-
) {
32-
(nameOpt, Opts.argument[String]("FOLDERPATH").orNone).mapN(AddCmdOptions)
33-
}
31+
)((nameOpt, Opts.argument[String]("FOLDERPATH").orNone).mapN(AddCmdOptions))
3432

3533
val list =
3634
Command(
3735
name = "list",
3836
header = "list all teleport points"
39-
) {
40-
Opts.unit.map(_ => ListCmdOptions)
41-
}
37+
)(Opts.unit.map(_ => ListCmdOptions))
4238

4339
val remove =
4440
Command(
4541
name = "remove",
4642
header = "remove a teleport point"
47-
) {
48-
nameOpt.map(RemoveCmdOptions)
49-
}
43+
)(nameOpt.map(RemoveCmdOptions))
5044

5145
val goto =
5246
Command(
5347
name = "goto",
5448
header = "go to a created teleport point"
55-
) {
56-
nameOpt.map(GotoCmdOptions)
57-
}
49+
)(nameOpt.map(GotoCmdOptions))
5850

5951
val version =
6052
Command(
6153
name = "version",
6254
header = "display version"
63-
) {
64-
Opts.unit.map(_ => VersionCmdOptions)
65-
}
55+
)(Opts.unit.map(_ => VersionCmdOptions))
6656

6757
Opts
6858
.subcommand(add)
@@ -72,7 +62,7 @@ object Commands {
7262
.orElse(Opts.subcommand(version))
7363
}
7464

75-
val allSubCommands: Opts[(GlobalFlags, CmdOptions)] = (flags, subcommands).tupled
65+
val appCmd: Opts[(GlobalFlags, CmdOptions)] = (flags, subcommands).tupled
7666

7767
private def booleanFlag(long: String, help: String): Opts[Boolean] =
7868
Opts.flag(long = long, help = help).map(_ => true).withDefault(false)

src/main/scala/pl/msitko/teleport/Main.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object Main extends IOApp {
1313
val command: Command[(GlobalFlags, CmdOptions)] = Command(
1414
name = "teleport-scala",
1515
header = "teleport: A tool to quickly switch between directories",
16-
helpFlag = true)(Commands.allSubCommands)
16+
helpFlag = true)(Commands.appCmd)
1717
val storage = new Storage(os.home / ".teleport-data")
1818
val handler = new Handler(storage)
1919

teleport.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
# Copied from https://unix.stackexchange.com/a/351658:
4+
if test -n "$BASH" ; then script=$BASH_SOURCE
5+
elif test -n "$TMOUT"; then script=${.sh.file}
6+
elif test -n "$ZSH_NAME" ; then script=${(%):-%x}
7+
elif test ${0##*/} = dash; then x=$(lsof -p $$ -Fn0 | tail -1); script=${x#n}
8+
else script=$0
9+
fi
10+
11+
TELEPORT_SCALA_DIR=`dirname $script`
12+
13+
# mostly copied from https://github.com/bollu/teleport
14+
function tp() {
15+
# $@ takes all arguments of the shell script and passes it along to `teleport-scala
16+
# which is our tool
17+
OUTPUT=`${TELEPORT_SCALA_DIR}/teleport-scala $@`
18+
# return code 2 tells the shell script to cd to whatever `teleport` outputs
19+
if [ $? -eq 2 ]
20+
then cd "$OUTPUT"
21+
else echo "$OUTPUT"
22+
fi
23+
}
24+
25+
fpath=(`pwd` $fpath)

0 commit comments

Comments
 (0)