waterui_cli/
water_dir.rs

1//! Management of the `.water` directory for playground projects.
2//!
3//! The `.water` directory stores auto-generated backends for playground projects.
4//! A metadata file tracks the CLI commit hash to detect when backends need regeneration.
5
6use std::path::Path;
7
8use color_eyre::eyre;
9use smol::fs;
10
11/// The CLI commit hash embedded at build time.
12pub const CLI_COMMIT: &str = env!("WATERUI_CLI_COMMIT");
13
14const METADATA_FILE: &str = ".water/metadata";
15
16/// Ensure the `.water` directory is valid for the current CLI version.
17///
18/// If the CLI commit hash has changed since the `.water` directory was created,
19/// the entire directory is deleted to force regeneration of backends.
20///
21/// # Errors
22///
23/// Returns an error if file operations fail.
24pub async fn ensure_valid(project_root: &Path) -> eyre::Result<()> {
25    let metadata_path = project_root.join(METADATA_FILE);
26    let water_dir = project_root.join(".water");
27
28    if water_dir.exists() {
29        // Check if metadata exists and matches current CLI commit
30        let should_clean = if metadata_path.exists() {
31            let stored_commit = fs::read_to_string(&metadata_path).await.unwrap_or_default();
32            stored_commit.trim() != CLI_COMMIT
33        } else {
34            // No metadata file - old .water directory, clean it
35            true
36        };
37
38        if should_clean {
39            tracing::info!("CLI version changed, cleaning .water directory");
40            fs::remove_dir_all(&water_dir).await?;
41        }
42    }
43
44    // Ensure .water directory exists with metadata
45    if !water_dir.exists() {
46        fs::create_dir_all(&water_dir).await?;
47        fs::write(&metadata_path, CLI_COMMIT).await?;
48    }
49
50    Ok(())
51}