$builds Keyed by framework key, e.g. "net11.0-windows". */ public function __construct( public string $version, public string $channel, public ?string $notes, public ?Carbon $builtAt, public array $builds, public string $manifestHash, ) {} public static function current(): ?static { $version = static::disk()->get(static::CURRENT_FILE); return $version === null ? null : static::find(trim($version)); } public static function find(string $version): ?static { if (preg_match(static::VERSION_PATTERN, $version) !== 1) { return null; } $manifestJson = static::disk()->get(static::path($version, static::MANIFEST_FILE_NAME)); $manifest = json_decode($manifestJson ?? '', true); if (! is_array($manifest)) { return null; } return new static( $version, $manifest['channel'] ?? '', $manifest['notes'] ?? null, isset($manifest['published']) ? Carbon::parse($manifest['published']) : null, $manifest['builds'] ?? [], hash('sha256', $manifestJson), ); } public static function findOrFail(string $version): static { return static::find($version) ?? abort(404); } /** * @return Collection Newest version first. */ public static function all(): Collection { return collect(static::disk()->directories('releases')) ->map(fn (string $directory) => static::find(basename($directory))) ->filter() ->sort(fn (self $a, self $b) => version_compare($b->version, $a->version)) ->values(); } public function makeCurrent(): bool { return static::disk()->put(static::CURRENT_FILE, $this->version); } public function isCurrent(): bool { return static::current()?->version === $this->version; } /** * Deletes the release folder. The current release is never deleted: launchers are downloading it. */ public function delete(): bool { return ! $this->isCurrent() && static::disk()->deleteDirectory(static::path($this->version)); } /** * Deletes the oldest versions beyond MAX_RELEASES, never the current release. * * @return Collection The deleted releases. */ public static function pruneOldReleases(): Collection { $currentVersion = static::current()?->version; return static::all() ->reject(fn (self $release) => $release->version === $currentVersion) ->slice($currentVersion === null ? static::MAX_RELEASES : static::MAX_RELEASES - 1) ->filter(fn (self $release) => $release->delete()) ->values(); } public function isStable(): bool { return $this->channel === static::STABLE_CHANNEL; } /** * @return string[] */ public function frameworks(): array { return array_keys($this->builds); } /** * Absolute path of a file the launcher may download from this release, or null for anything the manifest doesn't list. */ public function servableFilePath(string $fileName): ?string { $servableFileNames = [static::MANIFEST_FILE_NAME, static::SIGNATURE_FILE_NAME, ...array_column($this->builds, 'file')]; $path = static::path($this->version, $fileName); if (! in_array($fileName, $servableFileNames, true) || ! static::disk()->exists($path)) { return null; } return static::disk()->path($path); } /** * The build offered on the download page: the newest framework, e.g. net11.0-windows over net9.0-windows. * * @return array{file: string, size: int, sha256: string}|null */ public function downloadBuild(): ?array { return collect($this->builds) ->sortKeysUsing(fn (string $a, string $b) => strnatcmp($b, $a)) ->first(); } public static function path(string $version, string $fileName = ''): string { return rtrim("releases/{$version}/{$fileName}", '/'); } public static function disk(): FilesystemAdapter { return Storage::disk('launcher'); } }