utopia_detector/packager.rs
1//! Package managers: [`Packager`], the built-in [`Npm`], [`Pnpm`] and
2//! [`Yarn`], and the [`PackagerDetector`] that finds one from file names.
3
4/// A package manager, recognised by the files it leaves in a project.
5///
6/// PHP: `Utopia\Detector\Detection\Packager`.
7pub trait Packager: Send + Sync {
8 /// The packager's name, such as `pnpm`.
9 ///
10 /// PHP: `getName()`.
11 fn name(&self) -> &str;
12
13 /// The file names that reveal it, such as `pnpm-lock.yaml`.
14 ///
15 /// PHP: `getFiles()`.
16 fn files(&self) -> &[&str];
17}
18
19/// npm: `package.json` or `package-lock.json`.
20///
21/// PHP: `Utopia\Detector\Detection\Packager\NPM`.
22#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
23pub struct Npm;
24
25impl Packager for Npm {
26 /// `npm`.
27 fn name(&self) -> &str {
28 "npm"
29 }
30
31 /// `package.json`, `package-lock.json`.
32 fn files(&self) -> &[&str] {
33 &["package.json", "package-lock.json"]
34 }
35}
36
37/// pnpm: `pnpm-lock.yaml`.
38///
39/// PHP: `Utopia\Detector\Detection\Packager\PNPM`.
40#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
41pub struct Pnpm;
42
43impl Packager for Pnpm {
44 /// `pnpm`.
45 fn name(&self) -> &str {
46 "pnpm"
47 }
48
49 /// `pnpm-lock.yaml`.
50 fn files(&self) -> &[&str] {
51 &["pnpm-lock.yaml"]
52 }
53}
54
55/// Yarn: `yarn.lock`.
56///
57/// PHP: `Utopia\Detector\Detection\Packager\Yarn`.
58#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
59pub struct Yarn;
60
61impl Packager for Yarn {
62 /// `yarn`.
63 fn name(&self) -> &str {
64 "yarn"
65 }
66
67 /// `yarn.lock`.
68 fn files(&self) -> &[&str] {
69 &["yarn.lock"]
70 }
71}
72
73/// Finds the packager of a project from its file names: the first option,
74/// in the order they were added, with one of its files among the inputs.
75///
76/// PHP: `Utopia\Detector\Detector\Packager`.
77///
78/// ```
79/// use utopia_detector::{Npm, PackagerDetector, Pnpm, Yarn};
80///
81/// let mut detector = PackagerDetector::new();
82/// detector.add_option(Pnpm).add_option(Yarn).add_option(Npm);
83/// detector.add_input("package.json").add_input("yarn.lock");
84///
85/// assert_eq!(detector.detect().map(|p| p.name()), Some("yarn"));
86/// ```
87///
88/// ```php
89/// use Utopia\Detector\Detection\Packager\NPM;
90/// use Utopia\Detector\Detection\Packager\PNPM;
91/// use Utopia\Detector\Detection\Packager\Yarn;
92/// use Utopia\Detector\Detector\Packager;
93///
94/// $detector = new Packager();
95/// $detector->addOption(new PNPM())->addOption(new Yarn())->addOption(new NPM());
96/// $detector->addInput('package.json')->addInput('yarn.lock');
97///
98/// echo $detector->detect()?->getName(), "\n"; // yarn
99/// ```
100#[derive(Default)]
101pub struct PackagerDetector {
102 options: Vec<Box<dyn Packager>>,
103 inputs: Vec<String>,
104}
105
106impl PackagerDetector {
107 /// A detector without options or inputs.
108 ///
109 /// PHP: `new Packager()`.
110 pub fn new() -> Self {
111 Self::default()
112 }
113
114 /// Adds a packager, tried after the others.
115 ///
116 /// PHP: `addOption($packager)`.
117 pub fn add_option(&mut self, option: impl Packager + 'static) -> &mut Self {
118 self.options.push(Box::new(option));
119 self
120 }
121
122 /// Adds a file name of the project.
123 ///
124 /// PHP: `addInput($file)`.
125 pub fn add_input(&mut self, file: impl Into<String>) -> &mut Self {
126 self.inputs.push(file.into());
127 self
128 }
129
130 /// The first packager with a file among the inputs, or `None`.
131 ///
132 /// PHP: `detect()`.
133 pub fn detect(&self) -> Option<&dyn Packager> {
134 self.options.iter().find(|p| p.files().iter().any(|f| self.inputs.iter().any(|i| i == f))).map(|p| p.as_ref())
135 }
136}
137
138impl std::fmt::Debug for PackagerDetector {
139 /// The options' names and the inputs.
140 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
141 f.debug_struct("PackagerDetector")
142 .field("options", &self.options.iter().map(|o| o.name()).collect::<Vec<_>>())
143 .field("inputs", &self.inputs)
144 .finish()
145 }
146}