Skip to content

Commit

Permalink
Remove local caching of candidates (#1077)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgeraldino authored Apr 3, 2022
1 parent 94ccfd0 commit 535e3c9
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 131 deletions.
2 changes: 1 addition & 1 deletion contrib/completion/bash/sdk
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ __sdkman_complete_command() {
candidates=("force")
;;
flush)
candidates=("archives" "temp" "broadcast" "version")
candidates=("temp" "broadcast" "version")
;;
esac

Expand Down
2 changes: 1 addition & 1 deletion src/jreleaser/distributions/sdkman-cli/brew/formula.rb.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class {{brewFormulaName}} < Formula
def install
libexec.install Dir["*"]

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

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

Expand Down
4 changes: 0 additions & 4 deletions src/main/bash/sdkman-flush.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ function __sdk_flush() {
__sdkman_echo_green "Version file has been flushed."
fi
;;
archives)
__sdkman_cleanup_folder "archives"
;;
temp)
__sdkman_cleanup_folder "tmp"
;;
Expand All @@ -42,7 +39,6 @@ function __sdk_flush() {
__sdkman_cleanup_folder "var/metadata"
;;
*)
__sdkman_cleanup_folder "archives"
__sdkman_cleanup_folder "tmp"
__sdkman_cleanup_folder "var/metadata"
;;
Expand Down
2 changes: 1 addition & 1 deletion src/main/bash/sdkman-help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function __sdk_help() {
fi

__sdkman_echo_no_colour " update"
__sdkman_echo_no_colour " flush [archives|tmp|broadcast|metadata|version]"
__sdkman_echo_no_colour " flush [tmp|broadcast|metadata|version]"
__sdkman_echo_no_colour ""
__sdkman_echo_no_colour " candidate : the SDK to install: groovy, scala, grails, gradle, kotlin, etc."
__sdkman_echo_no_colour " use list command for comprehensive list of candidates"
Expand Down
93 changes: 42 additions & 51 deletions src/main/bash/sdkman-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function __sdkman_install_candidate_version() {
mkdir -p "${SDKMAN_CANDIDATES_DIR}/${candidate}"

rm -rf "${SDKMAN_DIR}/tmp/out"
unzip -oq "${SDKMAN_DIR}/archives/${candidate}-${version}.zip" -d "${SDKMAN_DIR}/tmp/out"
unzip -oq "${SDKMAN_DIR}/tmp/${candidate}-${version}.zip" -d "${SDKMAN_DIR}/tmp/out"
mv -f "$SDKMAN_DIR"/tmp/out/* "${SDKMAN_CANDIDATES_DIR}/${candidate}/${version}"
__sdkman_echo_green "Done installing!"
echo ""
Expand Down Expand Up @@ -114,65 +114,56 @@ function __sdkman_install_local_version() {
}

function __sdkman_download() {
local candidate version archives_folder
local candidate version

candidate="$1"
version="$2"

archives_folder="${SDKMAN_DIR}/archives"
metadata_folder="${SDKMAN_DIR}/var/metadata"
mkdir -p ${metadata_folder}

if [ ! -f "${archives_folder}/${candidate}-${version}.zip" ]; then
local platform_parameter="$(echo $SDKMAN_PLATFORM | tr '[:upper:]' '[:lower:]')"
local download_url="${SDKMAN_CANDIDATES_API}/broker/download/${candidate}/${version}/${platform_parameter}"
local base_name="${candidate}-${version}"
local zip_archive_target="${SDKMAN_DIR}/archives/${base_name}.zip"
local tmp_headers_file="${SDKMAN_DIR}/tmp/${base_name}.headers.tmp"
local headers_file="${metadata_folder}/${base_name}.headers"

# pre-installation hook: implements function __sdkman_pre_installation_hook
local pre_installation_hook="${SDKMAN_DIR}/tmp/hook_pre_${candidate}_${version}.sh"
__sdkman_echo_debug "Get pre-installation hook: ${SDKMAN_CANDIDATES_API}/hooks/pre/${candidate}/${version}/${platform_parameter}"
__sdkman_secure_curl "${SDKMAN_CANDIDATES_API}/hooks/pre/${candidate}/${version}/${platform_parameter}" >| "$pre_installation_hook"
__sdkman_echo_debug "Copy remote pre-installation hook: $pre_installation_hook"
source "$pre_installation_hook"
__sdkman_pre_installation_hook || return 1
__sdkman_echo_debug "Completed pre-installation hook..."

export local binary_input="${SDKMAN_DIR}/tmp/${base_name}.bin"
export local zip_output="${SDKMAN_DIR}/tmp/$base_name.zip"
local platform_parameter="$(echo $SDKMAN_PLATFORM | tr '[:upper:]' '[:lower:]')"
local download_url="${SDKMAN_CANDIDATES_API}/broker/download/${candidate}/${version}/${platform_parameter}"
local base_name="${candidate}-${version}"
local tmp_headers_file="${SDKMAN_DIR}/tmp/${base_name}.headers.tmp"
local headers_file="${metadata_folder}/${base_name}.headers"

# pre-installation hook: implements function __sdkman_pre_installation_hook
local pre_installation_hook="${SDKMAN_DIR}/tmp/hook_pre_${candidate}_${version}.sh"
__sdkman_echo_debug "Get pre-installation hook: ${SDKMAN_CANDIDATES_API}/hooks/pre/${candidate}/${version}/${platform_parameter}"
__sdkman_secure_curl "${SDKMAN_CANDIDATES_API}/hooks/pre/${candidate}/${version}/${platform_parameter}" >| "$pre_installation_hook"
__sdkman_echo_debug "Copy remote pre-installation hook: $pre_installation_hook"
source "$pre_installation_hook"
__sdkman_pre_installation_hook || return 1
__sdkman_echo_debug "Completed pre-installation hook..."

export local binary_input="${SDKMAN_DIR}/tmp/${base_name}.bin"
export local zip_output="${SDKMAN_DIR}/tmp/${base_name}.zip"

echo ""
__sdkman_echo_no_colour "Downloading: ${candidate} ${version}"
echo ""
__sdkman_echo_no_colour "In progress..."
echo ""
echo ""
__sdkman_echo_no_colour "Downloading: ${candidate} ${version}"
echo ""
__sdkman_echo_no_colour "In progress..."
echo ""

# download binary
__sdkman_secure_curl_download "${download_url}" --output "${binary_input}" --dump-header "${tmp_headers_file}"
grep '^X-Sdkman' "${tmp_headers_file}" > "${headers_file}"
__sdkman_echo_debug "Downloaded binary to: ${binary_input} (HTTP headers written to: ${headers_file})"

# post-installation hook: implements function __sdkman_post_installation_hook
# responsible for taking `binary_input` and producing `zip_output`
local post_installation_hook="${SDKMAN_DIR}/tmp/hook_post_${candidate}_${version}.sh"
__sdkman_echo_debug "Get post-installation hook: ${SDKMAN_CANDIDATES_API}/hooks/post/${candidate}/${version}/${platform_parameter}"
__sdkman_secure_curl "${SDKMAN_CANDIDATES_API}/hooks/post/${candidate}/${version}/${platform_parameter}" >| "$post_installation_hook"
__sdkman_echo_debug "Copy remote post-installation hook: ${post_installation_hook}"
source "$post_installation_hook"
__sdkman_post_installation_hook || return 1
__sdkman_echo_debug "Processed binary as: $zip_output"
__sdkman_echo_debug "Completed post-installation hook..."

mv -f "$zip_output" "$zip_archive_target"
__sdkman_echo_debug "Moved to archive folder: $zip_archive_target"
else
echo ""
__sdkman_echo_no_colour "Found a previously downloaded ${candidate} ${version} archive. Not downloading it again..."
fi
__sdkman_validate_zip "${archives_folder}/${candidate}-${version}.zip" || return 1
__sdkman_checksum_zip "${archives_folder}/${candidate}-${version}.zip" "${headers_file}" || return 1
# download binary
__sdkman_secure_curl_download "${download_url}" --output "${binary_input}" --dump-header "${tmp_headers_file}"
grep '^X-Sdkman' "${tmp_headers_file}" > "${headers_file}"
__sdkman_echo_debug "Downloaded binary to: ${binary_input} (HTTP headers written to: ${headers_file})"

# post-installation hook: implements function __sdkman_post_installation_hook
# responsible for taking `binary_input` and producing `zip_output`
local post_installation_hook="${SDKMAN_DIR}/tmp/hook_post_${candidate}_${version}.sh"
__sdkman_echo_debug "Get post-installation hook: ${SDKMAN_CANDIDATES_API}/hooks/post/${candidate}/${version}/${platform_parameter}"
__sdkman_secure_curl "${SDKMAN_CANDIDATES_API}/hooks/post/${candidate}/${version}/${platform_parameter}" >| "$post_installation_hook"
__sdkman_echo_debug "Copy remote post-installation hook: ${post_installation_hook}"
source "$post_installation_hook"
__sdkman_post_installation_hook || return 1
__sdkman_echo_debug "Processed binary as: $zip_output"
__sdkman_echo_debug "Completed post-installation hook..."

__sdkman_validate_zip "${zip_output}" || return 1
__sdkman_checksum_zip "${zip_output}" "${headers_file}" || return 1
echo ""
}

Expand Down
3 changes: 1 addition & 2 deletions src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SdkmanBashEnvBuilder {
sdkman_selfupdate_feature: 'true'
]

File sdkmanDir, sdkmanBinDir, sdkmanVarDir, sdkmanSrcDir, sdkmanEtcDir, sdkmanExtDir, sdkmanArchivesDir,
File sdkmanDir, sdkmanBinDir, sdkmanVarDir, sdkmanSrcDir, sdkmanEtcDir, sdkmanExtDir,
sdkmanTmpDir, sdkmanCandidatesDir, sdkmanMetadataDir, sdkmanContribDir

static SdkmanBashEnvBuilder create(File baseFolder) {
Expand Down Expand Up @@ -112,7 +112,6 @@ class SdkmanBashEnvBuilder {
sdkmanSrcDir = prepareDirectory(sdkmanDir, "src")
sdkmanEtcDir = prepareDirectory(sdkmanDir, "etc")
sdkmanExtDir = prepareDirectory(sdkmanDir, "ext")
sdkmanArchivesDir = prepareDirectory(sdkmanDir, "archives")
sdkmanTmpDir = prepareDirectory(sdkmanDir, "tmp")
sdkmanCandidatesDir = prepareDirectory(sdkmanDir, "candidates")
sdkmanMetadataDir = prepareDirectory(sdkmanVarDir, "metadata")
Expand Down
1 change: 0 additions & 1 deletion src/test/groovy/sdkman/steps/env.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ varDir = "${sdkmanDirEnv}/var" as File
metadataDir = "${varDir}/metadata" as File
etcDir = "${sdkmanDirEnv}/etc" as File
extDir = "${sdkmanDirEnv}/ext" as File
archiveDir = "${sdkmanDirEnv}/archives" as File
tmpDir = "${sdkmanDir}/tmp" as File

broadcastFile = new File(varDir, "broadcast")
Expand Down
13 changes: 0 additions & 13 deletions src/test/groovy/sdkman/steps/flush_steps.groovy
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package sdkman.steps

import java.nio.file.Files
import java.nio.file.Paths

import static cucumber.api.groovy.EN.And

And(~'^the candidate "([^"]*)" is known locally$') { String candidate ->
Expand All @@ -13,16 +10,6 @@ And(~'^no candidates are know locally$') { ->
assert !candidatesFile.exists()
}

And(~'^the archive "([^"]*)" has been cached$') { String archive ->
Files.copy(
Paths.get("src/test/resources/__files", archive),
Paths.get(archiveDir.getAbsolutePath(), archive))
}

And(~'^no archives are cached$') { ->
assert !archiveDir.listFiles()
}

And(~'^the file "([^"]*)" in temporary storage$') { String fileName ->
new File(tmpDir, fileName).createNewFile()
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/groovy/sdkman/steps/initialisation_steps.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ And(~'^the archive for candidate "([^"]*)" version "([^"]*)" is corrupt$') { Str
}

And(~'^the archive for candidate "([^"]*)" version "([^"]*)" is removed$') { String candidate, String version ->
def archive = new File("${sdkmanDir}/archives/${candidate}-${version}.zip")
def archive = new File("${sdkmanDir}/tmp/${candidate}-${version}.zip")
assert !archive.exists()
}

Expand Down
36 changes: 0 additions & 36 deletions src/test/resources/features/checksum_verification.feature
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,6 @@ Feature: Verify checksums
And the response headers file is created for candidate "grails" and version "1.3.9"
And the exit code is 0

Scenario: Install an already downloaded Candidate with a valid MD5 checksum and no local headers file
Given the system is bootstrapped
And the candidate "grails" version "1.3.9" is available for download
And the archive "grails-1.3.9.zip" has been cached
When I enter "sdk install grails 1.3.9"
Then I see "Found a previously downloaded grails 1.3.9 archive. Not downloading it again..."
And I see "Done installing!"
And I see "Skipping checksum for cached artifact"
And I do not see "Downloading: grails 1.3.9"
And the candidate "grails" version "1.3.9" is installed
And the exit code is 0

Scenario: Install an already downloaded Candidate with a valid MD5 checksum
Given the system is bootstrapped
And the candidate "grails" version "1.3.9" is available for download
And the archive "grails-1.3.9.zip" has been cached
And a headers file "grails-1.3.9.headers" in metadata directory with checksum "1e87a7d982a2f41da96fdec289908552" using algorithm "MD5"
When I enter "sdk install grails 1.3.9"
Then I see "Found a previously downloaded grails 1.3.9 archive. Not downloading it again..."
And I see "Done installing!"
And I do not see "Downloading: grails 1.3.9"
And the candidate "grails" version "1.3.9" is installed
And the exit code is 0

Scenario: Install an already downloaded Candidate with a valid SHA1 checksum
Given the system is bootstrapped
And the candidate "grails" version "1.3.9" is available for download
And the archive "grails-1.3.9.zip" has been cached
And a headers file "grails-1.3.9.headers" in metadata directory with checksum "c68e386a6deec9fc4c1e18df21f92739ba2ab36e" using algorithm "SHA1"
When I enter "sdk install grails 1.3.9"
Then I see "Found a previously downloaded grails 1.3.9 archive. Not downloading it again..."
And I see "Done installing!"
And I do not see "Downloading: grails 1.3.9"
And the candidate "grails" version "1.3.9" is installed
And the exit code is 0

Scenario: Do not fail if an unknown algorithm is used
Given the system is bootstrapped
And the candidate "grails" version "1.3.9" is available for download with checksum "abc-checksum-00000" using algorithm "ABC"
Expand Down
10 changes: 1 addition & 9 deletions src/test/resources/features/flush.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ Feature: Flush
And an initialised environment
And the system is bootstrapped

Scenario: Clear out the cached archives, the temporary storage and metadata
Given the archive "grails-1.3.9.zip" has been cached
Scenario: Clear out the temporary storage and metadata
And the file "res-1.2.0.zip" in temporary storage
And a headers file "grails-1.3.9.headers" in metadata directory with checksum "c68e386a6deec9fc4c1e18df21f92739ba2ab36e" using algorithm "SHA1"
When I enter "sdk flush"
Then no archives are cached
And no "res-1.2.0.zip" file is present in temporary storage
And no metadata is cached
And I see "1 archive(s) flushed"
Expand All @@ -33,12 +31,6 @@ Feature: Flush
Then no version file can be found
And I see "Version file has been flushed."

Scenario: Clear out the cached Archives
Given the archive "grails-1.3.9.zip" has been cached
When I enter "sdk flush archives"
Then no archives are cached
And I see "1 archive(s) flushed"

Scenario: Clear out the temporary storage
Given the file "res-1.2.0.zip" in temporary storage
When I enter "sdk flush temp"
Expand Down
11 changes: 0 additions & 11 deletions src/test/resources/features/install_candidate.feature
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,6 @@ Feature: Install Candidate
And the candidate "grails" version "1.3.9" should be the default
And the exit code is 0

Scenario: Install an already downloaded Candidate
Given the system is bootstrapped
And the candidate "grails" version "1.3.9" is available for download
And the archive "grails-1.3.9.zip" has been cached
When I enter "sdk install grails 1.3.9"
Then I see "Found a previously downloaded grails 1.3.9 archive. Not downloading it again..."
And I see "Done installing!"
And I do not see "Downloading: grails 1.3.9"
And the candidate "grails" version "1.3.9" is installed
And the exit code is 0

# revisit to redownload automatically

Scenario: Abort installation on download of a corrupt Candidate archive
Expand Down

0 comments on commit 535e3c9

Please sign in to comment.