use crate::args::Cli;
use crate::plugin_defaults::{self, PluginSelectionMode};
use anyhow::{Context, Result, bail};
use sqry_core::graph::CodeGraph;
use sqry_core::graph::unified::build::{BuildConfig, build_unified_graph_with_progress};
use sqry_core::graph::unified::persistence::{GraphStorage, load_from_path};
use sqry_core::progress::{SharedReporter, no_op_reporter};
use std::path::Path;
#[derive(Debug, Clone, Default)]
pub struct GraphLoadConfig {
pub include_hidden: bool,
pub follow_symlinks: bool,
pub max_depth: Option<usize>,
pub force_build: bool,
}
#[allow(dead_code)]
pub fn load_unified_graph(root: &Path, config: &GraphLoadConfig) -> Result<CodeGraph> {
load_unified_graph_with_progress_and_plugins(
root,
config,
&sqry_plugin_registry::create_plugin_manager_all(),
no_op_reporter(),
)
}
pub fn load_unified_graph_for_cli(
root: &Path,
config: &GraphLoadConfig,
cli: &Cli,
) -> Result<CodeGraph> {
let resolved_plugins =
plugin_defaults::resolve_plugin_selection(cli, root, PluginSelectionMode::ReadOnly)?;
load_unified_graph_with_progress_and_plugins(
root,
config,
&resolved_plugins.plugin_manager,
no_op_reporter(),
)
}
#[allow(dead_code)]
pub fn load_unified_graph_with_progress(
root: &Path,
config: &GraphLoadConfig,
progress: SharedReporter,
) -> Result<CodeGraph> {
load_unified_graph_with_progress_and_plugins(
root,
config,
&sqry_plugin_registry::create_plugin_manager_all(),
progress,
)
}
fn load_unified_graph_with_progress_and_plugins(
root: &Path,
config: &GraphLoadConfig,
plugins: &sqry_core::plugin::PluginManager,
progress: SharedReporter,
) -> Result<CodeGraph> {
if !root.exists() {
bail!("Path {} does not exist", root.display());
}
if config.force_build {
log::info!("Force build enabled, skipping snapshot load");
} else {
let storage = GraphStorage::new(root);
if storage.exists() {
log::info!(
"Loading unified graph from snapshot: {}",
storage.snapshot_path().display()
);
match load_from_path(storage.snapshot_path(), Some(plugins)) {
Ok(mut graph) => {
log::info!("Loaded graph from snapshot");
if let Ok(manifest) = storage.load_manifest()
&& !manifest.confidence.is_empty()
{
log::debug!(
"Restoring confidence metadata for {} languages",
manifest.confidence.len()
);
graph.set_confidence(manifest.confidence);
}
return Ok(graph);
}
Err(e) => {
bail!(
"Index at {} is corrupted or incomplete ({}). Run `sqry index --force` to rebuild.",
root.display(),
e
);
}
}
}
}
log::info!(
"Building unified graph from source files in {}",
root.display()
);
let build_config = BuildConfig {
include_hidden: config.include_hidden,
follow_links: config.follow_symlinks,
max_depth: config.max_depth,
num_threads: None,
..BuildConfig::default()
};
let (graph, _effective_threads) =
build_unified_graph_with_progress(root, plugins, &build_config, progress)
.context("Failed to build unified graph")?;
log::info!("Built unified graph with {} nodes", graph.node_count());
Ok(graph)
}