Skip to main content

FrameworkDetector

Struct FrameworkDetector 

Source
pub struct FrameworkDetector { /* private fields */ }
Expand description

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.

Options are scored by name: two options with one name share a score.

PHP: Utopia\Detector\Detector\Framework.

use utopia_detector::{FrameworkDetector, NextJs, React};

let mut detector = FrameworkDetector::new("npm");
detector.add_option(React).add_option(NextJs);
detector.add_file("next.config.js");
detector.add_manifest(r#"{"dependencies": {"react": "^18.0.0"}}"#);

let framework = detector.detect().expect("a framework");
assert_eq!(framework.name(), "nextjs");
assert_eq!(framework.install_command(), "npm install");
assert_eq!(framework.output_directory(), "./.next");
use Utopia\Detector\Detection\Framework\NextJs;
use Utopia\Detector\Detection\Framework\React;
use Utopia\Detector\Detector\Framework;

$detector = new Framework('npm');
$detector->addOption(new React())->addOption(new NextJs());
$detector->addInput('next.config.js', Framework::INPUT_FILE);
$detector->addInput('{"dependencies": {"react": "^18.0.0"}}', Framework::INPUT_PACKAGES);

$framework = $detector->detect();
echo $framework->getName(), "\n";            // nextjs
echo $framework->getInstallCommand(), "\n";  // npm install
echo $framework->getOutputDirectory(), "\n"; // ./.next

Implementations§

Source§

impl FrameworkDetector

Source

pub fn new(packager: impl Into<String>) -> Self

A detector for a project using packager (pnpm when unsure).

PHP: new Framework($packager).

Source

pub fn add_option(&mut self, option: impl Framework + 'static) -> &mut Self

Adds a framework to score.

PHP: addOption($framework).

Source

pub fn add_file(&mut self, file: impl Into<String>) -> &mut Self

Adds a file name of the project.

PHP: addInput($file, Framework::INPUT_FILE).

Source

pub fn add_manifest(&mut self, content: impl Into<Vec<u8>>) -> &mut Self

Adds the content of a manifest (package.json, pubspec.yaml), whose dependencies count for the frameworks that list them.

PHP: addInput($content, Framework::INPUT_PACKAGES).

Source

pub fn detect(&self) -> Option<Detected<'_, dyn Framework>>

The best scoring framework, with the detector’s packager, or None when no option scores.

PHP: detect(), which also sets the packager on the framework it returns.

Trait Implementations§

Source§

impl Debug for FrameworkDetector

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

The packager, options’ names and file inputs.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.