Skip to content

Commit d93fdd9

Browse files
committed
use github tokens for package downloads
1 parent c40d069 commit d93fdd9

4 files changed

Lines changed: 37 additions & 9 deletions

File tree

src/Package/Artifact/llvm_compiler_rt.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use StaticPHP\Artifact\ArtifactDownloader;
88
use StaticPHP\Artifact\Downloader\DownloadResult;
99
use StaticPHP\Artifact\Downloader\Type\CheckUpdateResult;
10+
use StaticPHP\Artifact\Downloader\Type\GitHubTokenSetupTrait;
1011
use StaticPHP\Attribute\Artifact\AfterBinaryExtract;
1112
use StaticPHP\Attribute\Artifact\CustomBinary;
1213
use StaticPHP\Attribute\Artifact\CustomBinaryCheckUpdate;
@@ -21,6 +22,8 @@
2122
*/
2223
class llvm_compiler_rt
2324
{
25+
use GitHubTokenSetupTrait;
26+
2427
#[CustomBinary('llvm-compiler-rt', [
2528
'linux-x86_64',
2629
'linux-aarch64',
@@ -34,7 +37,7 @@ public function downBinary(ArtifactDownloader $downloader): DownloadResult
3437
$tarball = "compiler-rt-{$llvmVersion}.src.tar.xz";
3538
$url = "https://github.com/llvm/llvm-project/releases/download/llvmorg-{$llvmVersion}/{$tarball}";
3639
$tarballPath = DOWNLOAD_PATH . '/' . $tarball;
37-
default_shell()->executeCurlDownload($url, $tarballPath, retries: $downloader->getRetry());
40+
default_shell()->executeCurlDownload($url, $tarballPath, headers: $this->getGitHubTokenHeaders(), retries: $downloader->getRetry());
3841
return DownloadResult::archive($tarball, ['url' => $url, 'version' => $llvmVersion], extract: '{source_path}/llvm-compiler-rt', verified: false, version: $llvmVersion);
3942
}
4043

src/Package/Artifact/llvm_tools.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use StaticPHP\Artifact\ArtifactDownloader;
88
use StaticPHP\Artifact\Downloader\DownloadResult;
99
use StaticPHP\Artifact\Downloader\Type\CheckUpdateResult;
10+
use StaticPHP\Artifact\Downloader\Type\GitHubTokenSetupTrait;
1011
use StaticPHP\Attribute\Artifact\AfterBinaryExtract;
1112
use StaticPHP\Attribute\Artifact\CustomBinary;
1213
use StaticPHP\Attribute\Artifact\CustomBinaryCheckUpdate;
@@ -17,6 +18,8 @@
1718

1819
class llvm_tools
1920
{
21+
use GitHubTokenSetupTrait;
22+
2023
public const array TOOLS = ['llvm-objcopy', 'llvm-strip', 'llvm-profdata'];
2124

2225
#[CustomBinary('llvm-tools', [
@@ -32,7 +35,7 @@ public function downBinary(ArtifactDownloader $downloader): DownloadResult
3235
$tarball = "llvm-project-{$llvmVersion}.src.tar.xz";
3336
$url = "https://github.com/llvm/llvm-project/releases/download/llvmorg-{$llvmVersion}/{$tarball}";
3437
$tarballPath = DOWNLOAD_PATH . '/' . $tarball;
35-
default_shell()->executeCurlDownload($url, $tarballPath, retries: $downloader->getRetry());
38+
default_shell()->executeCurlDownload($url, $tarballPath, headers: $this->getGitHubTokenHeaders(), retries: $downloader->getRetry());
3639
return DownloadResult::archive($tarball, ['url' => $url, 'version' => $llvmVersion], extract: '{source_path}/llvm-tools', verified: false, version: $llvmVersion);
3740
}
3841

src/StaticPHP/Artifact/Artifact.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ class Artifact
2727
/** @var null|callable Bind custom source fetcher callback */
2828
protected mixed $custom_source_callback = null;
2929

30+
/** @var null|string Display label describing where the custom source callback came from */
31+
protected ?string $custom_source_callback_origin = null;
32+
3033
/** @var null|callable Bind custom source check-update callback */
3134
protected mixed $custom_source_check_update_callback = null;
3235

3336
/** @var array<string, callable> Bind custom binary fetcher callbacks */
3437
protected mixed $custom_binary_callbacks = [];
3538

39+
/** @var array<string, string> Display label per platform describing where the custom binary callback came from */
40+
protected array $custom_binary_callback_origins = [];
41+
3642
/** @var array<string, callable> Bind custom binary check-update callbacks */
3743
protected array $custom_binary_check_update_callbacks = [];
3844

@@ -411,17 +417,25 @@ public function getBinaryDir(): ?string
411417

412418
/**
413419
* Set custom source fetcher callback.
420+
*
421+
* @param string $origin Short label shown in progress output (e.g. 'package downloader', 'custom url')
414422
*/
415-
public function setCustomSourceCallback(callable $callback): void
423+
public function setCustomSourceCallback(callable $callback, string $origin = 'package downloader'): void
416424
{
417425
$this->custom_source_callback = $callback;
426+
$this->custom_source_callback_origin = $origin;
418427
}
419428

420429
public function getCustomSourceCallback(): ?callable
421430
{
422431
return $this->custom_source_callback ?? null;
423432
}
424433

434+
public function getCustomSourceCallbackOrigin(): ?string
435+
{
436+
return $this->custom_source_callback_origin;
437+
}
438+
425439
/**
426440
* Set custom source check-update callback.
427441
*/
@@ -456,11 +470,19 @@ public function emitCustomBinary(): void
456470
*
457471
* @param string $target_os Target OS platform string (e.g. linux-x86_64)
458472
* @param callable $callback Custom binary fetcher callback
473+
* @param string $origin Short label shown in progress output (e.g. 'package downloader')
459474
*/
460-
public function setCustomBinaryCallback(string $target_os, callable $callback): void
475+
public function setCustomBinaryCallback(string $target_os, callable $callback, string $origin = 'package downloader'): void
461476
{
462477
ConfigValidator::validatePlatformString($target_os);
463478
$this->custom_binary_callbacks[$target_os] = $callback;
479+
$this->custom_binary_callback_origins[$target_os] = $origin;
480+
}
481+
482+
public function getCustomBinaryCallbackOrigin(): ?string
483+
{
484+
$current_platform = SystemTarget::getCurrentPlatformString();
485+
return $this->custom_binary_callback_origins[$current_platform] ?? null;
464486
}
465487

466488
/**

src/StaticPHP/Artifact/ArtifactDownloader.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,8 @@ private function downloadWithType(Artifact $artifact, int $current, int $total,
554554
$instance = null;
555555
$call = $this->downloaders[$item['config']['type']] ?? null;
556556
$type_display_name = match (true) {
557-
$item['lock'] === 'source' && ($callback = $artifact->getCustomSourceCallback()) !== null => 'user defined source downloader',
558-
$item['lock'] === 'binary' && ($callback = $artifact->getCustomBinaryCallback()) !== null => 'user defined binary downloader',
557+
$item['lock'] === 'source' && $artifact->getCustomSourceCallback() !== null => $artifact->getCustomSourceCallbackOrigin() ?? 'source package downloader',
558+
$item['lock'] === 'binary' && $artifact->getCustomBinaryCallback() !== null => $artifact->getCustomBinaryCallbackOrigin() ?? 'binary package downloader',
559559
default => SPC_DOWNLOAD_TYPE_DISPLAY_NAME[$item['config']['type']] ?? $item['config']['type'],
560560
};
561561
$try_h = $try ? 'Try downloading' : 'Downloading';
@@ -838,21 +838,21 @@ private function applyCustomDownloads(): void
838838
if (isset($this->artifacts[$artifact_name])) {
839839
$this->artifacts[$artifact_name]->setCustomSourceCallback(function (ArtifactDownloader $downloader) use ($artifact_name, $custom_url) {
840840
return (new Url())->download($artifact_name, ['url' => $custom_url], $downloader);
841-
});
841+
}, 'custom url');
842842
}
843843
}
844844
foreach ($this->custom_gits as $artifact_name => [$branch, $git_url]) {
845845
if (isset($this->artifacts[$artifact_name])) {
846846
$this->artifacts[$artifact_name]->setCustomSourceCallback(function (ArtifactDownloader $downloader) use ($artifact_name, $branch, $git_url) {
847847
return (new Git())->download($artifact_name, ['rev' => $branch, 'url' => $git_url], $downloader);
848-
});
848+
}, 'custom git');
849849
}
850850
}
851851
foreach ($this->custom_locals as $artifact_name => $local_path) {
852852
if (isset($this->artifacts[$artifact_name])) {
853853
$this->artifacts[$artifact_name]->setCustomSourceCallback(function (ArtifactDownloader $downloader) use ($artifact_name, $local_path) {
854854
return (new LocalDir())->download($artifact_name, ['dirname' => $local_path], $downloader);
855-
});
855+
}, 'custom local dir');
856856
}
857857
}
858858
}

0 commit comments

Comments
 (0)