From fa1e8fa9164bf7292c8c72ac445d4cad286bb557 Mon Sep 17 00:00:00 2001 From: Brandon Date: Fri, 24 Nov 2023 15:03:45 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Rewrite=20the=20Foundation=20upd?= =?UTF-8?q?ate=20script=20in=20PHP=20(#317)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/download-latest-foundation.sh | 36 ------- scripts/update-foundation | 143 ++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 36 deletions(-) delete mode 100755 scripts/download-latest-foundation.sh create mode 100644 scripts/update-foundation diff --git a/scripts/download-latest-foundation.sh b/scripts/download-latest-foundation.sh deleted file mode 100755 index ce5d9c38..00000000 --- a/scripts/download-latest-foundation.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env bash - -set -e - -# pro tip: maybe don't run this script. i mostly just hacked my way through this. this should definitely not be automated in any way. - -# thx u https://explainshell.com/ - -# YOINK! https://stackoverflow.com/a/4774063 -SCRIPTS_DIR="$(cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P)" - -TEMP_DIR="${SCRIPTS_DIR}/.temp" -FOUNDATION_DIR="${SCRIPTS_DIR}/../src/Illuminate/Foundation/" - -LARAVEL_VERSION="${1:-"$(curl --silent "https://api.github.com/repos/laravel/framework/tags" | jq -r '.[].name' | sort | tail -1)"}" - -ARCHIVE_FILE="${LARAVEL_VERSION}.tar.gz" - -rm -rf "${TEMP_DIR}" -mkdir -p "${TEMP_DIR}" - -wget -q "https://github.com/laravel/framework/archive/${ARCHIVE_FILE}" -O "${TEMP_DIR}/${ARCHIVE_FILE}" - -ROOT_FOLDER=$(tar ztf "${TEMP_DIR}/${ARCHIVE_FILE}" | sort | head -1) - -pushd "${TEMP_DIR}" - -# only extracts the Foundation folder -tar xzf "${TEMP_DIR}/${ARCHIVE_FILE}" "${ROOT_FOLDER}src/Illuminate/Foundation" --strip-components=4 --one-top-level=Foundation - -popd - -rm -rf "${FOUNDATION_DIR}" -mkdir -p "${FOUNDATION_DIR}" -rsync -a "${TEMP_DIR}/Foundation/" "${FOUNDATION_DIR}" -rm -rf "${TEMP_DIR}" diff --git a/scripts/update-foundation b/scripts/update-foundation new file mode 100644 index 00000000..b82a8c39 --- /dev/null +++ b/scripts/update-foundation @@ -0,0 +1,143 @@ +#!/usr/bin/php +name; + $current = getCurrentVersion(); + + if ($version === $current && ! in_array('--force', $_SERVER['argv'])) { + echo "Laravel is already on the latest version ({$current}).".PHP_EOL; + echo 'Use --force to force update.'.PHP_EOL; + exit(1); + } + + return $version; +} + +/** + * Get the current Laravel version. + */ +function getCurrentVersion(): ?string +{ + if (! file_exists($file = __DIR__.'/../src/Illuminate/Foundation/Application.php')) { + return null; + } + + $current = file_get_contents($file); + preg_match('/VERSION = \'(.*)\'/', $current, $matches); + + return "v{$matches[1]}"; +} + +/** + * Create a directory if it doesn't exist. + */ +function createDirectory(string $path): void +{ + if (file_exists($path)) { + deleteDirectory($path); + } + + mkdir($path, 0777, true); +} + +/** + * Download and extract the archive. + */ +function handleArchive(string $version, string $path): void +{ + $file = "{$version}.tar.gz"; + $archive = "{$path}/{$file}"; + + file_put_contents($archive, fopen(GITHUB_LARAVEL_ARCHIVE.$file, 'r')); + echo "Downloaded the archive to: {$archive}".PHP_EOL; + + (new PharData($archive))->extractTo($path); + echo "Extracted archive to: {$path}".PHP_EOL; +} + +/** + * Move the Foundation directory into the project. + */ +function moveDirectory(string $tempPath, string $targetPath, string $version): void +{ + $version = substr($version, 1); + $extracted = "{$tempPath}/framework-{$version}/src/Illuminate/Foundation"; + + if (! file_exists($extracted)) { + echo 'Error: Extracted Foundation directory not found.'; + exit(1); + } + + if (file_exists($targetPath)) { + deleteDirectory($targetPath); + } + + mkdir($targetPath, 0777, true); + rename($extracted, $targetPath); + + echo "Moved Foundation to: {$targetPath}".PHP_EOL; +} + +/** + * Delete a directory recursively. + */ +function deleteDirectory(string $directory): void +{ + if (! file_exists($directory)) { + return; + } + + $files = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS), + RecursiveIteratorIterator::CHILD_FIRST + ); + + foreach ($files as $fileInfo) { + $remove = ($fileInfo->isDir() ? 'rmdir' : 'unlink'); + $remove($fileInfo->getRealPath()); + } + + rmdir($directory); + + echo "Deleted directory: {$directory}".PHP_EOL; +}