Skip to content

Commit

Permalink
Add Homebrew support (#1042)
Browse files Browse the repository at this point in the history
* Add Homebrew formula template
* Add brew packager
* Remove selfupdate functionality when installed via Homebrew
* Configure JRELEASER_HOMEBREW_GITHUB_TOKEN
* Order configuration alphabetically
  • Loading branch information
helpermethod authored Mar 7, 2022
1 parent 65ee538 commit f61702c
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
JRELEASER_TWITTER_CONSUMER_SECRET: ${{ secrets.TWITTER_CONSUMER_API_SECRET }}
JRELEASER_TWITTER_ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }}
JRELEASER_TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
JRELEASER_HOMEBREW_GITHUB_TOKEN: ${{ secrets.HOMEBREW_GITHUB_TOKEN }}
services:
mongodb:
image: mongo:3.2
Expand Down
5 changes: 4 additions & 1 deletion gradle/release.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ jreleaser {
artifact {
path = "build/distributions/{{distributionName}}-${sdkmanVersion}.zip"
}
brew {
active = 'ALWAYS'
}
}
}

announce {
twitter {
active = 'RELEASE'
Expand Down
29 changes: 29 additions & 0 deletions src/jreleaser/distributions/sdkman-cli/brew/README.md.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SDKMAN! Homebrew Tap

A Homebrew tap containing the Formula for the SDKMAN! CLI.

## Installation

```sh
$ brew tap sdkman/tap
$ brew install sdkman
```

After successful installation add the following lines to the end of your `.bash_profile`

```sh
export SDKMAN_DIR=$(brew --prefix sdkman)/libexec
[[ -s "${SDKMAN_DIR}/bin/sdkman-init.sh" ]] && source "${SDKMAN_DIR}/bin/sdkman-init.sh"
```

Open a new terminal and type

```sh
sdk version
```

The output should look similar to this

```sh
SDKMAN {{version}}
```
35 changes: 35 additions & 0 deletions src/jreleaser/distributions/sdkman-cli/brew/formula.rb.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class {{brewFormulaName}} < Formula
desc "{{projectDescription}}"
homepage "{{projectWebsite}}"
url "{{distributionUrl}}"
version "{{projectVersion}}"
sha256 "{{distributionChecksumSha256}}"
license "{{projectLicense}}"

def install
libexec.install Dir["*"]

%w[tmp ext etc var archives candidates].each { |dir| mkdir libexec/dir }

system "curl", "-s", "https://api.sdkman.io/2/candidates/all", "-o", libexec/"var/candidates"

(libexec/"etc/config").write <<~EOS
sdkman_auto_answer=false
sdkman_auto_complete=true
sdkman_auto_env=false
sdkman_auto_update=false
sdkman_beta_channel=false
sdkman_colour_enable=true
sdkman_curl_connect_timeout=7
sdkman_curl_max_time=10
sdkman_debug_mode=false
sdkman_insecure_ssl=false
sdkman_rosetta2_compatible=false
sdkman_selfupdate_feature=false
EOS
end

test do
assert_match {{projectVersion}}, shell_output("export SDKMAN_DIR=#{libexec} && source #{libexec}/bin/sdkman-init.sh && sdk version")
end
end
6 changes: 5 additions & 1 deletion src/main/bash/sdkman-help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ function __sdk_help() {
__sdkman_echo_no_colour " broadcast or b"
__sdkman_echo_no_colour " help"
__sdkman_echo_no_colour " offline [enable|disable]"
__sdkman_echo_no_colour " selfupdate [force]"

if [[ "$sdkman_selfupdate_feature" == "true" ]]; then
__sdkman_echo_no_colour " selfupdate [force]"
fi

__sdkman_echo_no_colour " update"
__sdkman_echo_no_colour " flush [archives|tmp|broadcast|metadata|version]"
__sdkman_echo_no_colour ""
Expand Down
10 changes: 6 additions & 4 deletions src/main/bash/sdkman-main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ function sdk() {

# Check if it is a valid command
CMD_FOUND=""
CMD_TARGET="${SDKMAN_DIR}/src/sdkman-${COMMAND}.sh"
if [[ -f "$CMD_TARGET" ]]; then
CMD_FOUND="$CMD_TARGET"
if [[ "$COMMAND" != "selfupdate" || "$sdkman_selfupdate_feature" == "true" ]]; then
CMD_TARGET="${SDKMAN_DIR}/src/sdkman-${COMMAND}.sh"
if [[ -f "$CMD_TARGET" ]]; then
CMD_FOUND="$CMD_TARGET"
fi
fi

# Check if it is a sourced function
Expand Down Expand Up @@ -165,7 +167,7 @@ function sdk() {
fi

# Attempt upgrade after all is done
if [[ "$COMMAND" != "selfupdate" && "$sdkman_selfupdate_enable" == true ]]; then
if [[ "$COMMAND" != "selfupdate" && "$sdkman_selfupdate_feature" == "true" && "$sdkman_auto_update" == "true" ]]; then
__sdkman_auto_update "$SDKMAN_REMOTE_VERSION" "$SDKMAN_VERSION"
fi
return $final_rc
Expand Down
3 changes: 2 additions & 1 deletion src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class SdkmanBashEnvBuilder {

Map config = [
sdkman_auto_answer : 'false',
sdkman_beta_channel: 'false'
sdkman_beta_channel: 'false',
sdkman_selfupdate_feature: 'true'
]

File sdkmanDir, sdkmanBinDir, sdkmanVarDir, sdkmanSrcDir, sdkmanEtcDir, sdkmanExtDir, sdkmanArchivesDir,
Expand Down
91 changes: 91 additions & 0 deletions src/test/groovy/sdkman/specs/SelfupdateSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package sdkman.specs

import sdkman.support.SdkmanEnvSpecification

import java.time.Instant

import static java.time.temporal.ChronoUnit.DAYS

class SelfupdateSpec extends SdkmanEnvSpecification {
static final String CANDIDATES_API = "http://localhost:8080/2"
static final String BROADCAST_API_LATEST_ID_ENDPOINT = "$CANDIDATES_API/broadcast/latest/id"
static final String VERSION_ENDPOINT = "$CANDIDATES_API/broker/download/sdkman/version/stable"

def setup() {
curlStub.primeWith(BROADCAST_API_LATEST_ID_ENDPOINT, "echo dbfb025be9f97fda2052b5febcca0155")
curlStub.primeWith(VERSION_ENDPOINT, "echo 5.0.0")
}

def "should list selfupdate as a valid command when the selfupdate feature is toggled on"() {
given:
bash = sdkmanBashEnvBuilder
.withConfiguration("sdkman_selfupdate_feature", selfUpdateFeature)
.build()

bash.start()
bash.execute("source $bootstrapScript")

when:
bash.execute("sdk help")

then:
verifyOutput(bash.output)

where:
selfUpdateFeature | verifyOutput
"false" | { !it.contains("selfupdate") }
"true" | { it.contains("selfupdate") }
}

def "should source sdkman-selfupdate.sh when the selfupdate feature is toggled on"() {
given:
bash = sdkmanBashEnvBuilder
.withConfiguration("sdkman_selfupdate_feature", selfupdateFeature)
.build()

bash.start()
bash.execute("source $bootstrapScript")

when:
bash.execute("sdk selfupdate")

then:
verifyOutput(bash.output)

where:
selfupdateFeature | verifyOutput
"false" | { it.contains("Invalid command: selfupdate") }
"true" | { it.contains("No update available at this time.") }
}

def "should perform an autoupdate when the selfupdate feature is toggled on and autoupdate is enabled"() {
given:
new File("$sdkmanDotDirectory/var/delay_upgrade").with {
parentFile.mkdirs()
createNewFile()
lastModified = Instant.now().minus(2, DAYS).toEpochMilli()
}

bash = sdkmanBashEnvBuilder
.withSdkmanVersion("4.0.0")
.withConfiguration("sdkman_selfupdate_feature", selfupdateFeature)
.withConfiguration("sdkman_auto_update", autoUpdateEnabled)
.withConfiguration("sdkman_auto_answer", "true")
.build()

bash.start()
bash.execute("source $bootstrapScript")

when:
bash.execute("sdk version")

then:
verifyOutput(bash.output)

where:
selfupdateFeature | autoUpdateEnabled | verifyOutput
"true" | "true" | { it.contains("ATTENTION: A new version of SDKMAN is available...") }
"true" | "false" | { !it.contains("ATTENTION: A new version of SDKMAN is available...") }
"false" | "true" | { !it.contains("ATTENTION: A new version of SDKMAN is available...") }
}
}

0 comments on commit f61702c

Please sign in to comment.