From 91bd6b26c271cc30f882588631e6af5a391c384a Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Tue, 4 Aug 2020 20:47:30 +0530 Subject: [PATCH 1/3] Use cached composer builds --- __tests__/tools.test.ts | 14 +++++++------- dist/index.js | 9 ++++++--- src/scripts/darwin.sh | 8 +++++++- src/scripts/linux.sh | 8 +++++++- src/scripts/win32.ps1 | 3 +-- src/tools.ts | 10 +++++++--- 6 files changed, 35 insertions(+), 17 deletions(-) diff --git a/__tests__/tools.test.ts b/__tests__/tools.test.ts index a81e6bc8..abbdb424 100644 --- a/__tests__/tools.test.ts +++ b/__tests__/tools.test.ts @@ -424,7 +424,7 @@ describe('Tools tests', () => { 'add_tool https://get.blackfire.io/blackfire-player.phar blackfire-player' ); expect(script).toContain( - 'add_tool https://getcomposer.org/composer-stable.phar composer' + 'add_tool https://github.com/shivammathur/composer-cache/releases/latest/download/composer-stable.phar,https://getcomposer.org/composer-stable.phar composer' ); expect(script).toContain( 'add_tool https://github.com/staabm/annotate-pull-request-from-checkstyle/releases/latest/download/cs2pr cs2pr' @@ -499,7 +499,7 @@ describe('Tools tests', () => { 'add_tool https://get.blackfire.io/blackfire-player.phar blackfire-player' ); expect(script).toContain( - 'add_tool https://getcomposer.org/composer-stable.phar composer' + 'add_tool https://github.com/shivammathur/composer-cache/releases/latest/download/composer-stable.phar,https://getcomposer.org/composer-stable.phar composer' ); expect(script).toContain( 'add_tool https://github.com/ergebnis/composer-normalize/releases/latest/download/composer-normalize.phar composer-normalize' @@ -590,7 +590,7 @@ describe('Tools tests', () => { 'Add-Tool https://get.blackfire.io/blackfire-player-v1.8.1.phar blackfire-player' ); expect(script).toContain( - 'Add-Tool https://getcomposer.org/composer-stable.phar composer' + 'Add-Tool https://github.com/shivammathur/composer-cache/releases/latest/download/composer-stable.phar,https://getcomposer.org/composer-stable.phar composer' ); expect(script).toContain( 'Add-Tool https://github.com/staabm/annotate-pull-request-from-checkstyle/releases/latest/download/cs2pr cs2pr' @@ -631,7 +631,7 @@ describe('Tools tests', () => { ); expect(script).toContain( - 'Add-Tool https://getcomposer.org/composer-stable.phar composer' + 'Add-Tool https://github.com/shivammathur/composer-cache/releases/latest/download/composer-stable.phar,https://getcomposer.org/composer-stable.phar composer' ); expect(script).toContain('Add-Composertool prestissimo prestissimo hirak/'); expect(script).toContain('Add-Composertool phinx phinx robmorgan/'); @@ -649,12 +649,12 @@ describe('Tools tests', () => { ); expect(script).toContain( - 'add_tool https://getcomposer.org/composer-1.phar composer' + 'add_tool https://github.com/shivammathur/composer-cache/releases/latest/download/composer-1.phar,https://getcomposer.org/composer-1.phar composer' ); script = await tools.addTools('composer:preview', '7.4', 'linux'); expect(script).toContain( - 'add_tool https://getcomposer.org/composer-preview.phar composer' + 'add_tool https://github.com/shivammathur/composer-cache/releases/latest/download/composer-preview.phar,https://getcomposer.org/composer-preview.phar composer' ); script = await tools.addTools( 'composer:v1, composer:preview, composer:snapshot', @@ -662,7 +662,7 @@ describe('Tools tests', () => { 'linux' ); expect(script).toContain( - 'add_tool https://getcomposer.org/composer.phar composer' + 'add_tool https://github.com/shivammathur/composer-cache/releases/latest/download/composer-snapshot.phar,https://getcomposer.org/composer.phar composer' ); }); }); diff --git a/dist/index.js b/dist/index.js index 99b81b43..c57be4e1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2229,21 +2229,24 @@ exports.addComposer = addComposer; * @param version */ async function getComposerUrl(version) { + const cache_url = 'https://github.com/shivammathur/composer-cache/releases/latest/download/composer-' + + version.replace('latest', 'stable') + + '.phar,'; const getComposerUrlHelper = async function (version) { const client = new httpm.HttpClient('setup-php'); const response = await client.get('https://getcomposer.org/versions'); const data = JSON.parse(await response.readBody()); - return 'https://getcomposer.org' + data[version][0]['path']; + return cache_url + 'https://getcomposer.org' + data[version][0]['path']; }; switch (version) { case 'snapshot': - return 'https://getcomposer.org/composer.phar'; + return cache_url + 'https://getcomposer.org/composer.phar'; case 'preview': case '1': case '2': return await getComposerUrlHelper(version); default: - return 'https://getcomposer.org/composer-stable.phar'; + return cache_url + 'https://getcomposer.org/composer-stable.phar'; } } exports.getComposerUrl = getComposerUrl; diff --git a/src/scripts/darwin.sh b/src/scripts/darwin.sh index e27c4d59..e95fb321 100644 --- a/src/scripts/darwin.sh +++ b/src/scripts/darwin.sh @@ -167,7 +167,13 @@ add_tool() { rm -rf "$tool_path" fi - status_code=$(sudo curl -s -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "$url") + if [ "$tool" = "composer" ]; then + IFS="," read -r -a urls <<< "$url" + status_code=$(sudo curl -s -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "${urls[0]}") || + status_code=$(sudo curl -s -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "${urls[1]}") + else + status_code=$(sudo curl -s -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "$url") + fi if [ "$status_code" = "200" ]; then sudo chmod a+x "$tool_path" if [ "$tool" = "composer" ]; then diff --git a/src/scripts/linux.sh b/src/scripts/linux.sh index adcf9f23..f12e06dc 100644 --- a/src/scripts/linux.sh +++ b/src/scripts/linux.sh @@ -261,7 +261,13 @@ add_tool() { if [ ! -e "$tool_path" ]; then rm -rf "$tool_path" fi - status_code=$(sudo curl -s -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "$url") + if [ "$tool" = "composer" ]; then + IFS="," read -r -a urls <<< "$url" + status_code=$(sudo curl -s -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "${urls[0]}") || + status_code=$(sudo curl -s -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "${urls[1]}") + else + status_code=$(sudo curl -s -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "$url") + fi if [ "$status_code" = "200" ]; then sudo chmod a+x "$tool_path" if [ "$tool" = "composer" ]; then diff --git a/src/scripts/win32.ps1 b/src/scripts/win32.ps1 index 9731dcbc..8b5509ee 100644 --- a/src/scripts/win32.ps1 +++ b/src/scripts/win32.ps1 @@ -187,8 +187,6 @@ Function Add-Tool() { Param ( [Parameter(Position = 0, Mandatory = $true)] [ValidateNotNull()] - [ValidateLength(1, [int]::MaxValue)] - [string] $url, [Parameter(Position = 1, Mandatory = $true)] [ValidateNotNull()] @@ -199,6 +197,7 @@ Function Add-Tool() { if (Test-Path $bin_dir\$tool) { Remove-Item $bin_dir\$tool } + if($url.Count -gt 1) { $url = $url[0] } if ($tool -eq "symfony") { Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $bin_dir\$tool.exe Add-ToProfile $current_profile $tool "New-Alias $tool $bin_dir\$tool.exe" >$null 2>&1 diff --git a/src/tools.ts b/src/tools.ts index 50677a05..ba6ec43c 100644 --- a/src/tools.ts +++ b/src/tools.ts @@ -329,23 +329,27 @@ export async function addComposer(tools_list: string[]): Promise { * @param version */ export async function getComposerUrl(version: string): Promise { + const cache_url = + 'https://github.com/shivammathur/composer-cache/releases/latest/download/composer-' + + version.replace('latest', 'stable') + + '.phar,'; const getComposerUrlHelper = async function ( version: string ): Promise { const client: httpm.HttpClient = new httpm.HttpClient('setup-php'); const response: hcr = await client.get('https://getcomposer.org/versions'); const data = JSON.parse(await response.readBody()); - return 'https://getcomposer.org' + data[version][0]['path']; + return cache_url + 'https://getcomposer.org' + data[version][0]['path']; }; switch (version) { case 'snapshot': - return 'https://getcomposer.org/composer.phar'; + return cache_url + 'https://getcomposer.org/composer.phar'; case 'preview': case '1': case '2': return await getComposerUrlHelper(version); default: - return 'https://getcomposer.org/composer-stable.phar'; + return cache_url + 'https://getcomposer.org/composer-stable.phar'; } } From 3ca32c6b64bb1e0e72eb5000c26dc7d747eccba0 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Tue, 4 Aug 2020 21:16:48 +0530 Subject: [PATCH 2/3] Switch to exponential-backoff in cURL --- src/scripts/darwin.sh | 2 +- src/scripts/linux.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scripts/darwin.sh b/src/scripts/darwin.sh index e95fb321..686f0a8f 100644 --- a/src/scripts/darwin.sh +++ b/src/scripts/darwin.sh @@ -241,7 +241,7 @@ version=$1 nodot_version=${1/./} old_versions="5.[3-5]" tool_path_dir="/usr/local/bin" -curl_opts=(-sSL --retry 5 --retry-delay 1) +curl_opts=(-sSL --retry 5) existing_version=$(php-config --version 2>/dev/null | cut -c 1-3) read_env diff --git a/src/scripts/linux.sh b/src/scripts/linux.sh index f12e06dc..a11c1e82 100644 --- a/src/scripts/linux.sh +++ b/src/scripts/linux.sh @@ -398,7 +398,7 @@ debconf_fix="DEBIAN_FRONTEND=noninteractive" github="https://github.com/shivammathur" apt_install="sudo $debconf_fix apt-fast install -y" tool_path_dir="/usr/local/bin" -curl_opts=(-sSL --retry 5 --retry-delay 1) +curl_opts=(-sSL --retry 5) existing_version=$(php-config --version 2>/dev/null | cut -c 1-3) read_env From 494d047fa5c1c2b80d3ef94edb6ccafe0e86aa2a Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Tue, 4 Aug 2020 21:37:52 +0530 Subject: [PATCH 3/3] Fix permissions in protoc setup --- src/scripts/tools/protoc.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scripts/tools/protoc.sh b/src/scripts/tools/protoc.sh index 545077e6..c604d62d 100644 --- a/src/scripts/tools/protoc.sh +++ b/src/scripts/tools/protoc.sh @@ -19,7 +19,7 @@ add_protoc() { [ "$(uname -s)" = "Darwin" ] && platform='osx' curl -o /tmp/protobuf.zip "${curl_opts[@]:?}" "https://github.com/protocolbuffers/protobuf/releases/download/$protobuf_tag/protoc-${protobuf_tag:1}-$platform-x86_64.zip" sudo unzip /tmp/protobuf.zip -d /usr/local/ - sudo chmod a+x /usr/local/bin/protoc + sudo chmod 777 /usr/local/bin/protoc -R /usr/local/include/google ) >/dev/null 2>&1 add_log "${tick:?}" "protoc" "Added" printf "::group::\033[34;1m%s \033[0m\033[90;1m%s \033[0m\n" "protoc" "Click to read the protoc related license information"