mirror of
				https://github.com/shivammathur/setup-php.git
				synced 2025-11-04 17:06:37 +07:00 
			
		
		
		
	Fetch release with tool when latest one does not
This commit is contained in:
		@ -145,6 +145,10 @@ add_tool() {
 | 
			
		||||
  else
 | 
			
		||||
    status_code=$(sudo curl -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "$url")
 | 
			
		||||
  fi
 | 
			
		||||
  if [ "$status_code" != "200" ] && [[ "$url" =~ .*github.com.*releases.*latest.* ]]; then
 | 
			
		||||
    url=$(echo $url | sed -e "s|releases/latest/download|releases/download/$(curl "${curl_opts[@]}" "$(echo "$url" | cut -d '/' -f '1-5')/releases" | grep -Eo -m 1 "([0-9]+\.[0-9]+\.[0-9]+)/$(echo "$url" | sed -e "s/.*\///")" | cut -d '/' -f 1)|")
 | 
			
		||||
    status_code=$(sudo curl -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
 | 
			
		||||
 | 
			
		||||
@ -179,6 +179,10 @@ add_tool() {
 | 
			
		||||
  else
 | 
			
		||||
    status_code=$(sudo curl -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "$url")
 | 
			
		||||
  fi
 | 
			
		||||
  if [ "$status_code" != "200" ] && [[ "$url" =~ .*github.com.*releases.*latest.* ]]; then
 | 
			
		||||
    url=$(echo $url | sed -e "s|releases/latest/download|releases/download/$(curl "${curl_opts[@]}" "$(echo "$url" | cut -d '/' -f '1-5')/releases" | grep -Eo -m 1 "([0-9]+\.[0-9]+\.[0-9]+)/$(echo "$url" | sed -e "s/.*\///")" | cut -d '/' -f 1)|")
 | 
			
		||||
    status_code=$(sudo curl -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
 | 
			
		||||
 | 
			
		||||
@ -179,33 +179,41 @@ Function Add-Tool() {
 | 
			
		||||
  if($url.Count -gt 1) { $url = $url[0] }
 | 
			
		||||
  if ($tool -eq "symfony") {
 | 
			
		||||
    Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $php_dir\$tool.exe
 | 
			
		||||
    Add-ToProfile $current_profile $tool "New-Alias $tool $php_dir\$tool.exe" > $null 2>&1
 | 
			
		||||
    Add-ToProfile $current_profile $tool "New-Alias $tool $php_dir\$tool.exe" >$null 2>&1
 | 
			
		||||
  } else {
 | 
			
		||||
    try {
 | 
			
		||||
      Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $php_dir\$tool
 | 
			
		||||
      $bat_content = @()
 | 
			
		||||
      $bat_content += "@ECHO off"
 | 
			
		||||
      $bat_content += "setlocal DISABLEDELAYEDEXPANSION"
 | 
			
		||||
      $bat_content += "SET BIN_TARGET=%~dp0/" + $tool
 | 
			
		||||
      $bat_content += "php %BIN_TARGET% %*"
 | 
			
		||||
      Set-Content -Path $php_dir\$tool.bat -Value $bat_content
 | 
			
		||||
      Add-ToProfile $current_profile $tool "New-Alias $tool $php_dir\$tool.bat" > $null 2>&1
 | 
			
		||||
    } catch { }
 | 
			
		||||
    } catch {
 | 
			
		||||
      if($url -match '.*github.com.*releases.*latest.*') {
 | 
			
		||||
        try {
 | 
			
		||||
          $url = $url.replace("releases/latest/download", "releases/download/" + ([regex]::match((Invoke-WebRequest -UseBasicParsing -Uri ($url.split('/release')[0] + "/releases")).Content, "([0-9]+\.[0-9]+\.[0-9]+)/" + ($url.Substring($url.LastIndexOf("/") + 1))).Groups[0].Value).split('/')[0])
 | 
			
		||||
          Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $php_dir\$tool
 | 
			
		||||
        } catch { }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  if($tool -eq "phive") {
 | 
			
		||||
    Add-Extension curl >$null 2>&1
 | 
			
		||||
    Add-Extension mbstring >$null 2>&1
 | 
			
		||||
    Add-Extension xml >$null 2>&1
 | 
			
		||||
  } elseif($tool -eq "cs2pr") {
 | 
			
		||||
    (Get-Content $php_dir/cs2pr).replace('exit(9)', 'exit(0)') | Set-Content $php_dir/cs2pr
 | 
			
		||||
  } elseif($tool -eq "composer") {
 | 
			
		||||
    Edit-ComposerConfig $php_dir\$tool
 | 
			
		||||
  } elseif($tool -eq "wp-cli") {
 | 
			
		||||
    Copy-Item $php_dir\wp-cli.bat -Destination $php_dir\wp.bat
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (((Get-ChildItem -Path $php_dir/* | Where-Object Name -Match "^$tool(.exe|.phar)*$").Count -gt 0)) {
 | 
			
		||||
    Add-Log $tick $tool "Added"
 | 
			
		||||
    $bat_content = @()
 | 
			
		||||
    $bat_content += "@ECHO off"
 | 
			
		||||
    $bat_content += "setlocal DISABLEDELAYEDEXPANSION"
 | 
			
		||||
    $bat_content += "SET BIN_TARGET=%~dp0/" + $tool
 | 
			
		||||
    $bat_content += "php %BIN_TARGET% %*"
 | 
			
		||||
    Set-Content -Path $php_dir\$tool.bat -Value $bat_content
 | 
			
		||||
    Add-ToProfile $current_profile $tool "New-Alias $tool $php_dir\$tool.bat" >$null 2>&1
 | 
			
		||||
    if($tool -eq "phan") {
 | 
			
		||||
      Add-Extension fileinfo >$null 2>&1
 | 
			
		||||
      Add-Extension ast >$null 2>&1
 | 
			
		||||
    } elseif($tool -eq "phive") {
 | 
			
		||||
      Add-Extension xml >$null 2>&1
 | 
			
		||||
    } elseif($tool -eq "cs2pr") {
 | 
			
		||||
      (Get-Content $php_dir/cs2pr).replace('exit(9)', 'exit(0)') | Set-Content $php_dir/cs2pr
 | 
			
		||||
    } elseif($tool -eq "composer") {
 | 
			
		||||
      Edit-ComposerConfig $php_dir\$tool
 | 
			
		||||
    } elseif($tool -eq "wp-cli") {
 | 
			
		||||
      Copy-Item $php_dir\wp-cli.bat -Destination $php_dir\wp.bat
 | 
			
		||||
    }
 | 
			
		||||
    $tool_version = Get-ToolVersion $tool $ver_param
 | 
			
		||||
    Add-Log $tick $tool "Added $tool $tool_version"
 | 
			
		||||
  } else {
 | 
			
		||||
    Add-Log $cross $tool "Could not add $tool"
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user