zoi-package 1.25.1

Advanced Package Manager & Environment Orchestrator
Documentation
use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{Result, anyhow};

/// The content of the Zoi Lua API definitions for LSP.
const ZOI_LUA_DEFINITIONS: &str = include_str!("./builtin/lsp/zoi.lua");

/// Sets up the LSP workspace for Zoi package development.
///
/// This creates the necessary Lua definitions and a `.luarc.json` file in the
/// given path.
///
/// # Errors
///
/// Returns an error if:
/// - The LSP definitions directory cannot be created.
/// - The `zoi.lua` definition file cannot be written.
/// - The `.luarc.json` file cannot be written.
pub fn setup_lsp_workspace(path: &Path) -> Result<()> {
    let lsp_dir = get_lsp_definitions_dir()?;
    fs::create_dir_all(&lsp_dir)?;

    let zoi_lua_path = lsp_dir.join("zoi.lua");
    fs::write(&zoi_lua_path, ZOI_LUA_DEFINITIONS)?;

    let luarc_path = path.join(".luarc.json");
    if !luarc_path.exists() {
        let luarc_content = generate_luarc_json(&lsp_dir)?;
        fs::write(luarc_path, luarc_content)?;
    }

    Ok(())
}

/// Returns the directory where Zoi LSP definitions are stored.
///
/// # Errors
///
/// Returns an error if the user's home directory cannot be found.
pub fn get_lsp_definitions_dir() -> Result<PathBuf> {
    let home_dir = zoi_core::utils::get_user_home()
        .ok_or_else(|| anyhow!("Could not find home directory."))?;
    Ok(home_dir.join(".zoi").join("lsp"))
}

/// Generates the content for a `.luarc.json` file that includes Zoi
/// definitions.
fn generate_luarc_json(lsp_dir: &Path) -> Result<String> {
    let lsp_dir_str = lsp_dir.to_string_lossy().replace('\\', "/");

    let json = serde_json::json!({
        "$schema": "https://raw.githubusercontent.com/sumneko/lua-language-server/master/setting/schema.json",
        "runtime": {
            "version": "Luau"
        },
        "workspace": {
            "library": [
                lsp_dir_str
            ],
            "checkThirdParty": false
        },
        "diagnostics": {
            "disable": [
                "lowercase-global"
            ],
            "globals": [
                "SYSTEM", "ZOI", "PKG", "BUILD_DIR", "STAGING_DIR", "BUILD_TYPE", "SUBPKG",
                "metadata", "dependencies", "updates", "hooks", "service", "prepare", "package",
                "verify", "test", "uninstall", "cmd", "zcp", "zlicense", "zdoc", "zman", "zsed", "zpatch", "zln", "zchmod", "zchown", "zmkdir",
                "zrm", "IMPORT", "INCLUDE", "verifyHash", "verifySignature", "addPgpKey", "UTILS"
            ]
        }
    });

    Ok(serde_json::to_string_pretty(&json)?)
}