use php_std::path::pathinfo_extension;
use crate::{Detected, Error};
pub trait Runtime: Send + Sync {
fn name(&self) -> &str;
fn languages(&self) -> &[&str];
fn file_extensions(&self) -> &[&str];
fn files(&self) -> &[&str];
fn commands(&self, packager: &str) -> &str;
fn entrypoint(&self) -> &str;
}
macro_rules! runtime {
($(#[$doc:meta])* $ty:ident, $name:literal, languages: $languages:expr, extensions: $extensions:expr, files: $files:expr, commands: $commands:expr, entrypoint: $entrypoint:literal) => {
$(#[$doc])*
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct $ty;
impl Runtime for $ty {
fn name(&self) -> &str {
$name
}
fn languages(&self) -> &[&str] {
&$languages
}
fn file_extensions(&self) -> &[&str] {
&$extensions
}
fn files(&self) -> &[&str] {
&$files
}
fn commands(&self, packager: &str) -> &str {
let commands: fn(&str) -> &'static str = $commands;
commands(packager)
}
fn entrypoint(&self) -> &str {
$entrypoint
}
}
};
}
runtime!(
Bun, "bun",
languages: ["JavaScript", "TypeScript"],
extensions: ["ts", "tsx", "js", "jsx"],
files: ["bun.lockb"],
commands: |_| "bun install && bun build",
entrypoint: "main.ts"
);
runtime!(
Cpp, "cpp",
languages: ["C++"],
extensions: ["cpp", "h", "hpp", "cxx", "cc"],
files: ["main.cpp", "Solution", "CMakeLists.txt"],
commands: |_| "g++ -o main.cpp && ./output",
entrypoint: ""
);
runtime!(
Dart, "dart",
languages: ["Dart"],
extensions: ["dart"],
files: ["pubspec.yaml", "pubspec.lock"],
commands: |_| "dart pub get",
entrypoint: "main.dart"
);
runtime!(
Deno, "deno",
languages: ["TypeScript"],
extensions: ["ts", "tsx"],
files: ["mod.ts", "deps.ts"],
commands: |_| "deno cache main.ts",
entrypoint: "main.ts"
);
runtime!(
Dotnet, "dotnet",
languages: ["C#", "Visual Basic .NET"],
extensions: ["cs", "vb", "sln", "csproj", "vbproj"],
files: ["Program.cs", "Solution.sln", "Function.csproj", "Program.vb"],
commands: |_| "dotnet restore && dotnet build",
entrypoint: "Program.cs"
);
runtime!(
Java, "java",
languages: ["Java"],
extensions: ["java", "class", "jar"],
files: ["pom.xml", "pmd.xml", "build.gradle", "build.gradle.kts"],
commands: |_| "mvn install && mvn package",
entrypoint: "Main.java"
);
runtime!(
Node, "node",
languages: ["JavaScript", "TypeScript"],
extensions: ["js", "ts"],
files: ["package-lock.json", "yarn.lock", "tsconfig.json"],
commands: |packager| match packager {
"yarn" => "yarn install",
"npm" => "npm install",
_ => "pnpm install",
},
entrypoint: "index.js"
);
runtime!(
Php, "php",
languages: ["PHP"],
extensions: ["php"],
files: ["composer.json", "composer.lock"],
commands: |_| "composer install && composer run build",
entrypoint: "index.php"
);
runtime!(
Python, "python",
languages: ["Python"],
extensions: ["py"],
files: ["requirements.txt", "setup.py"],
commands: |_| "pip install",
entrypoint: "main.py"
);
runtime!(
Ruby, "ruby",
languages: ["Ruby"],
extensions: ["rb"],
files: ["Gemfile", "Gemfile.lock", "Rakefile", "Guardfile"],
commands: |_| "bundle install && bundle exec rake build",
entrypoint: "main.rb"
);
runtime!(
Swift, "swift",
languages: ["Swift"],
extensions: ["swift", "xcodeproj", "xcworkspace"],
files: ["Package.swift", "Podfile", "project.pbxproj"],
commands: |_| "swift build",
entrypoint: "main.swift"
);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Strategy {
FileMatch,
Extension,
Languages,
}
impl Strategy {
pub fn new(value: &str) -> Result<Self, Error> {
match value {
"filematch" => Ok(Strategy::FileMatch),
"extension" => Ok(Strategy::Extension),
"languages" => Ok(Strategy::Languages),
_ => Err(Error::Exception(format!("Invalid strategy: {value}"))),
}
}
pub fn value(&self) -> &'static str {
match self {
Strategy::FileMatch => "filematch",
Strategy::Extension => "extension",
Strategy::Languages => "languages",
}
}
}
pub struct RuntimeDetector {
strategy: Strategy,
packager: String,
options: Vec<Box<dyn Runtime>>,
inputs: Vec<String>,
}
impl RuntimeDetector {
pub fn new(strategy: Strategy, packager: impl Into<String>) -> Self {
RuntimeDetector { strategy, packager: packager.into(), options: Vec::new(), inputs: Vec::new() }
}
pub fn add_option(&mut self, option: impl Runtime + 'static) -> &mut Self {
self.options.push(Box::new(option));
self
}
pub fn add_input(&mut self, input: impl Into<String>) -> &mut Self {
self.inputs.push(input.into());
self
}
pub fn detect(&self) -> Option<Detected<'_, dyn Runtime>> {
let found = match self.strategy {
Strategy::FileMatch => {
self.options.iter().find(|r| r.files().iter().any(|f| self.inputs.iter().any(|i| i == f)))
}
Strategy::Extension => {
let extensions: Vec<&[u8]> = self.inputs.iter().map(|i| pathinfo_extension(i.as_bytes())).collect();
self.options.iter().find(|r| r.file_extensions().iter().any(|e| extensions.contains(&e.as_bytes())))
}
Strategy::Languages => {
self.options.iter().find(|r| r.languages().iter().any(|l| self.inputs.iter().any(|i| i == l)))
}
};
found.map(|r| Detected::new(r.as_ref(), &self.packager))
}
}
impl std::fmt::Debug for RuntimeDetector {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RuntimeDetector")
.field("strategy", &self.strategy)
.field("packager", &self.packager)
.field("options", &self.options.iter().map(|o| o.name()).collect::<Vec<_>>())
.field("inputs", &self.inputs)
.finish()
}
}
impl<T: Runtime + ?Sized> Detected<'_, T> {
pub fn commands(&self) -> &str {
self.item().commands(self.packager())
}
}