fix: Respect globally set SDKMAN_DIR

Also, print a warning during shell startup
if a custom install path is set, but
SDKMAN! is not installed there.

fixes #52
This commit is contained in:
Raphael Reitzig 2024-04-03 23:35:59 +02:00 committed by Raphael
parent 5f2ae91d5f
commit a0aa142288
5 changed files with 43 additions and 25 deletions

View file

@ -7,8 +7,13 @@ this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
## [Unreleased] ## [Unreleased]
### Added
- Warn on bad custom install path
### Fixes ### Fixes
- Respect `SDKMAN_DIR` if already set (#52; thanks, @xtexChooser!)
- Completions use custom SDKMAN! install path (#48; thanks, @Bryan2333!) - Completions use custom SDKMAN! install path (#48; thanks, @Bryan2333!)
## [2.0.0] - 2023-06-27 ## [2.0.0] - 2023-06-27

View file

@ -27,14 +27,17 @@ _Note:_
- Only compatible with fisher v4 upwards; v3 is no longer supported. - Only compatible with fisher v4 upwards; v3 is no longer supported.
- You have to install [SDKMAN!] separately. - You have to install [SDKMAN!] separately.
- If you have installed SDKMAN! at a custom location, you need to add - If you have installed SDKMAN! at a custom location, you need to either
```fish - set environment variable `SDKMAN_DIR` to that path using your preferred method, or
set -g __sdkman_custom_dir /your/path/to/sdkman - add
``` ```fish
to a fish config file set -g __sdkman_custom_dir /your/path/to/sdkman
[run _before_](https://fishshell.com/docs/current/language.html#configuration-files) ```
`.config/fish/conf.d/sdk.fish`; to a fish config file
for example, you can use `.config/fish/conf.d/config_sdk.fish`. [run _before_](https://fishshell.com/docs/current/language.html#configuration-files)
`.config/fish/conf.d/sdk.fish`;
for example, you can use `.config/fish/conf.d/config_sdk.fish`.
- If _both_ are set, `__sdkman_custom_dir` is used.
## Usage ## Usage

View file

@ -10,22 +10,29 @@
# Account for custom install locations # Account for custom install locations
if set -q __sdkman_custom_dir if set -q __sdkman_custom_dir
set -gx SDKMAN_DIR "$__sdkman_custom_dir" set -gx SDKMAN_DIR "$__sdkman_custom_dir"
else end
# This is the default location: # Guard: SDKMAN! needs to be installed
if set -q SDKMAN_DIR; and not test -f "$SDKMAN_DIR/bin/sdkman-init.sh"
echo "WARNING: SDKMAN! installation path set to $SDKMAN_DIR, but no installation found there"
exit 0
end
# Unless overridden, use the default location:
if not set -q SDKMAN_DIR
set -gx SDKMAN_DIR "$HOME/.sdkman" set -gx SDKMAN_DIR "$HOME/.sdkman"
end end
set __fish_sdkman_init "$SDKMAN_DIR/bin/sdkman-init.sh" set __fish_sdkman_init "$SDKMAN_DIR/bin/sdkman-init.sh"
# Copied from https://github.com/jorgebucaran/fisher/blob/main/functions/fisher.fish to be consistent:
set --query fisher_path || set --local fisher_path $__fish_config_dir
set __fish_sdkman_noexport_init "$fisher_path/functions/__sdkman-noexport-init.sh"
# Guard: SDKMAN! needs to be installed # Guard: SDKMAN! needs to be installed
if not test -f "$__fish_sdkman_init" if not test -f "$__fish_sdkman_init"
exit 0 exit 0
end end
# Copied from https://github.com/jorgebucaran/fisher/blob/main/functions/fisher.fish to be consistent:
set --query fisher_path || set --local fisher_path $__fish_config_dir
set __fish_sdkman_noexport_init "$fisher_path/functions/__sdkman-noexport-init.sh"
# Hack for issue #19: # Hack for issue #19:
# Create version of sdkman-init that doesn't export any environment variables. # Create version of sdkman-init that doesn't export any environment variables.
# Refresh if sdkman-init changed. # Refresh if sdkman-init changed.

View file

@ -5,14 +5,18 @@ Feature: Corner Cases
When a new Fish shell is launched When a new Fish shell is launched
Then environment variable SDKMAN_DIR has the original value Then environment variable SDKMAN_DIR has the original value
Scenario: sdk initialized for another user in this shell Scenario: Custom installation path via env var
# Use any directory outside of the user's home directory
Given environment variable SDKMAN_DIR is set to "/"
When a new Fish shell is launched
Then environment variable SDKMAN_DIR has the original value
Scenario: Custom installation path
Given SDKMAN! is installed at /tmp/sdkman Given SDKMAN! is installed at /tmp/sdkman
And environment variable SDKMAN_DIR is set to "/tmp/sdkman"
When we run "sdk version" in Fish
Then the exit code is 0
And the output contains "SDKMAN!"
And environment variable SDKMAN_DIR has value "/tmp/sdkman"
And environment variable ANT_HOME has value "/tmp/sdkman/candidates/ant/current"
Scenario: Custom installation path via fish config
Given SDKMAN! is installed at /tmp/sdkman
And environment variable SDKMAN_DIR is set to "/something/wicked"
And fish config file config_sdk.fish exists with content And fish config file config_sdk.fish exists with content
""" """
set -g __sdkman_custom_dir /tmp/sdkman set -g __sdkman_custom_dir /tmp/sdkman
@ -25,10 +29,7 @@ Feature: Corner Cases
Scenario Outline: Completions with custom installation path Scenario Outline: Completions with custom installation path
Given SDKMAN! is installed at /tmp/sdkman Given SDKMAN! is installed at /tmp/sdkman
And fish config file config_sdk.fish exists with content And environment variable SDKMAN_DIR is set to "/tmp/sdkman"
"""
set -g __sdkman_custom_dir /tmp/sdkman
"""
When the user enters "<cmd>" into the prompt When the user enters "<cmd>" into the prompt
Then completion should propose "<completions>" Then completion should propose "<completions>"
Examples: Examples:

View file

@ -64,10 +64,12 @@ def run_fish_command(cmd)
end.to_h end.to_h
out, status = Open3.capture2e(<<~FISH out, status = Open3.capture2e(<<~FISH
#{@command.nil? ? '' : @command}
fish -c '#{cmd} > #{files[:stdout]} 2> #{files[:stderr]}; \ fish -c '#{cmd} > #{files[:stdout]} 2> #{files[:stderr]}; \
echo $status > #{files[:status]}; \ echo $status > #{files[:status]}; \
env > #{files[:env]}; \ env > #{files[:env]}; \
' '
#{@command.nil? ? '' : ')'}
FISH FISH
) )