vtcode-config 0.183.0

Config loader components shared across VT Code and downstream adopters
Documentation
use std::fs;
use std::path::{Path, PathBuf};
use vtcode_commons::VtCodePaths;

use anyhow::{Context, Result};

use super::config::VTCodeConfig;
use crate::defaults::{self, ConfigDefaultsProvider};

const DEFAULT_GITIGNORE_FILE_NAME: &str = ".vtcodegitignore";

/// Determine where configuration and gitignore files should be created when
/// bootstrapping a workspace.
fn determine_bootstrap_targets(
    workspace: &Path,
    use_home_dir: bool,
    config_file_name: &str,
    defaults_provider: &dyn ConfigDefaultsProvider,
) -> Result<(PathBuf, PathBuf)> {
    if use_home_dir {
        if let Some(home_config_path) = select_home_config_path(defaults_provider, config_file_name)? {
            let gitignore_path = gitignore_path_for(&home_config_path);
            return Ok((home_config_path, gitignore_path));
        }
    }

    let config_path = workspace.join(config_file_name);
    let gitignore_path = workspace.join(DEFAULT_GITIGNORE_FILE_NAME);
    Ok((config_path, gitignore_path))
}

/// Returns the preferred gitignore path for a given configuration file.
fn gitignore_path_for(config_path: &Path) -> PathBuf {
    config_path
        .parent()
        .map(|parent| parent.join(DEFAULT_GITIGNORE_FILE_NAME))
        .unwrap_or_else(|| PathBuf::from(DEFAULT_GITIGNORE_FILE_NAME))
}

/// Ensures the parent directory for the provided path exists, creating it if
/// necessary.
fn ensure_parent_dir(path: &Path) -> Result<()> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).with_context(|| format!("Failed to create directory: {}", parent.display()))?;
    }

    Ok(())
}

/// Ensures a canonical user-level configuration parent exists privately.
fn ensure_private_parent_dir(path: &Path) -> Result<()> {
    if let Some(parent) = path.parent() {
        let _ = VtCodePaths::ensure_user_dir(parent)
            .with_context(|| format!("Failed to create private directory: {}", parent.display()))?;
    }

    Ok(())
}

/// Selects the canonical user configuration path from the defaults provider.
fn select_home_config_path(
    defaults_provider: &dyn ConfigDefaultsProvider,
    config_file_name: &str,
) -> Result<Option<PathBuf>> {
    defaults_provider
        .canonical_user_config_path(config_file_name)
        .context("Failed to resolve canonical user configuration path")
}

impl VTCodeConfig {
    /// Bootstrap project with config + gitignore
    pub fn bootstrap_project<P: AsRef<Path>>(workspace: P, force: bool) -> Result<Vec<String>> {
        Self::bootstrap_project_with_options(workspace, force, false)
    }

    /// Bootstrap project with config + gitignore, with option to create in home directory
    pub fn bootstrap_project_with_options<P: AsRef<Path>>(
        workspace: P,
        force: bool,
        use_home_dir: bool,
    ) -> Result<Vec<String>> {
        let workspace = workspace.as_ref().to_path_buf();
        defaults::with_config_defaults(|provider| {
            Self::bootstrap_project_with_provider(&workspace, force, use_home_dir, provider)
        })
    }

    /// Bootstrap project files using the supplied [`ConfigDefaultsProvider`].
    fn bootstrap_project_with_provider<P: AsRef<Path>>(
        workspace: P,
        force: bool,
        use_home_dir: bool,
        defaults_provider: &dyn ConfigDefaultsProvider,
    ) -> Result<Vec<String>> {
        let workspace = workspace.as_ref();
        let config_file_name = defaults_provider.config_file_name().to_string();
        let (config_path, gitignore_path) =
            determine_bootstrap_targets(workspace, use_home_dir, &config_file_name, defaults_provider)?;
        let vtcode_readme_path = workspace.join(".vtcode").join("README.md");
        let home_targets = use_home_dir && config_path != workspace.join(&config_file_name);
        if home_targets {
            ensure_private_parent_dir(&config_path)?;
            ensure_private_parent_dir(&gitignore_path)?;
        } else {
            ensure_parent_dir(&config_path)?;
            ensure_parent_dir(&gitignore_path)?;
        }
        ensure_parent_dir(&vtcode_readme_path)?;

        let mut created_files = Vec::new();

        if !config_path.exists() || force {
            let config_content = Self::default_vtcode_toml_template();

            fs::write(&config_path, config_content)
                .with_context(|| format!("Failed to write config file: {}", config_path.display()))?;

            if let Some(file_name) = config_path.file_name().and_then(|name| name.to_str()) {
                created_files.push(file_name.to_string());
            }
        }

        if !gitignore_path.exists() || force {
            let gitignore_content = Self::default_vtcode_gitignore();
            fs::write(&gitignore_path, gitignore_content)
                .with_context(|| format!("Failed to write gitignore file: {}", gitignore_path.display()))?;

            if let Some(file_name) = gitignore_path.file_name().and_then(|name| name.to_str()) {
                created_files.push(file_name.to_string());
            }
        }

        if !vtcode_readme_path.exists() || force {
            let vtcode_readme = Self::default_vtcode_readme_template();
            fs::write(&vtcode_readme_path, vtcode_readme)
                .with_context(|| format!("Failed to write VT Code README: {}", vtcode_readme_path.display()))?;
            created_files.push(".vtcode/README.md".to_string());
        }

        Ok(created_files)
    }

    /// Generate the default `vtcode.toml` template used by bootstrap helpers.
    fn default_vtcode_toml_template() -> String {
        include_str!("../../data/default_config.toml").to_owned()
    }

    fn default_vtcode_gitignore() -> String {
        r#"# Security-focused exclusions
.env, .env.local, secrets/, .aws/, .ssh/

# Development artifacts
target/, build/, dist/, node_modules/, vendor/

# Database files
*.db, *.sqlite, *.sqlite3

# Binary files
*.exe, *.dll, *.so, *.dylib, *.bin

# IDE files (comprehensive)
.vscode/, .idea/, *.swp, *.swo
"#
        .to_string()
    }

    fn default_vtcode_readme_template() -> &'static str {
        "# VT Code Workspace Files\n\n- Put always-on repository guidance in `AGENTS.md`.\n- Put path-scoped prompt rules in `.vtcode/rules/*.md` using YAML frontmatter.\n- Keep authoring notes and other workspace docs outside `.vtcode/rules/` so they are not loaded into prompt memory.\n"
    }

    /// Create sample configuration file
    pub fn create_sample_config<P: AsRef<Path>>(output: P) -> Result<()> {
        let output = output.as_ref();
        let config_content = Self::default_vtcode_toml_template();

        fs::write(output, config_content)
            .with_context(|| format!("Failed to write config file: {}", output.display()))?;

        Ok(())
    }
}

#[cfg(test)]
mod tests;