Skip to content

Commit

Permalink
KTOR-7395 Fix downloaded JDK paths on macOS (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stexxe authored Sep 5, 2024
1 parent 7fb29f0 commit 31bd486
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
14 changes: 1 addition & 13 deletions internal/app/cli/jdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,18 @@ import (
"errors"
"fmt"
"github.com/ktorio/ktor-cli/internal/app"
"github.com/ktorio/ktor-cli/internal/app/config"
"github.com/ktorio/ktor-cli/internal/app/jdk"
"log"
"net/http"
"os"
)

func DownloadJdk(homeDir string, client *http.Client, logger *log.Logger, attempt int) (string, error) {
jdkPath, err := jdk.FetchRecommendedJdk(client, homeDir, logger)

if err != nil {
var e *app.Error
var pe *os.PathError

isAppError := errors.As(err, &e)
// JDK is already downloaded but the config doesn't reflect that
if isAppError && errors.As(e.Err, &pe) && errors.Is(pe.Err, os.ErrExist) {
jdkPath = pe.Path
config.SetValue("jdk", pe.Path)
_ = config.Commit()
return pe.Path, nil
}

if isAppError && e.Kind == app.JdkVerificationFailed {
if errors.As(err, &e) && e.Kind == app.JdkVerificationFailed {
fmt.Println("JDK Verification Failed. Trying again...")

if attempt >= 2 {
Expand Down
40 changes: 25 additions & 15 deletions internal/app/jdk/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,34 @@ func JavaHome() (string, bool) {
}

func getCandidates() (paths []string) {
homeDir, err := os.UserHomeDir()
if err != nil {
homeDir = ""
}

switch runtime.GOOS {
case "linux":
paths = append(paths, getLinuxCandidates()...)
paths = append(paths, getLinuxCandidates(homeDir)...)
case "darwin":
paths = append(paths, getDarwinCandidates()...)
paths = append(paths, getDarwinCandidates(homeDir)...)
case "windows":
paths = append(paths, getWindowsCandidates()...)
paths = append(paths, getWindowsCandidates(homeDir)...)
}

ktorJdks := getChildDirs(config.JdksDir(homeDir))

if runtime.GOOS == "darwin" {
for i, p := range ktorJdks {
ktorJdks[i] = filepath.Join(p, "Contents", "Home")
}
}

paths = append(paths, ktorJdks...)

return
}

func getWindowsCandidates() (paths []string) {
func getWindowsCandidates(homeDir string) (paths []string) {
drives := []string{"C", "D", "E", "F", "G"}

for _, drive := range drives {
Expand All @@ -144,21 +159,17 @@ func getWindowsCandidates() (paths []string) {
paths = append(paths, getChildDirs(fmt.Sprintf("%s:\\Program Files\\Common Files\\Oracle\\Java", drive))...)
}

homeDir, err := os.UserHomeDir()
if err != nil {
return
if homeDir != "" {
paths = append(paths, getChildDirs(filepath.Join(homeDir, ".jdks"))...)
}

paths = append(paths, getChildDirs(filepath.Join(homeDir, ".jdks"))...)

return
}

func getDarwinCandidates() (paths []string) {
func getDarwinCandidates(homeDir string) (paths []string) {
paths = append(paths, getChildDirs("/Library/Java/JavaVirtualMachines")...)

homeDir, err := os.UserHomeDir()
if err != nil {
if homeDir == "" {
return
}

Expand All @@ -172,12 +183,11 @@ func getDarwinCandidates() (paths []string) {
return
}

func getLinuxCandidates() (paths []string) {
func getLinuxCandidates(homeDir string) (paths []string) {
paths = append(paths, getChildDirs("/usr/lib/jvm")...)
paths = append(paths, getChildDirs("/usr/lib64/jvm")...)

homeDir, err := os.UserHomeDir()
if err != nil {
if homeDir == "" {
return
}

Expand Down
6 changes: 6 additions & 0 deletions internal/app/jdk/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"sync"
)

Expand Down Expand Up @@ -94,5 +96,9 @@ func fetch(client *http.Client, d *Descriptor, outDir string, logger *log.Logger
return "", &app.Error{Err: errors.New("verify jdk: downloaded JDK checksum failed"), Kind: app.JdkVerificationFailed}
}

if runtime.GOOS == "darwin" {
extractDir = filepath.Join(extractDir, "Contents", "Home")
}

return extractDir, nil
}

0 comments on commit 31bd486

Please sign in to comment.