1use php_std::path::pathinfo_extension;
5
6use crate::{Detected, Error};
7
8pub trait Runtime: Send + Sync {
13 fn name(&self) -> &str;
17
18 fn languages(&self) -> &[&str];
22
23 fn file_extensions(&self) -> &[&str];
27
28 fn files(&self) -> &[&str];
32
33 fn commands(&self, packager: &str) -> &str;
39
40 fn entrypoint(&self) -> &str;
44}
45
46macro_rules! runtime {
47 ($(#[$doc:meta])* $ty:ident, $name:literal, languages: $languages:expr, extensions: $extensions:expr, files: $files:expr, commands: $commands:expr, entrypoint: $entrypoint:literal) => {
48 $(#[$doc])*
49 #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
50 pub struct $ty;
51
52 impl Runtime for $ty {
53 fn name(&self) -> &str {
54 $name
55 }
56
57 fn languages(&self) -> &[&str] {
58 &$languages
59 }
60
61 fn file_extensions(&self) -> &[&str] {
62 &$extensions
63 }
64
65 fn files(&self) -> &[&str] {
66 &$files
67 }
68
69 fn commands(&self, packager: &str) -> &str {
70 let commands: fn(&str) -> &'static str = $commands;
71 commands(packager)
72 }
73
74 fn entrypoint(&self) -> &str {
75 $entrypoint
76 }
77 }
78 };
79}
80
81runtime!(
82 Bun, "bun",
86 languages: ["JavaScript", "TypeScript"],
87 extensions: ["ts", "tsx", "js", "jsx"],
88 files: ["bun.lockb"],
89 commands: |_| "bun install && bun build",
90 entrypoint: "main.ts"
91);
92
93runtime!(
94 Cpp, "cpp",
98 languages: ["C++"],
99 extensions: ["cpp", "h", "hpp", "cxx", "cc"],
100 files: ["main.cpp", "Solution", "CMakeLists.txt"],
101 commands: |_| "g++ -o main.cpp && ./output",
102 entrypoint: ""
103);
104
105runtime!(
106 Dart, "dart",
110 languages: ["Dart"],
111 extensions: ["dart"],
112 files: ["pubspec.yaml", "pubspec.lock"],
113 commands: |_| "dart pub get",
114 entrypoint: "main.dart"
115);
116
117runtime!(
118 Deno, "deno",
122 languages: ["TypeScript"],
123 extensions: ["ts", "tsx"],
124 files: ["mod.ts", "deps.ts"],
125 commands: |_| "deno cache main.ts",
126 entrypoint: "main.ts"
127);
128
129runtime!(
130 Dotnet, "dotnet",
134 languages: ["C#", "Visual Basic .NET"],
135 extensions: ["cs", "vb", "sln", "csproj", "vbproj"],
136 files: ["Program.cs", "Solution.sln", "Function.csproj", "Program.vb"],
137 commands: |_| "dotnet restore && dotnet build",
138 entrypoint: "Program.cs"
139);
140
141runtime!(
142 Java, "java",
146 languages: ["Java"],
147 extensions: ["java", "class", "jar"],
148 files: ["pom.xml", "pmd.xml", "build.gradle", "build.gradle.kts"],
149 commands: |_| "mvn install && mvn package",
150 entrypoint: "Main.java"
151);
152
153runtime!(
154 Node, "node",
159 languages: ["JavaScript", "TypeScript"],
160 extensions: ["js", "ts"],
161 files: ["package-lock.json", "yarn.lock", "tsconfig.json"],
162 commands: |packager| match packager {
163 "yarn" => "yarn install",
164 "npm" => "npm install",
165 _ => "pnpm install",
166 },
167 entrypoint: "index.js"
168);
169
170runtime!(
171 Php, "php",
175 languages: ["PHP"],
176 extensions: ["php"],
177 files: ["composer.json", "composer.lock"],
178 commands: |_| "composer install && composer run build",
179 entrypoint: "index.php"
180);
181
182runtime!(
183 Python, "python",
187 languages: ["Python"],
188 extensions: ["py"],
189 files: ["requirements.txt", "setup.py"],
190 commands: |_| "pip install",
191 entrypoint: "main.py"
192);
193
194runtime!(
195 Ruby, "ruby",
199 languages: ["Ruby"],
200 extensions: ["rb"],
201 files: ["Gemfile", "Gemfile.lock", "Rakefile", "Guardfile"],
202 commands: |_| "bundle install && bundle exec rake build",
203 entrypoint: "main.rb"
204);
205
206runtime!(
207 Swift, "swift",
211 languages: ["Swift"],
212 extensions: ["swift", "xcodeproj", "xcworkspace"],
213 files: ["Package.swift", "Podfile", "project.pbxproj"],
214 commands: |_| "swift build",
215 entrypoint: "main.swift"
216);
217
218#[derive(Debug, Clone, Copy, PartialEq, Eq)]
222pub enum Strategy {
223 FileMatch,
227 Extension,
232 Languages,
236}
237
238impl Strategy {
239 pub fn new(value: &str) -> Result<Self, Error> {
243 match value {
244 "filematch" => Ok(Strategy::FileMatch),
245 "extension" => Ok(Strategy::Extension),
246 "languages" => Ok(Strategy::Languages),
247 _ => Err(Error::Exception(format!("Invalid strategy: {value}"))),
248 }
249 }
250
251 pub fn value(&self) -> &'static str {
255 match self {
256 Strategy::FileMatch => "filematch",
257 Strategy::Extension => "extension",
258 Strategy::Languages => "languages",
259 }
260 }
261}
262
263pub struct RuntimeDetector {
295 strategy: Strategy,
296 packager: String,
297 options: Vec<Box<dyn Runtime>>,
298 inputs: Vec<String>,
299}
300
301impl RuntimeDetector {
302 pub fn new(strategy: Strategy, packager: impl Into<String>) -> Self {
307 RuntimeDetector { strategy, packager: packager.into(), options: Vec::new(), inputs: Vec::new() }
308 }
309
310 pub fn add_option(&mut self, option: impl Runtime + 'static) -> &mut Self {
314 self.options.push(Box::new(option));
315 self
316 }
317
318 pub fn add_input(&mut self, input: impl Into<String>) -> &mut Self {
323 self.inputs.push(input.into());
324 self
325 }
326
327 pub fn detect(&self) -> Option<Detected<'_, dyn Runtime>> {
333 let found = match self.strategy {
334 Strategy::FileMatch => {
335 self.options.iter().find(|r| r.files().iter().any(|f| self.inputs.iter().any(|i| i == f)))
336 }
337 Strategy::Extension => {
338 let extensions: Vec<&[u8]> = self.inputs.iter().map(|i| pathinfo_extension(i.as_bytes())).collect();
339 self.options.iter().find(|r| r.file_extensions().iter().any(|e| extensions.contains(&e.as_bytes())))
340 }
341 Strategy::Languages => {
342 self.options.iter().find(|r| r.languages().iter().any(|l| self.inputs.iter().any(|i| i == l)))
343 }
344 };
345 found.map(|r| Detected::new(r.as_ref(), &self.packager))
346 }
347}
348
349impl std::fmt::Debug for RuntimeDetector {
350 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
352 f.debug_struct("RuntimeDetector")
353 .field("strategy", &self.strategy)
354 .field("packager", &self.packager)
355 .field("options", &self.options.iter().map(|o| o.name()).collect::<Vec<_>>())
356 .field("inputs", &self.inputs)
357 .finish()
358 }
359}
360
361impl<T: Runtime + ?Sized> Detected<'_, T> {
362 pub fn commands(&self) -> &str {
366 self.item().commands(self.packager())
367 }
368}