From c22009aab9f0e88941261ed7e2ceeeeb9f2230cb Mon Sep 17 00:00:00 2001 From: wheakerd Date: Tue, 10 Nov 2026 18:11:18 -0800 Subject: [PATCH 1/2] Regenerate the Composer autoloader on every build and create the matching skeleton A build now runs Composer's dump-autoload in the project before the generator, so a class added after composer install is known to a classmap-authoritative autoloader without a manual dump. Composer takes the optimization settings from the project's composer.json, and the dev flag of installed.json keeps a --no-dev installation without its development autoload rules. build, run, every --watch rebuild, and package's build step use the Composer that new and package locate; a Composer that cannot be provided or fails stops the build before it generates anything. The from-source self-build still runs without Composer. A released SKERNEL now creates super-kernel/skeleton at exactly its own version; one without a version keeps the development line. When Composer fails for a released SKERNEL, the message names that version and says to run skernel self-update. Co-Authored-By: Claude Opus 5.5 --- README.md | 16 +- README.zh-CN.md | 16 +- src/Build/ComposerAutoloader.php | 61 +++++++ src/Build/ProjectBuilder.php | 25 ++- src/Command/BuildCommand.php | 10 +- src/Command/NewCommand.php | 22 ++- src/Command/PackageCommand.php | 10 +- src/Command/ProjectRuntime.php | 12 +- src/Command/RunCommand.php | 7 +- src/Run/ProjectRunner.php | 19 ++- tests/BootstrapTest.php | 18 ++- tests/Integration/PackageCommandTest.php | 43 +++-- tests/Integration/ProjectCommandsTest.php | 110 ++++++++++++- tests/Unit/Build/ProjectBuilderTest.php | 187 ++++++++++++++++++---- tests/Unit/Command/NewCommandTest.php | 128 +++++++++++++++ 15 files changed, 594 insertions(+), 90 deletions(-) create mode 100644 src/Build/ComposerAutoloader.php create mode 100644 tests/Unit/Command/NewCommandTest.php diff --git a/README.md b/README.md index 4ae0ec0..9def267 100644 --- a/README.md +++ b/README.md @@ -87,13 +87,15 @@ new [--composer=] `new` creates a project in ``, a path relative to the global `-d`/`--working-dir` option or the current directory, from the [`super-kernel/skeleton`](https://github.com/super-kernel/skeleton) package with Composer's `create-project` command. Platform requirements are not checked against the PHP that runs Composer, because the project runs on its managed PHP. +A released SKERNEL creates the skeleton at exactly its own version, so SKERNEL 1.0.0-alpha.3 creates `super-kernel/skeleton` 1.0.0-alpha.3; SKERNEL run from source, which has no version, creates it from the development line. The version only selects the template: the template has no `composer.lock`, so Composer resolves its requirements when it creates the project. + With `--composer`, that binary is used. Otherwise SKERNEL uses the first working Composer 2 it finds: `composer` or `composer.phar` in `PATH`, then `~/.local/bin/composer`, `~/bin/composer`, `~/.config/composer/vendor/bin/composer`, and `~/.composer/vendor/bin/composer`. When there is none, it downloads a pinned Composer release, verified against its SHA-256 digest, into its home directory and runs it with a managed PHP, which it builds the first time. ```text Project created in /path/to/my-app. ``` -A directory that exists and is not empty, a Composer that is not found or fails, or a managed PHP that cannot be provided prints a short message on STDERR and exits with status 1. +A directory that exists and is not empty, a Composer that is not found or fails, or a managed PHP that cannot be provided prints a short message on STDERR and exits with status 1. When Composer fails for a released SKERNEL, the message also names the skeleton version it asked for and says to run `skernel self-update` and create the project again if Composer could not find that version. ### run @@ -103,7 +105,7 @@ run [-d|--working-dir=] [--php=] [--watch] [--] [...] `run` builds the project as `build` does and then runs it from source with its managed PHP, in a separate process in the project directory. `` go to the application, so `run swoole:start` starts its Swoole server; put options for the application after `--`. Without `--watch`, SKERNEL exits with the application's exit status. -With `--watch`, SKERNEL restarts the application whenever a project file changes. It checks once a second for changes to PHP files, `.env` and `.env.*` files, and `composer.lock`, leaving out the vendor directory, `target/`, and directories whose name starts with a dot, so the logs and caches a running application writes do not restart it. It sends the application SIGTERM and then SIGKILL after 10 seconds, and builds the project again before it restarts it. An application that exits on its own, or whose build fails, is started again by the next change. +With `--watch`, SKERNEL restarts the application whenever a project file changes. It checks once a second for changes to PHP files, `.env` and `.env.*` files, and `composer.lock`, leaving out the vendor directory, `target/`, and directories whose name starts with a dot, so the logs and caches a running application writes do not restart it. It sends the application SIGTERM and then SIGKILL after 10 seconds, and builds the project again, including its Composer autoloader, before it restarts it. An application that exits on its own, or whose build fails, is started again by the next change. SIGINT and SIGTERM stop the application before SKERNEL exits. The failures of `build`, and an application that cannot start, print a short message on STDERR and exit with status 1. @@ -133,9 +135,11 @@ build [-d|--working-dir=] [--php=] `build` generates the build artifacts that the application loads at start and writes them to the project's `/.cache` directory, honouring its `config.vendor-dir`: the package records with their class maps, the attributes collected from those classes, a build stamp, and whatever the project's components write when the build publishes `SuperKernel\Event\ApplicationBuild`, such as the prepared proxies of `super-kernel/aop`. A SuperKernel application started without them, or with artifacts from an older `composer.lock`, stops with a message that names `skernel build`. The project must have its dependencies installed, including `super-kernel/metadata`, `super-kernel/context`, and `super-kernel/di`, and the build covers the packages of that installation. +Before it generates anything, `build` regenerates the project's Composer autoloader with Composer's `dump-autoload` command, so the build and the application find a class added since `composer install` without a `composer dump-autoload` of your own. It uses the Composer that [`new`](#new) uses without `--composer`: the first working Composer 2 it finds, or else its own. Composer applies the project's own autoloader settings, such as `config.optimize-autoloader` and `config.classmap-authoritative` in `composer.json`, and the development autoload rules follow the installation: a project installed with `--no-dev` stays without them. Composer runs in its own process, and so do the project's `pre-autoload-dump` and `post-autoload-dump` scripts that it runs. When Composer cannot be provided or fails, the build stops before it generates anything and keeps the previous artifacts. + The build runs in two separate processes of the managed PHP CLI, so SKERNEL never loads the project's code itself. The first loads the project's own autoloader, checks that the installed `super-kernel/metadata` uses the build protocol this SKERNEL generates, maps the classes of every installed package, collects the attributes of the classes that declare any, and publishes the records through the project's own `super-kernel/metadata` and `super-kernel/annotator`. A class that cannot be loaded or whose attribute cannot be created is skipped, even when loading it ends the process with a fatal error. The second process bootstraps the container from the records and then publishes `ApplicationBuild` through the project's PSR-14 event dispatcher, when the project has both the event and a dispatcher, without resolving or running the application. -Everything the processes write goes to STDERR, followed by the skipped classes, one per line, sorted by package and class: +Everything Composer and the processes write goes to STDERR, followed by the skipped classes, one per line, sorted by package and class: ```text SKERNEL skipped 2 classes whose attributes could not be collected: @@ -149,7 +153,7 @@ On success STDOUT carries one line: Build artifacts written to /path/to/my-app/vendor/.cache. ``` -When the project's `super-kernel/metadata` uses another build protocol, the build stops before it writes anything and says which side to upgrade: SKERNEL, for example with `skernel self-update`, or the project's SuperKernel packages. A project without `super-kernel/metadata` predates SKERNEL-generated artifacts and needs SuperKernel 1.0.0-alpha.2 or later. That, a missing project or dependencies, an invalid configuration file or `--php` value, a managed PHP that cannot be provided, or a failed build prints a short message on STDERR and exits with status 1. +When the project's `super-kernel/metadata` uses another build protocol, the build stops before it writes anything and says which side to upgrade: SKERNEL, for example with `skernel self-update`, or the project's SuperKernel packages. A project without `super-kernel/metadata` predates SKERNEL-generated artifacts and needs SuperKernel 1.0.0-alpha.2 or later. That, a missing project or dependencies, an invalid configuration file or `--php` value, a managed PHP or Composer that cannot be provided, a Composer that fails to regenerate the autoloader, or a failed build prints a short message on STDERR and exits with status 1. ### package @@ -162,7 +166,7 @@ package [-d|--working-dir=] [--php=] [--phar] [--dev] [--output=

[--composer=] `new` 用 Composer 的 `create-project` 命令,从 [`super-kernel/skeleton`](https://github.com/super-kernel/skeleton) 包在 `` 创建项目;相对路径以全局选项 `-d`/`--working-dir` 或当前目录为基准。不会按运行 Composer 的 PHP 检查平台要求,因为项目运行在它的受管 PHP 上。 +已发布的 SKERNEL 会创建与自身版本完全相同的骨架,例如 SKERNEL 1.0.0-alpha.3 创建 `super-kernel/skeleton` 1.0.0-alpha.3;从源码运行的 SKERNEL 没有版本,会从开发线创建。这个版本只用于选择模板:模板不带 `composer.lock`,所以 Composer 在创建项目时解析它的依赖要求。 + 指定 `--composer` 时使用该程序。否则 SKERNEL 使用找到的第一个可用的 Composer 2:先找 `PATH` 中的 `composer` 或 `composer.phar`,再找 `~/.local/bin/composer`、`~/bin/composer`、`~/.config/composer/vendor/bin/composer` 和 `~/.composer/vendor/bin/composer`。都没有时,它把固定版本的 Composer 下载到自己的主目录,按 SHA-256 摘要校验,再用受管 PHP 运行;这个 PHP 在首次使用时构建。 ```text Project created in /path/to/my-app. ``` -目录已存在且不为空、找不到 Composer 或 Composer 执行失败、无法提供受管 PHP 时,会在 STDERR 上打印一条简短的提示,退出码为 1。 +目录已存在且不为空、找不到 Composer 或 Composer 执行失败、无法提供受管 PHP 时,会在 STDERR 上打印一条简短的提示,退出码为 1。已发布的 SKERNEL 在 Composer 执行失败时,提示中还会写明它请求的骨架版本,并说明:如果 Composer 找不到该版本,请运行 `skernel self-update` 后重新创建项目。 ### run @@ -103,7 +105,7 @@ run [-d|--working-dir=

] [--php=] [--watch] [--] [...] `run` 先像 `build` 一样构建项目,再用受管 PHP 从源码运行项目,在项目目录中以单独的进程执行。`` 传给应用,所以 `run swoole:start` 会启动它的 Swoole 服务;要传给应用的选项放在 `--` 之后。不加 `--watch` 时,SKERNEL 以应用的退出码退出。 -加上 `--watch` 后,项目文件变化时 SKERNEL 会重启应用。它每秒检查一次 PHP 文件、`.env` 和 `.env.*` 文件以及 `composer.lock` 的变化,并跳过 vendor 目录、`target/` 和名称以点开头的目录,所以运行中的应用写入日志和缓存不会触发重启。重启时先向应用发送 SIGTERM,10 秒后发送 SIGKILL,并在重启前重新构建项目。应用自行退出或构建失败后,下一次变化会再次启动它。 +加上 `--watch` 后,项目文件变化时 SKERNEL 会重启应用。它每秒检查一次 PHP 文件、`.env` 和 `.env.*` 文件以及 `composer.lock` 的变化,并跳过 vendor 目录、`target/` 和名称以点开头的目录,所以运行中的应用写入日志和缓存不会触发重启。重启时先向应用发送 SIGTERM,10 秒后发送 SIGKILL,并在重启前重新构建项目,包括它的 Composer autoloader。应用自行退出或构建失败后,下一次变化会再次启动它。 SIGINT 和 SIGTERM 会先停止应用,SKERNEL 再退出。出现 `build` 的任一种失败或应用无法启动时,会在 STDERR 上打印一条简短的提示,退出码为 1。 @@ -133,9 +135,11 @@ build [-d|--working-dir=] [--php=] `build` 生成应用启动时加载的构建产物,写到项目的 `/.cache` 目录,vendor 目录遵循项目的 `config.vendor-dir`:包括带类映射的包记录、从这些类收集到的注解、构建戳,以及构建发布 `SuperKernel\Event\ApplicationBuild` 时项目各组件写出的产物,例如 `super-kernel/aop` 预先准备好的代理。SuperKernel 应用在缺少这些产物,或产物来自较旧的 `composer.lock` 时启动,会停止并给出提到 `skernel build` 的提示。项目必须已经安装好依赖,其中包括 `super-kernel/metadata`、`super-kernel/context` 和 `super-kernel/di`;构建覆盖的包以这次安装为准。 +`build` 在生成任何内容之前,先用 Composer 的 `dump-autoload` 命令重新生成项目的 Composer autoloader,所以 `composer install` 之后新增的类能被构建和应用找到,不需要自己运行 `composer dump-autoload`。它使用的 Composer 与不指定 `--composer` 时的 [`new`](#new) 相同:找到的第一个可用的 Composer 2,否则是 SKERNEL 自己的。Composer 采用项目自己的 autoloader 设置,例如 `composer.json` 中的 `config.optimize-autoloader` 和 `config.classmap-authoritative`;开发环境的自动加载规则随安装方式而定:用 `--no-dev` 安装的项目不会重新获得这些规则。Composer 在自己的进程中运行,它执行的项目 `pre-autoload-dump` 和 `post-autoload-dump` 脚本也是如此。无法提供 Composer 或 Composer 执行失败时,构建会在生成任何内容之前停止,并保留之前的产物。 + 构建在受管 PHP CLI 的两个单独进程中运行,所以 SKERNEL 自己从不加载项目的代码。第一个进程加载项目自己的 autoloader,确认已安装的 `super-kernel/metadata` 使用本 SKERNEL 生成的构建协议,为每个已安装的包建立类映射,收集声明了注解的类的注解,再通过项目自己的 `super-kernel/metadata` 和 `super-kernel/annotator` 发布这些记录。无法加载的类,或注解无法创建的类,会被跳过,即使加载它时进程因致命错误而结束也是如此。第二个进程从这些记录引导出容器;项目同时具备该事件和事件分发器时,再通过项目的 PSR-14 事件分发器发布 `ApplicationBuild`。整个过程不解析也不运行应用。 -这些进程写出的所有内容都转到 STDERR,之后列出被跳过的类,每行一个,按包和类排序: +Composer 和这些进程写出的所有内容都转到 STDERR,之后列出被跳过的类,每行一个,按包和类排序: ```text SKERNEL skipped 2 classes whose attributes could not be collected: @@ -149,7 +153,7 @@ SKERNEL skipped 2 classes whose attributes could not be collected: Build artifacts written to /path/to/my-app/vendor/.cache. ``` -项目的 `super-kernel/metadata` 使用其他构建协议时,构建会在写入任何内容之前停止,并说明该升级哪一方:SKERNEL(例如用 `skernel self-update`),或项目的 SuperKernel 包。没有 `super-kernel/metadata` 的项目早于由 SKERNEL 生成产物的做法,需要 SuperKernel 1.0.0-alpha.2 或更高版本。出现这种情况,或项目或依赖不存在、配置文件或 `--php` 的值无效、无法提供受管 PHP、构建失败时,会在 STDERR 上打印一条简短的提示,退出码为 1。 +项目的 `super-kernel/metadata` 使用其他构建协议时,构建会在写入任何内容之前停止,并说明该升级哪一方:SKERNEL(例如用 `skernel self-update`),或项目的 SuperKernel 包。没有 `super-kernel/metadata` 的项目早于由 SKERNEL 生成产物的做法,需要 SuperKernel 1.0.0-alpha.2 或更高版本。出现这种情况,或项目或依赖不存在、配置文件或 `--php` 的值无效、无法提供受管 PHP 或 Composer、Composer 重新生成 autoloader 失败、构建失败时,会在 STDERR 上打印一条简短的提示,退出码为 1。 ### package @@ -162,7 +166,7 @@ package [-d|--working-dir=] [--php=] [--phar] [--dev] [--output=

/composer/installed.json` records, so that a project installed + * without development dependencies does not get them back. Composer runs in its own process, as do the project's + * scripts that it runs. + */ +final class ComposerAutoloader +{ + /** + * @param non-empty-list $composer The command that runs Composer + * + * @throws RuntimeException When the installed state cannot be read, or Composer cannot be started or fails + */ + public static function regenerate(Project $project, array $composer, OutputInterface $output): void + { + $installed = JsonFile::decode($project->vendorDirectory . '/composer/installed.json'); + $dev = is_array($installed) ? $installed['dev'] ?? null : null; + $command = [ + ...$composer, + 'dump-autoload', + '--no-interaction', + ]; + + if (is_bool($dev)) { + $command[] = $dev ? '--dev' : '--no-dev'; + } + + $status = ProcessRunner::run( + $command, + $project->directory, + $output, + sprintf('the Composer binary %s', $composer[0]), + ); + + if (0 !== $status) { + throw new RuntimeException(sprintf( + 'Regenerating the Composer autoloader failed with exit status %d.', + $status, + )); + } + } +} diff --git a/src/Build/ProjectBuilder.php b/src/Build/ProjectBuilder.php index c945f70..487b598 100644 --- a/src/Build/ProjectBuilder.php +++ b/src/Build/ProjectBuilder.php @@ -16,10 +16,11 @@ /** * Writes a project's build artifacts in separate PHP processes without creating or running the application. * - * The first process generates the package and annotation records. The second bootstraps the project's container from - * them, without any class the first loaded, and publishes `SuperKernel\Event\ApplicationBuild` so that components - * write the artifacts they prepare at build time, such as AOP proxies. Projects without that event or a PSR-14 - * dispatcher have no listener for it, so the event is skipped. + * Composer first regenerates the project's autoloader, so that the build and the application see the classes added + * since the project's dependencies were installed. The first PHP process then generates the package and annotation + * records. The second bootstraps the project's container from them, without any class the first loaded, and publishes + * `SuperKernel\Event\ApplicationBuild` so that components write the artifacts they prepare at build time, such as AOP + * proxies. Projects without that event or a PSR-14 dispatcher have no listener for it, so the event is skipped. */ final readonly class ProjectBuilder { @@ -50,13 +51,21 @@ public function __construct(private MetadataGenerator $metadataGenerator) * Runs the build in the project directory with the given PHP binary, a path or a name looked up in PATH, forwards * everything the processes write, and then lists the classes whose attributes could not be collected. * - * @param string $skernelVersion The running SKERNEL's version, `UNKNOWN` or empty when it has none + * @param non-empty-list $composer The command that runs Composer + * @param string $skernelVersion The running SKERNEL's version, `UNKNOWN` or empty when it has none * - * @throws RuntimeException When the PHP binary is not found or cannot be started, the project's build protocol - * differs, or either process fails + * @throws RuntimeException When Composer cannot be started or fails, the PHP binary is not found or cannot be + * started, the project's build protocol differs, or either PHP process fails */ - public function build(Project $project, string $phpBinary, string $skernelVersion, OutputInterface $output): void + public function build( + Project $project, + string $phpBinary, + array $composer, + string $skernelVersion, + OutputInterface $output, + ): void { + ComposerAutoloader::regenerate($project, $composer, $output); $diagnostics = $this->metadataGenerator->generate( $project, $phpBinary, diff --git a/src/Command/BuildCommand.php b/src/Command/BuildCommand.php index 6d999b7..1279fe4 100644 --- a/src/Command/BuildCommand.php +++ b/src/Command/BuildCommand.php @@ -16,7 +16,8 @@ use function sprintf; /** - * Generates the project's build artifacts with its managed PHP; the application loads them at start. + * Regenerates the project's Composer autoloader and generates its build artifacts with its managed PHP; the + * application loads them at start. */ #[AttributeCommand(name: 'build', description: 'Generate the build artifacts that the project loads at start')] final class BuildCommand extends Command @@ -40,16 +41,17 @@ protected function configure(): void } /** - * @throws RuntimeException When the project or its configuration is invalid, its managed PHP cannot be provided, - * the project's build protocol differs, or the build cannot start or fails + * @throws RuntimeException When the project or its configuration is invalid, its managed PHP or Composer cannot be + * provided, the project's build protocol differs, or the build cannot start or fails */ protected function execute(InputInterface $input, OutputInterface $output): int { $errorOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output; - [$project, $runtime] = $this->projectRuntime->resolve($input, $errorOutput); + [$project, $runtime, $composer] = $this->projectRuntime->resolve($input, $errorOutput); $this->projectBuilder->build( $project, $runtime->php, + $composer, (string) $this->getApplication()?->getVersion(), $errorOutput, ); diff --git a/src/Command/NewCommand.php b/src/Command/NewCommand.php index 6447482..15e94f0 100644 --- a/src/Command/NewCommand.php +++ b/src/Command/NewCommand.php @@ -26,8 +26,9 @@ /** * Creates a project from the SuperKernel skeleton with Composer. * - * Platform requirements are not checked against the PHP that runs Composer, because the project runs on its managed - * PHP, which `build`, `run`, and `package` build later. + * A released SKERNEL creates the skeleton at exactly its own version; one without a version, such as SKERNEL run from + * source, creates it from the development line. Platform requirements are not checked against the PHP that runs + * Composer, because the project runs on its managed PHP, which `build`, `run`, and `package` build later. */ #[AttributeCommand(name: 'new', description: 'Create a project from the SuperKernel skeleton')] final class NewCommand extends Command @@ -51,7 +52,8 @@ protected function configure(): void } /** - * @throws RuntimeException When the directory is in use, Composer cannot be provided, or it fails + * @throws RuntimeException When the directory is in use, Composer cannot be provided, or it fails, for a released + * SKERNEL also when the skeleton of its version does not exist */ protected function execute(InputInterface $input, OutputInterface $output): int { @@ -67,19 +69,29 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $errorOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output; + $version = (string) $this->getApplication()?->getVersion(); + $released = '' !== $version && 'UNKNOWN' !== $version; $status = ProcessRunner::run([ ...$this->composerLocator->command($input->getOption('composer'), $errorOutput), 'create-project', self::SKELETON, $directory, - '--stability=dev', + $released ? $version : '--stability=dev', '--no-interaction', '--no-progress', '--ignore-platform-reqs', ], $current, $errorOutput, 'Composer'); if (0 !== $status) { - throw new RuntimeException(sprintf('Creating the project failed with exit status %d.', $status)); + $message = sprintf('Creating the project failed with exit status %d.', $status); + + throw new RuntimeException($released ? sprintf( + '%s This SKERNEL creates %s %s; if Composer could not find that version, run skernel self-update' + . ' and create the project again.', + $message, + self::SKELETON, + $version, + ) : $message); } $output->writeln(sprintf('Project created in %s.', $directory), OutputInterface::OUTPUT_RAW); diff --git a/src/Command/PackageCommand.php b/src/Command/PackageCommand.php index 23844d3..24b673c 100644 --- a/src/Command/PackageCommand.php +++ b/src/Command/PackageCommand.php @@ -130,12 +130,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int $project->directory . '/target', $target, ]); + $composer = $this->composerLocator->command( + $input->getOption('composer') ?? $configuration->composer, + $errorOutput, + ); $status = $this->dependencyInstaller->install( $staging . '/project', - $this->composerLocator->command( - $input->getOption('composer') ?? $configuration->composer, - $errorOutput, - ), + $composer, $input->getOption('dev') || true === $configuration->dev, $errorOutput, ); @@ -156,6 +157,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->projectBuilder->build( $staged, $managed->php, + $composer, (string) $this->getApplication()?->getVersion(), $errorOutput, ); diff --git a/src/Command/ProjectRuntime.php b/src/Command/ProjectRuntime.php index d40e8dc..efba577 100644 --- a/src/Command/ProjectRuntime.php +++ b/src/Command/ProjectRuntime.php @@ -4,6 +4,7 @@ namespace Src\Command; +use Src\Composer\ComposerLocator; use Src\Configuration\ConfigurationLoader; use Src\Project\Project; use Src\Project\ProjectLocator; @@ -15,8 +16,8 @@ use Symfony\Component\Console\Output\OutputInterface; /** - * Provides what a command that runs an installed project needs: the project from `--working-dir`, and its managed PHP - * from `--php` or the project's configuration file. + * Provides what a command that builds an installed project needs: the project from `--working-dir`, its managed PHP + * from `--php` or the project's configuration file, and the Composer that regenerates its autoloader. */ final readonly class ProjectRuntime { @@ -24,14 +25,16 @@ public function __construct( private ProjectLocator $projectLocator, private ConfigurationLoader $configurationLoader, private ManagedPhp $managedPhp, + private ComposerLocator $composerLocator, ) { } /** - * @return array{Project, Runtime} + * @return array{Project, Runtime, non-empty-list} The project, its managed PHP, and the Composer command * - * @throws RuntimeException When the project or its configuration is invalid, or its managed PHP cannot be provided + * @throws RuntimeException When the project or its configuration is invalid, or its managed PHP or Composer cannot + * be provided */ public function resolve(InputInterface $input, OutputInterface $errorOutput): array { @@ -49,6 +52,7 @@ public function resolve(InputInterface $input, OutputInterface $errorOutput): ar $php ?? $this->configurationLoader->load($project->directory)->php, $errorOutput, ), + $this->composerLocator->command(null, $errorOutput), ]; } } diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index ca4facb..eacce96 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -48,17 +48,18 @@ protected function configure(): void /** * @return int The exit status of the application * - * @throws RuntimeException When the project or its configuration is invalid, its managed PHP cannot be provided, - * or the application cannot be built or started + * @throws RuntimeException When the project or its configuration is invalid, its managed PHP or Composer cannot be + * provided, or the application cannot be built or started */ protected function execute(InputInterface $input, OutputInterface $output): int { $errorOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output; - [$project, $runtime] = $this->projectRuntime->resolve($input, $errorOutput); + [$project, $runtime, $composer] = $this->projectRuntime->resolve($input, $errorOutput); return $this->projectRunner->run( $project, $runtime->php, + $composer, $input->getArgument('arguments'), $input->getOption('watch'), (string) $this->getApplication()?->getVersion(), diff --git a/src/Run/ProjectRunner.php b/src/Run/ProjectRunner.php index e324411..f6130fa 100644 --- a/src/Run/ProjectRunner.php +++ b/src/Run/ProjectRunner.php @@ -29,10 +29,10 @@ /** * Runs a project from source in a separate PHP process, and with watching restarts it whenever its files change. * - * Every start builds the project first, so the application loads artifacts that match its sources. The process runs - * the standard application entry sequence in the project directory. A watched application that exits on its own, or - * whose build fails, is started again by the next change, so that a fix restarts one that crashed. SIGINT and SIGTERM - * stop the application first, so that it does not outlive SKERNEL. + * Every start builds the project first, so the application loads an autoloader and artifacts that match its sources. + * The process runs the standard application entry sequence in the project directory. A watched application that exits + * on its own, or whose build fails, is started again by the next change, so that a fix restarts one that crashed. + * SIGINT and SIGTERM stop the application first, so that it does not outlive SKERNEL. */ final readonly class ProjectRunner { @@ -63,9 +63,10 @@ public function __construct(private ProjectBuilder $projectBuilder) } /** - * @param string $phpBinary A path or a name looked up in PATH - * @param list $arguments The arguments of the application - * @param string $skernelVersion The running SKERNEL's version, `UNKNOWN` or empty when it has none + * @param string $phpBinary A path or a name looked up in PATH + * @param non-empty-list $composer The command that runs Composer + * @param list $arguments The arguments of the application + * @param string $skernelVersion The running SKERNEL's version, `UNKNOWN` or empty when it has none * * @return int The exit status of the application * @@ -75,6 +76,7 @@ public function __construct(private ProjectBuilder $projectBuilder) public function run( Project $project, string $phpBinary, + array $composer, array $arguments, bool $watch, string $skernelVersion, @@ -102,6 +104,7 @@ public function run( $start = static function () use ( $builder, $phpBinary, + $composer, $skernelVersion, $command, $script, @@ -110,7 +113,7 @@ public function run( $output, $errorOutput, ): Process { - $builder->build($project, $phpBinary, $skernelVersion, $errorOutput); + $builder->build($project, $phpBinary, $composer, $skernelVersion, $errorOutput); $process = new Process([ ...$command, $script, diff --git a/tests/BootstrapTest.php b/tests/BootstrapTest.php index e2062a8..abef84a 100644 --- a/tests/BootstrapTest.php +++ b/tests/BootstrapTest.php @@ -17,11 +17,14 @@ use Src\Toolchain\ZigMirrors; use function array_filter; +use function chmod; use function dirname; use function end; use function escapeshellarg; use function exec; use function file_get_contents; +use function file_put_contents; +use function getenv; use function implode; use function json_decode; use function mkdir; @@ -140,12 +143,21 @@ public function testBuildWritesTheBuildArtifactsOfTheProjectWithItsManagedPhp(): self::assertTrue(mkdir($runtime, 0o700, true)); self::assertTrue(symlink(PHP_BINARY, $runtime . '/php')); self::assertTrue(touch($runtime . '/micro.sfx') && touch($runtime . '/manifest.json')); + // A stand-in Composer found first in PATH keeps the build from regenerating SKERNEL's own autoloader. + self::assertTrue(mkdir($home . '/path', 0o700)); + self::assertNotFalse(file_put_contents($home . '/path/composer', implode("\n", [ + '#!/bin/sh', + 'if [ "$1" = --version ]; then echo "Composer version 2.10.3"; exit 0; fi', + 'echo "$PWD $*" >> ' . escapeshellarg($home . '/composer-arguments'), + '', + ]))); + self::assertTrue(chmod($home . '/path/composer', 0o755)); $output = $this->bootstrap( 'build --php=' . $version . ' -d ' . escapeshellarg($root), $status, '2>/dev/null', - 'SKERNEL_HOME=' . escapeshellarg($home), + 'SKERNEL_HOME=' . escapeshellarg($home) . ' PATH=' . escapeshellarg($home . '/path:' . getenv('PATH')), ); self::assertSame(0, $status); @@ -153,6 +165,10 @@ public function testBuildWritesTheBuildArtifactsOfTheProjectWithItsManagedPhp(): 'Build artifacts written to ' . $root . '/vendor/.cache.', ], $output); self::assertFileExists($root . '/vendor/.cache/packages.ser'); + self::assertStringEqualsFile( + $home . '/composer-arguments', + $root . " dump-autoload --no-interaction --dev\n", + ); } finally { putenv('SKERNEL_HOME'); Directory::remove($home); diff --git a/tests/Integration/PackageCommandTest.php b/tests/Integration/PackageCommandTest.php index 0882c69..c2632d9 100644 --- a/tests/Integration/PackageCommandTest.php +++ b/tests/Integration/PackageCommandTest.php @@ -34,6 +34,7 @@ use function str_starts_with; use function strlen; use function substr; +use function substr_count; use function symlink; use function sys_get_temp_dir; use function tempnam; @@ -85,6 +86,15 @@ public function run(): void PHP; + private const array PACKAGES = [ + [ + 'name' => 'super-kernel/context', + ], + [ + 'name' => 'super-kernel/di', + ], + ]; + private string $directory; private string $project; @@ -112,11 +122,21 @@ protected function setUp(): void $this->write($this->project . '/.env', 'SECRET=1'); $this->write($this->project . '/.git/HEAD', 'ref: refs/heads/main'); $this->write($this->project . '/target/stale', 'stale'); + $this->write($this->directory . '/installed-dev.json', (string) json_encode([ + 'packages' => self::PACKAGES, + 'dev' => true, + ])); $this->write($this->directory . '/bin/composer', implode("\n", [ '#!/bin/sh', 'if [ "$1" = --version ]; then echo "Composer version 2.10.3"; exit 0; fi', + 'if [ "$1" = dump-autoload ]; then', + ' echo "$PWD $*" >> ' . escapeshellarg($this->directory . '/dump-arguments'), + ' exit 0', + 'fi', 'printf "%s" "$*" > ' . escapeshellarg($this->directory . '/composer-arguments'), 'cp -R ' . escapeshellarg($this->directory . '/template/vendor') . ' vendor', + 'case "$*" in *--no-dev*) ;; *) cp ' . escapeshellarg($this->directory . '/installed-dev.json') + . ' vendor/composer/installed.json ;; esac', '', ])); self::assertTrue(chmod($this->directory . '/bin/composer', 0o755)); @@ -158,6 +178,7 @@ public function testPackagesAPharWithoutMetadataSecretsOrOldOutput(): void 'install --no-interaction --no-progress --ignore-platform-reqs --no-dev', file_get_contents($this->directory . '/composer-arguments'), ); + $this->assertDumpedInTheStagedCopy('--no-dev'); self::assertStringStartsWith( "#!/usr/bin/env php\n", (string) file_get_contents($this->project . '/target/demo.phar'), @@ -193,6 +214,7 @@ public function testPackagesAStandaloneBinaryWithTheManagedMicroRuntime(): void 'install --no-interaction --no-progress --ignore-platform-reqs', file_get_contents($this->directory . '/composer-arguments'), ); + $this->assertDumpedInTheStagedCopy('--dev'); $binary = (string) file_get_contents($this->project . '/target/demo'); self::assertStringStartsWith('MICROdirectory . '/dump-arguments'); + + self::assertStringEndsWith('/project dump-autoload --no-interaction ' . $mode . "\n", $line); + self::assertStringStartsNotWith($this->project . ' ', $line); + self::assertSame(1, substr_count($line, "\n")); + } + private function composer(): string { return escapeshellarg($this->directory . '/bin/composer'); @@ -284,15 +316,8 @@ private function vendor(string $directory): void $this->write($directory . '/vendor/autoload.php', self::AUTOLOAD); BuildApiStandIn::write($directory, $directory . '/vendor'); $this->write($directory . '/vendor/composer/installed.json', (string) json_encode([ - 'packages' => [ - [ - 'name' => 'super-kernel/context', - ], - [ - 'name' => 'super-kernel/di', - ], - ], - 'dev' => false, + 'packages' => self::PACKAGES, + 'dev' => false, ])); } diff --git a/tests/Integration/ProjectCommandsTest.php b/tests/Integration/ProjectCommandsTest.php index 8f1e523..7050352 100644 --- a/tests/Integration/ProjectCommandsTest.php +++ b/tests/Integration/ProjectCommandsTest.php @@ -23,6 +23,7 @@ use function exec; use function file_get_contents; use function file_put_contents; +use function getenv; use function implode; use function json_encode; use function microtime; @@ -120,6 +121,7 @@ protected function setUp(): void 'dev' => false, ])); $this->write($this->project . '/src/App.php', 'composer(''); putenv('SKERNEL_HOME=' . $this->directory . '/home'); try { @@ -185,6 +187,10 @@ public function testRunForwardsTheArgumentsOutputAndExitStatusOfTheApplication() 'started in project with serve --port=1', ], $this->skernel($arguments . ' -- serve --port=1', $status)); self::assertSame(0, $status); + self::assertStringEqualsFile( + $this->directory . '/composer-arguments', + $this->project . " dump-autoload --no-interaction --no-dev\n", + ); self::assertSame([ 'application error output', @@ -207,6 +213,7 @@ public function testRunWithWatchRestartsOnAChangeAndStopsOnASignal(): void 'sleep', ], null, [ 'SKERNEL_HOME' => $this->directory . '/home', + 'PATH' => $this->path(), ], null, 60); $process->start(); @@ -220,6 +227,10 @@ public function testRunWithWatchRestartsOnAChangeAndStopsOnASignal(): void $this->write($this->project . '/src/App.php', 'waitFor(static fn(): bool => 2 === substr_count($process->getOutput(), 'started in project')); self::assertStringContainsString('The project changed; restarting.', $process->getErrorOutput()); + self::assertSame(2, substr_count( + (string) file_get_contents($this->directory . '/composer-arguments'), + $this->project . " dump-autoload --no-interaction --no-dev\n", + )); $process->signal(SIGTERM); $process->wait(); @@ -242,6 +253,7 @@ public function testRunWithWatchWaitsForAChangeAfterTheApplicationExits(): void 'fail', ], null, [ 'SKERNEL_HOME' => $this->directory . '/home', + 'PATH' => $this->path(), ], null, 60); $process->start(); @@ -259,6 +271,79 @@ public function testRunWithWatchWaitsForAChangeAfterTheApplicationExits(): void } } + public function testBuildRegeneratesTheAutoloaderWithTheComposerItFinds(): void + { + self::assertSame([ + 'Build artifacts written to ' . $this->project . '/vendor/.cache.', + ], $this->skernel('build --php=' . $this->version . ' -d ' . escapeshellarg($this->project), $status)); + self::assertSame(0, $status); + self::assertStringEqualsFile( + $this->directory . '/composer-arguments', + $this->project . " dump-autoload --no-interaction --no-dev\n", + ); + } + + public function testBuildAndRunStopWhenComposerFails(): void + { + $this->composer('exit 4'); + + foreach ([ + 'build', + 'run', + ] as $command) { + $output = $this->skernel( + $command . ' --php=' . $this->version . ' -d ' . escapeshellarg($this->project), + $status, + '2>&1', + ); + + self::assertSame(1, $status); + self::assertStringContainsString( + 'RegeneratingtheComposerautoloaderfailedwithexitstatus4.', + (string) preg_replace('/\s+/', '', implode('', $output)), + ); + self::assertStringNotContainsString('started in project', implode('', $output)); + } + + self::assertFileDoesNotExist($this->project . '/vendor/.cache/build.json'); + } + + public function testRunWithWatchWaitsForAChangeWhenComposerFailsOnARebuild(): void + { + $process = new Process([ + PHP_BINARY, + dirname(__DIR__, 2) . '/bin/bootstrap.php', + 'run', + '--watch', + '--php=' . $this->version, + '-d', + $this->project, + 'sleep', + ], null, [ + 'SKERNEL_HOME' => $this->directory . '/home', + 'PATH' => $this->path(), + ], null, 60); + $process->start(); + + try { + $this->waitFor(static fn(): bool => str_contains($process->getOutput(), 'started in project with sleep')); + + $this->composer('exit 4'); + $this->write($this->project . '/src/App.php', 'waitFor(static fn(): bool => str_contains( + $process->getErrorOutput(), + 'Regenerating the Composer autoloader failed with exit status 4. Waiting for a change.', + )); + self::assertTrue($process->isRunning()); + + $this->composer(''); + $this->write($this->project . '/src/Fix.php', 'waitFor(static fn(): bool => 2 === substr_count($process->getOutput(), 'started in project')); + } finally { + $process->stop(1); + } + } + public function testNewCreatesAProjectFromTheSkeletonWithComposer(): void { $this->write( @@ -340,11 +425,34 @@ public function testSelfUpdateRefusesToReplaceASourceCheckout(): void ); } + /** + * Writes the stand-in Composer that commands find first in PATH: it reports a Composer 2 version, records its + * working directory and arguments otherwise, and then runs the given shell code. + */ + private function composer(string $code): void + { + $this->write($this->directory . '/path/composer', implode("\n", [ + '#!/bin/sh', + 'if [ "$1" = --version ]; then echo "Composer version 2.10.3"; exit 0; fi', + 'echo "$PWD $*" >> ' . escapeshellarg($this->directory . '/composer-arguments'), + $code, + '', + ])); + self::assertTrue(chmod($this->directory . '/path/composer', 0o755)); + } + + /** The PATH in which commands find the stand-in Composer first */ + private function path(): string + { + return $this->directory . '/path:' . getenv('PATH'); + } + /** @return list */ private function skernel(string $arguments, ?int &$status, string $redirection = '2>/dev/null'): array { exec( - 'SKERNEL_HOME=' . escapeshellarg($this->directory . '/home') . ' ' + 'PATH=' . escapeshellarg($this->path()) + . ' SKERNEL_HOME=' . escapeshellarg($this->directory . '/home') . ' ' . escapeshellarg(PHP_BINARY) . ' ' . escapeshellarg(dirname(__DIR__, 2) . '/bin/bootstrap.php') . ' ' . $arguments . ' ' . $redirection, $output, diff --git a/tests/Unit/Build/ProjectBuilderTest.php b/tests/Unit/Build/ProjectBuilderTest.php index 74f25d7..0698b01 100644 --- a/tests/Unit/Build/ProjectBuilderTest.php +++ b/tests/Unit/Build/ProjectBuilderTest.php @@ -15,8 +15,12 @@ use Symfony\Component\Console\Exception\RuntimeException; use Symfony\Component\Console\Output\BufferedOutput; -use function dirname; +use function chmod; +use function escapeshellarg; use function file_put_contents; +use function implode; +use function is_dir; +use function json_encode; use function mkdir; use function realpath; use function sprintf; @@ -85,30 +89,69 @@ final class ApplicationBuild PHP; - private ?string $directory = null; + /** Loads only the classes in the class map that the stand-in Composer writes, as an authoritative class map does */ + private const string AUTHORITATIVE_LOADER = <<<'PHP' + namespace { + require __DIR__ . '/build-api.php'; + + $classMap = is_file(__DIR__ . '/composer/classmap.php') ? require __DIR__ . '/composer/classmap.php' : []; + + spl_autoload_register(static function (string $class) use ($classMap): void { + if (isset($classMap[$class])) { + require $classMap[$class]; + } + }); + } + + PHP; + + /** Maps every class under `src/` of the project in the working directory, as Composer's dump-autoload would */ + private const string DUMP = <<<'PHP' + directory = (string) realpath($directory); + $this->project = $this->directory . '/project'; + self::assertNotFalse(file_put_contents($this->directory . '/dump.php', self::DUMP)); + $this->composer('exec ' . escapeshellarg(PHP_BINARY) . ' ' . escapeshellarg($this->directory . '/dump.php')); + } protected function tearDown(): void { - if (null !== $this->directory) { - Directory::remove($this->directory); - } + Directory::remove($this->directory); } public function testRejectsAPhpBinaryThatIsNotFound(): void { - $root = dirname(__DIR__, 3); + $project = $this->project(true, true); $output = new BufferedOutput(); $this->expectException(RuntimeException::class); $this->expectExceptionMessageIs('The PHP binary skernel-missing-php was not found.'); try { - $this->builder()->build( - new Project($root, $root . '/vendor', null, null), - 'skernel-missing-php', - '1.0.0', - $output, - ); + $this->builder()->build($project, 'skernel-missing-php', $this->command(), '1.0.0', $output); } finally { self::assertSame('', $output->fetch()); } @@ -118,7 +161,7 @@ public function testGeneratesTheArtifactsAndPublishesTheBuildEventAfterCreatingT { $output = new BufferedOutput(); $project = $this->project(true, true); - $this->builder()->build($project, PHP_BINARY, '1.2.3', $output); + $this->builder()->build($project, PHP_BINARY, $this->command(), '1.2.3', $output); self::assertSame("container\ndispatched SuperKernel\\Event\\ApplicationBuild\n", $output->fetch()); self::assertStringEqualsFile($project->vendorDirectory . '/.cache/build.json', '1.2.3'); @@ -127,7 +170,7 @@ public function testGeneratesTheArtifactsAndPublishesTheBuildEventAfterCreatingT public function testStampsDevWhenTheRunningSkernelHasNoVersion(): void { $project = $this->project(true, true); - $this->builder()->build($project, PHP_BINARY, 'UNKNOWN', new BufferedOutput()); + $this->builder()->build($project, PHP_BINARY, $this->command(), 'UNKNOWN', new BufferedOutput()); self::assertStringEqualsFile($project->vendorDirectory . '/.cache/build.json', 'dev'); } @@ -135,7 +178,7 @@ public function testStampsDevWhenTheRunningSkernelHasNoVersion(): void public function testSkipsTheBuildEventWithoutADispatcher(): void { $output = new BufferedOutput(); - $this->builder()->build($this->project(true, false), PHP_BINARY, '1.0.0', $output); + $this->builder()->build($this->project(true, false), PHP_BINARY, $this->command(), '1.0.0', $output); self::assertSame("container\n", $output->fetch()); } @@ -143,7 +186,7 @@ public function testSkipsTheBuildEventWithoutADispatcher(): void public function testSkipsTheBuildEventWhenTheProjectDoesNotDeclareIt(): void { $output = new BufferedOutput(); - $this->builder()->build($this->project(false, true), PHP_BINARY, '1.0.0', $output); + $this->builder()->build($this->project(false, true), PHP_BINARY, $this->command(), '1.0.0', $output); self::assertSame("container\n", $output->fetch()); } @@ -155,7 +198,7 @@ public function testListsSkippedClassesAfterTheBuildAndSkipsAClassThatEndsThePro $this->source('Fatal', '#[\Attribute] final class Fatal implements \Countable {}'); $this->source('Kept', '#[\Attribute] final class Kept {}'); $output = new BufferedOutput(); - $this->builder()->build($project, PHP_BINARY, '1.0.0', $output); + $this->builder()->build($project, PHP_BINARY, $this->command(), '1.0.0', $output); self::assertSame( "container\n" @@ -167,13 +210,58 @@ public function testListsSkippedClassesAfterTheBuildAndSkipsAClassThatEndsThePro ); } + public function testRegeneratesTheComposerAutoloaderBeforeCollectingAttributes(): void + { + $project = $this->project(false, false, authoritative: true); + $this->source('Added', '#[\Attribute] final class Added {}'); + $output = new BufferedOutput(); + $this->builder()->build($project, PHP_BINARY, $this->command(), '1.0.0', $output); + + self::assertSame("container\n", $output->fetch()); + self::assertStringEqualsFile( + $this->directory . '/composer-arguments', + $project->directory . " dump-autoload --no-interaction --no-dev\n", + ); + } + + public function testKeepsTheDevelopmentAutoloadRulesOfADevelopmentInstallation(): void + { + $project = $this->project(false, false, dev: true); + $this->builder()->build($project, PHP_BINARY, $this->command(), '1.0.0', new BufferedOutput()); + + self::assertStringEqualsFile( + $this->directory . '/composer-arguments', + $project->directory . " dump-autoload --no-interaction --dev\n", + ); + } + + public function testStopsBeforeGeneratingWhenComposerFails(): void + { + $project = $this->project(true, true); + $this->composer('echo "composer error" >&2; exit 3'); + $output = new BufferedOutput(); + + try { + $this->builder()->build($project, PHP_BINARY, $this->command(), '1.0.0', $output); + self::fail('Built a project whose autoloader could not be regenerated.'); + } catch (RuntimeException $exception) { + self::assertSame( + 'Regenerating the Composer autoloader failed with exit status 3.', + $exception->getMessage(), + ); + } + + self::assertSame("composer error\n", $output->fetch()); + self::assertFileDoesNotExist($project->vendorDirectory . '/.cache/build.json'); + } + public function testRejectsAProjectWithoutTheMetadataPublisher(): void { $project = $this->project(true, true, false); $output = new BufferedOutput(); try { - $this->builder()->build($project, PHP_BINARY, '1.0.0', $output); + $this->builder()->build($project, PHP_BINARY, $this->command(), '1.0.0', $output); self::fail('Built a project without super-kernel/metadata.'); } catch (RuntimeException $exception) { self::assertStringStartsWith( @@ -190,33 +278,70 @@ private function builder(): ProjectBuilder return new ProjectBuilder(new MetadataGenerator(new GeneratorLocator(new Home()))); } + /** @return non-empty-list The command that runs the stand-in Composer */ + private function command(): array + { + return [ + $this->directory . '/bin/composer', + ]; + } + + /** + * Writes the stand-in Composer, which records its working directory and arguments and then runs the given shell + * code. + */ + private function composer(string $code): void + { + if (!is_dir($this->directory . '/bin')) { + self::assertTrue(mkdir($this->directory . '/bin', 0o700)); + } + + self::assertNotFalse(file_put_contents($this->directory . '/bin/composer', implode("\n", [ + '#!/bin/sh', + 'echo "$PWD $*" >> ' . escapeshellarg($this->directory . '/composer-arguments'), + $code, + '', + ]))); + self::assertTrue(chmod($this->directory . '/bin/composer', 0o755)); + } + /** * Writes a temporary project named acme/app, whose autoloader declares a recording container in place of - * SuperKernel's and maps `App\` to `src/`. + * SuperKernel's and maps `App\` to `src/`, or with an authoritative class map loads only the classes that the + * stand-in Composer mapped. */ - private function project(bool $event, bool $dispatcher, bool $metadata = true): Project + private function project( + bool $event, + bool $dispatcher, + bool $metadata = true, + bool $authoritative = false, + bool $dev = false, + ): Project { - $directory = tempnam(sys_get_temp_dir(), 'skernel-project-'); - self::assertIsString($directory); - self::assertTrue(unlink($directory) && mkdir($directory . '/src', 0o700, true)); - $directory = (string) realpath($directory); - $this->directory = $directory; + self::assertTrue(mkdir($this->project . '/src', 0o700, true)); self::assertNotFalse(file_put_contents( - $directory . '/composer.json', + $this->project . '/composer.json', '{"name": "acme/app", "autoload": {"psr-4": {"App\\\\": "src/"}}}', )); - BuildApiStandIn::write($directory, $directory . '/vendor'); + BuildApiStandIn::write($this->project, $this->project . '/vendor'); + self::assertNotFalse(file_put_contents( + $this->project . '/vendor/composer/installed.json', + (string) json_encode([ + 'packages' => [], + 'dev' => $dev, + ]), + )); $autoload = "project . '/vendor/autoload.php', $autoload)); - return new Project($directory, $directory . '/vendor', 'acme/app', null); + return new Project($this->project, $this->project . '/vendor', 'acme/app', null); } private function source(string $class, string $declaration): void { self::assertNotFalse(file_put_contents( - $this->directory . '/src/' . $class . '.php', + $this->project . '/src/' . $class . '.php', "composer(''); + + self::assertSame(Command::SUCCESS, $this->new('1.0.0-alpha.3')); + self::assertStringEqualsFile( + $this->home . '/arguments', + 'create-project super-kernel/skeleton ' . $this->home . '/my-app 1.0.0-alpha.3 --no-interaction' + . " --no-progress --ignore-platform-reqs\n", + ); + } + + /** @throws Exception If Console command initialization or execution fails */ + public function testCreatesTheSkeletonFromTheDevelopmentLineWithoutAVersion(): void + { + $this->composer(''); + + self::assertSame(Command::SUCCESS, $this->new(null)); + self::assertStringEqualsFile( + $this->home . '/arguments', + 'create-project super-kernel/skeleton ' . $this->home . '/my-app --stability=dev --no-interaction' + . " --no-progress --ignore-platform-reqs\n", + ); + } + + /** @throws Exception If Console command initialization or execution fails */ + public function testSaysWhatToDoWhenTheSkeletonOfItsVersionDoesNotExist(): void + { + $this->composer('exit 1'); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessageIs( + 'Creating the project failed with exit status 1. This SKERNEL creates super-kernel/skeleton 1.0.0-alpha.3;' + . ' if Composer could not find that version, run skernel self-update and create the project again.', + ); + + $this->new('1.0.0-alpha.3'); + } + + /** @throws Exception If Console command initialization or execution fails */ + public function testReportsAFailedComposerWithoutAVersion(): void + { + $this->composer('exit 1'); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessageIs('Creating the project failed with exit status 1.'); + + $this->new(null); + } + + /** Writes a stand-in Composer that records its arguments and then runs the given shell code */ + private function composer(string $code): void + { + $this->executable( + $this->home . '/bin/composer', + 'echo "$*" > ' . escapeshellarg($this->home . '/arguments') . "\n" . $code, + ); + } + + /** + * Runs `new my-app` in the temporary home with the stand-in Composer, in an application with the given version. + * + * @return int The exit status + * + * @throws Exception If Console command initialization or execution fails + */ + private function new(?string $version): int + { + $application = null === $version ? new Application('SKERNEL') : new Application('SKERNEL', $version); + $application->setAutoExit(false); + $application->setCatchExceptions(false); + $application->getDefinition()->addOption(new InputOption('working-dir', 'd', InputOption::VALUE_REQUIRED)); + // The command's name comes from its attribute, which SuperKernel's command registration reads. + $command = new NewCommand(new ComposerLocator(new ManagedComposer( + new Home(), + new PhpReleases(new ReleaseIndex(new DailyCache(new Home()))), + new RuntimeProvider( + new Home(), + new StaticPhpCli(new Home(), new ZigMirrors(new DailyCache(new Home()))), + new ExtensionVersions(new DailyCache(new Home())), + ), + ))); + $command->setName('new'); + $application->addCommand($command); + + return new ApplicationTester($application)->run([ + 'command' => 'new', + 'directory' => 'my-app', + '--working-dir' => $this->home, + '--composer' => $this->home . '/bin/composer', + ]); + } +} From 18ab720f1fafa5e4d83b842517d596c7198705ae Mon Sep 17 00:00:00 2001 From: wheakerd Date: Tue, 10 Nov 2026 18:26:50 -0800 Subject: [PATCH 2/2] Lock the components with their restructured READMEs Refresh the lock to the components' current commits, which changed only their READMEs. Co-Authored-By: Claude --- composer.lock | 104 +++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/composer.lock b/composer.lock index 54970b2..7564940 100644 --- a/composer.lock +++ b/composer.lock @@ -347,12 +347,12 @@ "source": { "type": "git", "url": "https://github.com/super-kernel/annotator.git", - "reference": "4e56d3f9f1d9a540bcc9ab3cd9ba17d5188f2e50" + "reference": "02bf8c73666290334032c340aa9199d1e5af76a8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/super-kernel/annotator/zipball/4e56d3f9f1d9a540bcc9ab3cd9ba17d5188f2e50", - "reference": "4e56d3f9f1d9a540bcc9ab3cd9ba17d5188f2e50", + "url": "https://api.github.com/repos/super-kernel/annotator/zipball/02bf8c73666290334032c340aa9199d1e5af76a8", + "reference": "02bf8c73666290334032c340aa9199d1e5af76a8", "shasum": "" }, "require": { @@ -379,7 +379,7 @@ "issues": "https://github.com/super-kernel/annotator/issues", "source": "https://github.com/super-kernel/annotator/tree/main" }, - "time": "2026-11-10T08:54:44+00:00" + "time": "2026-11-11T02:19:15+00:00" }, { "name": "super-kernel/attribute", @@ -387,12 +387,12 @@ "source": { "type": "git", "url": "https://github.com/super-kernel/attribute.git", - "reference": "bed9e4dd9323303fb0a29d445419c9c31271156b" + "reference": "9672f70903fc87f44fa8112c4dfa616ec0c4dadc" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/super-kernel/attribute/zipball/bed9e4dd9323303fb0a29d445419c9c31271156b", - "reference": "bed9e4dd9323303fb0a29d445419c9c31271156b", + "url": "https://api.github.com/repos/super-kernel/attribute/zipball/9672f70903fc87f44fa8112c4dfa616ec0c4dadc", + "reference": "9672f70903fc87f44fa8112c4dfa616ec0c4dadc", "shasum": "" }, "require": { @@ -421,7 +421,7 @@ "issues": "https://github.com/super-kernel/attribute/issues", "source": "https://github.com/super-kernel/attribute/tree/main" }, - "time": "2026-11-01T07:38:00+00:00" + "time": "2026-11-11T02:19:26+00:00" }, { "name": "super-kernel/command", @@ -429,12 +429,12 @@ "source": { "type": "git", "url": "https://github.com/super-kernel/command.git", - "reference": "c9b85e75d5d1491f8afe65e9946c4c60f9b86090" + "reference": "7e8175d0100c37ff62cea90314a56faad181b95b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/super-kernel/command/zipball/c9b85e75d5d1491f8afe65e9946c4c60f9b86090", - "reference": "c9b85e75d5d1491f8afe65e9946c4c60f9b86090", + "url": "https://api.github.com/repos/super-kernel/command/zipball/7e8175d0100c37ff62cea90314a56faad181b95b", + "reference": "7e8175d0100c37ff62cea90314a56faad181b95b", "shasum": "" }, "require": { @@ -466,7 +466,7 @@ "issues": "https://github.com/super-kernel/command/issues", "source": "https://github.com/super-kernel/command/tree/main" }, - "time": "2026-11-10T05:05:58+00:00" + "time": "2026-11-11T02:19:36+00:00" }, { "name": "super-kernel/context", @@ -474,12 +474,12 @@ "source": { "type": "git", "url": "https://github.com/super-kernel/context.git", - "reference": "dac168cf0c98246f790a8b344d5cd72ba59d9d31" + "reference": "fe42f4bef5d91b140ba67d6c22af8a576c5c93b2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/super-kernel/context/zipball/dac168cf0c98246f790a8b344d5cd72ba59d9d31", - "reference": "dac168cf0c98246f790a8b344d5cd72ba59d9d31", + "url": "https://api.github.com/repos/super-kernel/context/zipball/fe42f4bef5d91b140ba67d6c22af8a576c5c93b2", + "reference": "fe42f4bef5d91b140ba67d6c22af8a576c5c93b2", "shasum": "" }, "require": { @@ -513,7 +513,7 @@ "issues": "https://github.com/super-kernel/context/issues", "source": "https://github.com/super-kernel/context/tree/main" }, - "time": "2026-11-01T07:39:16+00:00" + "time": "2026-11-11T02:19:51+00:00" }, { "name": "super-kernel/contract", @@ -521,12 +521,12 @@ "source": { "type": "git", "url": "https://github.com/super-kernel/contract.git", - "reference": "e17b6bfe984a5f17e8844d837da6921c40d2c3c1" + "reference": "a6bd84dc44a446ca05d52b2781a70f2afb45ca52" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/super-kernel/contract/zipball/e17b6bfe984a5f17e8844d837da6921c40d2c3c1", - "reference": "e17b6bfe984a5f17e8844d837da6921c40d2c3c1", + "url": "https://api.github.com/repos/super-kernel/contract/zipball/a6bd84dc44a446ca05d52b2781a70f2afb45ca52", + "reference": "a6bd84dc44a446ca05d52b2781a70f2afb45ca52", "shasum": "" }, "require": { @@ -555,7 +555,7 @@ "issues": "https://github.com/super-kernel/contract/issues", "source": "https://github.com/super-kernel/contract/tree/main" }, - "time": "2026-11-01T07:39:29+00:00" + "time": "2026-11-11T02:19:56+00:00" }, { "name": "super-kernel/di", @@ -563,12 +563,12 @@ "source": { "type": "git", "url": "https://github.com/super-kernel/di.git", - "reference": "ed1a880310977dc5b6de748d304cba6e7ed734b8" + "reference": "0a41eb1fd6a7987e1887fa98578dfefc7efd11df" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/super-kernel/di/zipball/ed1a880310977dc5b6de748d304cba6e7ed734b8", - "reference": "ed1a880310977dc5b6de748d304cba6e7ed734b8", + "url": "https://api.github.com/repos/super-kernel/di/zipball/0a41eb1fd6a7987e1887fa98578dfefc7efd11df", + "reference": "0a41eb1fd6a7987e1887fa98578dfefc7efd11df", "shasum": "" }, "require": { @@ -603,7 +603,7 @@ "issues": "https://github.com/super-kernel/di/issues", "source": "https://github.com/super-kernel/di/tree/main" }, - "time": "2026-11-10T08:51:03+00:00" + "time": "2026-11-11T02:20:01+00:00" }, { "name": "super-kernel/event", @@ -611,12 +611,12 @@ "source": { "type": "git", "url": "https://github.com/super-kernel/event.git", - "reference": "4f53158d495dc6782ee1f528f36593e79f51259e" + "reference": "fc6c5b9e9371ef8a18626b80b2fb36c71d1d1a8a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/super-kernel/event/zipball/4f53158d495dc6782ee1f528f36593e79f51259e", - "reference": "4f53158d495dc6782ee1f528f36593e79f51259e", + "url": "https://api.github.com/repos/super-kernel/event/zipball/fc6c5b9e9371ef8a18626b80b2fb36c71d1d1a8a", + "reference": "fc6c5b9e9371ef8a18626b80b2fb36c71d1d1a8a", "shasum": "" }, "require": { @@ -643,7 +643,7 @@ "issues": "https://github.com/super-kernel/event/issues", "source": "https://github.com/super-kernel/event/tree/main" }, - "time": "2026-11-10T05:07:12+00:00" + "time": "2026-11-11T02:20:12+00:00" }, { "name": "super-kernel/event-dispatcher", @@ -651,12 +651,12 @@ "source": { "type": "git", "url": "https://github.com/super-kernel/event-dispatcher.git", - "reference": "0b5287e9cbb71a32fbb82e2b3e18b802ecbc3263" + "reference": "ae16d5da4d6df809813958a7285162f22035c5c7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/super-kernel/event-dispatcher/zipball/0b5287e9cbb71a32fbb82e2b3e18b802ecbc3263", - "reference": "0b5287e9cbb71a32fbb82e2b3e18b802ecbc3263", + "url": "https://api.github.com/repos/super-kernel/event-dispatcher/zipball/ae16d5da4d6df809813958a7285162f22035c5c7", + "reference": "ae16d5da4d6df809813958a7285162f22035c5c7", "shasum": "" }, "require": { @@ -690,7 +690,7 @@ "issues": "https://github.com/super-kernel/event-dispatcher/issues", "source": "https://github.com/super-kernel/event-dispatcher/tree/main" }, - "time": "2026-11-10T05:07:27+00:00" + "time": "2026-11-11T02:20:18+00:00" }, { "name": "super-kernel/exception-dispatcher", @@ -698,12 +698,12 @@ "source": { "type": "git", "url": "https://github.com/super-kernel/exception-dispatcher.git", - "reference": "86209600307121e72bc140de330f23685a1b944a" + "reference": "fd6e330e8b4358d2d8c8ff97448726cca913dd1e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/super-kernel/exception-dispatcher/zipball/86209600307121e72bc140de330f23685a1b944a", - "reference": "86209600307121e72bc140de330f23685a1b944a", + "url": "https://api.github.com/repos/super-kernel/exception-dispatcher/zipball/fd6e330e8b4358d2d8c8ff97448726cca913dd1e", + "reference": "fd6e330e8b4358d2d8c8ff97448726cca913dd1e", "shasum": "" }, "require": { @@ -733,7 +733,7 @@ "issues": "https://github.com/super-kernel/exception-dispatcher/issues", "source": "https://github.com/super-kernel/exception-dispatcher/tree/main" }, - "time": "2026-11-10T05:07:42+00:00" + "time": "2026-11-11T02:20:25+00:00" }, { "name": "super-kernel/framework", @@ -741,12 +741,12 @@ "source": { "type": "git", "url": "https://github.com/super-kernel/framework.git", - "reference": "accedabbe9fc1326fdee3e7d88341e7ff3c9ad40" + "reference": "166dbb22f664c4044ae477d5ae10e8d427e8bbf5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/super-kernel/framework/zipball/accedabbe9fc1326fdee3e7d88341e7ff3c9ad40", - "reference": "accedabbe9fc1326fdee3e7d88341e7ff3c9ad40", + "url": "https://api.github.com/repos/super-kernel/framework/zipball/166dbb22f664c4044ae477d5ae10e8d427e8bbf5", + "reference": "166dbb22f664c4044ae477d5ae10e8d427e8bbf5", "shasum": "" }, "require": { @@ -778,7 +778,7 @@ "issues": "https://github.com/super-kernel/framework/issues", "source": "https://github.com/super-kernel/framework/tree/main" }, - "time": "2026-11-10T05:07:54+00:00" + "time": "2026-11-11T02:20:36+00:00" }, { "name": "super-kernel/logger", @@ -786,12 +786,12 @@ "source": { "type": "git", "url": "https://github.com/super-kernel/logger.git", - "reference": "fc2e986d2b00c15dbc631fc4bcc25419a79fce4a" + "reference": "e8b69a59a7c092d6d6e7caf2d6791b259dcf2894" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/super-kernel/logger/zipball/fc2e986d2b00c15dbc631fc4bcc25419a79fce4a", - "reference": "fc2e986d2b00c15dbc631fc4bcc25419a79fce4a", + "url": "https://api.github.com/repos/super-kernel/logger/zipball/e8b69a59a7c092d6d6e7caf2d6791b259dcf2894", + "reference": "e8b69a59a7c092d6d6e7caf2d6791b259dcf2894", "shasum": "" }, "require": { @@ -821,7 +821,7 @@ "issues": "https://github.com/super-kernel/logger/issues", "source": "https://github.com/super-kernel/logger/tree/main" }, - "time": "2026-11-10T08:55:04+00:00" + "time": "2026-11-11T02:20:53+00:00" }, { "name": "super-kernel/metadata", @@ -829,12 +829,12 @@ "source": { "type": "git", "url": "https://github.com/super-kernel/metadata.git", - "reference": "9d8dda4b7a9c230493adc837cef8ed91fd9acdc9" + "reference": "cca9f143d3884c9eb853c66226fdd328c329efcf" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/super-kernel/metadata/zipball/9d8dda4b7a9c230493adc837cef8ed91fd9acdc9", - "reference": "9d8dda4b7a9c230493adc837cef8ed91fd9acdc9", + "url": "https://api.github.com/repos/super-kernel/metadata/zipball/cca9f143d3884c9eb853c66226fdd328c329efcf", + "reference": "cca9f143d3884c9eb853c66226fdd328c329efcf", "shasum": "" }, "require": { @@ -862,7 +862,7 @@ "issues": "https://github.com/super-kernel/metadata/issues", "source": "https://github.com/super-kernel/metadata/tree/main" }, - "time": "2026-11-10T08:41:07+00:00" + "time": "2026-11-11T02:20:58+00:00" }, { "name": "super-kernel/reflector", @@ -870,12 +870,12 @@ "source": { "type": "git", "url": "https://github.com/super-kernel/reflector.git", - "reference": "18e9553f71bf81784d9c99186bb90136b8c9a5b2" + "reference": "31b9a0700bfb8f0780ba60e58f23f4732fd546c0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/super-kernel/reflector/zipball/18e9553f71bf81784d9c99186bb90136b8c9a5b2", - "reference": "18e9553f71bf81784d9c99186bb90136b8c9a5b2", + "url": "https://api.github.com/repos/super-kernel/reflector/zipball/31b9a0700bfb8f0780ba60e58f23f4732fd546c0", + "reference": "31b9a0700bfb8f0780ba60e58f23f4732fd546c0", "shasum": "" }, "require": { @@ -904,7 +904,7 @@ "issues": "https://github.com/super-kernel/reflector/issues", "source": "https://github.com/super-kernel/reflector/tree/main" }, - "time": "2026-11-10T05:09:31+00:00" + "time": "2026-11-11T02:21:29+00:00" }, { "name": "symfony/console",