diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1683d0f..0f191bc 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -14,6 +14,7 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: '8.3' + extensions: yaml - name: Validate composer.json and composer.lock run: composer validate --strict diff --git a/README.md b/README.md index 8445262..13cff2e 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,8 @@ Install using composer: composer require utopia-php/detector ``` +Requires the `yaml` extension — see [System Requirements](#system-requirements). + Init in your application: ```php =8.0" + "php": ">=8.0", + "ext-json": "*", + "ext-yaml": "*" }, "require-dev": { "phpunit/phpunit": "^9.4", diff --git a/composer.lock b/composer.lock index 5c8a509..04b9ace 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e106153e273a11ae7a40f7074d982de3", + "content-hash": "baa1099760c61325eb4c62ffded763ca", "packages": [], "packages-dev": [ { @@ -1936,7 +1936,9 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=8.0" + "php": ">=8.0", + "ext-json": "*", + "ext-yaml": "*" }, "platform-dev": [], "plugin-api-version": "2.3.0" diff --git a/src/Detection/Framework/Flutter.php b/src/Detection/Framework/Flutter.php index 35349ec..4ce61cd 100644 --- a/src/Detection/Framework/Flutter.php +++ b/src/Detection/Framework/Flutter.php @@ -19,6 +19,14 @@ public function getFiles(): array return ['pubspec.yaml', 'pubspec.lock']; } + /** + * @return array + */ + public function getPackages(): array + { + return ['flutter']; + } + public function getInstallCommand(): string { return ''; diff --git a/src/Detection/Framework/Jaspr.php b/src/Detection/Framework/Jaspr.php new file mode 100644 index 0000000..a9947e5 --- /dev/null +++ b/src/Detection/Framework/Jaspr.php @@ -0,0 +1,67 @@ + + */ + public function getPackages(): array + { + return \array_merge(['jaspr', 'jaspr_builder'], parent::getPackages()); + } + + public function getInstallCommand(): string + { + return 'dart pub get'; + } + + public function getBuildCommand(): string + { + return 'jaspr build'; + } + + public function getOutputDirectory(): string + { + return './build/jaspr'; + } + + /** + * @return array + */ + public function getConfigFiles(): array + { + return ['pubspec.yaml']; + } + + /** + * Reads `jaspr.mode`, which builds a server executable in `server` mode + * and web assets in `static` and `client` mode. + */ + public function getAdapter(string $configContent): string + { + $pubspec = @\yaml_parse($configContent); + $config = \is_array($pubspec) ? ($pubspec['jaspr'] ?? null) : null; + + if (! \is_array($config)) { + return ''; + } + + return match ($config['mode'] ?? null) { + 'server' => 'ssr', + 'static', 'client' => 'static', + default => '', + }; + } +} diff --git a/src/Detection/Rendering/SSR.php b/src/Detection/Rendering/SSR.php index 697aa25..eaefb0a 100644 --- a/src/Detection/Rendering/SSR.php +++ b/src/Detection/Rendering/SSR.php @@ -16,6 +16,7 @@ class SSR extends Rendering 'analog' => ['server/index.mjs'], 'tanstack-start' => ['server/server.js', 'server/index.mjs'], 'flutter' => [], + 'jaspr' => ['app'], 'lynx' => [], ]; diff --git a/src/Detector/Framework.php b/src/Detector/Framework.php index a3a79e5..af0e271 100644 --- a/src/Detector/Framework.php +++ b/src/Detector/Framework.php @@ -11,6 +11,15 @@ class Framework extends Detector public const INPUT_FILE = 'file'; public const INPUT_PACKAGES = 'packages'; + private const DEPENDENCY_SECTIONS = [ + 'dependencies', + 'devDependencies', + 'peerDependencies', + 'optionalDependencies', + 'dev_dependencies', + 'dependency_overrides', + ]; + /** * @var array */ @@ -46,7 +55,7 @@ public function detect(): ?FrameworkDetection $files = array_map(fn ($input) => $input['content'], $files); $packages = array_filter($this->inputs, fn ($input) => $input['type'] === self::INPUT_PACKAGES); - $packages = array_map(fn ($input) => $input['content'], $packages); + $packages = array_map(fn ($input) => $this->dependencies($input['content']), $packages); // List of frameworks with count of matches $frameworkMatches = []; @@ -63,9 +72,9 @@ public function detect(): ?FrameworkDetection foreach ($this->options as $detector) { // Check package-based detection - foreach ($packages as $packageJson) { + foreach ($packages as $dependencies) { foreach ($detector->getPackages() as $packageNeeded) { - if (str_contains($packageJson, '"'.$packageNeeded.'"')) { + if (\in_array($packageNeeded, $dependencies, true)) { $frameworkMatches[$detector->getName()] += 1; } } @@ -125,4 +134,34 @@ public function detect(): ?FrameworkDetection return null; } + + /** + * Reads declared dependency names out of a `package.json` or a `pubspec.yaml`. + * + * @return array + */ + protected function dependencies(string $manifest): array + { + $parsed = \json_decode($manifest, true); + + if (! \is_array($parsed)) { + $parsed = @\yaml_parse($manifest); + } + + if (! \is_array($parsed)) { + return []; + } + + $dependencies = []; + + foreach (self::DEPENDENCY_SECTIONS as $section) { + $packages = $parsed[$section] ?? null; + + if (\is_array($packages)) { + $dependencies = \array_merge($dependencies, \array_map('strval', \array_keys($packages))); + } + } + + return $dependencies; + } } diff --git a/tests/unit/DetectorTest.php b/tests/unit/DetectorTest.php index 251bdfc..06f3bf0 100644 --- a/tests/unit/DetectorTest.php +++ b/tests/unit/DetectorTest.php @@ -7,6 +7,7 @@ use Utopia\Detector\Detection\Framework\Angular; use Utopia\Detector\Detection\Framework\Astro; use Utopia\Detector\Detection\Framework\Flutter; +use Utopia\Detector\Detection\Framework\Jaspr; use Utopia\Detector\Detection\Framework\Lynx; use Utopia\Detector\Detection\Framework\NextJs; use Utopia\Detector\Detection\Framework\Nuxt; @@ -370,6 +371,8 @@ public function renderingDataProvider(): array [['server/index.mjs'], 'tanstack-start', 'ssr', null], [['index.html', '_nuxt/something.js'], 'nuxt', 'static', 'index.html'], [['server/pages/index.js', 'prerendered/about.html', 'handler.js'], 'sveltekit', 'ssr', null], + [['app', 'index.html', 'main.dart.js'], 'jaspr', 'ssr', null], + [['index.html', 'main.dart.js', 'styles.css'], 'jaspr', 'static', 'index.html'], [['index.html', 'about.html'], 'sveltekit', 'static', null], [['index.html', 'style.css'], 'nextjs', 'static', 'index.html'], [['server/entry.mjs', 'server/renderers.mjs', 'server/pages/'], 'astro', 'ssr', null], @@ -850,4 +853,276 @@ public function testRemixAdapterDetection(): void $this->assertSame('ssr', $fw->getAdapter('')); $this->assertContains('package.json', $fw->getConfigFiles()); } + + /** + * @param array $files + * + * @dataProvider dartFrameworkDataProvider + */ + public function testDartFrameworkDetection(string $pubspec, array $files, string $framework): void + { + // Registration order must not decide between two frameworks sharing the pubspec files + foreach ([[new Flutter(), new Jaspr()], [new Jaspr(), new Flutter()]] as $options) { + $detector = new Framework('npm'); + + foreach ($options as $option) { + $detector->addOption($option); + } + + $detector->addInput($pubspec, Framework::INPUT_PACKAGES); + + foreach ($files as $file) { + $detector->addInput($file, Framework::INPUT_FILE); + } + + $detectedFramework = $detector->detect(); + + $this->assertNotNull($detectedFramework); + $this->assertSame($framework, $detectedFramework?->getName()); + } + } + + /** + * @return array, string}> + */ + public function dartFrameworkDataProvider(): array + { + $pubspecFiles = ['pubspec.yaml', 'pubspec.lock']; + + return [ + 'jaspr' => [ + <<<'YAML' + name: my_site + environment: + sdk: ^3.8.0 + + dependencies: + jaspr: ^0.23.0 + + dev_dependencies: + jaspr_builder: ^0.23.0 + + jaspr: + mode: server + YAML, + $pubspecFiles, + 'jaspr', + ], + 'jaspr with quoted dependency key' => [ + <<<'YAML' + name: my_site + + dependencies: + 'jaspr': ^0.23.0 + YAML, + $pubspecFiles, + 'jaspr', + ], + 'jaspr declared in a flow mapping' => [ + <<<'YAML' + name: my_site + + dependencies: {jaspr: ^0.23.0, shelf: ^1.4.0} + YAML, + $pubspecFiles, + 'jaspr', + ], + 'jaspr embedding flutter' => [ + <<<'YAML' + name: my_site + + dependencies: + jaspr: ^0.23.0 + flutter: + sdk: flutter + + jaspr: + mode: server + flutter: embedded + YAML, + $pubspecFiles, + 'jaspr', + ], + 'jaspr without a lockfile' => [ + <<<'YAML' + name: my_site + + dependencies: + jaspr: ^0.23.0 + YAML, + ['pubspec.yaml'], + 'jaspr', + ], + 'flutter' => [ + <<<'YAML' + name: my_app + environment: + sdk: ^3.8.0 + + dependencies: + flutter: + sdk: flutter + + flutter: + uses-material-design: true + YAML, + $pubspecFiles, + 'flutter', + ], + 'jaspr in a multiline flow mapping' => [ + <<<'YAML' + name: my_site + + dependencies: {shelf: ^1.4.0, + jaspr: ^0.23.0} + YAML, + $pubspecFiles, + 'jaspr', + ], + 'jaspr beside a quoted hash' => [ + <<<'YAML' + name: my_site + + dependencies: {local_pkg: {path: 'packages # local'}, jaspr: ^0.23.0} + YAML, + $pubspecFiles, + 'jaspr', + ], + 'jaspr beside an anchored quoted hash' => [ + <<<'YAML' + name: my_site + + dependencies: {local_pkg: {path: &local 'packages # local'}, jaspr: ^0.23.0} + YAML, + $pubspecFiles, + 'jaspr', + ], + 'flutter with an apostrophe before a commented jaspr map' => [ + <<<'YAML' + name: my_app + description: Don't enable this # {jaspr: 'example'} + + dependencies: + flutter: + sdk: flutter + YAML, + $pubspecFiles, + 'flutter', + ], + 'flutter with a commented out jaspr dependency' => [ + <<<'YAML' + name: my_app + + dependencies: + flutter: + sdk: flutter + # jaspr: ^0.23.0 + # dependencies: {jaspr: ^0.23.0} + YAML, + $pubspecFiles, + 'flutter', + ], + 'flutter with a jaspr config but no jaspr dependency' => [ + <<<'YAML' + name: my_app + + dependencies: + flutter: + sdk: flutter + + jaspr: + mode: server + YAML, + $pubspecFiles, + 'flutter', + ], + 'flutter with an unparsable pubspec' => [ + <<<'YAML' + name: my_app + dependencies: {flutter: {sdk: flutter} + YAML, + $pubspecFiles, + 'flutter', + ], + // Appwrite fetches package.json only, so a Dart repo supplies no manifest today + 'pubspec files with no manifest content' => [ + '', + $pubspecFiles, + 'flutter', + ], + ]; + } + + public function testJasprBuildCommands(): void + { + $detector = new Framework('npm'); + + $detector + ->addOption(new Flutter()) + ->addOption(new Jaspr()); + + $detector->addInput("name: my_site\n\ndependencies:\n jaspr: ^0.23.0\n", Framework::INPUT_PACKAGES); + $detector->addInput('pubspec.yaml', Framework::INPUT_FILE); + + $detectedFramework = $detector->detect(); + + $this->assertNotNull($detectedFramework); + // Makes static code analyser smarter + if (is_null($detectedFramework)) { + throw new \Exception('Framework not detected'); + } + + // Must stay in sync with the jaspr adapter in Appwrite's frameworks config + $this->assertSame('dart pub get', $detectedFramework->getInstallCommand()); + $this->assertSame('jaspr build', $detectedFramework->getBuildCommand()); + $this->assertSame('./build/jaspr', $detectedFramework->getOutputDirectory()); + } + + /** + * @dataProvider jasprAdapterDataProvider + */ + public function testJasprAdapterDetection(string $pubspec, string $adapter): void + { + $this->assertSame($adapter, (new Jaspr())->getAdapter($pubspec)); + } + + /** + * @return array + */ + public function jasprAdapterDataProvider(): array + { + return [ + 'server mode' => ["jaspr:\n mode: server\n", 'ssr'], + 'static mode' => ["jaspr:\n mode: static\n", 'static'], + 'client mode' => ["jaspr:\n mode: client\n", 'static'], + 'quoted mode value' => ["jaspr:\n mode: 'server'\n", 'ssr'], + 'quoted jaspr key' => ["\"jaspr\":\n mode: server\n", 'ssr'], + 'flow mapping' => ["jaspr: {mode: server}\n", 'ssr'], + 'trailing comment' => ["jaspr:\n mode: server # keep in sync\n", 'ssr'], + 'comment on the jaspr key line' => ["jaspr: # mode: static\n mode: server\n", 'ssr'], + 'commented out mode above the real one' => ["jaspr:\n # mode: static\n mode: server\n", 'ssr'], + 'commented out flow mapping' => ["jaspr: {mode: server} # {mode: static}\n", 'ssr'], + 'mode nested under another option' => ["jaspr:\n dev:\n mode: static\n mode: server\n", 'ssr'], + 'multiline flow mapping' => ["jaspr: {flutter: embedded,\n mode: server}\n", 'ssr'], + 'nested flow mapping' => ["jaspr: {dev: {port: 8080}, mode: server}\n", 'ssr'], + 'escaped quotes in a flow value' => ['jaspr: {dev-command: "echo \'\\"\'", mode: server}'."\n", 'ssr'], + 'apostrophe in a plain scalar' => ["jaspr:\n mode: server\ndescription: Don't ship # mode: static\n", 'ssr'], + 'doubled quotes in a value' => ["jaspr: {port: 'it''s 8080', mode: server}\n", 'ssr'], + 'anchor before a quoted value' => ['jaspr: {dev-command: &cmd "echo # hello", mode: server}'."\n", 'ssr'], + 'tag before a quoted value' => ["jaspr:\n port: !!str '8080 # default'\n mode: server\n", 'ssr'], + 'quoted hash before the mode' => ["jaspr:\n dev-command: 'serve # fast'\n mode: server\n", 'ssr'], + 'surrounded by other keys' => ["name: my_site\n\njaspr:\n mode: server\n\ndependencies:\n jaspr: ^0.23.0\n", 'ssr'], + 'commented out config' => ["# jaspr:\n# mode: server\n", ''], + 'jaspr dependency but no config' => ["name: my_site\ndependencies:\n jaspr: ^0.23.0\n", ''], + 'config without a mode' => ["jaspr:\n flutter: embedded\n", ''], + 'unknown mode' => ["jaspr:\n mode: hybrid\n", ''], + 'not a jaspr project' => ["name: my_app\ndependencies:\n flutter:\n sdk: flutter\n", ''], + 'unparsable config' => ["jaspr: {mode: server\n", ''], + ]; + } + + public function testJasprConfigFiles(): void + { + $this->assertContains('pubspec.yaml', (new Jaspr())->getConfigFiles()); + } }