From a41af753d811ba6579088c34964c707304e6ca5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 19 Sep 2026 12:26:59 +0200 Subject: [PATCH 1/9] feat: add Jaspr framework detection Adds a Jaspr detection adapter so Jaspr sites are recognised when importing a repository. Jaspr projects are Dart projects, so they share pubspec.yaml and pubspec.lock with Flutter and cannot be told apart by filename alone. Detection therefore keys off the `jaspr` dependency in the manifest, which required teaching the package matcher to read YAML manifests as well as package.json. Adapter detection reads the `jaspr.mode` key from pubspec.yaml: `mode: static` maps to static, anything else to ssr. Co-Authored-By: Claude Opus 5 (1M context) --- README.md | 1 + src/Detection/Framework/Jaspr.php | 67 +++++++++++++++++++++++++ src/Detector/Framework.php | 12 ++++- tests/unit/DetectorTest.php | 81 +++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 src/Detection/Framework/Jaspr.php diff --git a/README.md b/README.md index 8445262..da199f4 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,7 @@ Framework Adapters: |---------|---------| | Astro | ✅ | | Flutter | ✅ | +| Jaspr | ✅ | | NextJs | ✅ | | Nuxt | ✅ | | Remix | ✅ | diff --git a/src/Detection/Framework/Jaspr.php b/src/Detection/Framework/Jaspr.php new file mode 100644 index 0000000..2b145db --- /dev/null +++ b/src/Detection/Framework/Jaspr.php @@ -0,0 +1,67 @@ + + */ + public function getFiles(): array + { + return ['pubspec.yaml', 'pubspec.lock']; + } + + /** + * @return array + */ + public function getPackages(): array + { + return ['jaspr']; + } + + public function getInstallCommand(): string + { + return 'dart pub get'; + } + + public function getBuildCommand(): string + { + return 'dart run jaspr_cli:jaspr build'; + } + + public function getOutputDirectory(): string + { + return './build/jaspr'; + } + + /** + * @return array + */ + public function getConfigFiles(): array + { + return ['pubspec.yaml']; + } + + public function getAdapter(string $configContent): string + { + $stripped = \preg_replace('/^\s*#[^\n]*/m', '', $configContent) ?? $configContent; + + if (\preg_match('/^jaspr\s*:(?.*?)(?=^\S|\z)/ms', $stripped, $matches) !== 1) { + return ''; + } + + if (\preg_match('/^\s+mode\s*:\s*[\x27\x22]?(?[a-z-]+)/m', $matches['body'], $mode) !== 1) { + return 'static'; + } + + return $mode['mode'] === 'static' ? 'static' : 'ssr'; + } +} diff --git a/src/Detector/Framework.php b/src/Detector/Framework.php index a3a79e5..0e42977 100644 --- a/src/Detector/Framework.php +++ b/src/Detector/Framework.php @@ -63,9 +63,17 @@ public function detect(): ?FrameworkDetection foreach ($this->options as $detector) { // Check package-based detection - foreach ($packages as $packageJson) { + foreach ($packages as $manifest) { foreach ($detector->getPackages() as $packageNeeded) { - if (str_contains($packageJson, '"'.$packageNeeded.'"')) { + if (str_contains($manifest, '"'.$packageNeeded.'"')) { + $frameworkMatches[$detector->getName()] += 1; + + continue; + } + + // YAML manifests (pubspec.yaml) declare dependencies as unquoted keys, + // so the JSON check above never matches them. + if (preg_match('/^\s+'.preg_quote($packageNeeded, '/').'\s*:/m', $manifest) === 1) { $frameworkMatches[$detector->getName()] += 1; } } diff --git a/tests/unit/DetectorTest.php b/tests/unit/DetectorTest.php index 251bdfc..5703324 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; @@ -850,4 +851,84 @@ public function testRemixAdapterDetection(): void $this->assertSame('ssr', $fw->getAdapter('')); $this->assertContains('package.json', $fw->getConfigFiles()); } + + public function testJasprDetectionWithPubspec(): void + { + $detector = new Framework('npm'); + + $detector + ->addOption(new Flutter()) + ->addOption(new Jaspr()); + + $pubspec = <<<'YAML' + name: my_site + environment: + sdk: ^3.10.0 + + dependencies: + jaspr: ^0.20.0 + jaspr_router: ^0.10.0 + + jaspr: + mode: server + YAML; + + $detector->addInput($pubspec, Framework::INPUT_PACKAGES); + $detector->addInput('pubspec.yaml', Framework::INPUT_FILE); + $detector->addInput('pubspec.lock', Framework::INPUT_FILE); + + $detectedFramework = $detector->detect(); + + $this->assertNotNull($detectedFramework); + // Makes static code analyser smarter + if (is_null($detectedFramework)) { + throw new \Exception('Framework not detected'); + } + + $this->assertSame('jaspr', $detectedFramework->getName()); + $this->assertSame('dart pub get', $detectedFramework->getInstallCommand()); + $this->assertSame('dart run jaspr_cli:jaspr build', $detectedFramework->getBuildCommand()); + $this->assertSame('./build/jaspr', $detectedFramework->getOutputDirectory()); + } + + public function testFlutterStillWinsWithoutJasprDependency(): void + { + $detector = new Framework('npm'); + + $detector + ->addOption(new Flutter()) + ->addOption(new Jaspr()); + + $pubspec = <<<'YAML' + name: my_app + environment: + sdk: ^3.10.0 + + dependencies: + flutter: + sdk: flutter + YAML; + + $detector->addInput($pubspec, Framework::INPUT_PACKAGES); + $detector->addInput('pubspec.yaml', Framework::INPUT_FILE); + $detector->addInput('pubspec.lock', Framework::INPUT_FILE); + + $detectedFramework = $detector->detect(); + + $this->assertNotNull($detectedFramework); + $this->assertSame('flutter', $detectedFramework?->getName()); + } + + public function testJasprAdapterDetection(): void + { + $fw = new Jaspr(); + + $this->assertSame('ssr', $fw->getAdapter("jaspr:\n mode: server\n")); + $this->assertSame('static', $fw->getAdapter("jaspr:\n mode: static\n")); + $this->assertSame('static', $fw->getAdapter("jaspr:\n uses-flutter: true\n")); + $this->assertSame('ssr', $fw->getAdapter("name: my_site\n\njaspr:\n mode: server\n\ndependencies:\n jaspr: ^0.20.0\n")); + $this->assertSame('', $fw->getAdapter("name: my_app\ndependencies:\n flutter:\n sdk: flutter\n")); + $this->assertSame('', $fw->getAdapter("# jaspr:\n# mode: server\n")); + $this->assertContains('pubspec.yaml', $fw->getConfigFiles()); + } } From b71f18dc54d8dca560cef0d0f01e5d70c8276cff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 19 Sep 2026 14:44:27 +0200 Subject: [PATCH 2/9] fix: make Jaspr and Flutter detection order independent Jaspr now extends Flutter, so the two share the pubspec markers and the detector's specificity tie-break picks Flutter for a pubspec that names neither framework, whatever order the options were registered in. Flutter claims the flutter package and Jaspr the jaspr ones, so either wins on its own dependencies. Package matching accepts quoted and flow-mapping YAML keys, adapter detection parses the jaspr mapping in either YAML style and maps client mode to static, and a jaspr server build is recognised as SSR from its compiled app executable. Build command is now `jaspr build`, matching the jaspr adapter merged in appwrite/appwrite#13710 and the CLI preinstalled in the open-runtimes flutter runtime. Co-Authored-By: Claude Opus 5 (1M context) --- src/Detection/Framework/Flutter.php | 8 ++ src/Detection/Framework/Jaspr.php | 39 +++--- src/Detection/Rendering/SSR.php | 1 + src/Detector/Framework.php | 24 ++-- tests/unit/DetectorTest.php | 210 +++++++++++++++++++++------- 5 files changed, 203 insertions(+), 79 deletions(-) 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 index 2b145db..16a8850 100644 --- a/src/Detection/Framework/Jaspr.php +++ b/src/Detection/Framework/Jaspr.php @@ -2,29 +2,24 @@ namespace Utopia\Detector\Detection\Framework; -use Utopia\Detector\Detection\Framework; - -class Jaspr extends Framework +/** + * Not a Flutter derivative, but detected from the same pubspec files. Extending + * Flutter makes Jaspr the more specific match, so it wins once a jaspr package + * shows up and loses an ambiguous pubspec whatever order the options were added in. + */ +class Jaspr extends Flutter { public function getName(): string { return 'jaspr'; } - /** - * @return array - */ - public function getFiles(): array - { - return ['pubspec.yaml', 'pubspec.lock']; - } - /** * @return array */ public function getPackages(): array { - return ['jaspr']; + return \array_merge(['jaspr', 'jaspr_builder'], parent::getPackages()); } public function getInstallCommand(): string @@ -34,7 +29,7 @@ public function getInstallCommand(): string public function getBuildCommand(): string { - return 'dart run jaspr_cli:jaspr build'; + return 'jaspr build'; } public function getOutputDirectory(): string @@ -50,18 +45,26 @@ 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 { - $stripped = \preg_replace('/^\s*#[^\n]*/m', '', $configContent) ?? $configContent; + $stripped = \preg_replace('/^[ \t]*#[^\n]*/m', '', $configContent) ?? $configContent; - if (\preg_match('/^jaspr\s*:(?.*?)(?=^\S|\z)/ms', $stripped, $matches) !== 1) { + if (\preg_match('/^[\x27\x22]?jaspr[\x27\x22]?[ \t]*:(?.*?)(?=^\S|\z)/ms', $stripped, $block) !== 1) { return ''; } - if (\preg_match('/^\s+mode\s*:\s*[\x27\x22]?(?[a-z-]+)/m', $matches['body'], $mode) !== 1) { - return 'static'; + if (\preg_match('/(?:^|[\s{,])[\x27\x22]?mode[\x27\x22]?[ \t]*:[ \t]*[\x27\x22]?(?[a-z]+)/m', $block['body'], $mode) !== 1) { + return ''; } - return $mode['mode'] === 'static' ? 'static' : 'ssr'; + return match ($mode['mode']) { + '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 0e42977..e79429e 100644 --- a/src/Detector/Framework.php +++ b/src/Detector/Framework.php @@ -65,15 +65,7 @@ public function detect(): ?FrameworkDetection // Check package-based detection foreach ($packages as $manifest) { foreach ($detector->getPackages() as $packageNeeded) { - if (str_contains($manifest, '"'.$packageNeeded.'"')) { - $frameworkMatches[$detector->getName()] += 1; - - continue; - } - - // YAML manifests (pubspec.yaml) declare dependencies as unquoted keys, - // so the JSON check above never matches them. - if (preg_match('/^\s+'.preg_quote($packageNeeded, '/').'\s*:/m', $manifest) === 1) { + if ($this->hasPackage($manifest, $packageNeeded)) { $frameworkMatches[$detector->getName()] += 1; } } @@ -133,4 +125,18 @@ public function detect(): ?FrameworkDetection return null; } + + /** + * Matches a dependency key in JSON manifests, where keys are always double + * quoted, and in YAML ones, where a key may be unquoted, quoted, or inside + * a flow mapping such as `dependencies: {jaspr: ^0.23.0}`. + */ + protected function hasPackage(string $manifest, string $package): bool + { + if (\str_contains($manifest, '"'.$package.'"')) { + return true; + } + + return \preg_match('/(?:^|[\s{,])[\x27\x22]?'.\preg_quote($package, '/').'[\x27\x22]?[ \t]*:/m', $manifest) === 1; + } } diff --git a/tests/unit/DetectorTest.php b/tests/unit/DetectorTest.php index 5703324..ca20d81 100644 --- a/tests/unit/DetectorTest.php +++ b/tests/unit/DetectorTest.php @@ -371,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], @@ -852,7 +854,131 @@ public function testRemixAdapterDetection(): void $this->assertContains('package.json', $fw->getConfigFiles()); } - public function testJasprDetectionWithPubspec(): void + /** + * @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', + ], + // 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'); @@ -860,22 +986,8 @@ public function testJasprDetectionWithPubspec(): void ->addOption(new Flutter()) ->addOption(new Jaspr()); - $pubspec = <<<'YAML' - name: my_site - environment: - sdk: ^3.10.0 - - dependencies: - jaspr: ^0.20.0 - jaspr_router: ^0.10.0 - - jaspr: - mode: server - YAML; - - $detector->addInput($pubspec, Framework::INPUT_PACKAGES); + $detector->addInput("name: my_site\n\ndependencies:\n jaspr: ^0.23.0\n", Framework::INPUT_PACKAGES); $detector->addInput('pubspec.yaml', Framework::INPUT_FILE); - $detector->addInput('pubspec.lock', Framework::INPUT_FILE); $detectedFramework = $detector->detect(); @@ -885,50 +997,44 @@ public function testJasprDetectionWithPubspec(): void throw new \Exception('Framework not detected'); } - $this->assertSame('jaspr', $detectedFramework->getName()); + // Must stay in sync with the jaspr adapter in Appwrite's frameworks config $this->assertSame('dart pub get', $detectedFramework->getInstallCommand()); - $this->assertSame('dart run jaspr_cli:jaspr build', $detectedFramework->getBuildCommand()); + $this->assertSame('jaspr build', $detectedFramework->getBuildCommand()); $this->assertSame('./build/jaspr', $detectedFramework->getOutputDirectory()); } - public function testFlutterStillWinsWithoutJasprDependency(): void + /** + * @dataProvider jasprAdapterDataProvider + */ + public function testJasprAdapterDetection(string $pubspec, string $adapter): void { - $detector = new Framework('npm'); - - $detector - ->addOption(new Flutter()) - ->addOption(new Jaspr()); - - $pubspec = <<<'YAML' - name: my_app - environment: - sdk: ^3.10.0 - - dependencies: - flutter: - sdk: flutter - YAML; - - $detector->addInput($pubspec, Framework::INPUT_PACKAGES); - $detector->addInput('pubspec.yaml', Framework::INPUT_FILE); - $detector->addInput('pubspec.lock', Framework::INPUT_FILE); - - $detectedFramework = $detector->detect(); + $this->assertSame($adapter, (new Jaspr())->getAdapter($pubspec)); + } - $this->assertNotNull($detectedFramework); - $this->assertSame('flutter', $detectedFramework?->getName()); + /** + * @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'], + '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", ''], + ]; } - public function testJasprAdapterDetection(): void + public function testJasprConfigFiles(): void { - $fw = new Jaspr(); - - $this->assertSame('ssr', $fw->getAdapter("jaspr:\n mode: server\n")); - $this->assertSame('static', $fw->getAdapter("jaspr:\n mode: static\n")); - $this->assertSame('static', $fw->getAdapter("jaspr:\n uses-flutter: true\n")); - $this->assertSame('ssr', $fw->getAdapter("name: my_site\n\njaspr:\n mode: server\n\ndependencies:\n jaspr: ^0.20.0\n")); - $this->assertSame('', $fw->getAdapter("name: my_app\ndependencies:\n flutter:\n sdk: flutter\n")); - $this->assertSame('', $fw->getAdapter("# jaspr:\n# mode: server\n")); - $this->assertContains('pubspec.yaml', $fw->getConfigFiles()); + $this->assertContains('pubspec.yaml', (new Jaspr())->getConfigFiles()); } } From 068820b887fd48c6212db4c8a2d56b185175648a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 19 Sep 2026 14:53:43 +0200 Subject: [PATCH 3/9] fix: ignore YAML comments when matching packages and jaspr mode A commented out dependency no longer counts as a package match: keys must sit at the start of a line or inside a flow mapping, and comments are stripped first. Adapter detection strips comments the same way, including one trailing the `jaspr:` key itself, and reads `mode` from the direct children of the jaspr mapping rather than the first `mode:` anywhere in it. Co-Authored-By: Claude Opus 5 (1M context) --- src/Detection/Framework/Jaspr.php | 37 ++++++++++++++++++++++++++----- src/Detector/Framework.php | 7 ++++-- tests/unit/DetectorTest.php | 17 ++++++++++++++ 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/Detection/Framework/Jaspr.php b/src/Detection/Framework/Jaspr.php index 16a8850..2dff46c 100644 --- a/src/Detection/Framework/Jaspr.php +++ b/src/Detection/Framework/Jaspr.php @@ -51,20 +51,45 @@ public function getConfigFiles(): array */ public function getAdapter(string $configContent): string { - $stripped = \preg_replace('/^[ \t]*#[^\n]*/m', '', $configContent) ?? $configContent; + $stripped = \preg_replace('/(^|[ \t])#[^\n]*/m', '$1', $configContent) ?? $configContent; if (\preg_match('/^[\x27\x22]?jaspr[\x27\x22]?[ \t]*:(?.*?)(?=^\S|\z)/ms', $stripped, $block) !== 1) { return ''; } - if (\preg_match('/(?:^|[\s{,])[\x27\x22]?mode[\x27\x22]?[ \t]*:[ \t]*[\x27\x22]?(?[a-z]+)/m', $block['body'], $mode) !== 1) { - return ''; - } - - return match ($mode['mode']) { + return match ($this->readOption($block['body'], 'mode')) { 'server' => 'ssr', 'static', 'client' => 'static', default => '', }; } + + /** + * Reads a direct child of the jaspr mapping, written in either block or flow style. + */ + private function readOption(string $body, string $key): ?string + { + if (\preg_match('/^[ \t]*\{(?[^}]*)}/', $body, $flow) === 1) { + $entries = \explode(',', $flow['flow']); + } else { + $lines = \array_values(\array_filter(\explode("\n", $body), fn ($line) => \trim($line) !== '')); + + if (\count($lines) === 0) { + return null; + } + + $indent = \min(\array_map(fn ($line) => \strlen($line) - \strlen(\ltrim($line)), $lines)); + $entries = \array_filter($lines, fn ($line) => \strlen($line) - \strlen(\ltrim($line)) === $indent); + } + + $pattern = '/^[ \t]*[\x27\x22]?'.\preg_quote($key, '/').'[\x27\x22]?[ \t]*:[ \t]*[\x27\x22]?(?[^\x27\x22\s]*)/'; + + foreach ($entries as $entry) { + if (\preg_match($pattern, $entry, $match) === 1) { + return $match['value']; + } + } + + return null; + } } diff --git a/src/Detector/Framework.php b/src/Detector/Framework.php index e79429e..dba5bdc 100644 --- a/src/Detector/Framework.php +++ b/src/Detector/Framework.php @@ -129,7 +129,8 @@ public function detect(): ?FrameworkDetection /** * Matches a dependency key in JSON manifests, where keys are always double * quoted, and in YAML ones, where a key may be unquoted, quoted, or inside - * a flow mapping such as `dependencies: {jaspr: ^0.23.0}`. + * a flow mapping such as `dependencies: {jaspr: ^0.23.0}`. Commented out + * dependencies are not matched. */ protected function hasPackage(string $manifest, string $package): bool { @@ -137,6 +138,8 @@ protected function hasPackage(string $manifest, string $package): bool return true; } - return \preg_match('/(?:^|[\s{,])[\x27\x22]?'.\preg_quote($package, '/').'[\x27\x22]?[ \t]*:/m', $manifest) === 1; + $manifest = \preg_replace('/(^|[ \t])#[^\n]*/m', '$1', $manifest) ?? $manifest; + + return \preg_match('/(?:^[ \t]*|[{,][ \t]*)[\x27\x22]?'.\preg_quote($package, '/').'[\x27\x22]?[ \t]*:/m', $manifest) === 1; } } diff --git a/tests/unit/DetectorTest.php b/tests/unit/DetectorTest.php index ca20d81..b78f872 100644 --- a/tests/unit/DetectorTest.php +++ b/tests/unit/DetectorTest.php @@ -969,6 +969,19 @@ public function dartFrameworkDataProvider(): array $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', + ], // Appwrite fetches package.json only, so a Dart repo supplies no manifest today 'pubspec files with no manifest content' => [ '', @@ -1024,6 +1037,10 @@ public function jasprAdapterDataProvider(): array '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'], '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", ''], From b168b6b0064f7a5c2a5cc37e12469317570e3fb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 19 Sep 2026 14:59:40 +0200 Subject: [PATCH 4/9] refactor: move YAML matching into a single helper Package matching and jaspr mode detection were each doing their own comment stripping and key matching, and each missed cases the other handled. Both now go through Utopia\Detector\Yaml, which leaves a `#` inside a quoted value alone, and splits a flow mapping on its own commas so nested collections and multiline mappings read correctly. Co-Authored-By: Claude Opus 5 (1M context) --- src/Detection/Framework/Jaspr.php | 35 ++-------- src/Detector/Framework.php | 8 +-- src/Yaml.php | 106 ++++++++++++++++++++++++++++++ tests/unit/DetectorTest.php | 22 +++++++ 4 files changed, 135 insertions(+), 36 deletions(-) create mode 100644 src/Yaml.php diff --git a/src/Detection/Framework/Jaspr.php b/src/Detection/Framework/Jaspr.php index 2dff46c..910f9f2 100644 --- a/src/Detection/Framework/Jaspr.php +++ b/src/Detection/Framework/Jaspr.php @@ -2,6 +2,8 @@ namespace Utopia\Detector\Detection\Framework; +use Utopia\Detector\Yaml; + /** * Not a Flutter derivative, but detected from the same pubspec files. Extending * Flutter makes Jaspr the more specific match, so it wins once a jaspr package @@ -51,45 +53,16 @@ public function getConfigFiles(): array */ public function getAdapter(string $configContent): string { - $stripped = \preg_replace('/(^|[ \t])#[^\n]*/m', '$1', $configContent) ?? $configContent; + $stripped = Yaml::stripComments($configContent); if (\preg_match('/^[\x27\x22]?jaspr[\x27\x22]?[ \t]*:(?.*?)(?=^\S|\z)/ms', $stripped, $block) !== 1) { return ''; } - return match ($this->readOption($block['body'], 'mode')) { + return match (Yaml::readKey($block['body'], 'mode')) { 'server' => 'ssr', 'static', 'client' => 'static', default => '', }; } - - /** - * Reads a direct child of the jaspr mapping, written in either block or flow style. - */ - private function readOption(string $body, string $key): ?string - { - if (\preg_match('/^[ \t]*\{(?[^}]*)}/', $body, $flow) === 1) { - $entries = \explode(',', $flow['flow']); - } else { - $lines = \array_values(\array_filter(\explode("\n", $body), fn ($line) => \trim($line) !== '')); - - if (\count($lines) === 0) { - return null; - } - - $indent = \min(\array_map(fn ($line) => \strlen($line) - \strlen(\ltrim($line)), $lines)); - $entries = \array_filter($lines, fn ($line) => \strlen($line) - \strlen(\ltrim($line)) === $indent); - } - - $pattern = '/^[ \t]*[\x27\x22]?'.\preg_quote($key, '/').'[\x27\x22]?[ \t]*:[ \t]*[\x27\x22]?(?[^\x27\x22\s]*)/'; - - foreach ($entries as $entry) { - if (\preg_match($pattern, $entry, $match) === 1) { - return $match['value']; - } - } - - return null; - } } diff --git a/src/Detector/Framework.php b/src/Detector/Framework.php index dba5bdc..66c1b3a 100644 --- a/src/Detector/Framework.php +++ b/src/Detector/Framework.php @@ -5,6 +5,7 @@ use Utopia\Detector\Detection\Framework as FrameworkDetection; use Utopia\Detector\Detection\Framework\Astro; use Utopia\Detector\Detector; +use Utopia\Detector\Yaml; class Framework extends Detector { @@ -129,8 +130,7 @@ public function detect(): ?FrameworkDetection /** * Matches a dependency key in JSON manifests, where keys are always double * quoted, and in YAML ones, where a key may be unquoted, quoted, or inside - * a flow mapping such as `dependencies: {jaspr: ^0.23.0}`. Commented out - * dependencies are not matched. + * a flow mapping such as `dependencies: {jaspr: ^0.23.0}`. */ protected function hasPackage(string $manifest, string $package): bool { @@ -138,8 +138,6 @@ protected function hasPackage(string $manifest, string $package): bool return true; } - $manifest = \preg_replace('/(^|[ \t])#[^\n]*/m', '$1', $manifest) ?? $manifest; - - return \preg_match('/(?:^[ \t]*|[{,][ \t]*)[\x27\x22]?'.\preg_quote($package, '/').'[\x27\x22]?[ \t]*:/m', $manifest) === 1; + return Yaml::hasKey($manifest, $package); } } diff --git a/src/Yaml.php b/src/Yaml.php new file mode 100644 index 0000000..146c801 --- /dev/null +++ b/src/Yaml.php @@ -0,0 +1,106 @@ +\x27[^\x27\n]*\x27|"[^"\n]*")|(?^|[ \t])#[^\n]*/m'; + + return \preg_replace_callback( + $pattern, + fn (array $match) => ($match['quoted'] ?? '') !== '' ? $match['quoted'] : ($match['before'] ?? ''), + $yaml + ) ?? $yaml; + } + + /** + * Checks for a mapping key, written at the start of a line or inside a flow mapping. + */ + public static function hasKey(string $yaml, string $key): bool + { + $pattern = '/(?:^[ \t]*|[{,]\s*)[\x27\x22]?'.\preg_quote($key, '/').'[\x27\x22]?[ \t]*:/m'; + + return \preg_match($pattern, self::stripComments($yaml)) === 1; + } + + /** + * Reads a direct child of a mapping body, written in either block or flow style. + */ + public static function readKey(string $body, string $key): ?string + { + $trimmed = \ltrim($body); + + if (\str_starts_with($trimmed, '{')) { + $entries = self::splitFlow(\substr($trimmed, 1)); + } else { + $lines = \array_values(\array_filter(\explode("\n", $body), fn ($line) => \trim($line) !== '')); + + if (\count($lines) === 0) { + return null; + } + + $indent = \min(\array_map(fn ($line) => \strlen($line) - \strlen(\ltrim($line)), $lines)); + $entries = \array_filter($lines, fn ($line) => \strlen($line) - \strlen(\ltrim($line)) === $indent); + } + + $pattern = '/^[ \t]*[\x27\x22]?'.\preg_quote($key, '/').'[\x27\x22]?[ \t]*:[ \t]*[\x27\x22]?(?[^\x27\x22\s]*)/'; + + foreach ($entries as $entry) { + if (\preg_match($pattern, $entry, $match) === 1) { + return $match['value']; + } + } + + return null; + } + + /** + * Splits a flow mapping on its own commas, ignoring nested collections and + * quoted values, and stops at the brace closing it. + * + * @return array + */ + private static function splitFlow(string $flow): array + { + $entries = []; + $entry = ''; + $depth = 0; + $quote = ''; + + foreach (\str_split($flow) as $character) { + if ($quote !== '') { + $quote = $character === $quote ? '' : $quote; + } elseif ($character === "'" || $character === '"') { + $quote = $character; + } elseif ($character === '{' || $character === '[') { + $depth++; + } elseif ($character === '}' || $character === ']') { + if ($depth === 0) { + break; + } + + $depth--; + } elseif ($character === ',' && $depth === 0) { + $entries[] = $entry; + $entry = ''; + + continue; + } + + $entry .= $character; + } + + $entries[] = $entry; + + return \array_map(fn ($value) => \trim($value), $entries); + } +} diff --git a/tests/unit/DetectorTest.php b/tests/unit/DetectorTest.php index b78f872..d3dd93e 100644 --- a/tests/unit/DetectorTest.php +++ b/tests/unit/DetectorTest.php @@ -969,6 +969,25 @@ public function dartFrameworkDataProvider(): array $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', + ], 'flutter with a commented out jaspr dependency' => [ <<<'YAML' name: my_app @@ -1041,6 +1060,9 @@ public function jasprAdapterDataProvider(): array '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'], + '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", ''], From 26eb5bf749526c167bcd9f3447f1398d9298a43d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 19 Sep 2026 15:08:35 +0200 Subject: [PATCH 5/9] fix: track YAML quotes the way YAML does A quoted scalar opens only at a value boundary, so the apostrophe in `description: Don't` no longer starts one and a comment after it is stripped again. Closing is escape aware: doubled quotes inside a single quoted scalar and backslash escaped quotes inside a double quoted one stay inside the value. Co-Authored-By: Claude Opus 5 (1M context) --- src/Yaml.php | 120 +++++++++++++++++++++++++++++++++--- tests/unit/DetectorTest.php | 15 +++++ 2 files changed, 125 insertions(+), 10 deletions(-) diff --git a/src/Yaml.php b/src/Yaml.php index 146c801..fe67c82 100644 --- a/src/Yaml.php +++ b/src/Yaml.php @@ -9,17 +9,67 @@ final class Yaml { /** - * Removes comments, leaving a `#` inside a quoted value alone. + * Characters a quoted scalar may open after. An apostrophe anywhere else + * belongs to a plain scalar, as in `description: Don't`. + */ + private const BOUNDARIES = ':,-[{?'; + + /** + * Removes comments, leaving a `#` inside a quoted value alone. Quote state + * is tracked per line, so an unterminated quote cannot swallow the rest of + * the document: the worst it costs is text, never a key it invents. */ public static function stripComments(string $yaml): string { - $pattern = '/(?\x27[^\x27\n]*\x27|"[^"\n]*")|(?^|[ \t])#[^\n]*/m'; + $stripped = ''; + $quote = ''; + $previous = ''; + $length = \strlen($yaml); + + for ($position = 0; $position < $length; $position++) { + $character = $yaml[$position]; + + if ($character === "\n") { + $stripped .= $character; + $quote = ''; + $previous = ''; + + continue; + } + + if ($quote !== '') { + $stripped .= $character; + + if ($character === $quote) { + if ($quote === "'" && ($yaml[$position + 1] ?? '') === "'") { + $stripped .= "'"; + $position++; + } elseif (! self::isEscaped($yaml, $position, $quote)) { + $quote = ''; + } + } + + continue; + } + + if (self::opensQuote($character, $previous)) { + $quote = $character; + } elseif ($character === '#' && ($previous === '' || $yaml[$position - 1] === ' ' || $yaml[$position - 1] === "\t")) { + while ($position + 1 < $length && $yaml[$position + 1] !== "\n") { + $position++; + } + + continue; + } + + if ($character !== ' ' && $character !== "\t") { + $previous = $character; + } + + $stripped .= $character; + } - return \preg_replace_callback( - $pattern, - fn (array $match) => ($match['quoted'] ?? '') !== '' ? $match['quoted'] : ($match['before'] ?? ''), - $yaml - ) ?? $yaml; + return $stripped; } /** @@ -75,11 +125,28 @@ private static function splitFlow(string $flow): array $entry = ''; $depth = 0; $quote = ''; + $previous = ''; + $length = \strlen($flow); + + for ($position = 0; $position < $length; $position++) { + $character = $flow[$position]; - foreach (\str_split($flow) as $character) { if ($quote !== '') { - $quote = $character === $quote ? '' : $quote; - } elseif ($character === "'" || $character === '"') { + $entry .= $character; + + if ($character === $quote) { + if ($quote === "'" && ($flow[$position + 1] ?? '') === "'") { + $entry .= "'"; + $position++; + } elseif (! self::isEscaped($flow, $position, $quote)) { + $quote = ''; + } + } + + continue; + } + + if (self::opensQuote($character, $previous)) { $quote = $character; } elseif ($character === '{' || $character === '[') { $depth++; @@ -92,10 +159,15 @@ private static function splitFlow(string $flow): array } elseif ($character === ',' && $depth === 0) { $entries[] = $entry; $entry = ''; + $previous = ','; continue; } + if (\trim($character) !== '') { + $previous = $character; + } + $entry .= $character; } @@ -103,4 +175,32 @@ private static function splitFlow(string $flow): array return \array_map(fn ($value) => \trim($value), $entries); } + + private static function opensQuote(string $character, string $previous): bool + { + if ($character !== "'" && $character !== '"') { + return false; + } + + return $previous === '' || \str_contains(self::BOUNDARIES, $previous); + } + + /** + * Only double quoted scalars use backslash escapes, and only an odd number + * of them escapes the quote that follows. + */ + private static function isEscaped(string $text, int $position, string $quote): bool + { + if ($quote !== '"') { + return false; + } + + $backslashes = 0; + + for ($index = $position - 1; $index >= 0 && $text[$index] === '\\'; $index--) { + $backslashes++; + } + + return $backslashes % 2 === 1; + } } diff --git a/tests/unit/DetectorTest.php b/tests/unit/DetectorTest.php index d3dd93e..ad5d3d8 100644 --- a/tests/unit/DetectorTest.php +++ b/tests/unit/DetectorTest.php @@ -988,6 +988,18 @@ public function dartFrameworkDataProvider(): array $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 @@ -1062,6 +1074,9 @@ public function jasprAdapterDataProvider(): array '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'], '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", ''], From 3723465297dd3f11ab1142b4477004fbd9e37942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 19 Sep 2026 15:16:01 +0200 Subject: [PATCH 6/9] fix: see past anchors and tags when a value opens `path: &local 'packages # local'` left the anchor name as the last character before the quote, so the value read as plain text and its hash was stripped as a comment, taking a real dependency with it. Anchors, aliases and tags are now read as their own token and leave the value boundary standing. Co-Authored-By: Claude Opus 5 (1M context) --- src/Yaml.php | 60 +++++++++++++++++++++++++++---------- tests/unit/DetectorTest.php | 11 +++++++ 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/src/Yaml.php b/src/Yaml.php index fe67c82..c511e16 100644 --- a/src/Yaml.php +++ b/src/Yaml.php @@ -9,11 +9,19 @@ final class Yaml { /** - * Characters a quoted scalar may open after. An apostrophe anywhere else - * belongs to a plain scalar, as in `description: Don't`. + * Characters a value may start after. An apostrophe anywhere else belongs + * to a plain scalar, as in `description: Don't`. */ private const BOUNDARIES = ':,-[{?'; + /** + * Anchors, aliases and tags sit between the boundary and the value itself, + * as in `path: &local 'packages'`, so they leave the boundary standing. + */ + private const TRANSPARENT = '&*!'; + + private const DELIMITERS = " \t\n,{}[]"; + /** * Removes comments, leaving a `#` inside a quoted value alone. Quote state * is tracked per line, so an unterminated quote cannot swallow the rest of @@ -23,7 +31,7 @@ public static function stripComments(string $yaml): string { $stripped = ''; $quote = ''; - $previous = ''; + $boundary = true; $length = \strlen($yaml); for ($position = 0; $position < $length; $position++) { @@ -32,7 +40,7 @@ public static function stripComments(string $yaml): string if ($character === "\n") { $stripped .= $character; $quote = ''; - $previous = ''; + $boundary = true; continue; } @@ -52,9 +60,17 @@ public static function stripComments(string $yaml): string continue; } - if (self::opensQuote($character, $previous)) { + if ($boundary && \str_contains(self::TRANSPARENT, $character)) { + $token = self::readToken($yaml, $position); + $stripped .= $token; + $position += \strlen($token) - 1; + + continue; + } + + if ($boundary && ($character === "'" || $character === '"')) { $quote = $character; - } elseif ($character === '#' && ($previous === '' || $yaml[$position - 1] === ' ' || $yaml[$position - 1] === "\t")) { + } elseif ($character === '#' && ($position === 0 || $yaml[$position - 1] === ' ' || $yaml[$position - 1] === "\t" || $yaml[$position - 1] === "\n")) { while ($position + 1 < $length && $yaml[$position + 1] !== "\n") { $position++; } @@ -63,7 +79,7 @@ public static function stripComments(string $yaml): string } if ($character !== ' ' && $character !== "\t") { - $previous = $character; + $boundary = \str_contains(self::BOUNDARIES, $character); } $stripped .= $character; @@ -125,7 +141,7 @@ private static function splitFlow(string $flow): array $entry = ''; $depth = 0; $quote = ''; - $previous = ''; + $boundary = true; $length = \strlen($flow); for ($position = 0; $position < $length; $position++) { @@ -146,7 +162,15 @@ private static function splitFlow(string $flow): array continue; } - if (self::opensQuote($character, $previous)) { + if ($boundary && \str_contains(self::TRANSPARENT, $character)) { + $token = self::readToken($flow, $position); + $entry .= $token; + $position += \strlen($token) - 1; + + continue; + } + + if ($boundary && ($character === "'" || $character === '"')) { $quote = $character; } elseif ($character === '{' || $character === '[') { $depth++; @@ -159,13 +183,13 @@ private static function splitFlow(string $flow): array } elseif ($character === ',' && $depth === 0) { $entries[] = $entry; $entry = ''; - $previous = ','; + $boundary = true; continue; } if (\trim($character) !== '') { - $previous = $character; + $boundary = \str_contains(self::BOUNDARIES, $character); } $entry .= $character; @@ -176,13 +200,19 @@ private static function splitFlow(string $flow): array return \array_map(fn ($value) => \trim($value), $entries); } - private static function opensQuote(string $character, string $previous): bool + /** + * Reads an anchor, alias or tag token, up to whatever delimits it. + */ + private static function readToken(string $text, int $position): string { - if ($character !== "'" && $character !== '"') { - return false; + $end = $position; + $length = \strlen($text); + + while ($end < $length && ! \str_contains(self::DELIMITERS, $text[$end])) { + $end++; } - return $previous === '' || \str_contains(self::BOUNDARIES, $previous); + return \substr($text, $position, \max($end - $position, 1)); } /** diff --git a/tests/unit/DetectorTest.php b/tests/unit/DetectorTest.php index ad5d3d8..e4a4a07 100644 --- a/tests/unit/DetectorTest.php +++ b/tests/unit/DetectorTest.php @@ -988,6 +988,15 @@ public function dartFrameworkDataProvider(): array $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 @@ -1077,6 +1086,8 @@ public function jasprAdapterDataProvider(): array '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", ''], From bb783c494e4029c34efb155e9900b006a67c26f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 19 Sep 2026 23:29:00 +0200 Subject: [PATCH 7/9] refactor: parse manifests with the JSON and YAML extensions Replaces the hand written Yaml helper with ext-json and ext-yaml. Package matching now reads dependency sections out of a parsed manifest instead of searching the raw text, and the jaspr adapter reads jaspr.mode off the parsed tree instead of a regex. Appwrite, the only consumer, already requires ext-yaml. Co-Authored-By: Claude Opus 5 (1M context) --- .github/workflows/tests.yml | 1 + composer.json | 4 +- composer.lock | 6 +- src/Detection/Framework/Jaspr.php | 8 +- src/Detector/Framework.php | 22 +-- src/Manifest.php | 56 +++++++ src/Yaml.php | 236 ------------------------------ tests/unit/DetectorTest.php | 23 +++ 8 files changed, 95 insertions(+), 261 deletions(-) create mode 100644 src/Manifest.php delete mode 100644 src/Yaml.php 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/composer.json b/composer.json index 4171219..93e657b 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,9 @@ } }, "require": { - "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/Jaspr.php b/src/Detection/Framework/Jaspr.php index 910f9f2..e77828a 100644 --- a/src/Detection/Framework/Jaspr.php +++ b/src/Detection/Framework/Jaspr.php @@ -2,7 +2,7 @@ namespace Utopia\Detector\Detection\Framework; -use Utopia\Detector\Yaml; +use Utopia\Detector\Manifest; /** * Not a Flutter derivative, but detected from the same pubspec files. Extending @@ -53,13 +53,13 @@ public function getConfigFiles(): array */ public function getAdapter(string $configContent): string { - $stripped = Yaml::stripComments($configContent); + $config = Manifest::parse($configContent)['jaspr'] ?? null; - if (\preg_match('/^[\x27\x22]?jaspr[\x27\x22]?[ \t]*:(?.*?)(?=^\S|\z)/ms', $stripped, $block) !== 1) { + if (! \is_array($config)) { return ''; } - return match (Yaml::readKey($block['body'], 'mode')) { + return match ($config['mode'] ?? null) { 'server' => 'ssr', 'static', 'client' => 'static', default => '', diff --git a/src/Detector/Framework.php b/src/Detector/Framework.php index 66c1b3a..c20e3a7 100644 --- a/src/Detector/Framework.php +++ b/src/Detector/Framework.php @@ -5,7 +5,7 @@ use Utopia\Detector\Detection\Framework as FrameworkDetection; use Utopia\Detector\Detection\Framework\Astro; use Utopia\Detector\Detector; -use Utopia\Detector\Yaml; +use Utopia\Detector\Manifest; class Framework extends Detector { @@ -47,7 +47,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) => Manifest::dependencies($input['content']), $packages); // List of frameworks with count of matches $frameworkMatches = []; @@ -64,9 +64,9 @@ public function detect(): ?FrameworkDetection foreach ($this->options as $detector) { // Check package-based detection - foreach ($packages as $manifest) { + foreach ($packages as $dependencies) { foreach ($detector->getPackages() as $packageNeeded) { - if ($this->hasPackage($manifest, $packageNeeded)) { + if (\in_array($packageNeeded, $dependencies, true)) { $frameworkMatches[$detector->getName()] += 1; } } @@ -126,18 +126,4 @@ public function detect(): ?FrameworkDetection return null; } - - /** - * Matches a dependency key in JSON manifests, where keys are always double - * quoted, and in YAML ones, where a key may be unquoted, quoted, or inside - * a flow mapping such as `dependencies: {jaspr: ^0.23.0}`. - */ - protected function hasPackage(string $manifest, string $package): bool - { - if (\str_contains($manifest, '"'.$package.'"')) { - return true; - } - - return Yaml::hasKey($manifest, $package); - } } diff --git a/src/Manifest.php b/src/Manifest.php new file mode 100644 index 0000000..b41d944 --- /dev/null +++ b/src/Manifest.php @@ -0,0 +1,56 @@ + + */ + public static function parse(string $content): array + { + $manifest = \json_decode($content, true); + + if (! \is_array($manifest)) { + $manifest = @\yaml_parse($content); + } + + return \is_array($manifest) ? $manifest : []; + } + + /** + * @return array + */ + public static function dependencies(string $content): array + { + $manifest = self::parse($content); + $dependencies = []; + + foreach (self::DEPENDENCY_SECTIONS as $section) { + $packages = $manifest[$section] ?? null; + + if (! \is_array($packages)) { + continue; + } + + foreach (\array_keys($packages) as $package) { + $dependencies[] = (string) $package; + } + } + + return $dependencies; + } +} diff --git a/src/Yaml.php b/src/Yaml.php deleted file mode 100644 index c511e16..0000000 --- a/src/Yaml.php +++ /dev/null @@ -1,236 +0,0 @@ - \trim($line) !== '')); - - if (\count($lines) === 0) { - return null; - } - - $indent = \min(\array_map(fn ($line) => \strlen($line) - \strlen(\ltrim($line)), $lines)); - $entries = \array_filter($lines, fn ($line) => \strlen($line) - \strlen(\ltrim($line)) === $indent); - } - - $pattern = '/^[ \t]*[\x27\x22]?'.\preg_quote($key, '/').'[\x27\x22]?[ \t]*:[ \t]*[\x27\x22]?(?[^\x27\x22\s]*)/'; - - foreach ($entries as $entry) { - if (\preg_match($pattern, $entry, $match) === 1) { - return $match['value']; - } - } - - return null; - } - - /** - * Splits a flow mapping on its own commas, ignoring nested collections and - * quoted values, and stops at the brace closing it. - * - * @return array - */ - private static function splitFlow(string $flow): array - { - $entries = []; - $entry = ''; - $depth = 0; - $quote = ''; - $boundary = true; - $length = \strlen($flow); - - for ($position = 0; $position < $length; $position++) { - $character = $flow[$position]; - - if ($quote !== '') { - $entry .= $character; - - if ($character === $quote) { - if ($quote === "'" && ($flow[$position + 1] ?? '') === "'") { - $entry .= "'"; - $position++; - } elseif (! self::isEscaped($flow, $position, $quote)) { - $quote = ''; - } - } - - continue; - } - - if ($boundary && \str_contains(self::TRANSPARENT, $character)) { - $token = self::readToken($flow, $position); - $entry .= $token; - $position += \strlen($token) - 1; - - continue; - } - - if ($boundary && ($character === "'" || $character === '"')) { - $quote = $character; - } elseif ($character === '{' || $character === '[') { - $depth++; - } elseif ($character === '}' || $character === ']') { - if ($depth === 0) { - break; - } - - $depth--; - } elseif ($character === ',' && $depth === 0) { - $entries[] = $entry; - $entry = ''; - $boundary = true; - - continue; - } - - if (\trim($character) !== '') { - $boundary = \str_contains(self::BOUNDARIES, $character); - } - - $entry .= $character; - } - - $entries[] = $entry; - - return \array_map(fn ($value) => \trim($value), $entries); - } - - /** - * Reads an anchor, alias or tag token, up to whatever delimits it. - */ - private static function readToken(string $text, int $position): string - { - $end = $position; - $length = \strlen($text); - - while ($end < $length && ! \str_contains(self::DELIMITERS, $text[$end])) { - $end++; - } - - return \substr($text, $position, \max($end - $position, 1)); - } - - /** - * Only double quoted scalars use backslash escapes, and only an odd number - * of them escapes the quote that follows. - */ - private static function isEscaped(string $text, int $position, string $quote): bool - { - if ($quote !== '"') { - return false; - } - - $backslashes = 0; - - for ($index = $position - 1; $index >= 0 && $text[$index] === '\\'; $index--) { - $backslashes++; - } - - return $backslashes % 2 === 1; - } -} diff --git a/tests/unit/DetectorTest.php b/tests/unit/DetectorTest.php index e4a4a07..06f3bf0 100644 --- a/tests/unit/DetectorTest.php +++ b/tests/unit/DetectorTest.php @@ -1022,6 +1022,28 @@ public function dartFrameworkDataProvider(): array $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' => [ '', @@ -1095,6 +1117,7 @@ public function jasprAdapterDataProvider(): array '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", ''], ]; } From 584576a61c9b7e398f3580d4f489cab0fc4af0c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 19 Sep 2026 23:39:40 +0200 Subject: [PATCH 8/9] refactor: inline manifest parsing instead of a Manifest class The detector reads dependency names itself, and Jaspr calls yaml_parse directly since a pubspec is always YAML. Drops src/Manifest.php. Co-Authored-By: Claude Opus 5 (1M context) --- src/Detection/Framework/Jaspr.php | 5 ++- src/Detector/Framework.php | 42 +++++++++++++++++++++-- src/Manifest.php | 56 ------------------------------- 3 files changed, 42 insertions(+), 61 deletions(-) delete mode 100644 src/Manifest.php diff --git a/src/Detection/Framework/Jaspr.php b/src/Detection/Framework/Jaspr.php index e77828a..a9947e5 100644 --- a/src/Detection/Framework/Jaspr.php +++ b/src/Detection/Framework/Jaspr.php @@ -2,8 +2,6 @@ namespace Utopia\Detector\Detection\Framework; -use Utopia\Detector\Manifest; - /** * Not a Flutter derivative, but detected from the same pubspec files. Extending * Flutter makes Jaspr the more specific match, so it wins once a jaspr package @@ -53,7 +51,8 @@ public function getConfigFiles(): array */ public function getAdapter(string $configContent): string { - $config = Manifest::parse($configContent)['jaspr'] ?? null; + $pubspec = @\yaml_parse($configContent); + $config = \is_array($pubspec) ? ($pubspec['jaspr'] ?? null) : null; if (! \is_array($config)) { return ''; diff --git a/src/Detector/Framework.php b/src/Detector/Framework.php index c20e3a7..af0e271 100644 --- a/src/Detector/Framework.php +++ b/src/Detector/Framework.php @@ -5,13 +5,21 @@ use Utopia\Detector\Detection\Framework as FrameworkDetection; use Utopia\Detector\Detection\Framework\Astro; use Utopia\Detector\Detector; -use Utopia\Detector\Manifest; 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 */ @@ -47,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) => Manifest::dependencies($input['content']), $packages); + $packages = array_map(fn ($input) => $this->dependencies($input['content']), $packages); // List of frameworks with count of matches $frameworkMatches = []; @@ -126,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/src/Manifest.php b/src/Manifest.php deleted file mode 100644 index b41d944..0000000 --- a/src/Manifest.php +++ /dev/null @@ -1,56 +0,0 @@ - - */ - public static function parse(string $content): array - { - $manifest = \json_decode($content, true); - - if (! \is_array($manifest)) { - $manifest = @\yaml_parse($content); - } - - return \is_array($manifest) ? $manifest : []; - } - - /** - * @return array - */ - public static function dependencies(string $content): array - { - $manifest = self::parse($content); - $dependencies = []; - - foreach (self::DEPENDENCY_SECTIONS as $section) { - $packages = $manifest[$section] ?? null; - - if (! \is_array($packages)) { - continue; - } - - foreach (\array_keys($packages) as $package) { - $dependencies[] = (string) $package; - } - } - - return $dependencies; - } -} From 4c4772ef470d6eaa2ab793bd349c8e0c21b7aad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 19 Sep 2026 23:41:23 +0200 Subject: [PATCH 9/9] docs: note the yaml extension in the requirements Co-Authored-By: Claude Opus 5 (1M context) --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index da199f4..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