diff --git a/.sonarcloud.properties b/.sonarcloud.properties new file mode 100644 index 0000000..616e68f --- /dev/null +++ b/.sonarcloud.properties @@ -0,0 +1,4 @@ +# Test files are tests, not product code: they are not measured for duplication. +sonar.sources=. +sonar.tests=. +sonar.test.inclusions=tests/** diff --git a/CLAUDE.md b/CLAUDE.md index 84c5f9d..66642f0 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -17,7 +17,7 @@ All read from the root `composer.json` `extra` section. Constants in `Installer. | Key | Constant | Default | |---|---|---| | `module-dir` | `DEFAULT_ROOT` | `modules` | -| `module-type` | `DEFAULT_MODULE_TYPE` | `laravel-module` | +| `module-type` | `DEFAULT_MODULE_TYPES` | `saucebase-module`, `laravel-module` | | `module-exclude-dirs` | `DEFAULT_EXCLUDED_DIRS` | `['.github', '.git']` | | `module-update-strategy` | `DEFAULT_UPDATE_STRATEGY` | `merge` | | `module-delete-on-remove` | *(no constant)* | `false` | @@ -26,7 +26,7 @@ The Saucebase app (`saucebase/composer.json`) uses `module-type: saucebase-modul ## Core Behaviours -**Install** (`install()`): Downloads the package, removes excluded directories, and deploys frontend framework files if `frontend.json` is present. Skips entirely for path-repository packages, locally tracked dirs, and any install path that already contains files (copy-and-own: a committed `modules/` dir must survive `composer install` on a fresh deploy). +**Install** (`install()`): Downloads the package, removes excluded directories, and deploys frontend framework files if `saucebase.json` is present. Skips entirely for path-repository packages, locally tracked dirs, and any install path that already contains files (copy-and-own: a committed `modules/` dir must survive `composer install` on a fresh deploy). **Update** (`update()`): Two strategies: - `merge` (default) — stashes the user's copy, downloads the new version, downloads the original version as a base, runs a 3-way `git merge-file`. Conflicts are staged in the git index (stages 1/2/3) so they appear in `git status`. @@ -38,7 +38,7 @@ Skips for path-repository packages. 2. `module-delete-on-remove` not set → skip by default (folder stays, package removed from Composer tracking). Only calls `parent::uninstall()` (which deletes the folder) when `module-delete-on-remove: true` is explicitly set. -**Frontend framework support**: Reads `frontend.json` from the project root. Copies files from the matching framework subdirectory (`vue/`, `react/`, or `svelte/`) flat into `resources/js/` and removes all framework subdirs. Silent-skips when `frontend.json` is absent or `"dev": true`. +**Frontend framework support**: Reads `saucebase.json` from the project root. Copies files from the matching framework subdirectory (`vue/` or `react/`) flat into `resources/js/` and removes all framework subdirs. Silent-skips when `saucebase.json` is absent or `"contributor": true`. ## Development Commands diff --git a/README.md b/README.md index 9f96af6..1a1a7ac 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,12 @@ A Composer plugin that installs Saucebase modules into the correct directory. It ## How It Works -- Registers a Composer installer for a configurable module package type (defaults to `laravel-module`; the Saucebase app uses `saucebase-module`). +- Registers a Composer installer for a configurable module package type (defaults to `saucebase-module` and `laravel-module`). - Installs each module inside a configurable modules directory (`modules/` by default, lowercase). - Derives the directory name from the package slug: `saucebase/example-module` → `modules/example-module`. - On `composer update`, **merges** the new version into your existing module directory by default, preserving local edits. Conflicts are staged in the git index for review. - Removes configurable directories (e.g. `.github`, `.git`) after each install or update. -- Deploys frontend framework files (Vue / React / Svelte) based on the project's `frontend.json` selection. +- Deploys frontend framework files (Vue / React) based on the project's `saucebase.json` selection. - Protects path-repository modules (symlinks or local `.git` clones) from being overwritten or deleted. - Skips folder deletion by default when a module is removed via `composer remove` — the directory stays on disk for manual review. @@ -40,7 +40,7 @@ All options live in the `extra` section of the **root** `composer.json`. Every k | Key | Default | Description | |---|---|---| -| `module-type` | `laravel-module` | Composer package type the installer handles. | +| `module-type` | `saucebase-module` and `laravel-module` | Composer package type the installer handles. Setting it replaces both defaults with that one type. | | `module-dir` | `modules` | Directory modules are installed into. | | `module-exclude-dirs` | `[".github", ".git"]` | Directories removed from each module after install/update. | | `module-update-strategy` | `merge` | `merge` preserves local edits; `overwrite` replaces entirely. | @@ -59,12 +59,12 @@ Example (the Saucebase app configuration): ## Configuring the Module Type -The installer registers the `laravel-module` package type by default. Override it in the root package `extra` section: +The installer registers the `saucebase-module` and `laravel-module` package types by default. Override it in the root package `extra` section: ```json { "extra": { - "module-type": "saucebase-module" + "module-type": "acme-module" } } ``` @@ -167,7 +167,7 @@ To restore the old behaviour and delete the folder automatically: When a module ships JavaScript resources for multiple frameworks (Vue, React, or Svelte), the installer copies only the files for the active framework flat into `resources/js/` and removes the other framework subdirectories. -The active framework is read from `frontend.json` in the project root: +The active framework is read from `saucebase.json` in the project root: ```json { @@ -175,7 +175,7 @@ The active framework is read from `frontend.json` in the project root: } ``` -Valid values are `vue`, `react`, and `svelte`. If `frontend.json` is absent, unreadable, or has `"dev": true`, no framework files are copied and all framework subdirectories are left in place. +Valid values are `vue` and `react`. If `saucebase.json` is absent, unreadable, or has `"contributor": true`, no framework files are copied and all framework subdirectories are left in place. Module packages should ship framework files under subdirectories matching the framework name: diff --git a/src/Installer.php b/src/Installer.php index 50269dc..a780a61 100644 --- a/src/Installer.php +++ b/src/Installer.php @@ -22,13 +22,14 @@ class Installer extends LibraryInstaller { const DEFAULT_ROOT = 'modules'; - const DEFAULT_MODULE_TYPE = 'laravel-module'; + /** Saucebase's own modules, and the ones an application writes. */ + const DEFAULT_MODULE_TYPES = ['saucebase-module', 'laravel-module']; const DEFAULT_EXCLUDED_DIRS = ['.github', '.git']; const DEFAULT_UPDATE_STRATEGY = 'merge'; - const KNOWN_FRAMEWORKS = ['vue', 'react', 'svelte']; + const KNOWN_FRAMEWORKS = ['vue', 'react']; const UPDATE_STRATEGY_MERGE = 'merge'; @@ -85,22 +86,25 @@ protected function getModuleName(PackageInterface $package) public function supports($packageType) { - return $packageType === $this->getSupportedModuleType(); + return in_array($packageType, $this->getSupportedModuleTypes(), true); } - protected function getSupportedModuleType() + /** + * @return list + */ + protected function getSupportedModuleTypes(): array { if (! $this->composer || ! $this->composer->getPackage()) { - return self::DEFAULT_MODULE_TYPE; + return self::DEFAULT_MODULE_TYPES; } $extra = $this->composer->getPackage()->getExtra(); if (! $extra || empty($extra['module-type'])) { - return self::DEFAULT_MODULE_TYPE; + return self::DEFAULT_MODULE_TYPES; } - return $extra['module-type']; + return [$extra['module-type']]; } /** @@ -424,20 +428,20 @@ protected function resolveRegistrationTarget( } /** - * Returns the path to frontend.json. Extracted for testability. + * Returns the path to saucebase.json. Extracted for testability. */ - protected function getFrontendJsonPath(): string + protected function getSaucebaseJsonPath(): string { - return getcwd().'/frontend.json'; + return getcwd().'/saucebase.json'; } /** - * Reads the selected framework from frontend.json. - * Returns null when: file missing, invalid JSON, dev mode active, framework not set, or invalid name. + * Reads the selected framework from saucebase.json. + * Returns null when: file missing, invalid JSON, contributor checkout, framework not set, or invalid name. */ protected function getSelectedFramework(): ?string { - $path = $this->getFrontendJsonPath(); + $path = $this->getSaucebaseJsonPath(); if (! file_exists($path)) { return null; @@ -445,7 +449,7 @@ protected function getSelectedFramework(): ?string $data = json_decode(file_get_contents($path), true); - if (! is_array($data) || ($data['dev'] ?? false)) { + if (! is_array($data) || ($data['contributor'] ?? false)) { return null; } diff --git a/tests/ModuleInstallerTest.php b/tests/ModuleInstallerTest.php index dc2951e..c247805 100644 --- a/tests/ModuleInstallerTest.php +++ b/tests/ModuleInstallerTest.php @@ -99,16 +99,16 @@ public function callResolveRegistrationTarget(PackageInterface $initial, Package // ---- Framework-aware file copying ---- - private string $frontendJsonPath = ''; + private string $saucebaseJsonPath = ''; - public function setFrontendJsonPath(string $path): void + public function setSaucebaseJsonPath(string $path): void { - $this->frontendJsonPath = $path; + $this->saucebaseJsonPath = $path; } - protected function getFrontendJsonPath(): string + protected function getSaucebaseJsonPath(): string { - return $this->frontendJsonPath !== '' ? $this->frontendJsonPath : parent::getFrontendJsonPath(); + return $this->saucebaseJsonPath !== '' ? $this->saucebaseJsonPath : parent::getSaucebaseJsonPath(); } public function callGetSelectedFramework(): ?string @@ -184,6 +184,8 @@ public function test_supports_default_module_type(): void $installer = new TestableInstaller($io, $composer); + // Saucebase's own modules and the ones an application writes, so neither needs setup. + $this->assertTrue($installer->supports('saucebase-module')); $this->assertTrue($installer->supports('laravel-module')); $this->assertFalse($installer->supports('library')); $this->assertFalse($installer->supports('composer-plugin')); @@ -1359,10 +1361,18 @@ public function test_install_downloads_when_existing_module_dir_is_empty(): void // getSelectedFramework // ------------------------------------------------------------------------- - public function test_get_selected_framework_returns_null_when_frontend_json_missing(): void + public function test_reads_the_project_config_from_saucebase_json(): void + { + $installer = new TestableInstaller($this->createStub(IOInterface::class), null); + $path = (new \ReflectionMethod($installer, 'getSaucebaseJsonPath'))->invoke($installer); + + $this->assertSame(getcwd().'/saucebase.json', $path); + } + + public function test_get_selected_framework_returns_null_when_saucebase_json_missing(): void { $installer = new TestableInstaller($this->createStub(IOInterface::class), null); - $installer->setFrontendJsonPath('/nonexistent/path/frontend.json'); + $installer->setSaucebaseJsonPath('/nonexistent/path/saucebase.json'); $this->assertNull($installer->callGetSelectedFramework()); } @@ -1370,18 +1380,18 @@ public function test_get_selected_framework_returns_null_when_frontend_json_miss public function test_get_selected_framework_returns_null_when_framework_key_is_null(): void { $installer = new TestableInstaller($this->createStub(IOInterface::class), null); - $installer->setFrontendJsonPath($this->writeFrontendJson(['framework' => null])); + $installer->setSaucebaseJsonPath($this->writeSaucebaseJson(['framework' => null])); $this->assertNull($installer->callGetSelectedFramework()); } public function test_get_selected_framework_returns_null_when_json_is_invalid(): void { - $path = sys_get_temp_dir().'/frontend-invalid-'.uniqid('', true).'.json'; + $path = sys_get_temp_dir().'/saucebase-invalid-'.uniqid('', true).'.json'; file_put_contents($path, 'not-valid-json{{{'); $installer = new TestableInstaller($this->createStub(IOInterface::class), null); - $installer->setFrontendJsonPath($path); + $installer->setSaucebaseJsonPath($path); $this->assertNull($installer->callGetSelectedFramework()); @@ -1391,7 +1401,7 @@ public function test_get_selected_framework_returns_null_when_json_is_invalid(): public function test_get_selected_framework_returns_null_when_dev_mode_is_true(): void { $installer = new TestableInstaller($this->createStub(IOInterface::class), null); - $installer->setFrontendJsonPath($this->writeFrontendJson(['framework' => 'vue', 'dev' => true])); + $installer->setSaucebaseJsonPath($this->writeSaucebaseJson(['framework' => 'vue', 'contributor' => true])); $this->assertNull($installer->callGetSelectedFramework()); } @@ -1399,7 +1409,7 @@ public function test_get_selected_framework_returns_null_when_dev_mode_is_true() public function test_get_selected_framework_returns_vue_when_set(): void { $installer = new TestableInstaller($this->createStub(IOInterface::class), null); - $installer->setFrontendJsonPath($this->writeFrontendJson(['framework' => 'vue'])); + $installer->setSaucebaseJsonPath($this->writeSaucebaseJson(['framework' => 'vue'])); $this->assertSame('vue', $installer->callGetSelectedFramework()); } @@ -1407,7 +1417,7 @@ public function test_get_selected_framework_returns_vue_when_set(): void public function test_get_selected_framework_returns_react_when_set(): void { $installer = new TestableInstaller($this->createStub(IOInterface::class), null); - $installer->setFrontendJsonPath($this->writeFrontendJson(['framework' => 'react'])); + $installer->setSaucebaseJsonPath($this->writeSaucebaseJson(['framework' => 'react'])); $this->assertSame('react', $installer->callGetSelectedFramework()); } @@ -1417,7 +1427,7 @@ public function test_get_selected_framework_returns_null_for_unknown_framework() $installer = new TestableInstaller($this->createStub(IOInterface::class), null); foreach (['angular', 'solid', '../etc', '/abs', 'Vue', ''] as $unknown) { - $installer->setFrontendJsonPath($this->writeFrontendJson(['framework' => $unknown])); + $installer->setSaucebaseJsonPath($this->writeSaucebaseJson(['framework' => $unknown])); $this->assertNull($installer->callGetSelectedFramework(), "Expected null for framework='$unknown'"); } } @@ -1434,7 +1444,7 @@ public function test_copy_framework_files_silent_skips_when_no_resources_js_dir( mkdir($moduleDir, 0755, true); $installer = $this->makeInstallerWithModuleDir($baseDir); - $installer->setFrontendJsonPath($this->writeFrontendJson(['framework' => 'vue'])); + $installer->setSaucebaseJsonPath($this->writeSaucebaseJson(['framework' => 'vue'])); $pkg = new Package('saucebase/no-js', '1.0.0.0', '1.0.0'); @@ -1456,7 +1466,7 @@ public function test_copy_framework_files_silent_skips_when_framework_is_null(): file_put_contents($jsRoot.'/vue/app.ts', 'content'); $installer = $this->makeInstallerWithModuleDir($baseDir); - $installer->setFrontendJsonPath('/nonexistent/frontend.json'); // framework = null + $installer->setSaucebaseJsonPath('/nonexistent/saucebase.json'); // framework = null $pkg = new Package('saucebase/skip-module', '1.0.0.0', '1.0.0'); @@ -1477,7 +1487,7 @@ public function test_copy_framework_files_hard_fails_when_framework_subdir_missi mkdir($moduleDir.'/resources/js', 0755, true); $installer = $this->makeInstallerWithModuleDir($baseDir); - $installer->setFrontendJsonPath($this->writeFrontendJson(['framework' => 'react'])); + $installer->setSaucebaseJsonPath($this->writeSaucebaseJson(['framework' => 'react'])); $pkg = new Package('saucebase/vue-only', '1.0.0.0', '1.0.0'); @@ -1504,7 +1514,7 @@ public function test_copy_framework_files_flattens_files_and_removes_framework_s file_put_contents($jsRoot.'/react/app.tsx', 'react app'); $installer = $this->makeInstallerWithModuleDir($baseDir); - $installer->setFrontendJsonPath($this->writeFrontendJson(['framework' => 'vue'])); + $installer->setSaucebaseJsonPath($this->writeSaucebaseJson(['framework' => 'vue'])); $pkg = new Package('saucebase/flatten-test', '1.0.0.0', '1.0.0'); @@ -1527,13 +1537,11 @@ public function test_copy_framework_files_removes_all_known_framework_subdirs(): $jsRoot = $moduleDir.'/resources/js'; mkdir($jsRoot.'/vue', 0755, true); mkdir($jsRoot.'/react', 0755, true); - mkdir($jsRoot.'/svelte', 0755, true); file_put_contents($jsRoot.'/vue/app.ts', 'vue app'); file_put_contents($jsRoot.'/react/app.tsx', 'react app'); - file_put_contents($jsRoot.'/svelte/app.svelte', 'svelte app'); $installer = $this->makeInstallerWithModuleDir($baseDir); - $installer->setFrontendJsonPath($this->writeFrontendJson(['framework' => 'vue'])); + $installer->setSaucebaseJsonPath($this->writeSaucebaseJson(['framework' => 'vue'])); $pkg = new Package('saucebase/multi-fw', '1.0.0.0', '1.0.0'); $installer->callCopyFrameworkFiles($pkg); @@ -1541,7 +1549,6 @@ public function test_copy_framework_files_removes_all_known_framework_subdirs(): $this->assertFileExists($jsRoot.'/app.ts'); $this->assertDirectoryDoesNotExist($jsRoot.'/vue'); $this->assertDirectoryDoesNotExist($jsRoot.'/react'); - $this->assertDirectoryDoesNotExist($jsRoot.'/svelte'); (new Filesystem)->remove($moduleDir); } @@ -1574,7 +1581,7 @@ public function test_install_invokes_copy_framework_files_for_non_path_repo(): v { $io = $this->createStub(IOInterface::class); $installer = new TestableInstaller($io, null); - $installer->setFrontendJsonPath('/nonexistent/frontend.json'); // returns null → skips copy + $installer->setSaucebaseJsonPath('/nonexistent/saucebase.json'); // returns null → skips copy $pkg = $this->createStub(PackageInterface::class); $pkg->method('getDistType')->willReturn('zip'); @@ -1597,9 +1604,9 @@ public function test_install_invokes_copy_framework_files_for_non_path_repo(): v // ------------------------------------------------------------------------- /** @param array $data */ - private function writeFrontendJson(array $data): string + private function writeSaucebaseJson(array $data): string { - $path = sys_get_temp_dir().'/frontend-'.uniqid('', true).'.json'; + $path = sys_get_temp_dir().'/saucebase-'.uniqid('', true).'.json'; file_put_contents($path, json_encode($data)); return $path;