feat: build the deployment zip with scaleway-packageUnverified
Copies the application to a temp build dir without local, test and secret files, installs production dependencies from composer.lock with an authoritative class map, warms the prod cache when bin/console exists, and zips the result, warning above Scaleway's size limits. Relative path repositories are resolved against the application dir for the build only, and .gitattributes keeps the package's own dev files out of mirrored path installs.
0df9229 parent: 934f5cd added
.gitattributes +17 -0 | new file mode 100644 | ||
| @@ -0,0 +1,17 @@ | ||
| 1 | +# Kept out of the Composer dist; Composer also honors this when mirroring the package | |
| 2 | +# through a "path" repository, as scaleway-package does for example/. | |
| 3 | +/.github export-ignore | |
| 4 | +/docs export-ignore | |
| 5 | +/example export-ignore | |
| 6 | +/probe export-ignore | |
| 7 | +/tests export-ignore | |
| 8 | +/vendor export-ignore | |
| 9 | +/.gitattributes export-ignore | |
| 10 | +/.gitignore export-ignore | |
| 11 | +/.php-cs-fixer.dist.php export-ignore | |
| 12 | +/GLOSSARY.md export-ignore | |
| 13 | +/phpstan.neon.dist export-ignore | |
| 14 | +/phpunit.xml.dist export-ignore | |
| 15 | +/composer.lock export-ignore | |
| 16 | +/.phpunit.cache export-ignore | |
| 17 | +/.php-cs-fixer.cache export-ignore | |
| new file mode 100644 | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | +# Kept out of the Composer dist; Composer also honors this when mirroring the package | ||
| 2 | +# through a "path" repository, as scaleway-package does for example/. | ||
| 3 | +/.github export-ignore | ||
| 4 | +/docs export-ignore | ||
| 5 | +/example export-ignore | ||
| 6 | +/probe export-ignore | ||
| 7 | +/tests export-ignore | ||
| 8 | +/vendor export-ignore | ||
| 9 | +/.gitattributes export-ignore | ||
| 10 | +/.gitignore export-ignore | ||
| 11 | +/.php-cs-fixer.dist.php export-ignore | ||
| 12 | +/GLOSSARY.md export-ignore | ||
| 13 | +/phpstan.neon.dist export-ignore | ||
| 14 | +/phpunit.xml.dist export-ignore | ||
| 15 | +/composer.lock export-ignore | ||
| 16 | +/.phpunit.cache export-ignore | ||
| 17 | +/.php-cs-fixer.cache export-ignore | ||
modified
.php-cs-fixer.dist.php +1 -1 | @@ -4,7 +4,7 @@ declare(strict_types=1); | ||
| 4 | 4 | |
| 5 | 5 | $finder = (new PhpCsFixer\Finder()) |
| 6 | 6 | ->in([__DIR__.'/src', __DIR__.'/tests']) |
| 7 | - ->append([__FILE__, __DIR__.'/bin/scaleway-init']); | |
| 7 | + ->append([__FILE__, __DIR__.'/bin/scaleway-init', __DIR__.'/bin/scaleway-package']); | |
| 8 | 8 | |
| 9 | 9 | return (new PhpCsFixer\Config()) |
| 10 | 10 | ->setRiskyAllowed(true) |
| @@ -4,7 +4,7 @@ declare(strict_types=1); | |||
| 4 | 4 | ||
| 5 | $finder = (new PhpCsFixer\Finder()) | 5 | $finder = (new PhpCsFixer\Finder()) |
| 6 | ->in([__DIR__.'/src', __DIR__.'/tests']) | 6 | ->in([__DIR__.'/src', __DIR__.'/tests']) |
| 7 | - ->append([__FILE__, __DIR__.'/bin/scaleway-init']); | 7 | + ->append([__FILE__, __DIR__.'/bin/scaleway-init', __DIR__.'/bin/scaleway-package']); |
| 8 | 8 | ||
| 9 | return (new PhpCsFixer\Config()) | 9 | return (new PhpCsFixer\Config()) |
| 10 | ->setRiskyAllowed(true) | 10 | ->setRiskyAllowed(true) |
added
bin/scaleway-package +21 -0 | new file mode 100755 | ||
| @@ -0,0 +1,21 @@ | ||
| 1 | +#!/usr/bin/env php | |
| 2 | +<?php | |
| 3 | + | |
| 4 | +declare(strict_types=1); | |
| 5 | + | |
| 6 | +use Composer\InstalledVersions; | |
| 7 | +use JDecool\ScalewayRuntime\Packager; | |
| 8 | + | |
| 9 | +require $_composer_autoload_path ?? __DIR__.'/../vendor/autoload.php'; | |
| 10 | + | |
| 11 | +if (in_array($argv[1] ?? null, ['-h', '--help'], true)) { | |
| 12 | + echo 'Usage: scaleway-package [<zip-file>] (default: function.zip)', \PHP_EOL; | |
| 13 | + exit(0); | |
| 14 | +} | |
| 15 | + | |
| 16 | +try { | |
| 17 | + (new Packager((string) realpath(InstalledVersions::getRootPackage()['install_path']), \STDERR))->package($argv[1] ?? 'function.zip'); | |
| 18 | +} catch (Throwable $e) { | |
| 19 | + fwrite(\STDERR, 'Packaging failed: '.$e->getMessage().\PHP_EOL); | |
| 20 | + exit(1); | |
| 21 | +} | |
| new file mode 100755 | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | +#!/usr/bin/env php | ||
| 2 | +<?php | ||
| 3 | + | ||
| 4 | +declare(strict_types=1); | ||
| 5 | + | ||
| 6 | +use Composer\InstalledVersions; | ||
| 7 | +use JDecool\ScalewayRuntime\Packager; | ||
| 8 | + | ||
| 9 | +require $_composer_autoload_path ?? __DIR__.'/../vendor/autoload.php'; | ||
| 10 | + | ||
| 11 | +if (in_array($argv[1] ?? null, ['-h', '--help'], true)) { | ||
| 12 | + echo 'Usage: scaleway-package [<zip-file>] (default: function.zip)', \PHP_EOL; | ||
| 13 | + exit(0); | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +try { | ||
| 17 | + (new Packager((string) realpath(InstalledVersions::getRootPackage()['install_path']), \STDERR))->package($argv[1] ?? 'function.zip'); | ||
| 18 | +} catch (Throwable $e) { | ||
| 19 | + fwrite(\STDERR, 'Packaging failed: '.$e->getMessage().\PHP_EOL); | ||
| 20 | + exit(1); | ||
| 21 | +} | ||
modified
composer.json +5 -1 | @@ -16,6 +16,9 @@ | ||
| 16 | 16 | "symfony/http-kernel": "^7.4 || ^8.0", |
| 17 | 17 | "symfony/runtime": "^7.4 || ^8.0" |
| 18 | 18 | }, |
| 19 | + "suggest": { | |
| 20 | + "ext-zip": "To build the deployment zip with vendor/bin/scaleway-package" | |
| 21 | + }, | |
| 19 | 22 | "require-dev": { |
| 20 | 23 | "friendsofphp/php-cs-fixer": "^3.95", |
| 21 | 24 | "phpstan/phpstan": "^2.3", |
| @@ -29,7 +32,8 @@ | ||
| 29 | 32 | } |
| 30 | 33 | }, |
| 31 | 34 | "bin": [ |
| 32 | - "bin/scaleway-init" | |
| 35 | + "bin/scaleway-init", | |
| 36 | + "bin/scaleway-package" | |
| 33 | 37 | ], |
| 34 | 38 | "autoload-dev": { |
| 35 | 39 | "psr-4": { |
| @@ -16,6 +16,9 @@ | |||
| 16 | "symfony/http-kernel": "^7.4 || ^8.0", | 16 | "symfony/http-kernel": "^7.4 || ^8.0", |
| 17 | "symfony/runtime": "^7.4 || ^8.0" | 17 | "symfony/runtime": "^7.4 || ^8.0" |
| 18 | }, | 18 | }, |
| 19 | + "suggest": { | ||
| 20 | + "ext-zip": "To build the deployment zip with vendor/bin/scaleway-package" | ||
| 21 | + }, | ||
| 19 | "require-dev": { | 22 | "require-dev": { |
| 20 | "friendsofphp/php-cs-fixer": "^3.95", | 23 | "friendsofphp/php-cs-fixer": "^3.95", |
| 21 | "phpstan/phpstan": "^2.3", | 24 | "phpstan/phpstan": "^2.3", |
| @@ -29,7 +32,8 @@ | |||
| 29 | } | 32 | } |
| 30 | }, | 33 | }, |
| 31 | "bin": [ | 34 | "bin": [ |
| 32 | - "bin/scaleway-init" | 35 | + "bin/scaleway-init", |
| 36 | + "bin/scaleway-package" | ||
| 33 | ], | 37 | ], |
| 34 | "autoload-dev": { | 38 | "autoload-dev": { |
| 35 | "psr-4": { | 39 | "psr-4": { |
modified
docs/adr/0003-ship-vendor-and-warm-cache-in-zip.md +6 -0 | @@ -2,3 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | Scaleway can install Composer dependencies itself at deploy time, which is its documented path. |
| 4 | 4 | We instead build the zip with `composer install --no-dev -o` and a prod `cache:warmup`, so builds are reproducible and Invocations never pay container compilation on a possibly read-only filesystem. |
| 5 | + | |
| 6 | +## Consequences | |
| 7 | + | |
| 8 | +- `scaleway-package` requires a committed `composer.lock` and installs with `--classmap-authoritative`, as Symfony recommends for production: the warmed container is loaded by path, not through the autoloader, so nothing generated at runtime depends on autoload fallbacks. | |
| 9 | +- `.env` files are shipped as they are, without `composer dump-env prod`: that command comes from Symfony Flex, which applications may not have, and `.env` is parsed once per Warm Process anyway; `.env.local` and `.env.local.php` stay out of the zip as machine-local configuration. | |
| 10 | +- The zip also ships `composer.json`, which the Kernel needs to find its project dir; whether Scaleway then runs Composer over the shipped `vendor/` is checked by the probe function. | |
| @@ -2,3 +2,9 @@ | |||
| 2 | 2 | ||
| 3 | Scaleway can install Composer dependencies itself at deploy time, which is its documented path. | 3 | Scaleway can install Composer dependencies itself at deploy time, which is its documented path. |
| 4 | We instead build the zip with `composer install --no-dev -o` and a prod `cache:warmup`, so builds are reproducible and Invocations never pay container compilation on a possibly read-only filesystem. | 4 | We instead build the zip with `composer install --no-dev -o` and a prod `cache:warmup`, so builds are reproducible and Invocations never pay container compilation on a possibly read-only filesystem. |
| 5 | + | ||
| 6 | +## Consequences | ||
| 7 | + | ||
| 8 | +- `scaleway-package` requires a committed `composer.lock` and installs with `--classmap-authoritative`, as Symfony recommends for production: the warmed container is loaded by path, not through the autoloader, so nothing generated at runtime depends on autoload fallbacks. | ||
| 9 | +- `.env` files are shipped as they are, without `composer dump-env prod`: that command comes from Symfony Flex, which applications may not have, and `.env` is parsed once per Warm Process anyway; `.env.local` and `.env.local.php` stay out of the zip as machine-local configuration. | ||
| 10 | +- The zip also ships `composer.json`, which the Kernel needs to find its project dir; whether Scaleway then runs Composer over the shipped `vendor/` is checked by the probe function. | ||
modified
docs/plan.md +1 -1 | @@ -13,7 +13,7 @@ Vocabulary follows [GLOSSARY.md](../GLOSSARY.md); decisions are recorded in [doc | ||
| 13 | 13 | - HTTP mapping: base64 bodies decoded on input; output marked `isBase64Encoded` when the body is not valid UTF-8; multi-value headers sent as arrays; streamed and binary file responses buffered; query string rebuilt from `queryStringParameters`; `X-Forwarded-*` trusted; platform limits documented. |
| 14 | 14 | - Errors escaping the Application are logged to stderr and returned as a 500 Function Response (generic body, details when `APP_DEBUG=1`). |
| 15 | 15 | - Cache is warmed at build time; cache and log dirs fall back to `/tmp` when the project dir is not writable; logs go to stderr. |
| 16 | -- `vendor/bin/scaleway-package` builds a zip with `vendor/` (`--no-dev -o`) and a warmed prod cache, excluding `var/`, `tests/`, `.env.local` (ADR 0003). | |
| 16 | +- `vendor/bin/scaleway-package` builds a zip with `vendor/` (`--no-dev --classmap-authoritative`, from `composer.lock`) and a warmed prod cache, excluding `var/`, `tests/`, `.env.local` (ADR 0003). | |
| 17 | 17 | - CRON Invocations become `POST /` HTTP requests by default; a JSON body `{"command": "...", "args": {...}}` makes it a Command Invocation run through the Console Application of the same Kernel; a non-zero exit code yields a 500 with the command output. |
| 18 | 18 | - Tests: PHPUnit on conversions, GitHub Actions CI (PHPUnit, PHPStan max, PHP-CS-Fixer; PHP 8.4/8.5 x Symfony 7.4/8.x), local Emulator `vendor/bin/scaleway-serve`, manual real E2E deployed with Terraform. |
| 19 | 19 | |
| @@ -13,7 +13,7 @@ Vocabulary follows [GLOSSARY.md](../GLOSSARY.md); decisions are recorded in [doc | |||
| 13 | - HTTP mapping: base64 bodies decoded on input; output marked `isBase64Encoded` when the body is not valid UTF-8; multi-value headers sent as arrays; streamed and binary file responses buffered; query string rebuilt from `queryStringParameters`; `X-Forwarded-*` trusted; platform limits documented. | 13 | - HTTP mapping: base64 bodies decoded on input; output marked `isBase64Encoded` when the body is not valid UTF-8; multi-value headers sent as arrays; streamed and binary file responses buffered; query string rebuilt from `queryStringParameters`; `X-Forwarded-*` trusted; platform limits documented. |
| 14 | - Errors escaping the Application are logged to stderr and returned as a 500 Function Response (generic body, details when `APP_DEBUG=1`). | 14 | - Errors escaping the Application are logged to stderr and returned as a 500 Function Response (generic body, details when `APP_DEBUG=1`). |
| 15 | - Cache is warmed at build time; cache and log dirs fall back to `/tmp` when the project dir is not writable; logs go to stderr. | 15 | - Cache is warmed at build time; cache and log dirs fall back to `/tmp` when the project dir is not writable; logs go to stderr. |
| 16 | -- `vendor/bin/scaleway-package` builds a zip with `vendor/` (`--no-dev -o`) and a warmed prod cache, excluding `var/`, `tests/`, `.env.local` (ADR 0003). | 16 | +- `vendor/bin/scaleway-package` builds a zip with `vendor/` (`--no-dev --classmap-authoritative`, from `composer.lock`) and a warmed prod cache, excluding `var/`, `tests/`, `.env.local` (ADR 0003). |
| 17 | - CRON Invocations become `POST /` HTTP requests by default; a JSON body `{"command": "...", "args": {...}}` makes it a Command Invocation run through the Console Application of the same Kernel; a non-zero exit code yields a 500 with the command output. | 17 | - CRON Invocations become `POST /` HTTP requests by default; a JSON body `{"command": "...", "args": {...}}` makes it a Command Invocation run through the Console Application of the same Kernel; a non-zero exit code yields a 500 with the command output. |
| 18 | - Tests: PHPUnit on conversions, GitHub Actions CI (PHPUnit, PHPStan max, PHP-CS-Fixer; PHP 8.4/8.5 x Symfony 7.4/8.x), local Emulator `vendor/bin/scaleway-serve`, manual real E2E deployed with Terraform. | 18 | - Tests: PHPUnit on conversions, GitHub Actions CI (PHPUnit, PHPStan max, PHP-CS-Fixer; PHP 8.4/8.5 x Symfony 7.4/8.x), local Emulator `vendor/bin/scaleway-serve`, manual real E2E deployed with Terraform. |
| 19 | 19 | ||
modified
phpstan.neon.dist +1 -0 | @@ -8,3 +8,4 @@ parameters: | ||
| 8 | 8 | - src |
| 9 | 9 | - tests |
| 10 | 10 | - bin/scaleway-init |
| 11 | + - bin/scaleway-package | |
| @@ -8,3 +8,4 @@ parameters: | |||
| 8 | - src | 8 | - src |
| 9 | - tests | 9 | - tests |
| 10 | - bin/scaleway-init | 10 | - bin/scaleway-init |
| 11 | + - bin/scaleway-package | ||
added
src/Packager.php +243 -0 | new file mode 100644 | ||
| @@ -0,0 +1,243 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace JDecool\ScalewayRuntime; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Builds the deployment zip: a copy of the application with production dependencies | |
| 9 | + * and a warmed-up prod cache (ADR 0003), so Invocations never compile the container. | |
| 10 | + */ | |
| 11 | +final readonly class Packager | |
| 12 | +{ | |
| 13 | + /** | |
| 14 | + * Patterns starting with "/" match from the application root, the others match a file or directory name anywhere. | |
| 15 | + * vendor/ and var/ are rebuilt from scratch, the rest is local, test or secret material. | |
| 16 | + */ | |
| 17 | + public const array EXCLUDES = [ | |
| 18 | + '/vendor', '/var', '/tests', | |
| 19 | + '/.env.local', '/.env.local.php', '/.env.*.local', | |
| 20 | + '/phpunit.xml', '/phpunit.xml.dist', '/.phpunit.cache', '/.phpunit.result.cache', '/.php-cs-fixer.cache', | |
| 21 | + '.git', '.idea', '.vscode', '.DS_Store', 'node_modules', | |
| 22 | + // Terraform state holds secrets in clear text. | |
| 23 | + '.terraform', '*.tfstate', '*.tfstate.*', | |
| 24 | + ]; | |
| 25 | + | |
| 26 | + // https://www.scaleway.com/en/docs/serverless-functions/reference-content/functions-limitations/ | |
| 27 | + public const int MAX_ZIP_SIZE = 100 * 1024 * 1024; | |
| 28 | + public const int MAX_CODE_SIZE = 500 * 1024 * 1024; | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * @param resource $log | |
| 32 | + */ | |
| 33 | + public function __construct( | |
| 34 | + private string $appDir, | |
| 35 | + private mixed $log, | |
| 36 | + ) { | |
| 37 | + } | |
| 38 | + | |
| 39 | + public function package(string $zipFile): void | |
| 40 | + { | |
| 41 | + if (!class_exists(\ZipArchive::class)) { | |
| 42 | + throw new \RuntimeException('The zip PHP extension is required to build the deployment zip.'); | |
| 43 | + } | |
| 44 | + foreach (['handler.php' => 'run "vendor/bin/scaleway-init" first', 'composer.lock' => 'run "composer update" and commit it, builds must be reproducible'] as $file => $hint) { | |
| 45 | + if (!is_file($this->appDir.'/'.$file)) { | |
| 46 | + throw new \RuntimeException(\sprintf('"%s" is missing in "%s": %s.', $file, $this->appDir, $hint)); | |
| 47 | + } | |
| 48 | + } | |
| 49 | + | |
| 50 | + $buildDir = sys_get_temp_dir().'/scaleway-package-'.bin2hex(random_bytes(4)); | |
| 51 | + try { | |
| 52 | + $this->log('Copying the application to '.$buildDir); | |
| 53 | + $this->copy($buildDir, $this->relativeToApp($zipFile)); | |
| 54 | + | |
| 55 | + $this->log('Installing production dependencies'); | |
| 56 | + $lock = (string) file_get_contents($buildDir.'/composer.lock'); | |
| 57 | + $this->writeJson($buildDir.'/composer.lock', self::resolvePathRepositories($this->decodeJson($lock), $this->appDir)); | |
| 58 | + try { | |
| 59 | + $this->run(['composer', 'install', '--no-dev', '--classmap-authoritative', '--no-interaction', '--no-progress'], $buildDir); | |
| 60 | + } finally { | |
| 61 | + // The zip ships the lock file exactly as committed. | |
| 62 | + file_put_contents($buildDir.'/composer.lock', $lock); | |
| 63 | + } | |
| 64 | + | |
| 65 | + if (is_file($buildDir.'/bin/console')) { | |
| 66 | + $this->log('Warming up the prod cache'); | |
| 67 | + $this->run([\PHP_BINARY, 'bin/console', 'cache:warmup', '--no-interaction'], $buildDir); | |
| 68 | + } else { | |
| 69 | + $this->log('No bin/console found: the cache is not warmed up, the container will be compiled at the first Invocation'); | |
| 70 | + } | |
| 71 | + | |
| 72 | + $this->log('Writing '.$zipFile); | |
| 73 | + [$zipSize, $codeSize] = $this->zip($buildDir, $zipFile); | |
| 74 | + $this->log(\sprintf('Done: %.1f MiB zipped, %.1f MiB unzipped', $zipSize / 1024 ** 2, $codeSize / 1024 ** 2)); | |
| 75 | + if ($zipSize > self::MAX_ZIP_SIZE) { | |
| 76 | + $this->log('WARNING: the zip exceeds the 100 MiB Scaleway accepts'); | |
| 77 | + } | |
| 78 | + if ($codeSize > self::MAX_CODE_SIZE) { | |
| 79 | + $this->log('WARNING: the unzipped code exceeds the 500 MiB Scaleway accepts'); | |
| 80 | + } | |
| 81 | + } finally { | |
| 82 | + $this->remove($buildDir); | |
| 83 | + } | |
| 84 | + } | |
| 85 | + | |
| 86 | + public static function isExcluded(string $relativePath): bool | |
| 87 | + { | |
| 88 | + $name = basename($relativePath); | |
| 89 | + foreach (self::EXCLUDES as $pattern) { | |
| 90 | + if (str_starts_with($pattern, '/') ? fnmatch($pattern, '/'.$relativePath, \FNM_PATHNAME) : fnmatch($pattern, $name)) { | |
| 91 | + return true; | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 95 | + return false; | |
| 96 | + } | |
| 97 | + | |
| 98 | + /** | |
| 99 | + * Relative "path" repositories point outside the application, so they no longer resolve from | |
| 100 | + * the build dir: they are made absolute, against the application dir they are relative to. | |
| 101 | + * | |
| 102 | + * @param array<mixed> $lock | |
| 103 | + * | |
| 104 | + * @return array<mixed> | |
| 105 | + */ | |
| 106 | + public static function resolvePathRepositories(array $lock, string $appDir): array | |
| 107 | + { | |
| 108 | + foreach (['packages', 'packages-dev'] as $section) { | |
| 109 | + $packages = \is_array($lock[$section] ?? null) ? $lock[$section] : []; | |
| 110 | + foreach ($packages as $i => $package) { | |
| 111 | + $dist = \is_array($package) ? $package['dist'] ?? null : null; | |
| 112 | + if (\is_array($package) && \is_array($dist) && 'path' === ($dist['type'] ?? null) && \is_string($dist['url'] ?? null) && !str_starts_with($dist['url'], '/')) { | |
| 113 | + $dist['url'] = $appDir.'/'.$dist['url']; | |
| 114 | + $package['dist'] = $dist; | |
| 115 | + $packages[$i] = $package; | |
| 116 | + } | |
| 117 | + } | |
| 118 | + if (isset($lock[$section])) { | |
| 119 | + $lock[$section] = $packages; | |
| 120 | + } | |
| 121 | + } | |
| 122 | + | |
| 123 | + return $lock; | |
| 124 | + } | |
| 125 | + | |
| 126 | + private function copy(string $buildDir, ?string $skip): void | |
| 127 | + { | |
| 128 | + $files = new \RecursiveIteratorIterator( | |
| 129 | + new \RecursiveCallbackFilterIterator( | |
| 130 | + new \RecursiveDirectoryIterator($this->appDir, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS), | |
| 131 | + static function (\SplFileInfo $file, string $path, \RecursiveDirectoryIterator $iterator) use ($skip): bool { | |
| 132 | + $relativePath = $iterator->getSubPathname(); | |
| 133 | + | |
| 134 | + return $relativePath !== $skip && !self::isExcluded($relativePath); | |
| 135 | + }, | |
| 136 | + ), | |
| 137 | + \RecursiveIteratorIterator::SELF_FIRST, | |
| 138 | + ); | |
| 139 | + | |
| 140 | + $this->mkdir($buildDir); | |
| 141 | + foreach ($files as $file) { | |
| 142 | + \assert($file instanceof \SplFileInfo); | |
| 143 | + $target = $buildDir.'/'.substr($file->getPathname(), \strlen($this->appDir) + 1); | |
| 144 | + if ($file->isDir()) { | |
| 145 | + $this->mkdir($target); | |
| 146 | + } elseif (!copy($file->getPathname(), $target) || !chmod($target, $file->getPerms() & 0o777)) { | |
| 147 | + throw new \RuntimeException(\sprintf('Cannot copy "%s" to "%s".', $file->getPathname(), $target)); | |
| 148 | + } | |
| 149 | + } | |
| 150 | + } | |
| 151 | + | |
| 152 | + /** | |
| 153 | + * @return array{int, int} the zip size and the total size of the zipped files | |
| 154 | + */ | |
| 155 | + private function zip(string $buildDir, string $zipFile): array | |
| 156 | + { | |
| 157 | + $zip = new \ZipArchive(); | |
| 158 | + if (true !== $zip->open($zipFile, \ZipArchive::CREATE | \ZipArchive::OVERWRITE)) { | |
| 159 | + throw new \RuntimeException(\sprintf('Cannot create "%s".', $zipFile)); | |
| 160 | + } | |
| 161 | + | |
| 162 | + $codeSize = 0; | |
| 163 | + $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($buildDir, \FilesystemIterator::SKIP_DOTS)); | |
| 164 | + foreach ($files as $file) { | |
| 165 | + \assert($file instanceof \SplFileInfo); | |
| 166 | + $name = substr($file->getPathname(), \strlen($buildDir) + 1); | |
| 167 | + $zip->addFile($file->getPathname(), $name); | |
| 168 | + // Keeps the executable bits, as for bin/console. | |
| 169 | + $zip->setExternalAttributesName($name, \ZipArchive::OPSYS_UNIX, ($file->getPerms() & 0o777 | 0o100000) << 16); | |
| 170 | + $codeSize += $file->getSize(); | |
| 171 | + } | |
| 172 | + | |
| 173 | + if (!$zip->close()) { | |
| 174 | + throw new \RuntimeException(\sprintf('Cannot write "%s": %s', $zipFile, $zip->getStatusString())); | |
| 175 | + } | |
| 176 | + clearstatcache(true, $zipFile); | |
| 177 | + | |
| 178 | + return [(int) filesize($zipFile), $codeSize]; | |
| 179 | + } | |
| 180 | + | |
| 181 | + /** | |
| 182 | + * @param list<string> $command | |
| 183 | + */ | |
| 184 | + private function run(array $command, string $cwd): void | |
| 185 | + { | |
| 186 | + $env = ['APP_ENV' => 'prod', 'APP_DEBUG' => '0', 'COMPOSER_MIRROR_PATH_REPOS' => '1'] + getenv(); | |
| 187 | + // Child output goes to our stderr, keeping stdout clean like the rest of the log. | |
| 188 | + $process = proc_open($command, [0 => ['file', '/dev/null', 'r'], 1 => $this->log, 2 => $this->log], $pipes, $cwd, $env); | |
| 189 | + if (false === $process || 0 !== $exitCode = proc_close($process)) { | |
| 190 | + throw new \RuntimeException(\sprintf('"%s" failed%s.', implode(' ', $command), isset($exitCode) ? ' with exit code '.$exitCode : '')); | |
| 191 | + } | |
| 192 | + } | |
| 193 | + | |
| 194 | + private function relativeToApp(string $file): ?string | |
| 195 | + { | |
| 196 | + $dir = realpath(\dirname($file)); | |
| 197 | + | |
| 198 | + return false !== $dir && str_starts_with($dir.'/', $this->appDir.'/') ? ltrim(substr($dir, \strlen($this->appDir)).'/'.basename($file), '/') : null; | |
| 199 | + } | |
| 200 | + | |
| 201 | + private function remove(string $dir): void | |
| 202 | + { | |
| 203 | + if (!is_dir($dir)) { | |
| 204 | + return; | |
| 205 | + } | |
| 206 | + $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST); | |
| 207 | + foreach ($files as $file) { | |
| 208 | + \assert($file instanceof \SplFileInfo); | |
| 209 | + $file->isDir() && !$file->isLink() ? rmdir($file->getPathname()) : unlink($file->getPathname()); | |
| 210 | + } | |
| 211 | + rmdir($dir); | |
| 212 | + } | |
| 213 | + | |
| 214 | + private function mkdir(string $dir): void | |
| 215 | + { | |
| 216 | + if (!is_dir($dir) && !mkdir($dir, 0o777, true)) { | |
| 217 | + throw new \RuntimeException(\sprintf('Cannot create "%s".', $dir)); | |
| 218 | + } | |
| 219 | + } | |
| 220 | + | |
| 221 | + /** | |
| 222 | + * @return array<mixed> | |
| 223 | + */ | |
| 224 | + private function decodeJson(string $json): array | |
| 225 | + { | |
| 226 | + $data = json_decode($json, true, 512, \JSON_THROW_ON_ERROR); | |
| 227 | + | |
| 228 | + return \is_array($data) ? $data : throw new \UnexpectedValueException('composer.lock must hold a JSON object.'); | |
| 229 | + } | |
| 230 | + | |
| 231 | + /** | |
| 232 | + * @param array<mixed> $data | |
| 233 | + */ | |
| 234 | + private function writeJson(string $file, array $data): void | |
| 235 | + { | |
| 236 | + file_put_contents($file, json_encode($data, \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES)); | |
| 237 | + } | |
| 238 | + | |
| 239 | + private function log(string $message): void | |
| 240 | + { | |
| 241 | + fwrite($this->log, '> '.$message.\PHP_EOL); | |
| 242 | + } | |
| 243 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,243 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
| 4 | + | ||
| 5 | +namespace JDecool\ScalewayRuntime; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * Builds the deployment zip: a copy of the application with production dependencies | ||
| 9 | + * and a warmed-up prod cache (ADR 0003), so Invocations never compile the container. | ||
| 10 | + */ | ||
| 11 | +final readonly class Packager | ||
| 12 | +{ | ||
| 13 | + /** | ||
| 14 | + * Patterns starting with "/" match from the application root, the others match a file or directory name anywhere. | ||
| 15 | + * vendor/ and var/ are rebuilt from scratch, the rest is local, test or secret material. | ||
| 16 | + */ | ||
| 17 | + public const array EXCLUDES = [ | ||
| 18 | + '/vendor', '/var', '/tests', | ||
| 19 | + '/.env.local', '/.env.local.php', '/.env.*.local', | ||
| 20 | + '/phpunit.xml', '/phpunit.xml.dist', '/.phpunit.cache', '/.phpunit.result.cache', '/.php-cs-fixer.cache', | ||
| 21 | + '.git', '.idea', '.vscode', '.DS_Store', 'node_modules', | ||
| 22 | + // Terraform state holds secrets in clear text. | ||
| 23 | + '.terraform', '*.tfstate', '*.tfstate.*', | ||
| 24 | + ]; | ||
| 25 | + | ||
| 26 | + // https://www.scaleway.com/en/docs/serverless-functions/reference-content/functions-limitations/ | ||
| 27 | + public const int MAX_ZIP_SIZE = 100 * 1024 * 1024; | ||
| 28 | + public const int MAX_CODE_SIZE = 500 * 1024 * 1024; | ||
| 29 | + | ||
| 30 | + /** | ||
| 31 | + * @param resource $log | ||
| 32 | + */ | ||
| 33 | + public function __construct( | ||
| 34 | + private string $appDir, | ||
| 35 | + private mixed $log, | ||
| 36 | + ) { | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + public function package(string $zipFile): void | ||
| 40 | + { | ||
| 41 | + if (!class_exists(\ZipArchive::class)) { | ||
| 42 | + throw new \RuntimeException('The zip PHP extension is required to build the deployment zip.'); | ||
| 43 | + } | ||
| 44 | + foreach (['handler.php' => 'run "vendor/bin/scaleway-init" first', 'composer.lock' => 'run "composer update" and commit it, builds must be reproducible'] as $file => $hint) { | ||
| 45 | + if (!is_file($this->appDir.'/'.$file)) { | ||
| 46 | + throw new \RuntimeException(\sprintf('"%s" is missing in "%s": %s.', $file, $this->appDir, $hint)); | ||
| 47 | + } | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + $buildDir = sys_get_temp_dir().'/scaleway-package-'.bin2hex(random_bytes(4)); | ||
| 51 | + try { | ||
| 52 | + $this->log('Copying the application to '.$buildDir); | ||
| 53 | + $this->copy($buildDir, $this->relativeToApp($zipFile)); | ||
| 54 | + | ||
| 55 | + $this->log('Installing production dependencies'); | ||
| 56 | + $lock = (string) file_get_contents($buildDir.'/composer.lock'); | ||
| 57 | + $this->writeJson($buildDir.'/composer.lock', self::resolvePathRepositories($this->decodeJson($lock), $this->appDir)); | ||
| 58 | + try { | ||
| 59 | + $this->run(['composer', 'install', '--no-dev', '--classmap-authoritative', '--no-interaction', '--no-progress'], $buildDir); | ||
| 60 | + } finally { | ||
| 61 | + // The zip ships the lock file exactly as committed. | ||
| 62 | + file_put_contents($buildDir.'/composer.lock', $lock); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + if (is_file($buildDir.'/bin/console')) { | ||
| 66 | + $this->log('Warming up the prod cache'); | ||
| 67 | + $this->run([\PHP_BINARY, 'bin/console', 'cache:warmup', '--no-interaction'], $buildDir); | ||
| 68 | + } else { | ||
| 69 | + $this->log('No bin/console found: the cache is not warmed up, the container will be compiled at the first Invocation'); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + $this->log('Writing '.$zipFile); | ||
| 73 | + [$zipSize, $codeSize] = $this->zip($buildDir, $zipFile); | ||
| 74 | + $this->log(\sprintf('Done: %.1f MiB zipped, %.1f MiB unzipped', $zipSize / 1024 ** 2, $codeSize / 1024 ** 2)); | ||
| 75 | + if ($zipSize > self::MAX_ZIP_SIZE) { | ||
| 76 | + $this->log('WARNING: the zip exceeds the 100 MiB Scaleway accepts'); | ||
| 77 | + } | ||
| 78 | + if ($codeSize > self::MAX_CODE_SIZE) { | ||
| 79 | + $this->log('WARNING: the unzipped code exceeds the 500 MiB Scaleway accepts'); | ||
| 80 | + } | ||
| 81 | + } finally { | ||
| 82 | + $this->remove($buildDir); | ||
| 83 | + } | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + public static function isExcluded(string $relativePath): bool | ||
| 87 | + { | ||
| 88 | + $name = basename($relativePath); | ||
| 89 | + foreach (self::EXCLUDES as $pattern) { | ||
| 90 | + if (str_starts_with($pattern, '/') ? fnmatch($pattern, '/'.$relativePath, \FNM_PATHNAME) : fnmatch($pattern, $name)) { | ||
| 91 | + return true; | ||
| 92 | + } | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + return false; | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + /** | ||
| 99 | + * Relative "path" repositories point outside the application, so they no longer resolve from | ||
| 100 | + * the build dir: they are made absolute, against the application dir they are relative to. | ||
| 101 | + * | ||
| 102 | + * @param array<mixed> $lock | ||
| 103 | + * | ||
| 104 | + * @return array<mixed> | ||
| 105 | + */ | ||
| 106 | + public static function resolvePathRepositories(array $lock, string $appDir): array | ||
| 107 | + { | ||
| 108 | + foreach (['packages', 'packages-dev'] as $section) { | ||
| 109 | + $packages = \is_array($lock[$section] ?? null) ? $lock[$section] : []; | ||
| 110 | + foreach ($packages as $i => $package) { | ||
| 111 | + $dist = \is_array($package) ? $package['dist'] ?? null : null; | ||
| 112 | + if (\is_array($package) && \is_array($dist) && 'path' === ($dist['type'] ?? null) && \is_string($dist['url'] ?? null) && !str_starts_with($dist['url'], '/')) { | ||
| 113 | + $dist['url'] = $appDir.'/'.$dist['url']; | ||
| 114 | + $package['dist'] = $dist; | ||
| 115 | + $packages[$i] = $package; | ||
| 116 | + } | ||
| 117 | + } | ||
| 118 | + if (isset($lock[$section])) { | ||
| 119 | + $lock[$section] = $packages; | ||
| 120 | + } | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + return $lock; | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + private function copy(string $buildDir, ?string $skip): void | ||
| 127 | + { | ||
| 128 | + $files = new \RecursiveIteratorIterator( | ||
| 129 | + new \RecursiveCallbackFilterIterator( | ||
| 130 | + new \RecursiveDirectoryIterator($this->appDir, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS), | ||
| 131 | + static function (\SplFileInfo $file, string $path, \RecursiveDirectoryIterator $iterator) use ($skip): bool { | ||
| 132 | + $relativePath = $iterator->getSubPathname(); | ||
| 133 | + | ||
| 134 | + return $relativePath !== $skip && !self::isExcluded($relativePath); | ||
| 135 | + }, | ||
| 136 | + ), | ||
| 137 | + \RecursiveIteratorIterator::SELF_FIRST, | ||
| 138 | + ); | ||
| 139 | + | ||
| 140 | + $this->mkdir($buildDir); | ||
| 141 | + foreach ($files as $file) { | ||
| 142 | + \assert($file instanceof \SplFileInfo); | ||
| 143 | + $target = $buildDir.'/'.substr($file->getPathname(), \strlen($this->appDir) + 1); | ||
| 144 | + if ($file->isDir()) { | ||
| 145 | + $this->mkdir($target); | ||
| 146 | + } elseif (!copy($file->getPathname(), $target) || !chmod($target, $file->getPerms() & 0o777)) { | ||
| 147 | + throw new \RuntimeException(\sprintf('Cannot copy "%s" to "%s".', $file->getPathname(), $target)); | ||
| 148 | + } | ||
| 149 | + } | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + /** | ||
| 153 | + * @return array{int, int} the zip size and the total size of the zipped files | ||
| 154 | + */ | ||
| 155 | + private function zip(string $buildDir, string $zipFile): array | ||
| 156 | + { | ||
| 157 | + $zip = new \ZipArchive(); | ||
| 158 | + if (true !== $zip->open($zipFile, \ZipArchive::CREATE | \ZipArchive::OVERWRITE)) { | ||
| 159 | + throw new \RuntimeException(\sprintf('Cannot create "%s".', $zipFile)); | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + $codeSize = 0; | ||
| 163 | + $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($buildDir, \FilesystemIterator::SKIP_DOTS)); | ||
| 164 | + foreach ($files as $file) { | ||
| 165 | + \assert($file instanceof \SplFileInfo); | ||
| 166 | + $name = substr($file->getPathname(), \strlen($buildDir) + 1); | ||
| 167 | + $zip->addFile($file->getPathname(), $name); | ||
| 168 | + // Keeps the executable bits, as for bin/console. | ||
| 169 | + $zip->setExternalAttributesName($name, \ZipArchive::OPSYS_UNIX, ($file->getPerms() & 0o777 | 0o100000) << 16); | ||
| 170 | + $codeSize += $file->getSize(); | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + if (!$zip->close()) { | ||
| 174 | + throw new \RuntimeException(\sprintf('Cannot write "%s": %s', $zipFile, $zip->getStatusString())); | ||
| 175 | + } | ||
| 176 | + clearstatcache(true, $zipFile); | ||
| 177 | + | ||
| 178 | + return [(int) filesize($zipFile), $codeSize]; | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + /** | ||
| 182 | + * @param list<string> $command | ||
| 183 | + */ | ||
| 184 | + private function run(array $command, string $cwd): void | ||
| 185 | + { | ||
| 186 | + $env = ['APP_ENV' => 'prod', 'APP_DEBUG' => '0', 'COMPOSER_MIRROR_PATH_REPOS' => '1'] + getenv(); | ||
| 187 | + // Child output goes to our stderr, keeping stdout clean like the rest of the log. | ||
| 188 | + $process = proc_open($command, [0 => ['file', '/dev/null', 'r'], 1 => $this->log, 2 => $this->log], $pipes, $cwd, $env); | ||
| 189 | + if (false === $process || 0 !== $exitCode = proc_close($process)) { | ||
| 190 | + throw new \RuntimeException(\sprintf('"%s" failed%s.', implode(' ', $command), isset($exitCode) ? ' with exit code '.$exitCode : '')); | ||
| 191 | + } | ||
| 192 | + } | ||
| 193 | + | ||
| 194 | + private function relativeToApp(string $file): ?string | ||
| 195 | + { | ||
| 196 | + $dir = realpath(\dirname($file)); | ||
| 197 | + | ||
| 198 | + return false !== $dir && str_starts_with($dir.'/', $this->appDir.'/') ? ltrim(substr($dir, \strlen($this->appDir)).'/'.basename($file), '/') : null; | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + private function remove(string $dir): void | ||
| 202 | + { | ||
| 203 | + if (!is_dir($dir)) { | ||
| 204 | + return; | ||
| 205 | + } | ||
| 206 | + $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST); | ||
| 207 | + foreach ($files as $file) { | ||
| 208 | + \assert($file instanceof \SplFileInfo); | ||
| 209 | + $file->isDir() && !$file->isLink() ? rmdir($file->getPathname()) : unlink($file->getPathname()); | ||
| 210 | + } | ||
| 211 | + rmdir($dir); | ||
| 212 | + } | ||
| 213 | + | ||
| 214 | + private function mkdir(string $dir): void | ||
| 215 | + { | ||
| 216 | + if (!is_dir($dir) && !mkdir($dir, 0o777, true)) { | ||
| 217 | + throw new \RuntimeException(\sprintf('Cannot create "%s".', $dir)); | ||
| 218 | + } | ||
| 219 | + } | ||
| 220 | + | ||
| 221 | + /** | ||
| 222 | + * @return array<mixed> | ||
| 223 | + */ | ||
| 224 | + private function decodeJson(string $json): array | ||
| 225 | + { | ||
| 226 | + $data = json_decode($json, true, 512, \JSON_THROW_ON_ERROR); | ||
| 227 | + | ||
| 228 | + return \is_array($data) ? $data : throw new \UnexpectedValueException('composer.lock must hold a JSON object.'); | ||
| 229 | + } | ||
| 230 | + | ||
| 231 | + /** | ||
| 232 | + * @param array<mixed> $data | ||
| 233 | + */ | ||
| 234 | + private function writeJson(string $file, array $data): void | ||
| 235 | + { | ||
| 236 | + file_put_contents($file, json_encode($data, \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES)); | ||
| 237 | + } | ||
| 238 | + | ||
| 239 | + private function log(string $message): void | ||
| 240 | + { | ||
| 241 | + fwrite($this->log, '> '.$message.\PHP_EOL); | ||
| 242 | + } | ||
| 243 | +} | ||
added
tests/PackagerTest.php +58 -0 | new file mode 100644 | ||
| @@ -0,0 +1,58 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace JDecool\ScalewayRuntime\Tests; | |
| 6 | + | |
| 7 | +use JDecool\ScalewayRuntime\Packager; | |
| 8 | +use PHPUnit\Framework\Attributes\TestWith; | |
| 9 | +use PHPUnit\Framework\TestCase; | |
| 10 | + | |
| 11 | +final class PackagerTest extends TestCase | |
| 12 | +{ | |
| 13 | + #[TestWith(['vendor'])] | |
| 14 | + #[TestWith(['var'])] | |
| 15 | + #[TestWith(['tests'])] | |
| 16 | + #[TestWith(['.env.local'])] | |
| 17 | + #[TestWith(['.env.prod.local'])] | |
| 18 | + #[TestWith(['.git'])] | |
| 19 | + #[TestWith(['assets/node_modules'])] | |
| 20 | + #[TestWith(['terraform/.terraform'])] | |
| 21 | + #[TestWith(['terraform/terraform.tfstate'])] | |
| 22 | + #[TestWith(['terraform/terraform.tfstate.backup'])] | |
| 23 | + public function testExcluded(string $path): void | |
| 24 | + { | |
| 25 | + self::assertTrue(Packager::isExcluded($path)); | |
| 26 | + } | |
| 27 | + | |
| 28 | + #[TestWith(['.env'])] | |
| 29 | + #[TestWith(['.env.prod'])] | |
| 30 | + #[TestWith(['src/vendor'])] | |
| 31 | + #[TestWith(['src/Tests/Fixture.php'])] | |
| 32 | + #[TestWith(['config/packages/var.yaml'])] | |
| 33 | + #[TestWith(['public/index.php'])] | |
| 34 | + #[TestWith(['handler.php'])] | |
| 35 | + public function testIncluded(string $path): void | |
| 36 | + { | |
| 37 | + self::assertFalse(Packager::isExcluded($path)); | |
| 38 | + } | |
| 39 | + | |
| 40 | + public function testRelativePathRepositoriesAreResolvedAgainstTheApplication(): void | |
| 41 | + { | |
| 42 | + $lock = [ | |
| 43 | + 'packages' => [ | |
| 44 | + ['name' => 'acme/local', 'dist' => ['type' => 'path', 'url' => '../lib']], | |
| 45 | + ['name' => 'acme/absolute', 'dist' => ['type' => 'path', 'url' => '/opt/lib']], | |
| 46 | + ['name' => 'acme/remote', 'dist' => ['type' => 'zip', 'url' => 'https://example.test/a.zip']], | |
| 47 | + ], | |
| 48 | + 'packages-dev' => [['name' => 'acme/dev', 'dist' => ['type' => 'path', 'url' => 'packages/dev']]], | |
| 49 | + 'content-hash' => 'abc', | |
| 50 | + ]; | |
| 51 | + | |
| 52 | + $expected = $lock; | |
| 53 | + $expected['packages'][0]['dist']['url'] = '/srv/app/../lib'; | |
| 54 | + $expected['packages-dev'][0]['dist']['url'] = '/srv/app/packages/dev'; | |
| 55 | + | |
| 56 | + self::assertSame($expected, Packager::resolvePathRepositories($lock, '/srv/app')); | |
| 57 | + } | |
| 58 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
| 4 | + | ||
| 5 | +namespace JDecool\ScalewayRuntime\Tests; | ||
| 6 | + | ||
| 7 | +use JDecool\ScalewayRuntime\Packager; | ||
| 8 | +use PHPUnit\Framework\Attributes\TestWith; | ||
| 9 | +use PHPUnit\Framework\TestCase; | ||
| 10 | + | ||
| 11 | +final class PackagerTest extends TestCase | ||
| 12 | +{ | ||
| 13 | + #[TestWith(['vendor'])] | ||
| 14 | + #[TestWith(['var'])] | ||
| 15 | + #[TestWith(['tests'])] | ||
| 16 | + #[TestWith(['.env.local'])] | ||
| 17 | + #[TestWith(['.env.prod.local'])] | ||
| 18 | + #[TestWith(['.git'])] | ||
| 19 | + #[TestWith(['assets/node_modules'])] | ||
| 20 | + #[TestWith(['terraform/.terraform'])] | ||
| 21 | + #[TestWith(['terraform/terraform.tfstate'])] | ||
| 22 | + #[TestWith(['terraform/terraform.tfstate.backup'])] | ||
| 23 | + public function testExcluded(string $path): void | ||
| 24 | + { | ||
| 25 | + self::assertTrue(Packager::isExcluded($path)); | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + #[TestWith(['.env'])] | ||
| 29 | + #[TestWith(['.env.prod'])] | ||
| 30 | + #[TestWith(['src/vendor'])] | ||
| 31 | + #[TestWith(['src/Tests/Fixture.php'])] | ||
| 32 | + #[TestWith(['config/packages/var.yaml'])] | ||
| 33 | + #[TestWith(['public/index.php'])] | ||
| 34 | + #[TestWith(['handler.php'])] | ||
| 35 | + public function testIncluded(string $path): void | ||
| 36 | + { | ||
| 37 | + self::assertFalse(Packager::isExcluded($path)); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + public function testRelativePathRepositoriesAreResolvedAgainstTheApplication(): void | ||
| 41 | + { | ||
| 42 | + $lock = [ | ||
| 43 | + 'packages' => [ | ||
| 44 | + ['name' => 'acme/local', 'dist' => ['type' => 'path', 'url' => '../lib']], | ||
| 45 | + ['name' => 'acme/absolute', 'dist' => ['type' => 'path', 'url' => '/opt/lib']], | ||
| 46 | + ['name' => 'acme/remote', 'dist' => ['type' => 'zip', 'url' => 'https://example.test/a.zip']], | ||
| 47 | + ], | ||
| 48 | + 'packages-dev' => [['name' => 'acme/dev', 'dist' => ['type' => 'path', 'url' => 'packages/dev']]], | ||
| 49 | + 'content-hash' => 'abc', | ||
| 50 | + ]; | ||
| 51 | + | ||
| 52 | + $expected = $lock; | ||
| 53 | + $expected['packages'][0]['dist']['url'] = '/srv/app/../lib'; | ||
| 54 | + $expected['packages-dev'][0]['dist']['url'] = '/srv/app/packages/dev'; | ||
| 55 | + | ||
| 56 | + self::assertSame($expected, Packager::resolvePathRepositories($lock, '/srv/app')); | ||
| 57 | + } | ||
| 58 | +} | ||