Use cached composer builds

This commit is contained in:
Shivam Mathur 2020-08-04 20:47:30 +05:30
parent 9dae796baf
commit 91bd6b26c2
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
6 changed files with 35 additions and 17 deletions

View File

@ -424,7 +424,7 @@ describe('Tools tests', () => {
'add_tool https://get.blackfire.io/blackfire-player.phar blackfire-player' 'add_tool https://get.blackfire.io/blackfire-player.phar blackfire-player'
); );
expect(script).toContain( 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( expect(script).toContain(
'add_tool https://github.com/staabm/annotate-pull-request-from-checkstyle/releases/latest/download/cs2pr cs2pr' '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' 'add_tool https://get.blackfire.io/blackfire-player.phar blackfire-player'
); );
expect(script).toContain( 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( expect(script).toContain(
'add_tool https://github.com/ergebnis/composer-normalize/releases/latest/download/composer-normalize.phar composer-normalize' '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' 'Add-Tool https://get.blackfire.io/blackfire-player-v1.8.1.phar blackfire-player'
); );
expect(script).toContain( 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( expect(script).toContain(
'Add-Tool https://github.com/staabm/annotate-pull-request-from-checkstyle/releases/latest/download/cs2pr cs2pr' '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( 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 prestissimo prestissimo hirak/');
expect(script).toContain('Add-Composertool phinx phinx robmorgan/'); expect(script).toContain('Add-Composertool phinx phinx robmorgan/');
@ -649,12 +649,12 @@ describe('Tools tests', () => {
); );
expect(script).toContain( 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'); script = await tools.addTools('composer:preview', '7.4', 'linux');
expect(script).toContain( 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( script = await tools.addTools(
'composer:v1, composer:preview, composer:snapshot', 'composer:v1, composer:preview, composer:snapshot',
@ -662,7 +662,7 @@ describe('Tools tests', () => {
'linux' 'linux'
); );
expect(script).toContain( 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'
); );
}); });
}); });

9
dist/index.js vendored
View File

@ -2229,21 +2229,24 @@ exports.addComposer = addComposer;
* @param version * @param version
*/ */
async function getComposerUrl(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 getComposerUrlHelper = async function (version) {
const client = new httpm.HttpClient('setup-php'); const client = new httpm.HttpClient('setup-php');
const response = await client.get('https://getcomposer.org/versions'); const response = await client.get('https://getcomposer.org/versions');
const data = JSON.parse(await response.readBody()); 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) { switch (version) {
case 'snapshot': case 'snapshot':
return 'https://getcomposer.org/composer.phar'; return cache_url + 'https://getcomposer.org/composer.phar';
case 'preview': case 'preview':
case '1': case '1':
case '2': case '2':
return await getComposerUrlHelper(version); return await getComposerUrlHelper(version);
default: default:
return 'https://getcomposer.org/composer-stable.phar'; return cache_url + 'https://getcomposer.org/composer-stable.phar';
} }
} }
exports.getComposerUrl = getComposerUrl; exports.getComposerUrl = getComposerUrl;

View File

@ -167,7 +167,13 @@ add_tool() {
rm -rf "$tool_path" rm -rf "$tool_path"
fi 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 if [ "$status_code" = "200" ]; then
sudo chmod a+x "$tool_path" sudo chmod a+x "$tool_path"
if [ "$tool" = "composer" ]; then if [ "$tool" = "composer" ]; then

View File

@ -261,7 +261,13 @@ add_tool() {
if [ ! -e "$tool_path" ]; then if [ ! -e "$tool_path" ]; then
rm -rf "$tool_path" rm -rf "$tool_path"
fi 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 if [ "$status_code" = "200" ]; then
sudo chmod a+x "$tool_path" sudo chmod a+x "$tool_path"
if [ "$tool" = "composer" ]; then if [ "$tool" = "composer" ]; then

View File

@ -187,8 +187,6 @@ Function Add-Tool() {
Param ( Param (
[Parameter(Position = 0, Mandatory = $true)] [Parameter(Position = 0, Mandatory = $true)]
[ValidateNotNull()] [ValidateNotNull()]
[ValidateLength(1, [int]::MaxValue)]
[string]
$url, $url,
[Parameter(Position = 1, Mandatory = $true)] [Parameter(Position = 1, Mandatory = $true)]
[ValidateNotNull()] [ValidateNotNull()]
@ -199,6 +197,7 @@ Function Add-Tool() {
if (Test-Path $bin_dir\$tool) { if (Test-Path $bin_dir\$tool) {
Remove-Item $bin_dir\$tool Remove-Item $bin_dir\$tool
} }
if($url.Count -gt 1) { $url = $url[0] }
if ($tool -eq "symfony") { if ($tool -eq "symfony") {
Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $bin_dir\$tool.exe 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 Add-ToProfile $current_profile $tool "New-Alias $tool $bin_dir\$tool.exe" >$null 2>&1

View File

@ -329,23 +329,27 @@ export async function addComposer(tools_list: string[]): Promise<string[]> {
* @param version * @param version
*/ */
export async function getComposerUrl(version: string): Promise<string> { export async function getComposerUrl(version: string): Promise<string> {
const cache_url =
'https://github.com/shivammathur/composer-cache/releases/latest/download/composer-' +
version.replace('latest', 'stable') +
'.phar,';
const getComposerUrlHelper = async function ( const getComposerUrlHelper = async function (
version: string version: string
): Promise<string> { ): Promise<string> {
const client: httpm.HttpClient = new httpm.HttpClient('setup-php'); const client: httpm.HttpClient = new httpm.HttpClient('setup-php');
const response: hcr = await client.get('https://getcomposer.org/versions'); const response: hcr = await client.get('https://getcomposer.org/versions');
const data = JSON.parse(await response.readBody()); 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) { switch (version) {
case 'snapshot': case 'snapshot':
return 'https://getcomposer.org/composer.phar'; return cache_url + 'https://getcomposer.org/composer.phar';
case 'preview': case 'preview':
case '1': case '1':
case '2': case '2':
return await getComposerUrlHelper(version); return await getComposerUrlHelper(version);
default: default:
return 'https://getcomposer.org/composer-stable.phar'; return cache_url + 'https://getcomposer.org/composer-stable.phar';
} }
} }