Skip to content

Commit f61702c

Browse files
author
Oliver Weiler
authored
Add Homebrew support (#1042)
* Add Homebrew formula template * Add brew packager * Remove selfupdate functionality when installed via Homebrew * Configure JRELEASER_HOMEBREW_GITHUB_TOKEN * Order configuration alphabetically
1 parent 65ee538 commit f61702c

File tree

8 files changed

+173
-7
lines changed

8 files changed

+173
-7
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
JRELEASER_TWITTER_CONSUMER_SECRET: ${{ secrets.TWITTER_CONSUMER_API_SECRET }}
1818
JRELEASER_TWITTER_ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }}
1919
JRELEASER_TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
20+
JRELEASER_HOMEBREW_GITHUB_TOKEN: ${{ secrets.HOMEBREW_GITHUB_TOKEN }}
2021
services:
2122
mongodb:
2223
image: mongo:3.2

gradle/release.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ jreleaser {
3535
artifact {
3636
path = "build/distributions/{{distributionName}}-${sdkmanVersion}.zip"
3737
}
38+
brew {
39+
active = 'ALWAYS'
40+
}
3841
}
3942
}
40-
43+
4144
announce {
4245
twitter {
4346
active = 'RELEASE'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SDKMAN! Homebrew Tap
2+
3+
A Homebrew tap containing the Formula for the SDKMAN! CLI.
4+
5+
## Installation
6+
7+
```sh
8+
$ brew tap sdkman/tap
9+
$ brew install sdkman
10+
```
11+
12+
After successful installation add the following lines to the end of your `.bash_profile`
13+
14+
```sh
15+
export SDKMAN_DIR=$(brew --prefix sdkman)/libexec
16+
[[ -s "${SDKMAN_DIR}/bin/sdkman-init.sh" ]] && source "${SDKMAN_DIR}/bin/sdkman-init.sh"
17+
```
18+
19+
Open a new terminal and type
20+
21+
```sh
22+
sdk version
23+
```
24+
25+
The output should look similar to this
26+
27+
```sh
28+
SDKMAN {{version}}
29+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class {{brewFormulaName}} < Formula
2+
desc "{{projectDescription}}"
3+
homepage "{{projectWebsite}}"
4+
url "{{distributionUrl}}"
5+
version "{{projectVersion}}"
6+
sha256 "{{distributionChecksumSha256}}"
7+
license "{{projectLicense}}"
8+
9+
def install
10+
libexec.install Dir["*"]
11+
12+
%w[tmp ext etc var archives candidates].each { |dir| mkdir libexec/dir }
13+
14+
system "curl", "-s", "https://api.sdkman.io/2/candidates/all", "-o", libexec/"var/candidates"
15+
16+
(libexec/"etc/config").write <<~EOS
17+
sdkman_auto_answer=false
18+
sdkman_auto_complete=true
19+
sdkman_auto_env=false
20+
sdkman_auto_update=false
21+
sdkman_beta_channel=false
22+
sdkman_colour_enable=true
23+
sdkman_curl_connect_timeout=7
24+
sdkman_curl_max_time=10
25+
sdkman_debug_mode=false
26+
sdkman_insecure_ssl=false
27+
sdkman_rosetta2_compatible=false
28+
sdkman_selfupdate_feature=false
29+
EOS
30+
end
31+
32+
test do
33+
assert_match {{projectVersion}}, shell_output("export SDKMAN_DIR=#{libexec} && source #{libexec}/bin/sdkman-init.sh && sdk version")
34+
end
35+
end

src/main/bash/sdkman-help.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ function __sdk_help() {
3636
__sdkman_echo_no_colour " broadcast or b"
3737
__sdkman_echo_no_colour " help"
3838
__sdkman_echo_no_colour " offline [enable|disable]"
39-
__sdkman_echo_no_colour " selfupdate [force]"
39+
40+
if [[ "$sdkman_selfupdate_feature" == "true" ]]; then
41+
__sdkman_echo_no_colour " selfupdate [force]"
42+
fi
43+
4044
__sdkman_echo_no_colour " update"
4145
__sdkman_echo_no_colour " flush [archives|tmp|broadcast|metadata|version]"
4246
__sdkman_echo_no_colour ""

src/main/bash/sdkman-main.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ function sdk() {
103103

104104
# Check if it is a valid command
105105
CMD_FOUND=""
106-
CMD_TARGET="${SDKMAN_DIR}/src/sdkman-${COMMAND}.sh"
107-
if [[ -f "$CMD_TARGET" ]]; then
108-
CMD_FOUND="$CMD_TARGET"
106+
if [[ "$COMMAND" != "selfupdate" || "$sdkman_selfupdate_feature" == "true" ]]; then
107+
CMD_TARGET="${SDKMAN_DIR}/src/sdkman-${COMMAND}.sh"
108+
if [[ -f "$CMD_TARGET" ]]; then
109+
CMD_FOUND="$CMD_TARGET"
110+
fi
109111
fi
110112

111113
# Check if it is a sourced function
@@ -165,7 +167,7 @@ function sdk() {
165167
fi
166168

167169
# Attempt upgrade after all is done
168-
if [[ "$COMMAND" != "selfupdate" && "$sdkman_selfupdate_enable" == true ]]; then
170+
if [[ "$COMMAND" != "selfupdate" && "$sdkman_selfupdate_feature" == "true" && "$sdkman_auto_update" == "true" ]]; then
169171
__sdkman_auto_update "$SDKMAN_REMOTE_VERSION" "$SDKMAN_VERSION"
170172
fi
171173
return $final_rc

src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class SdkmanBashEnvBuilder {
3030

3131
Map config = [
3232
sdkman_auto_answer : 'false',
33-
sdkman_beta_channel: 'false'
33+
sdkman_beta_channel: 'false',
34+
sdkman_selfupdate_feature: 'true'
3435
]
3536

3637
File sdkmanDir, sdkmanBinDir, sdkmanVarDir, sdkmanSrcDir, sdkmanEtcDir, sdkmanExtDir, sdkmanArchivesDir,
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package sdkman.specs
2+
3+
import sdkman.support.SdkmanEnvSpecification
4+
5+
import java.time.Instant
6+
7+
import static java.time.temporal.ChronoUnit.DAYS
8+
9+
class SelfupdateSpec extends SdkmanEnvSpecification {
10+
static final String CANDIDATES_API = "http://localhost:8080/2"
11+
static final String BROADCAST_API_LATEST_ID_ENDPOINT = "$CANDIDATES_API/broadcast/latest/id"
12+
static final String VERSION_ENDPOINT = "$CANDIDATES_API/broker/download/sdkman/version/stable"
13+
14+
def setup() {
15+
curlStub.primeWith(BROADCAST_API_LATEST_ID_ENDPOINT, "echo dbfb025be9f97fda2052b5febcca0155")
16+
curlStub.primeWith(VERSION_ENDPOINT, "echo 5.0.0")
17+
}
18+
19+
def "should list selfupdate as a valid command when the selfupdate feature is toggled on"() {
20+
given:
21+
bash = sdkmanBashEnvBuilder
22+
.withConfiguration("sdkman_selfupdate_feature", selfUpdateFeature)
23+
.build()
24+
25+
bash.start()
26+
bash.execute("source $bootstrapScript")
27+
28+
when:
29+
bash.execute("sdk help")
30+
31+
then:
32+
verifyOutput(bash.output)
33+
34+
where:
35+
selfUpdateFeature | verifyOutput
36+
"false" | { !it.contains("selfupdate") }
37+
"true" | { it.contains("selfupdate") }
38+
}
39+
40+
def "should source sdkman-selfupdate.sh when the selfupdate feature is toggled on"() {
41+
given:
42+
bash = sdkmanBashEnvBuilder
43+
.withConfiguration("sdkman_selfupdate_feature", selfupdateFeature)
44+
.build()
45+
46+
bash.start()
47+
bash.execute("source $bootstrapScript")
48+
49+
when:
50+
bash.execute("sdk selfupdate")
51+
52+
then:
53+
verifyOutput(bash.output)
54+
55+
where:
56+
selfupdateFeature | verifyOutput
57+
"false" | { it.contains("Invalid command: selfupdate") }
58+
"true" | { it.contains("No update available at this time.") }
59+
}
60+
61+
def "should perform an autoupdate when the selfupdate feature is toggled on and autoupdate is enabled"() {
62+
given:
63+
new File("$sdkmanDotDirectory/var/delay_upgrade").with {
64+
parentFile.mkdirs()
65+
createNewFile()
66+
lastModified = Instant.now().minus(2, DAYS).toEpochMilli()
67+
}
68+
69+
bash = sdkmanBashEnvBuilder
70+
.withSdkmanVersion("4.0.0")
71+
.withConfiguration("sdkman_selfupdate_feature", selfupdateFeature)
72+
.withConfiguration("sdkman_auto_update", autoUpdateEnabled)
73+
.withConfiguration("sdkman_auto_answer", "true")
74+
.build()
75+
76+
bash.start()
77+
bash.execute("source $bootstrapScript")
78+
79+
when:
80+
bash.execute("sdk version")
81+
82+
then:
83+
verifyOutput(bash.output)
84+
85+
where:
86+
selfupdateFeature | autoUpdateEnabled | verifyOutput
87+
"true" | "true" | { it.contains("ATTENTION: A new version of SDKMAN is available...") }
88+
"true" | "false" | { !it.contains("ATTENTION: A new version of SDKMAN is available...") }
89+
"false" | "true" | { !it.contains("ATTENTION: A new version of SDKMAN is available...") }
90+
}
91+
}

0 commit comments

Comments
 (0)