use anyhow::Context as _;
use serde_json::Value;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use vivacity_resolver::flex_filter::FlexVersions;
const DEFAULT_ENDPOINTS: [&str; 2] = [
"https://raw.githubusercontent.com/symfony/recipes/flex/main/index.json",
"https://raw.githubusercontent.com/symfony/recipes-contrib/flex/main/index.json",
];
pub fn endpoints(manifest: &Value) -> Result<Vec<String>, String> {
let configured = manifest
.get("extra")
.and_then(|e| e.get("symfony"))
.and_then(|s| s.get("endpoint"));
let mut list: Option<Vec<String>> = match configured {
None => Some(DEFAULT_ENDPOINTS.iter().map(|s| (*s).to_owned()).collect()),
Some(Value::Array(a)) => Some(
a.iter()
.filter_map(Value::as_str)
.map(str::to_owned)
.collect(),
),
Some(Value::String(s)) if s.contains(".json") => {
Some(vec![s.clone(), "flex://defaults".to_owned()])
}
Some(Value::String(s)) if s == "flex://defaults" => Some(vec![s.clone()]),
Some(Value::String(s)) => {
return Err(format!(
"extra.symfony.endpoint {s:?} is a legacy Flex endpoint (versions.json), not emulated"
))
}
Some(other) => {
return Err(format!(
"extra.symfony.endpoint {other} is not a string or a list"
))
}
};
if let Ok(env) = std::env::var("SYMFONY_ENDPOINT") {
if env.contains(".json") || env == "flex://defaults" {
let l = list
.get_or_insert_with(|| DEFAULT_ENDPOINTS.iter().map(|s| (*s).to_owned()).collect());
l.insert(0, env);
} else if !env.is_empty() {
return Err(format!(
"SYMFONY_ENDPOINT {env:?} is a legacy Flex endpoint (versions.json), not emulated"
));
}
}
let mut out: Vec<String> = Vec::new();
for e in list.unwrap_or_default() {
if e == "flex://defaults" {
out.extend(DEFAULT_ENDPOINTS.iter().map(|s| (*s).to_owned()));
} else {
out.push(e);
}
}
let mut seen = std::collections::HashSet::new();
out.retain(|e| seen.insert(e.clone()));
Ok(out)
}
fn cache_key(url: &str) -> String {
let mut u = url.to_owned();
if let Some(rest) = u.strip_prefix("https://api.github.com/repos/") {
let mut parts = rest.splitn(3, '/');
if let (Some(owner), Some(repo), Some(tail)) = (parts.next(), parts.next(), parts.next()) {
if let Some(after) = tail.strip_prefix("contents/") {
u = format!("{owner}/{repo}/{after}");
}
}
} else if let Some(rest) = u.strip_prefix("https://raw.githubusercontent.com/") {
let mut parts = rest.splitn(3, '/');
if let (Some(owner), Some(repo), Some(tail)) = (parts.next(), parts.next(), parts.next()) {
u = format!("{owner}/{repo}/{tail}");
}
}
let key: String = u
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || c == '.' {
c
} else {
'-'
}
})
.collect();
if key.len() > 140 {
use md5::Digest as _;
let digest = md5::Md5::digest(url.as_bytes());
digest.iter().map(|b| format!("{b:02x}")).collect()
} else {
key
}
}
fn cache_dir() -> PathBuf {
vivacity_core::fetch::composer_cache_dir()
.join("repo")
.join("flex")
}
fn fetch_index(
url: &str,
runtime: &tokio::runtime::Runtime,
fetcher: &vivacity_core::fetch::Fetcher,
offline: bool,
) -> anyhow::Result<Value> {
if let Some(path) = url.strip_prefix("file://") {
let text = std::fs::read_to_string(path)
.with_context(|| format!("cannot read Flex index {path}"))?;
return serde_json::from_str(&text)
.with_context(|| format!("Flex index {path}: invalid JSON"));
}
let cache_file = cache_dir().join(cache_key(url));
let cached: Option<Value> = vivacity_core::jsonfile::read(&cache_file).map(|v| (*v).clone());
let cached_body = cached.as_ref().and_then(|c| c.get("body").cloned());
let last_modified = cached
.as_ref()
.and_then(|c| c.get("headers"))
.and_then(|h| h.get("last-modified"))
.and_then(|v| v.get(0))
.and_then(Value::as_str)
.map(str::to_owned);
if offline {
return cached_body.context(format!("offline: Flex index {url} is not cached"));
}
let response = runtime
.block_on(fetcher.metadata_fetch(url, last_modified.as_deref()))
.with_context(|| format!("cannot fetch Flex index {url}"))?;
use vivacity_core::fetch::MetadataResponse as R;
match response {
R::NotModified => {
cached_body.context(format!("Flex index {url}: 304 without a cached body"))
}
R::NotFound => anyhow::bail!("Flex index {url}: not found"),
R::Body {
bytes,
last_modified,
} => {
let body: Value = serde_json::from_slice(&bytes)
.with_context(|| format!("Flex index {url}: invalid JSON"))?;
let mut headers = serde_json::Map::new();
if let Some(lm) = last_modified {
headers.insert(
"last-modified".to_owned(),
Value::Array(vec![Value::String(lm)]),
);
}
let record = serde_json::json!({"body": body, "headers": headers});
if std::fs::create_dir_all(cache_dir()).is_ok() {
let tmp = cache_file.with_extension(format!("tmp.{}", std::process::id()));
if let Ok(text) = serde_json::to_string(&record) {
if std::fs::write(&tmp, text).is_ok()
&& std::fs::rename(&tmp, &cache_file).is_err()
{
let _ = std::fs::remove_file(&tmp);
}
}
}
Ok(record["body"].clone())
}
}
}
pub fn versions(project: &Path, manifest: &Value, offline: bool) -> anyhow::Result<FlexVersions> {
let merged = FlexVersions::merge_endpoints(&fetch_indexes(project, manifest, offline)?);
FlexVersions::from_value(&merged).map_err(|e| anyhow::anyhow!("{e}"))
}
fn fetch_indexes(project: &Path, manifest: &Value, offline: bool) -> anyhow::Result<Vec<Value>> {
let urls = endpoints(manifest).map_err(|e| anyhow::anyhow!("{e}"))?;
let runtime = tokio::runtime::Runtime::new().context("cannot start the async runtime")?;
let fetcher = Arc::new(vivacity_core::fetch::Fetcher::new(
vivacity_core::fetch::composer_cache_dir(),
vivacity_core::fetch::Auth::load(project),
)?);
let mut indexes = Vec::new();
for url in &urls {
indexes.push(fetch_index(url, &runtime, &fetcher, offline)?);
}
Ok(indexes)
}
#[derive(Debug, Default, Clone)]
pub struct RecipeIndex {
recipes: std::collections::BTreeMap<String, Vec<String>>,
}
pub fn recipe_index(
project: &Path,
manifest: &Value,
offline: bool,
) -> anyhow::Result<RecipeIndex> {
let mut recipes: std::collections::BTreeMap<String, Vec<String>> = Default::default();
for index in fetch_indexes(project, manifest, offline)? {
let Some(map) = index.get("recipes").and_then(Value::as_object) else {
continue;
};
for (name, versions) in map {
let list: Vec<String> = versions
.as_array()
.map(|a| {
a.iter()
.filter_map(Value::as_str)
.map(str::to_owned)
.collect()
})
.unwrap_or_default();
recipes.entry(name.clone()).or_insert(list);
}
}
Ok(RecipeIndex { recipes })
}
impl RecipeIndex {
pub fn applies_to(&self, name: &str, pretty_version: &str, extra: Option<&Value>) -> bool {
let Some(versions) = self.recipes.get(name) else {
return false;
};
let mut version = pretty_version.to_owned();
if version.starts_with("dev-") {
if let Some(aliases) = extra.and_then(|e| e.get("branch-alias")) {
const PREFERRED: [&str; 11] = [
"dev-main",
"dev-trunk",
"dev-develop",
"dev-default",
"dev-latest",
"dev-next",
"dev-current",
"dev-support",
"dev-tip",
"dev-master",
"",
];
let own = aliases.get(&version).and_then(Value::as_str);
let alias = own.or_else(|| {
PREFERRED
.iter()
.filter(|k| !k.is_empty())
.find_map(|k| aliases.get(*k).and_then(Value::as_str))
});
if let Some(a) = alias {
version = a.to_owned();
}
}
}
let stripped = strip_version(&version);
let mut parts = stripped.split('.');
let major = parts.next().unwrap_or("").to_owned();
let minor = parts.next().unwrap_or("9999999").to_owned();
let cut = format!("{major}.{minor}");
versions
.iter()
.rev()
.any(|v| match compare_dotted(&cut, v) {
Some(std::cmp::Ordering::Less) => false,
_ => true,
})
}
}
fn strip_version(v: &str) -> String {
let out = v.to_owned();
for (prefix, pattern) in [
(true, "dev-"),
(true, "v"),
(false, ".x-dev"),
(false, "-dev"),
] {
if prefix {
if let Some(rest) = out.strip_prefix(pattern) {
return rest.to_owned();
}
} else if let Some(rest) = out.strip_suffix(pattern) {
return rest.to_owned();
}
}
out
}
fn compare_dotted(a: &str, b: &str) -> Option<std::cmp::Ordering> {
let parse =
|s: &str| -> Option<Vec<u64>> { s.split('.').map(|p| p.parse::<u64>().ok()).collect() };
let (a, b) = (parse(a)?, parse(b)?);
let len = a.len().max(b.len());
for i in 0..len {
let (x, y) = (
a.get(i).copied().unwrap_or(0),
b.get(i).copied().unwrap_or(0),
);
if x != y {
return Some(x.cmp(&y));
}
}
Some(std::cmp::Ordering::Equal)
}
pub fn has_bundle_class(vendor: &Path, pretty_name: &str, package: &Value) -> bool {
let autoload = package.get("autoload");
let is_sylius_plugin = package.get("type").and_then(Value::as_str) == Some("sylius-plugin");
for (key, is_psr4) in [("psr-4", true), ("psr-0", false)] {
let Some(map) = autoload.and_then(|a| a.get(key)).and_then(Value::as_object) else {
continue;
};
for (namespace, paths) in map {
let paths: Vec<&str> = match paths {
Value::String(s) => vec![s.as_str()],
Value::Array(a) => a.iter().filter_map(Value::as_str).collect(),
_ => continue,
};
for path in paths {
for class in extract_class_names(namespace, is_sylius_plugin) {
if is_bundle_class(vendor, pretty_name, &class, path, is_psr4) {
return true;
}
}
}
}
}
false
}
fn extract_class_names(namespace: &str, is_sylius_plugin: bool) -> Vec<String> {
let namespace = namespace.trim_matches('\\');
let class = format!("{namespace}\\");
let parts: Vec<&str> = namespace.split('\\').collect();
let last = parts.last().copied().unwrap_or("");
let end_of_word = if last.len() >= 6 {
&last[last.len() - 6..]
} else {
last
};
let mut suffix = last.to_owned();
if is_sylius_plugin {
if end_of_word != "Bundle" && end_of_word != "Plugin" {
suffix.push_str("Bundle");
}
} else if end_of_word != "Bundle" {
suffix.push_str("Bundle");
}
let mut classes = vec![format!("{class}{suffix}")];
let mut acc = String::new();
for part in parts.iter().take(parts.len().saturating_sub(1)) {
if *part == "Bundle" || (is_sylius_plugin && *part == "Plugin") {
continue;
}
classes.push(format!("{class}{part}{suffix}"));
acc.push_str(part);
classes.push(format!("{class}{acc}{suffix}"));
}
let mut seen = std::collections::HashSet::new();
classes.retain(|c| seen.insert(c.clone()));
classes
}
fn is_bundle_class(
vendor: &Path,
pretty_name: &str,
class: &str,
path: &str,
is_psr4: bool,
) -> bool {
let parts: Vec<&str> = class.split('\\').collect();
let last = parts.last().copied().unwrap_or("");
let mut class_path = vendor.join(pretty_name).join(path);
if !is_psr4 {
let joined: String = parts[..parts.len() - 1].join("/").replace('\\', "");
if !joined.is_empty() {
class_path = class_path.join(joined);
}
}
let class_path = class_path.join(format!("{}.php", last.replace('\\', "/")));
let Ok(contents) = std::fs::read_to_string(&class_path) else {
return false;
};
contents.contains("Symfony\\Component\\HttpKernel\\Bundle\\Bundle")
|| contents.contains("Symfony\\Component\\HttpKernel\\Bundle\\AbstractBundle")
}
pub fn lock_names(project: &Path) -> std::collections::BTreeSet<String> {
let path = match std::env::var_os("SYMFONY_LOCKFILE") {
Some(p) if !p.is_empty() => std::path::PathBuf::from(p),
_ => project.join("symfony.lock"),
};
let Some(value) = vivacity_core::jsonfile::read(&path) else {
return Default::default();
};
value
.as_object()
.map(|m| m.keys().cloned().collect())
.unwrap_or_default()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn recipe_version_selection_follows_the_downloader() {
let index = RecipeIndex {
recipes: [
(
"a/b".to_owned(),
vec!["3.3".to_owned(), "5.4".to_owned(), "7.0".to_owned()],
),
("c/d".to_owned(), vec!["9.9".to_owned()]),
]
.into_iter()
.collect(),
};
assert!(index.applies_to("a/b", "v7.1.2", None));
assert!(index.applies_to("a/b", "5.4.0", None));
assert!(index.applies_to("a/b", "3.3", None));
assert!(!index.applies_to("a/b", "3.2.9", None));
assert!(!index.applies_to("c/d", "1.0.0", None));
assert!(!index.applies_to("x/y", "9.9.9", None));
assert!(index.applies_to("c/d", "9", None));
let extra = serde_json::json!({"branch-alias": {"dev-main": "7.1.x-dev"}});
assert!(index.applies_to("a/b", "dev-main", Some(&extra)));
let low = serde_json::json!({"branch-alias": {"dev-main": "3.2.x-dev"}});
assert!(!index.applies_to("a/b", "dev-main", Some(&low)));
assert!(index.applies_to("a/b", "dev-feature", None));
}
#[test]
fn bundle_class_names_follow_symfony_bundle() {
let names = extract_class_names("Acme\\FooBundle", false);
assert_eq!(
names,
vec![
"Acme\\FooBundle\\FooBundle",
"Acme\\FooBundle\\AcmeFooBundle"
]
);
let names = extract_class_names("Acme\\Foo", false);
assert_eq!(
names,
vec!["Acme\\Foo\\FooBundle", "Acme\\Foo\\AcmeFooBundle"],
"the accumulator and the part give the same class here"
);
let names = extract_class_names("A\\B\\C", false);
assert_eq!(
names,
vec![
"A\\B\\C\\CBundle",
"A\\B\\C\\ACBundle",
"A\\B\\C\\BCBundle",
"A\\B\\C\\ABCBundle"
]
);
let names = extract_class_names("Acme\\Plugin\\ShopPlugin", true);
assert_eq!(
names,
vec![
"Acme\\Plugin\\ShopPlugin\\ShopPlugin",
"Acme\\Plugin\\ShopPlugin\\AcmeShopPlugin"
]
);
}
#[test]
fn a_bundle_is_found_only_when_the_file_says_so() {
let d = tempfile::tempdir().expect("tmp");
let vendor = d.path();
let src = vendor.join("acme/foo-bundle/src");
std::fs::create_dir_all(&src).expect("dirs");
let package = serde_json::json!({"autoload": {"psr-4": {"Acme\\FooBundle\\": "src"}}});
assert!(!has_bundle_class(vendor, "acme/foo-bundle", &package));
std::fs::write(src.join("FooBundle.php"), "<?php class FooBundle {}").expect("w");
assert!(!has_bundle_class(vendor, "acme/foo-bundle", &package));
std::fs::write(
src.join("FooBundle.php"),
"<?php use Symfony\\Component\\HttpKernel\\Bundle\\AbstractBundle;",
)
.expect("w");
assert!(has_bundle_class(vendor, "acme/foo-bundle", &package));
}
#[test]
fn cache_keys_match_flex() {
assert_eq!(
cache_key("https://raw.githubusercontent.com/symfony/recipes/flex/main/index.json"),
"symfony-recipes-flex-main-index.json"
);
assert_eq!(
cache_key(
"https://raw.githubusercontent.com/symfony/recipes-contrib/flex/main/index.json"
),
"symfony-recipes-contrib-flex-main-index.json"
);
assert_eq!(
cache_key("https://example.com/a b.json"),
"https---example.com-a-b.json"
);
}
#[test]
fn endpoint_forms() {
std::env::remove_var("SYMFONY_ENDPOINT");
let d = endpoints(&serde_json::json!({})).expect("defaults");
assert_eq!(d.len(), 2);
let one = endpoints(
&serde_json::json!({"extra": {"symfony": {"endpoint": "https://x/index.json"}}}),
)
.expect("string");
assert_eq!(one[0], "https://x/index.json");
assert_eq!(one.len(), 3);
let arr = endpoints(
&serde_json::json!({"extra": {"symfony": {"endpoint": ["file:///tmp/i.json"]}}}),
)
.expect("array");
assert_eq!(arr, vec!["file:///tmp/i.json"]);
assert!(endpoints(
&serde_json::json!({"extra": {"symfony": {"endpoint": "https://flex.symfony.com"}}})
)
.is_err());
}
}