first commit
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "prometeu-deps"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
description = ""
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
prometeu-core = { path = "../prometeu-core" }
|
||||
prometeu-language-api = { path = "../prometeu-language-api" }
|
||||
prometeu-languages-registry = { path = "../languages/prometeu-languages-registry" }
|
||||
anyhow = "1.0.101"
|
||||
camino = "1.2.2"
|
||||
walkdir = "2.5.0"
|
||||
serde_json = "1.0.149"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
@@ -0,0 +1,19 @@
|
||||
mod model;
|
||||
mod load_sources;
|
||||
mod workspace;
|
||||
|
||||
pub use workspace::resolve_workspace;
|
||||
pub use load_sources::load_sources;
|
||||
|
||||
pub use model::manifest::*;
|
||||
pub use model::resolved_project::ResolvedWorkspace;
|
||||
pub use model::deps_config::DepsConfig;
|
||||
pub use model::project_descriptor::ProjectDescriptor;
|
||||
pub use model::build_stack::BuildStack;
|
||||
pub use model::resolved_graph::ResolvedGraph;
|
||||
pub use model::loaded_sources::LoadedSources;
|
||||
pub use model::project_sources::ProjectSources;
|
||||
pub use model::loaded_file::LoadedFile;
|
||||
pub use model::cache_blobs::CacheBlobs;
|
||||
pub use model::cache_plan::CachePlan;
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
use anyhow::{Context, Result};
|
||||
use camino::Utf8PathBuf;
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use crate::{
|
||||
DepsConfig,
|
||||
LoadedFile,
|
||||
LoadedSources,
|
||||
ProjectSources,
|
||||
ResolvedWorkspace,
|
||||
};
|
||||
|
||||
pub fn load_sources(cfg: &DepsConfig, resolved: &ResolvedWorkspace) -> Result<LoadedSources> {
|
||||
let mut per_project = Vec::with_capacity(resolved.stack.projects.len());
|
||||
|
||||
for project_id in &resolved.stack.projects {
|
||||
let project = resolved
|
||||
.graph
|
||||
.project(project_id)
|
||||
.with_context(|| format!("deps: unknown project_id {:?} in build stack", project_id))?;
|
||||
|
||||
if cfg.explain {
|
||||
eprintln!(
|
||||
"[deps] load_sources: project {}@{} ({:?})",
|
||||
project.name, project.version, project.project_dir
|
||||
);
|
||||
}
|
||||
|
||||
let mut files: Vec<LoadedFile> = Vec::new();
|
||||
|
||||
for root in &project.source_roots {
|
||||
let abs_root = project.project_dir.join(root);
|
||||
|
||||
if cfg.explain {
|
||||
eprintln!("[deps] scanning {:?}", abs_root);
|
||||
}
|
||||
|
||||
if !abs_root.exists() {
|
||||
anyhow::bail!(
|
||||
"deps: source root does not exist for project {}@{}: {:?}",
|
||||
project.name,
|
||||
project.version,
|
||||
abs_root
|
||||
);
|
||||
}
|
||||
|
||||
// Walk recursively.
|
||||
for entry in WalkDir::new(&abs_root)
|
||||
.follow_links(false)
|
||||
.into_iter()
|
||||
.filter_map(|e| e.ok())
|
||||
{
|
||||
let ft = entry.file_type();
|
||||
if !ft.is_file() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let path = entry.path();
|
||||
|
||||
|
||||
// TODO: precisamos mexer no prometeu.json para configurar o frontend do projeto
|
||||
// Filter extensions: start with PBS only.
|
||||
if path.extension().and_then(|s| s.to_str()) != Some("pbs") {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Convert to Utf8Path (the best effort) and use a stable "uri".
|
||||
let path_utf8: Utf8PathBuf = match Utf8PathBuf::from_path_buf(path.to_path_buf()) {
|
||||
Ok(p) => p,
|
||||
Err(_) => {
|
||||
anyhow::bail!("deps: non-utf8 path found while scanning sources: {:?}", path);
|
||||
}
|
||||
};
|
||||
|
||||
let text = std::fs::read_to_string(&path_utf8)
|
||||
.with_context(|| format!("deps: failed to read source file {:?}", path_utf8))?;
|
||||
|
||||
// TODO: normalize newlines
|
||||
|
||||
files.push(LoadedFile {
|
||||
uri: path_utf8.to_string(),
|
||||
text,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Determinism: sort a file list by uri (important for stable builds).
|
||||
files.sort_by(|a, b| a.uri.cmp(&b.uri));
|
||||
|
||||
per_project.push(ProjectSources {
|
||||
project_id: project_id.clone(),
|
||||
files,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(LoadedSources { per_project })
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
use prometeu_core::ProjectId;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BuildStack {
|
||||
pub projects: Vec<ProjectId>,
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
/// Cache blobs computed/validated by deps.
|
||||
/// The pipeline may decide when to store, but deps executes IO and cache validity.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CacheBlobs {
|
||||
// placeholder
|
||||
pub _unused: (),
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CachePlan {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
use camino::Utf8PathBuf;
|
||||
|
||||
pub struct DepsConfig {
|
||||
pub explain: bool,
|
||||
pub cache_dir: Utf8PathBuf,
|
||||
pub registry_dirs: Vec<Utf8PathBuf>, // or sources ?
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LoadedFile {
|
||||
pub uri: String,
|
||||
pub text: String,
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
use crate::model::project_sources::ProjectSources;
|
||||
|
||||
/// Sources already loaded by deps (IO happens in deps, not in pipeline).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LoadedSources {
|
||||
/// For each project in the stack, a list of files (uri + text).
|
||||
pub per_project: Vec<ProjectSources>,
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
use camino::Utf8PathBuf;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Manifest {
|
||||
pub name: String,
|
||||
pub version: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub source_roots: Vec<String>,
|
||||
|
||||
pub language: LanguageDecl,
|
||||
|
||||
#[serde(default)]
|
||||
pub deps: Vec<DepDecl>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LanguageDecl {
|
||||
pub id: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum DepDecl {
|
||||
Local {
|
||||
path: String,
|
||||
},
|
||||
Git {
|
||||
git: String,
|
||||
rev: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PrometeuLock {
|
||||
pub schema: u32,
|
||||
#[serde(default)]
|
||||
pub mappings: Vec<LockMapping>,
|
||||
}
|
||||
|
||||
impl PrometeuLock {
|
||||
pub fn blank() -> Self {
|
||||
Self {
|
||||
schema: 0,
|
||||
mappings: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn lookup_git_local_dir(&self, url: &str, rev: &str) -> Option<&String> {
|
||||
self.mappings.iter().find_map(|m| match m {
|
||||
LockMapping::Git {
|
||||
git, rev: r, local_dir
|
||||
} if git == url && r == rev => Some(local_dir),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "kind", rename_all = "lowercase")]
|
||||
pub enum LockMapping {
|
||||
Git {
|
||||
git: String,
|
||||
rev: String,
|
||||
local_dir: String,
|
||||
},
|
||||
Registry {
|
||||
registry: String,
|
||||
version: String,
|
||||
local_dir: String,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
pub mod deps_config;
|
||||
pub mod project_descriptor;
|
||||
pub mod build_stack;
|
||||
pub mod resolved_graph;
|
||||
pub mod loaded_sources;
|
||||
pub mod project_sources;
|
||||
pub mod loaded_file;
|
||||
pub mod cache_blobs;
|
||||
pub mod resolved_project;
|
||||
pub mod cache_plan;
|
||||
pub mod manifest;
|
||||
@@ -0,0 +1,14 @@
|
||||
use camino::Utf8PathBuf;
|
||||
use prometeu_core::ProjectId;
|
||||
use prometeu_language_api::SourcePolicy;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ProjectDescriptor {
|
||||
pub project_id: ProjectId,
|
||||
pub name: String,
|
||||
pub version: String,
|
||||
pub project_dir: Utf8PathBuf,
|
||||
pub source_roots: Vec<Utf8PathBuf>,
|
||||
pub language_id: String,
|
||||
pub source_policy: SourcePolicy,
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
use prometeu_core::ProjectId;
|
||||
use crate::model::loaded_file::LoadedFile;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ProjectSources {
|
||||
pub project_id: ProjectId,
|
||||
pub files: Vec<LoadedFile>,
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
use prometeu_core::ProjectId;
|
||||
use crate::ProjectDescriptor;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ResolvedGraph {
|
||||
pub root: ProjectId,
|
||||
pub projects: Vec<ProjectDescriptor>, // arena
|
||||
// opcional: adjacency list para checks
|
||||
pub edges: Vec<Vec<ProjectId>>, // edges[from] = vec[to]
|
||||
}
|
||||
|
||||
impl ResolvedGraph {
|
||||
pub fn project(&self, id: &ProjectId) -> Option<&ProjectDescriptor> {
|
||||
self.projects.get(id.0 as usize)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
use prometeu_core::ProjectId;
|
||||
use crate::{BuildStack, ResolvedGraph};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ResolvedWorkspace {
|
||||
pub project_id: ProjectId,
|
||||
pub graph: ResolvedGraph,
|
||||
pub stack: BuildStack,
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
use anyhow::{Context, Result};
|
||||
use camino::{Utf8Path, Utf8PathBuf};
|
||||
|
||||
use crate::workspace::model::DepRef;
|
||||
|
||||
pub trait DepsHost {
|
||||
fn read_to_string(&self, path: &Utf8Path) -> Result<String>;
|
||||
|
||||
// fn ensure_project_local(&self, from_dir: &Utf8Path, dep: &DepRef) -> Result<Utf8PathBuf>;
|
||||
}
|
||||
|
||||
pub struct FsHost;
|
||||
|
||||
impl DepsHost for FsHost {
|
||||
fn read_to_string(&self, path: &Utf8Path) -> Result<String> {
|
||||
std::fs::read_to_string(path)
|
||||
.with_context(|| format!("failed to read {:?}", path))
|
||||
}
|
||||
|
||||
// fn ensure_project_local(&self, from_dir: &Utf8Path, dep: &DepRef) -> Result<Utf8PathBuf> {
|
||||
// match dep {
|
||||
// DepRef::Local { path } => {
|
||||
// let joined = from_dir.join(path);
|
||||
// let canon = joined.canonicalize()
|
||||
// .with_context(|| format!("deps: dep path does not exist: {:?}", joined))?;
|
||||
// Utf8PathBuf::from_path_buf(canon)
|
||||
// .map_err(|p| anyhow::anyhow!("deps: non-utf8 dep dir: {:?}", p))
|
||||
// }
|
||||
// _ => unimplemented!(),
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
mod resolve_workspace;
|
||||
mod host;
|
||||
mod model;
|
||||
mod phases;
|
||||
|
||||
pub use resolve_workspace::resolve_workspace;
|
||||
@@ -0,0 +1,31 @@
|
||||
use camino::Utf8PathBuf;
|
||||
use prometeu_core::ProjectId;
|
||||
use prometeu_language_api::SourcePolicy;
|
||||
|
||||
use crate::Manifest;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RawProjectNode {
|
||||
pub dir: Utf8PathBuf,
|
||||
pub manifest_path: Utf8PathBuf,
|
||||
pub manifest: Manifest,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum DepRef {
|
||||
Local {
|
||||
path: Utf8PathBuf
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ProjectNode {
|
||||
pub id: ProjectId,
|
||||
pub dir: Utf8PathBuf,
|
||||
pub name: String,
|
||||
pub version: String,
|
||||
pub source_roots: Vec<Utf8PathBuf>,
|
||||
pub language_id: String,
|
||||
pub deps: Vec<DepRef>,
|
||||
pub source_policy: SourcePolicy,
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
use crate::model::manifest::DepDecl;
|
||||
use crate::workspace::host::DepsHost;
|
||||
use crate::workspace::model::RawProjectNode;
|
||||
use crate::workspace::phases::state::ResolverState;
|
||||
use crate::Manifest;
|
||||
use anyhow::{anyhow, bail, Context, Result};
|
||||
use camino::Utf8PathBuf;
|
||||
use serde_json;
|
||||
use std::fs::canonicalize;
|
||||
|
||||
/// Phase 1: Discover all projects in the workspace.
|
||||
///
|
||||
/// - Reads `prometeu.json` from each pending project directory.
|
||||
/// - Parses `Manifest`.
|
||||
/// - Registers the raw node.
|
||||
/// - Enqueues local-path deps for discovery (v0).
|
||||
///
|
||||
/// Does NOT:
|
||||
/// - assign ProjectId
|
||||
/// - build edges
|
||||
/// - validate versions
|
||||
pub fn discover(
|
||||
cfg: &crate::DepsConfig,
|
||||
host: &dyn DepsHost,
|
||||
state: &mut ResolverState,
|
||||
) -> Result<()> {
|
||||
while let Some(canon_dir) = state.pending.pop_front() {
|
||||
// de-dup by directory
|
||||
if state.raw_by_dir.contains_key(&canon_dir) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let manifest_path = canon_dir.join("prometeu.json");
|
||||
if !manifest_path.exists() || !manifest_path.is_file() {
|
||||
bail!(
|
||||
"deps: manifest not found: expected a file {:?} (project dir {:?})",
|
||||
manifest_path,
|
||||
canon_dir
|
||||
);
|
||||
}
|
||||
|
||||
if cfg.explain {
|
||||
eprintln!("[deps][discover] reading {:?}", manifest_path);
|
||||
}
|
||||
|
||||
let text = host
|
||||
.read_to_string(&manifest_path)
|
||||
.with_context(|| format!("deps: failed to read manifest {:?}", manifest_path))?;
|
||||
|
||||
let manifest: Manifest = serde_json::from_str(&text)
|
||||
.with_context(|| format!("deps: invalid manifest JSON {:?}", manifest_path))?;
|
||||
|
||||
// Register raw node
|
||||
let raw_idx = state.raw.len();
|
||||
state.raw.push(RawProjectNode {
|
||||
dir: canon_dir.clone(),
|
||||
manifest_path: manifest_path.clone(),
|
||||
manifest: manifest.clone(),
|
||||
});
|
||||
state.raw_by_dir.insert(canon_dir.clone(), raw_idx);
|
||||
|
||||
for dep in &manifest.deps {
|
||||
match dep {
|
||||
DepDecl::Local { path } => {
|
||||
let dep_dir = canon_dir.join(path);
|
||||
|
||||
let dep_dir_std = dep_dir.canonicalize().with_context(|| {
|
||||
format!(
|
||||
"deps: dep path does not exist: {:?} (from {:?})",
|
||||
dep_dir, canon_dir
|
||||
)
|
||||
})?;
|
||||
|
||||
let dep_dir_canon = Utf8PathBuf::from_path_buf(dep_dir_std)
|
||||
.map_err(|p| anyhow!("deps: non-utf8 dep dir: {:?}", p))?;
|
||||
|
||||
if cfg.explain {
|
||||
eprintln!("[deps][discover] local dep '{}' -> {:?}", path, dep_dir_canon);
|
||||
}
|
||||
state.pending.push_back(dep_dir_canon);
|
||||
}
|
||||
|
||||
DepDecl::Git { git, rev } => {
|
||||
let Some(rev) = rev.as_deref() else {
|
||||
bail!(
|
||||
"deps: git dependency '{}' requires an explicit 'rev' (commit hash) for now",
|
||||
git
|
||||
);
|
||||
};
|
||||
|
||||
let Some(local_dir) = state.lock.lookup_git_local_dir(git, rev) else {
|
||||
bail!(
|
||||
"deps: git dependency requires prometeu.lock mapping, but entry not found: git='{}' rev='{}'",
|
||||
git,
|
||||
rev
|
||||
);
|
||||
};
|
||||
|
||||
// canonicalize the lock-provided local dir to keep identity stable
|
||||
let local_dir_std = canonicalize(local_dir)
|
||||
.with_context(|| format!("deps: prometeu.lock local_dir does not exist: {:?}", local_dir))?;
|
||||
|
||||
let local_dir_canon = Utf8PathBuf::from_path_buf(local_dir_std)
|
||||
.map_err(|p| anyhow!("deps: non-utf8 lock local_dir: {:?}", p))?;
|
||||
|
||||
// validate manifest exists at the mapped project root
|
||||
// (this check should not belong here, but it is ok)
|
||||
let mapped_manifest = local_dir_canon.join("prometeu.json");
|
||||
if !mapped_manifest.exists() || !mapped_manifest.is_file() {
|
||||
bail!(
|
||||
"deps: prometeu.lock maps git dep to {:?}, but manifest is missing: {:?}",
|
||||
local_dir_canon,
|
||||
mapped_manifest
|
||||
);
|
||||
}
|
||||
|
||||
if cfg.explain {
|
||||
eprintln!(
|
||||
"[deps][discover] git dep '{}' rev '{}' -> {:?}",
|
||||
git, rev, local_dir_canon
|
||||
);
|
||||
}
|
||||
|
||||
state.pending.push_back(local_dir_canon);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
use prometeu_core::ProjectId;
|
||||
|
||||
use crate::workspace::model::DepRef;
|
||||
use crate::workspace::phases::state::ResolverState;
|
||||
|
||||
/// Phase 3: Localize dependencies and build graph edges.
|
||||
///
|
||||
/// For each project node:
|
||||
/// - For each DepRef:
|
||||
/// - host.ensure_project_local(from_dir, dep) -> dep_dir (local on disk)
|
||||
/// - map dep_dir to ProjectId via st.by_dir
|
||||
/// - st.edges[from].push(dep_id)
|
||||
///
|
||||
/// v0 policy:
|
||||
/// - Only DepRef::LocalPath is supported.
|
||||
/// - Git/Registry cause a hard error (future extension point).
|
||||
pub fn localize(cfg: &crate::DepsConfig, state: &mut ResolverState) -> Result<()> {
|
||||
// Reset edges (allows re-run / deterministic behavior)
|
||||
for e in &mut state.edges {
|
||||
e.clear();
|
||||
}
|
||||
|
||||
for from_idx in 0..state.nodes.len() {
|
||||
let from_id: ProjectId = state.nodes[from_idx].id;
|
||||
let from_dir = state.nodes[from_idx].dir.clone();
|
||||
|
||||
if cfg.explain {
|
||||
eprintln!(
|
||||
"[deps][localize] from id={:?} dir={:?}",
|
||||
from_id, from_dir
|
||||
);
|
||||
}
|
||||
|
||||
// Clone deps to avoid borrow conflicts (simple + safe for now)
|
||||
let deps = state.nodes[from_idx].deps.clone();
|
||||
|
||||
for dep in deps {
|
||||
match &dep {
|
||||
DepRef::Local {
|
||||
path
|
||||
} => {
|
||||
let dep_id = state.by_dir.get(path).copied().with_context(|| {
|
||||
format!(
|
||||
"deps: localized dep dir {:?} was not discovered; \
|
||||
ensure the dep has a prometeu.json and is reachable via local paths",
|
||||
path
|
||||
)
|
||||
})?;
|
||||
state.edges[from_id.0 as usize].push(dep_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Optional: keep edges deterministic
|
||||
state.edges[from_id.0 as usize].sort_by_key(|id| id.0);
|
||||
state.edges[from_id.0 as usize].dedup();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
use crate::model::manifest::DepDecl;
|
||||
use crate::workspace::model::{DepRef, ProjectNode};
|
||||
use crate::workspace::phases::state::ResolverState;
|
||||
use anyhow::{anyhow, bail, Context, Result};
|
||||
use camino::Utf8PathBuf;
|
||||
use prometeu_core::ProjectId;
|
||||
use prometeu_languages_registry::get_language_spec;
|
||||
use std::fs::canonicalize;
|
||||
|
||||
/// Phase 2: Materialize projects (allocate ProjectId / arena nodes).
|
||||
///
|
||||
/// Inputs:
|
||||
/// - st.raw (RawProjectNode: dir + manifest)
|
||||
///
|
||||
/// Outputs:
|
||||
/// - st.nodes (ProjectNode arena)
|
||||
/// - st.by_dir (dir -> ProjectId)
|
||||
/// - st.edges (allocated adjacency lists, empty for now)
|
||||
/// - st.root (ProjectId for root_dir)
|
||||
///
|
||||
/// Does NOT:
|
||||
/// - resolve deps to local dirs (that's phase localize)
|
||||
/// - validate version conflicts/cycles
|
||||
/// - resolve language/source policy
|
||||
pub fn materialize(cfg: &crate::DepsConfig, state: &mut ResolverState) -> Result<()> {
|
||||
// Reset materialized state (allows rerun in future refactors/tests)
|
||||
state.nodes.clear();
|
||||
state.by_dir.clear();
|
||||
state.edges.clear();
|
||||
state.root = None;
|
||||
|
||||
state.nodes.reserve(state.raw.len());
|
||||
state.edges.reserve(state.raw.len());
|
||||
|
||||
for (idx, raw) in state.raw.iter().enumerate() {
|
||||
let id = ProjectId(idx as u32);
|
||||
|
||||
// Default source roots if omitted
|
||||
let source_roots: Vec<Utf8PathBuf> = raw
|
||||
.manifest
|
||||
.source_roots
|
||||
.iter()
|
||||
.map(|root| Utf8PathBuf::from(root))
|
||||
.collect();
|
||||
if source_roots.is_empty() {
|
||||
bail!(
|
||||
"deps: no source roots specified for project {}",
|
||||
raw.manifest.name
|
||||
)
|
||||
}
|
||||
|
||||
// Convert DepDecl -> DepRef (no localization yet)
|
||||
let mut deps: Vec<DepRef> = Vec::with_capacity(raw.manifest.deps.len());
|
||||
for d in &raw.manifest.deps {
|
||||
match d {
|
||||
DepDecl::Local { path } => {
|
||||
let joined = raw.dir.join(path);
|
||||
let dir_std = joined.canonicalize()
|
||||
.with_context(|| format!("deps: local dep path does not exist: {:?} (from {:?})", joined, raw.dir))?;
|
||||
|
||||
let dir_canon = Utf8PathBuf::from_path_buf(dir_std)
|
||||
.map_err(|p| anyhow!("deps: non-utf8 dep dir: {:?}", p))?;
|
||||
deps.push(DepRef::Local {
|
||||
path: dir_canon
|
||||
});
|
||||
}
|
||||
DepDecl::Git { git, rev } => {
|
||||
let Some(rev) = rev.as_deref() else {
|
||||
bail!(
|
||||
"deps: git dependency '{}' requires an explicit 'rev' (commit hash) for now",
|
||||
git
|
||||
);
|
||||
};
|
||||
|
||||
let Some(local_dir) = state.lock.lookup_git_local_dir(git, rev) else {
|
||||
bail!(
|
||||
"deps: git dependency requires prometeu.lock mapping, but entry not found: git='{}' rev='{}'",
|
||||
git,
|
||||
rev
|
||||
);
|
||||
};
|
||||
|
||||
// canonicalize the lock-provided local dir to keep identity stable
|
||||
let path = canonicalize(local_dir).with_context(|| {
|
||||
format!(
|
||||
"deps: prometeu.lock local_dir does not exist: {:?}",
|
||||
local_dir
|
||||
)
|
||||
})?;
|
||||
|
||||
let local_dir_canon = Utf8PathBuf::from_path_buf(path)
|
||||
.map_err(|p| anyhow!("deps: non-utf8 lock local_dir: {:?}", p))?;
|
||||
|
||||
deps.push(DepRef::Local {
|
||||
path: local_dir_canon,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.explain {
|
||||
eprintln!(
|
||||
"[deps][materialize] id={:?} {}@{} dir={:?} language={}",
|
||||
id, raw.manifest.name, raw.manifest.version, raw.dir, raw.manifest.language.id
|
||||
);
|
||||
}
|
||||
|
||||
let source_policy = get_language_spec(raw.manifest.language.id.as_str())
|
||||
.map(|spec| spec.source_policy.clone())
|
||||
.ok_or(anyhow!(
|
||||
"deps: unknown language spec: {}",
|
||||
raw.manifest.language.id
|
||||
))?;
|
||||
|
||||
// Record node
|
||||
state.nodes.push(ProjectNode {
|
||||
id,
|
||||
dir: raw.dir.clone(),
|
||||
name: raw.manifest.name.clone(),
|
||||
version: raw.manifest.version.clone(),
|
||||
source_roots,
|
||||
language_id: raw.manifest.language.id.clone(),
|
||||
deps,
|
||||
source_policy,
|
||||
});
|
||||
|
||||
state.by_dir.insert(raw.dir.clone(), id);
|
||||
state.edges.push(Vec::new());
|
||||
}
|
||||
|
||||
// Determine root id
|
||||
if let Some(root_id) = state.by_dir.get(&state.root_dir).copied() {
|
||||
state.root = Some(root_id);
|
||||
} else {
|
||||
// This should never happen if seed/discover worked.
|
||||
// Keep it as a hard failure (in a later validate phase you can convert to a nicer diagnostic).
|
||||
anyhow::bail!(
|
||||
"deps: root project dir {:?} was not discovered/materialized",
|
||||
state.root_dir
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
mod run_all;
|
||||
mod state;
|
||||
mod discover;
|
||||
mod materialize;
|
||||
mod localize;
|
||||
mod validate;
|
||||
mod policy;
|
||||
mod stack;
|
||||
|
||||
pub use run_all::run_all;
|
||||
@@ -0,0 +1,17 @@
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
use crate::workspace::phases::state::ResolverState;
|
||||
|
||||
pub fn policy(_cfg: &crate::DepsConfig, state: &mut ResolverState) -> Result<()> {
|
||||
for node in &state.nodes {
|
||||
if node.source_policy.extensions.is_empty() {
|
||||
bail!(
|
||||
"deps: project {}@{} has empty source_policy.extensions (language={})",
|
||||
node.name,
|
||||
node.version,
|
||||
node.language_id
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
use anyhow::{Context, Result};
|
||||
use camino::Utf8Path;
|
||||
|
||||
use crate::{BuildStack, DepsConfig, ProjectDescriptor, ResolvedGraph, ResolvedWorkspace};
|
||||
use crate::workspace::host::FsHost;
|
||||
use crate::workspace::phases::{discover, localize, materialize, policy, stack, state, validate};
|
||||
|
||||
pub fn run_all(cfg: &DepsConfig, fs_host: &FsHost, root_dir: &Utf8Path) -> Result<ResolvedWorkspace> {
|
||||
let mut st = state::seed(cfg, root_dir)?;
|
||||
|
||||
discover::discover(cfg, fs_host, &mut st)?;
|
||||
materialize::materialize(cfg, &mut st)?;
|
||||
localize::localize(cfg, &mut st)?;
|
||||
|
||||
validate::validate(cfg, &st)?;
|
||||
policy::policy(cfg, &mut st)?;
|
||||
|
||||
let build_stack: BuildStack = stack::stack(cfg, &mut st)?;
|
||||
|
||||
let root = st
|
||||
.root
|
||||
.context("deps: internal error: root ProjectId not set")?;
|
||||
|
||||
// Build the arena expected by ResolvedGraph: index == ProjectId.0
|
||||
// materialize already assigns ProjectId(idx), so st.nodes order is stable.
|
||||
let mut projects: Vec<ProjectDescriptor> = Vec::with_capacity(st.nodes.len());
|
||||
for n in &st.nodes {
|
||||
projects.push(ProjectDescriptor {
|
||||
project_id: n.id,
|
||||
name: n.name.clone(),
|
||||
version: n.version.clone(),
|
||||
project_dir: n.dir.clone(),
|
||||
source_roots: n.source_roots.clone(),
|
||||
language_id: n.language_id.clone(),
|
||||
source_policy: n.source_policy.clone(),
|
||||
});
|
||||
}
|
||||
|
||||
let graph = ResolvedGraph {
|
||||
root,
|
||||
projects,
|
||||
edges: st.edges,
|
||||
};
|
||||
|
||||
Ok(ResolvedWorkspace {
|
||||
project_id: root,
|
||||
graph,
|
||||
stack: build_stack,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
use anyhow::{Context, Result};
|
||||
use prometeu_core::ProjectId;
|
||||
use std::collections::VecDeque;
|
||||
use crate::BuildStack;
|
||||
use crate::workspace::phases::state::ResolverState;
|
||||
|
||||
/// Phase: BuildStack (deps-first topo order).
|
||||
///
|
||||
/// Output:
|
||||
/// - state.stack: Vec<ProjectId> where deps appear before dependents.
|
||||
///
|
||||
/// Determinism:
|
||||
/// - ties are resolved by ProjectId order (stable across runs if discovery is stable).
|
||||
pub fn stack(cfg: &crate::DepsConfig, state: &mut ResolverState) -> Result<BuildStack> {
|
||||
let n = state.nodes.len();
|
||||
let _root = state.root.context("deps: internal error: root ProjectId not set")?;
|
||||
|
||||
// Build indegree
|
||||
let mut indeg = vec![0usize; n];
|
||||
for outs in &state.edges {
|
||||
for &to in outs {
|
||||
indeg[to.0 as usize] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Deterministic queue: push in ProjectId order
|
||||
let mut q = VecDeque::new();
|
||||
for i in 0..n {
|
||||
if indeg[i] == 0 {
|
||||
q.push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
let mut order: Vec<ProjectId> = Vec::with_capacity(n);
|
||||
|
||||
while let Some(i) = q.pop_front() {
|
||||
order.push(ProjectId(i as u32));
|
||||
|
||||
// Ensure deterministic traversal of outgoing edges too
|
||||
// (your localize already sort/dedup edges, but this doesn't hurt)
|
||||
for &to in &state.edges[i] {
|
||||
let j = to.0 as usize;
|
||||
indeg[j] -= 1;
|
||||
if indeg[j] == 0 {
|
||||
// Deterministic insert: keep queue ordered by ProjectId
|
||||
// Simple O(n) insertion is fine for now.
|
||||
insert_sorted_by_id(&mut q, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If validate ran, this should already be cycle-free; still keep a guard.
|
||||
if order.len() != n {
|
||||
anyhow::bail!(
|
||||
"deps: internal error: stack generation did not visit all nodes ({} of {})",
|
||||
order.len(),
|
||||
n
|
||||
);
|
||||
}
|
||||
|
||||
if cfg.explain {
|
||||
eprintln!("[deps][stack] build order:");
|
||||
for id in &order {
|
||||
let node = &state.nodes[id.0 as usize];
|
||||
eprintln!(" - {:?} {}@{} dir={:?}", id, node.name, node.version, node.dir);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(BuildStack {
|
||||
projects: order,
|
||||
})
|
||||
}
|
||||
|
||||
/// Insert node index `i` into queue `q` keeping it sorted by ProjectId (index).
|
||||
fn insert_sorted_by_id(q: &mut VecDeque<usize>, i: usize) {
|
||||
// Common fast path: append if >= last
|
||||
if let Some(&last) = q.back() {
|
||||
if i >= last {
|
||||
q.push_back(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise find insertion point
|
||||
let mut pos = 0usize;
|
||||
for &v in q.iter() {
|
||||
if i < v {
|
||||
break;
|
||||
}
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
// VecDeque has no insert, so rebuild (small sizes OK for hard reset)
|
||||
let mut tmp: Vec<usize> = q.drain(..).collect();
|
||||
tmp.insert(pos, i);
|
||||
*q = VecDeque::from(tmp);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
use camino::{Utf8Path, Utf8PathBuf};
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use anyhow::Context;
|
||||
use crate::workspace::model::{RawProjectNode, ProjectNode};
|
||||
use prometeu_core::ProjectId;
|
||||
use crate::PrometeuLock;
|
||||
use serde_json;
|
||||
|
||||
pub struct ResolverState {
|
||||
pub root_dir: Utf8PathBuf,
|
||||
|
||||
// phase1 output
|
||||
pub raw: Vec<RawProjectNode>,
|
||||
pub raw_by_dir: HashMap<Utf8PathBuf, usize>,
|
||||
pub pending: VecDeque<Utf8PathBuf>,
|
||||
|
||||
// phase2+
|
||||
pub nodes: Vec<ProjectNode>,
|
||||
pub by_dir: HashMap<Utf8PathBuf, ProjectId>,
|
||||
pub edges: Vec<Vec<ProjectId>>,
|
||||
|
||||
pub root: Option<ProjectId>,
|
||||
|
||||
pub lock: PrometeuLock,
|
||||
}
|
||||
|
||||
pub fn seed(_cfg: &crate::DepsConfig, root_dir: &Utf8Path) -> anyhow::Result<ResolverState> {
|
||||
let path_buf = root_dir.canonicalize()?;
|
||||
let root_dir_canon = Utf8PathBuf::from_path_buf(path_buf)
|
||||
.map_err(|p| anyhow::anyhow!("deps: non-utf8 root dir: {:?}", p))?;
|
||||
|
||||
let lock_path = root_dir_canon.join("prometeu.lock");
|
||||
let lock = if lock_path.exists() {
|
||||
let txt = std::fs::read_to_string(&lock_path)?;
|
||||
serde_json::from_str::<PrometeuLock>(&txt)
|
||||
.with_context(|| format!("invalid prometeu.lock at {:?}", lock_path))?
|
||||
} else {
|
||||
PrometeuLock::blank()
|
||||
};
|
||||
|
||||
let mut pending = VecDeque::new();
|
||||
pending.push_back(root_dir_canon.clone());
|
||||
|
||||
Ok(ResolverState {
|
||||
root_dir: root_dir_canon.clone(),
|
||||
raw: vec![],
|
||||
raw_by_dir: HashMap::new(),
|
||||
pending,
|
||||
|
||||
nodes: vec![],
|
||||
by_dir: HashMap::new(),
|
||||
edges: vec![],
|
||||
|
||||
root: None,
|
||||
|
||||
lock,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
use anyhow::{bail, Context, Result};
|
||||
use prometeu_core::ProjectId;
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
|
||||
use crate::workspace::phases::state::ResolverState;
|
||||
|
||||
/// Phase: Validate workspace graph & invariants (v0).
|
||||
///
|
||||
/// Checks:
|
||||
/// - root present
|
||||
/// - edges are in-range
|
||||
/// - no cycles
|
||||
/// - no version conflicts for same project name
|
||||
pub fn validate(cfg: &crate::DepsConfig, state: &ResolverState) -> Result<()> {
|
||||
// 1) root present
|
||||
let root = state.root.context("deps: internal error: root ProjectId not set")?;
|
||||
if cfg.explain {
|
||||
eprintln!("[deps][validate] root={:?}", root);
|
||||
}
|
||||
|
||||
// 2) edges sanity
|
||||
let n = state.nodes.len();
|
||||
for (from_idx, outs) in state.edges.iter().enumerate() {
|
||||
for &to in outs {
|
||||
let to_idx = to.0 as usize;
|
||||
if to_idx >= n {
|
||||
bail!(
|
||||
"deps: invalid edge: from {:?} -> {:?} (to out of range; nodes={})",
|
||||
ProjectId(from_idx as u32),
|
||||
to,
|
||||
n
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3) version conflicts by name
|
||||
// name -> (version -> ProjectId)
|
||||
let mut by_name: HashMap<&str, HashMap<&str, ProjectId>> = HashMap::new();
|
||||
for node in &state.nodes {
|
||||
let vmap = by_name.entry(node.name.as_str()).or_default();
|
||||
vmap.entry(node.version.as_str()).or_insert(node.id);
|
||||
}
|
||||
for (name, versions) in &by_name {
|
||||
if versions.len() > 1 {
|
||||
// create deterministic message
|
||||
let mut vs: Vec<(&str, ProjectId)> = versions.iter().map(|(v, id)| (*v, *id)).collect();
|
||||
vs.sort_by(|a, b| a.0.cmp(b.0));
|
||||
|
||||
let mut msg = format!("deps: version conflict for project '{}':", name);
|
||||
for (v, id) in vs {
|
||||
let dir = &state.nodes[id.0 as usize].dir;
|
||||
msg.push_str(&format!("\n - {} at {:?} (id={:?})", v, dir, id));
|
||||
}
|
||||
bail!(msg);
|
||||
}
|
||||
}
|
||||
|
||||
// 4) cycle detection (Kahn + leftover nodes)
|
||||
// Build indegree
|
||||
let mut indeg = vec![0usize; n];
|
||||
for outs in &state.edges {
|
||||
for &to in outs {
|
||||
indeg[to.0 as usize] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let mut q = VecDeque::new();
|
||||
for i in 0..n {
|
||||
if indeg[i] == 0 {
|
||||
q.push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
let mut visited = 0usize;
|
||||
while let Some(i) = q.pop_front() {
|
||||
visited += 1;
|
||||
for &to in &state.edges[i] {
|
||||
let j = to.0 as usize;
|
||||
indeg[j] -= 1;
|
||||
if indeg[j] == 0 {
|
||||
q.push_back(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if visited != n {
|
||||
// Nodes with indeg>0 are part of cycles (or downstream of them)
|
||||
let mut cyclic: Vec<ProjectId> = Vec::new();
|
||||
for i in 0..n {
|
||||
if indeg[i] > 0 {
|
||||
cyclic.push(ProjectId(i as u32));
|
||||
}
|
||||
}
|
||||
|
||||
// Deterministic error output
|
||||
cyclic.sort_by_key(|id| id.0);
|
||||
|
||||
let mut msg = "deps: dependency cycle detected among:".to_string();
|
||||
for id in cyclic {
|
||||
let node = &state.nodes[id.0 as usize];
|
||||
msg.push_str(&format!("\n - {:?} {}@{} dir={:?}", id, node.name, node.version, node.dir));
|
||||
}
|
||||
bail!(msg);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
use anyhow::Result;
|
||||
use camino::Utf8Path;
|
||||
|
||||
use crate::{DepsConfig, ResolvedWorkspace};
|
||||
use crate::workspace::host::FsHost;
|
||||
|
||||
pub fn resolve_workspace(cfg: &DepsConfig, root_dir: &Utf8Path) -> Result<ResolvedWorkspace> {
|
||||
let host = FsHost;
|
||||
crate::workspace::phases::run_all(cfg, &host, root_dir)
|
||||
}
|
||||
Reference in New Issue
Block a user