Skip to main content

Crate utopia_detector

Crate utopia_detector 

Source
Expand description

Detection of what a project is from what it contains: its package manager, its language runtime, its web framework, and whether a build is served by a server or as static files.

Each question has a detector fed with inputs (file names, language names or manifest contents) and options to choose from:

Options are traits, so a project can add its own. Runtime and framework detections come back as Detected, which carries the detector’s packager into the install and build commands.

use utopia_detector::{FrameworkDetector, NextJs, Nuxt, SvelteKit};

let mut detector = FrameworkDetector::new("pnpm");
detector.add_option(NextJs).add_option(Nuxt).add_option(SvelteKit);
for file in ["src", "package.json", "svelte.config.js"] {
    detector.add_file(file);
}

let framework = detector.detect().expect("a framework");
assert_eq!(framework.name(), "sveltekit");
assert_eq!(framework.build_command(), "pnpm run build");
use Utopia\Detector\Detection\Framework\NextJs;
use Utopia\Detector\Detection\Framework\Nuxt;
use Utopia\Detector\Detection\Framework\SvelteKit;
use Utopia\Detector\Detector\Framework;

$detector = new Framework('pnpm');
$detector->addOption(new NextJs())->addOption(new Nuxt())->addOption(new SvelteKit());
foreach (['src', 'package.json', 'svelte.config.js'] as $file) {
    $detector->addInput($file, Framework::INPUT_FILE);
}

$framework = $detector->detect();
echo $framework->getName(), "\n";         // sveltekit
echo $framework->getBuildCommand(), "\n"; // pnpm run build

PHP: ports utopia-php/detector.

PHPRust
Utopia\Detector\Detector (abstract), Detection (abstract)one detector type per question; no common base
Detector::addOption(), addInput(), detect()add_option, add_input (add_file / add_manifest on FrameworkDetector), detect on each detector
Detector\Packager, Detection\PackagerPackagerDetector, Packager
Detection\Packager\NPM, PNPM, YarnNpm, Pnpm, Yarn
Detector\Runtime, Detection\RuntimeRuntimeDetector, Runtime
Detector\Strategy (FILEMATCH, EXTENSION, LANGUAGES)Strategy
Detection\Runtime\Bun, CPP, Dart, Deno, Dotnet, Java, Node, PHP, Python, Ruby, SwiftBun, Cpp, Dart, Deno, Dotnet, Java, Node, Php, Python, Ruby, Swift
Detector\Framework, Detection\FrameworkFrameworkDetector, Framework
Framework::INPUT_FILE, INPUT_PACKAGESInput
Framework::dependencies()dependencies
Detection\Framework\JS (abstract)the JavaScript frameworks’ shared install and build commands
Detection\Framework\Analog, Angular, Astro, Flutter, Jaspr, Lynx, NextJs, Nuxt, React, ReactNative, Remix, Svelte, SvelteKit, TanStackStart, VueAnalog, Angular, Astro, Flutter, Jaspr, Lynx, NextJs, Nuxt, React, ReactNative, Remix, Svelte, SvelteKit, TanStackStart, Vue
getAdapter() ('ssr', 'static', '')Framework::adapter (Adapter, None)
setPackager() on a runtime or frameworkthe packager argument of Runtime::commands, Framework::install_command, Framework::build_command; Detected passes the detector’s
Detector\Rendering, Detection\RenderingRenderingDetector, Rendering (Rendered from detect)
Detection\Rendering\SSR, XStaticSsr, Static
\InvalidArgumentException, \ExceptionError::InvalidArgument, Error::Exception

PHP passes file names as byte strings; here they are Strings, so a name that is not UTF-8 has to be converted before it is added.

Structs§

Analog
Analog, the Angular meta-framework.
Angular
Angular.
Astro
Astro. It renders components of other frameworks, so their files and packages count for it too; a tie between Astro and another framework goes to the other one.
Bun
Bun.
Cpp
C++.
Dart
Dart.
Deno
Deno.
Detected
A runtime or framework a detector chose, with the detector’s packager: its commands are the ones for that packager. Everything else is the option itself, through Deref.
Dotnet
.NET.
Flutter
Flutter (web).
FrameworkDetector
Finds a project’s framework by scoring every option: one point per framework file among the file inputs, and one per framework package declared in each manifest input. The highest score wins; a tie goes to the framework with the fewest parents, except that Astro yields to the runner-up.
Jaspr
Jaspr, the Dart web framework. It is detected from the same pubspec files as Flutter and wins once a jaspr package appears, whatever order the options were added in.
Java
Java.
Lynx
Lynx.
NextJs
Next.js.
Node
Node.js: the only runtime whose install command follows the packager.
Npm
npm: package.json or package-lock.json.
Nuxt
Nuxt.
PackagerDetector
Finds the packager of a project from its file names: the first option, in the order they were added, with one of its files among the inputs.
Php
PHP.
Pnpm
pnpm: pnpm-lock.yaml.
Python
Python.
React
React.
ReactNative
React Native.
Remix
Remix: always server-side.
RenderingDetector
Tells how a framework’s build is served from the build’s file names: the first option, in the order they were added, with one of its files for the framework among the inputs; otherwise static output, falling back to the only HTML file when there is exactly one.
Ruby
Ruby.
RuntimeDetector
Finds the runtime of a project: the first option, in the order they were added, that matches an input the way the Strategy says.
Score
One option as a framework detector scores it.
Ssr
Server-side rendering: the build has a server entry for the framework.
Static
Static output: plain files, with an optional fallback page.
Svelte
Svelte.
SvelteKit
SvelteKit: static when the configuration (or package.json) uses @sveltejs/adapter-static, server-side otherwise.
Swift
Swift.
TanStackStart
TanStack Start: server-side with a Nitro plugin or without prerendering, static when it prerenders.
Vue
Vue.
Yarn
Yarn: yarn.lock.

Enums§

Adapter
How a framework’s build is served, as its configuration says.
Error
Why a detector refused an argument.
Input
What a framework detector input is.
Rendered
What a RenderingDetector found: one of its options, or static output it made up because none matched.
Strategy
What a RuntimeDetector’s inputs are, and so how they are matched.

Traits§

Framework
A web framework, recognised by its configuration files and the packages a project depends on.
Packager
A package manager, recognised by the files it leaves in a project.
Rendering
How a framework’s build output is served, recognised by the files the build leaves for a given framework.
Runtime
A language runtime, recognised by its languages, file extensions or files.

Functions§

best
The name of the framework a detector picks from scores, one per option in the order the options were added: options sharing a name add up; the most matches win; a tie goes to the fewest parents, except that Astro yields to the runner-up. None when nothing matches.
dependencies
The dependency names declared in a package.json (JSON) or pubspec.yaml (YAML), section by section; none when it parses as neither.
html_fallback
The only file among inputs with the extension html, the static output’s fallback; None with none or several.
matches
How many of an option’s files are among the file inputs, plus how many of its packages each manifest’s dependencies declare. A file or package the option lists twice counts twice.