Skip to content

Commit

Permalink
0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
shyiko committed Mar 25, 2016
1 parent 4d29a57 commit e2a27a9
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 10 deletions.
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [0.2.0](https://github.com/shyiko/mysql-binlog-connector-java/compare/0.1.0...0.2.0) - 2016-03-24

### Added
* `alias default`/`unalias default`, `which`, `deactivate` commands.

## 0.1.0 - 2016-03-23
29 changes: 29 additions & 0 deletions command/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package command

import (
"errors"
"io/ioutil"
"github.com/shyiko/jabba/cfg"
"path"
"os"
)

func SetAlias(name string, ver string) (err error) {
if name != "default" {
return errors.New("At this point only 'default' alias is allowed")
}
if ver == "" {
err = os.Remove(path.Join(cfg.Dir(), name + ".alias"))
} else {
err = ioutil.WriteFile(path.Join(cfg.Dir(), name + ".alias"), []byte(ver), 0666)
}
return
}

func GetAlias(name string) string {
b, err := ioutil.ReadFile(path.Join(cfg.Dir(), name + ".alias"))
if err != nil {
return ""
}
return string(b)
}
2 changes: 1 addition & 1 deletion command/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Current() string {
javaPath, err := lookPath("java")
if err == nil {
prefix := path.Join(cfg.Dir(), "jdk") + "/"
if (strings.HasPrefix(javaPath, prefix)) {
if strings.HasPrefix(javaPath, prefix) {
index := strings.Index(javaPath[len(prefix):], "/")
if index != -1 {
return javaPath[len(prefix):len(prefix) + index]
Expand Down
24 changes: 24 additions & 0 deletions command/deactivate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package command

import (
"os"
"github.com/shyiko/jabba/cfg"
"path"
"regexp"
)

func Deactivate() ([]string, error) {
pth, _ := os.LookupEnv("PATH")
rgxp := regexp.MustCompile(regexp.QuoteMeta(path.Join(cfg.Dir(), "jdk")) + "[^:]+[:]")
// strip references to ~/.jabba/jdk/*, otherwise leave unchanged
pth = rgxp.ReplaceAllString(pth, "")
javaHome, overrideWasSet := os.LookupEnv("JAVA_HOME_BEFORE_JABBA")
if !overrideWasSet {
javaHome, _ = os.LookupEnv("JAVA_HOME")
}
return []string{
"export PATH=" + pth,
"export JAVA_HOME=" + javaHome,
"unset JAVA_HOME_BEFORE_JABBA",
}, nil
}
16 changes: 13 additions & 3 deletions command/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,29 @@ import (
)

func Use(ver string) ([]string, error) {
aliasValue := GetAlias(ver)
if aliasValue != "" {
ver = aliasValue
}
resolved, err := resolveLocal(ver)
if err != nil {
return nil, err
}
pth, _ := os.LookupEnv("PATH")
rgxp := regexp.MustCompile(regexp.QuoteMeta(path.Join(cfg.Dir(), "jdk")) + "[^:]+[:]")
p, _ := os.LookupEnv("PATH")
p = rgxp.ReplaceAllString(p, "")
// strip references to ~/.jabba/jdk/*, otherwise leave unchanged
pth = rgxp.ReplaceAllString(pth, "")
javaHome := path.Join(cfg.Dir(), "jdk", resolved)
if runtime.GOOS == "darwin" {
javaHome = path.Join(javaHome, "Contents", "Home")
}
systemJavaHome, overrideWasSet := os.LookupEnv("JAVA_HOME_BEFORE_JABBA")
if !overrideWasSet {
systemJavaHome, _ = os.LookupEnv("JAVA_HOME")
}
return []string{
"export PATH=" + path.Join(javaHome, "bin") + ":" + p,
"export PATH=" + path.Join(javaHome, "bin") + ":" + pth,
"export JAVA_HOME=" + javaHome,
"export JAVA_HOME_BEFORE_JABBA=" + systemJavaHome,
}, nil
}
14 changes: 14 additions & 0 deletions command/which.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package command

import (
"path"
"github.com/shyiko/jabba/cfg"
)

func Which(ver string) (string, error) {
resolved, err := resolveLocal(ver)
if err != nil {
return "", err
}
return path.Join(cfg.Dir(), "jdk", resolved), nil
}
2 changes: 2 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jabba() {
rm \${fd3}
(exit \${exit_code})
}
[ ! -z "\$(jabba alias default)" ] && jabba use default
EOF

SOURCE_JABBA="\n[ -s \"$JABBA_DIR/jabba.sh\" ] && source \"$JABBA_DIR/jabba.sh\""
Expand Down
74 changes: 68 additions & 6 deletions jabba.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
Use: "install [version to install]",
Short: "Download and install JDK",
RunE: func(cmd *cobra.Command, args []string) error {
if (len(args) == 0) {
if len(args) == 0 {
return pflag.ErrHelp
}
ver, err := command.Install(args[0])
Expand All @@ -51,7 +51,7 @@ func main() {
Use: "uninstall [version to uninstall]",
Short: "Uninstall JDK",
RunE: func(cmd *cobra.Command, args []string) error {
if (len(args) == 0) {
if len(args) == 0 {
return pflag.ErrHelp
}
err := command.Uninstall(args[0])
Expand All @@ -66,7 +66,7 @@ func main() {
Use: "use [version to use]",
Short: "Modify PATH & JAVA_HOME to use specific JDK",
RunE: func(cmd *cobra.Command, args []string) error {
if (len(args) == 0) {
if len(args) == 0 {
return pflag.ErrHelp
}
return use(args[0])
Expand All @@ -78,7 +78,7 @@ func main() {
Short: "Display currently 'use'ed version",
Run: func(cmd *cobra.Command, args []string) {
ver := command.Current()
if (ver != "") {
if ver != "" {
println(ver)
}
},
Expand Down Expand Up @@ -117,6 +117,65 @@ func main() {
return nil
},
},
&cobra.Command{
Use: "deactivate",
Short: "Undo effects of `jabba` on current shell",
RunE: func(cmd *cobra.Command, args []string) error {
out, err := command.Deactivate()
if err != nil {
log.Fatal(err)
}
printForShellToEval(out)
return nil
},
},
&cobra.Command{
Use: "alias [name] [version]",
Short: "Resolve or update an alias",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return pflag.ErrHelp
}
if len(args) == 1 {
if value := command.GetAlias(args[0]); value != "" {
fmt.Println(value)
}
} else
if err := command.SetAlias(args[0], args[1]); err != nil {
log.Fatal(err)
}
return nil
},
Example: " jabba alias default 1.8\n" +
" jabba alias default # show value bound to an alias",
},
&cobra.Command{
Use: "unalias [name]",
Short: "Delete an alias",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return pflag.ErrHelp
}
if err := command.SetAlias(args[0], ""); err != nil {
log.Fatal(err)
}
return nil
},
},
&cobra.Command{
Use: "which [version]",
Short: "Display path to installed JDK",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return pflag.ErrHelp
}
dir, _ := command.Which(args[0])
if dir != "" {
fmt.Println(dir)
}
return nil
},
},
)
rootCmd.Flags().Bool("version", false, "version of jabba")
if err := rootCmd.Execute(); err != nil {
Expand All @@ -129,10 +188,13 @@ func use(ver string) error {
if err != nil {
log.Fatal(err)
}
// writing "export ..." to 3rd fd for shell to eval
printForShellToEval(out)
return nil
}

func printForShellToEval(out []string) {
fd3 := os.NewFile(3, "fd3")
for _, line := range out {
fmt.Fprintln(fd3, line)
}
return nil
}
28 changes: 28 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,26 @@ in Go ([gvm](https://github.com/moovweb/gvm)) or Ruby ([rvm](https://rvm.io)).

Tested <sup>at this point **very** lightly</sup> on Mac OS X and Linux. Windows support is coming in [#1](https://github.com/shyiko/jabba/issues/1).

It's written in [Go](https://golang.org/) to make maintenance easier (significantly shorter, easier to understand and less prone to errors
compared to pure shell implementation). Plus it enables us to support Windows natively (no Cygwin) without rewriting
the whole thing in PowerShell or whatever.

The goal is to provide unified pain-free experience of installing (and switching between different versions of) JDK.

How does it compare to "apt-get install" (and alike)?
Single command (`jabba install <version>`) works regardless of the OS (so you don't need to remember different ways to
install JDK on Mac OS X, Arch and two versions of Debian). Package name no longer matters, as well as, whether LTS is over
or not.
And you can install ANY version, not just the latest stable one. Wanna try upcoming 1.9.0 release? No need to wait -
`jabba install 1.9.0-110`. How about specific build of 1.8? No problem - `jabba install 1.8.73`.

> `jabba` has single responsibility - managing different versions of JDK. Maven/Gradle/SBT/... are out of scope (for those use
[mvnw](https://github.com/shyiko/mvnw)/[gradlew](https://docs.gradle.org/current/userguide/gradle_wrapper.html)/[sbt-launcher](http://www.scala-sbt.org/0.13/docs/Manual-Installation.html)/...).

## Installation

> (you can use same command to upgrade)
```sh
curl -sL https://github.com/shyiko/jabba/raw/master/install.sh | bash && . ~/.jabba/jabba.sh
```
Expand All @@ -30,6 +48,9 @@ jabba use 1.6.65

# list available jdk's
jabba ls-remote

# set default java version on shell (available since 0.2.0)
jabba alias default 1.6.65
```

For more information see `jabba --help`.
Expand All @@ -52,6 +73,13 @@ make test # or "test-coverage" if you want to get a coverage breakdown
make build # or "build-release" (latter is cross-compiling jabba to different OSs/ARCHs)
```

## FAQ

Q: What if I already have `java` installed?

A: `jabba` feels perfectly fine in the environment where `java` has already been installed using some other means. You
can continue using system JDK and switch to `jabba`-provided one only when needed (`jabba use 1.6.65`).

## License

[Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
Expand Down

0 comments on commit e2a27a9

Please sign in to comment.