zoi-resolver 1.25.2

Advanced Package Manager & Environment Orchestrator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! Local package management and store interaction.
//!
//! This module provides functions for interacting with the local package store,
//! listing installed packages, and managing package manifests and dependencies.

use std::fs;
use std::path::{Path, PathBuf};

use anyhow::Result;
use walkdir::WalkDir;
use zoi_core::types::{self, InstallManifest, Scope};
use zoi_core::{config, utils};

use crate::resolve::{PackageRequest, get_db_root};

/// Returns the base directory of the package store for a given scope.
///
/// # Errors
///
/// Returns an error if the store base directory cannot be determined.
pub fn get_store_base_dir(scope: Scope) -> Result<PathBuf> {
    utils::get_store_base_dir(scope)
}

/// Returns the directory for a specific package in the store.
///
/// # Errors
///
/// Returns an error if the package directory path cannot be constructed.
pub fn get_package_dir(
    scope: Scope,
    registry_handle: &str,
    repo_path: &str,
    package_name: &str
) -> Result<PathBuf> {
    let base_dir = get_store_base_dir(scope)?;
    let package_id =
        utils::generate_package_id(registry_handle, repo_path, package_name);
    let package_dir_name =
        utils::get_package_dir_name(&package_id, package_name);
    Ok(base_dir.join(package_dir_name))
}

/// Returns the directory for a specific version of a package in the store.
///
/// # Errors
///
/// Returns an error if the package version directory path cannot be
/// constructed.
pub fn get_package_version_dir(
    scope: Scope,
    registry_handle: &str,
    repo_path: &str,
    package_name: &str,
    version: &str
) -> Result<PathBuf> {
    let package_dir =
        get_package_dir(scope, registry_handle, repo_path, package_name)?;
    Ok(package_dir.join(version))
}

use rayon::prelude::*;

/// Returns a list of all installed packages across all scopes.
///
/// # Errors
///
/// Returns an error if the store cannot be accessed or manifests cannot be
/// read.
pub fn get_installed_packages() -> Result<Vec<InstallManifest>> {
    let scopes = [Scope::User, Scope::System, Scope::Project];

    let installed: Vec<InstallManifest> = scopes
        .into_par_iter()
        .map(|scope| {
            let mut manifests = Vec::new();
            if let Ok(store_root) = get_store_base_dir(scope)
                && store_root.exists()
                && let Ok(entries) = fs::read_dir(store_root)
            {
                for entry in entries.flatten() {
                    let path = entry.path();
                    if !path.is_dir() {
                        continue;
                    }
                    let latest_path = path.join("latest");
                    if (latest_path.is_symlink() || latest_path.is_dir())
                        && let Ok(sub_entries) = fs::read_dir(&latest_path)
                    {
                        for sub_entry in sub_entries.flatten() {
                            let file_name = sub_entry
                                .file_name()
                                .to_string_lossy()
                                .to_string();
                            if file_name.starts_with("manifest")
                                && std::path::Path::new(&file_name)
                                    .extension()
                                    .is_some_and(|ext| {
                                        ext.eq_ignore_ascii_case("yaml")
                                    })
                            {
                                let manifest_path = sub_entry.path();
                                if manifest_path.exists()
                                    && let Ok(content) =
                                        fs::read_to_string(manifest_path)
                                    && let Ok(manifest) =
                                        serde_yaml::from_str::<InstallManifest>(
                                            &content
                                        )
                                {
                                    manifests.push(manifest);
                                }
                            }
                        }
                    }
                }
            }
            manifests
        })
        .flatten()
        .collect();

    let mut sorted_installed = installed;
    sorted_installed.sort_by(|a, b| a.name.cmp(&b.name));
    Ok(sorted_installed)
}

/// Represents an installed package with basic metadata.
#[derive(Debug)]
pub struct InstalledPackage {
    /// The name of the package.
    pub name: String,
    /// The sub-package name, if any.
    pub sub_package: Option<String>,
    /// The installed version.
    pub version: String,
    /// The repository the package was installed from.
    pub repo: String,
    /// The type of package.
    pub package_type: zoi_core::types::PackageType
}

/// Returns a list of all installed packages with their basic metadata.
///
/// # Errors
///
/// Returns an error if installed packages cannot be retrieved.
pub fn get_installed_packages_with_type() -> Result<Vec<InstalledPackage>> {
    let manifests = get_installed_packages()?;
    Ok(manifests
        .into_iter()
        .map(|m| InstalledPackage {
            name: m.name,
            sub_package: m.sub_package,
            version: m.version,
            repo: m.repo,
            package_type: m.package_type
        })
        .collect())
}

/// Checks if a package is installed in a given scope and returns its manifest
/// if found.
///
/// # Errors
///
/// Returns an error if the store cannot be accessed or manifests cannot be
/// read.
pub fn is_package_installed(
    package_name: &str,
    sub_package_name: Option<&str>,
    scope: Scope
) -> Result<Option<InstallManifest>> {
    let store_root = get_store_base_dir(scope)?;
    if !store_root.exists() {
        return Ok(None);
    }

    for entry in fs::read_dir(store_root)? {
        let entry = entry?;
        let path = entry.path();
        if !path.is_dir() {
            continue;
        }

        if let Some(file_name) = path.file_name().and_then(|n| n.to_str()) {
            let parts: Vec<&str> = file_name.splitn(2, '-').collect();
            if parts.len() == 2
                && parts.get(1) == Some(&package_name)
                && parts.first().is_some_and(|p| p.len() == 32)
            {
                let latest_path = path.join("latest");
                if (latest_path.is_symlink() || latest_path.is_dir())
                    && let Ok(entries) = fs::read_dir(&latest_path)
                {
                    for entry in entries.filter_map(Result::ok) {
                        let file_name =
                            entry.file_name().to_string_lossy().to_string();
                        if file_name.starts_with("manifest")
                            && std::path::Path::new(&file_name)
                                .extension()
                                .is_some_and(|ext| {
                                    ext.eq_ignore_ascii_case("yaml")
                                })
                        {
                            let manifest_path = entry.path();
                            if manifest_path.exists() {
                                let content =
                                    fs::read_to_string(manifest_path)?;
                                let manifest: InstallManifest =
                                    serde_yaml::from_str(&content)?;
                                if manifest.name == package_name
                                    && manifest.sub_package.as_deref()
                                        == sub_package_name
                                {
                                    return Ok(Some(manifest));
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    Ok(None)
}

/// Returns all installed manifests in a specific scope.
///
/// # Errors
///
/// Returns an error if the store cannot be accessed or manifests cannot be
/// read.
pub fn get_installed_manifests_in_scope(
    scope: Scope
) -> Result<Vec<InstallManifest>> {
    let store_root = get_store_base_dir(scope)?;
    if !store_root.exists() {
        return Ok(Vec::new());
    }

    let mut manifests = Vec::new();
    for entry in fs::read_dir(store_root)? {
        let entry = entry?;
        let path = entry.path();
        if !path.is_dir() {
            continue;
        }

        let latest_path = path.join("latest");
        if !(latest_path.is_symlink() || latest_path.is_dir()) {
            continue;
        }

        let Ok(entries) = fs::read_dir(&latest_path) else {
            continue;
        };

        for entry in entries.filter_map(Result::ok) {
            let file_name = entry.file_name().to_string_lossy().to_string();
            if !file_name.starts_with("manifest")
                || !std::path::Path::new(&file_name)
                    .extension()
                    .is_some_and(|ext| ext.eq_ignore_ascii_case("yaml"))
            {
                continue;
            }

            let manifest_path = entry.path();
            if !manifest_path.exists() {
                continue;
            }

            let content = fs::read_to_string(manifest_path)?;
            let manifest: InstallManifest = serde_yaml::from_str(&content)?;
            manifests.push(manifest);
        }
    }

    Ok(manifests)
}

/// Finds installed manifests matching a package request in a specific scope.
///
/// # Errors
///
/// Returns an error if installed manifests cannot be retrieved.
pub fn find_installed_manifests_matching(
    request: &PackageRequest,
    scope: Scope
) -> Result<Vec<InstallManifest>> {
    let manifests = get_installed_manifests_in_scope(scope)?;
    Ok(manifests
        .into_iter()
        .filter(|manifest| {
            manifest.name == request.name
                && manifest.sub_package == request.sub_package
                && request
                    .handle
                    .as_ref()
                    .is_none_or(|handle| manifest.registry_handle == *handle)
                && request
                    .repo
                    .as_ref()
                    .is_none_or(|repo| manifest.repo == *repo)
                && request
                    .version_spec
                    .as_ref()
                    .is_none_or(|version| manifest.version == *version)
        })
        .collect())
}

/// Formats a package source string from its components.
pub fn package_source_string(
    registry_handle: &str,
    repo: &str,
    name: &str,
    sub_package: Option<&str>,
    version: &str
) -> String {
    let registry_handle = registry_handle.trim();
    let repo = repo.trim();
    let name = name.trim();
    let version = version.trim();

    if let Some(sub_package) = sub_package {
        format!(
            "#{}@{}/{}:{}@{}",
            registry_handle,
            repo,
            name,
            sub_package.trim(),
            version
        )
    } else {
        format!("#{registry_handle}@{repo}/{name}@{version}")
    }
}

/// Returns the source string for an installed manifest.
pub fn installed_manifest_source(manifest: &InstallManifest) -> String {
    package_source_string(
        &manifest.registry_handle,
        &manifest.repo,
        &manifest.name,
        manifest.sub_package.as_deref(),
        &manifest.version
    )
}

/// Returns all available packages from a list of repositories.
///
/// # Errors
///
/// Returns an error if the database root cannot be determined or if package
/// files cannot be parsed.
pub fn get_packages_from_repos(
    repos: &[String]
) -> Result<Vec<zoi_core::types::Package>> {
    let db_root = get_db_root()?;
    if !db_root.exists() {
        return Err(anyhow::anyhow!(
            "Package database not found. Please run 'zoi sync' first."
        ));
    }

    let mut available = Vec::new();

    for repo_name in repos {
        let repo_path = db_root.join(repo_name);
        if !repo_path.exists() {
            continue;
        }
        for entry in WalkDir::new(repo_path).into_iter().filter_map(Result::ok)
        {
            if !entry.file_type().is_dir() {
                continue;
            }

            let pkg_name = entry.file_name().to_string_lossy();
            let pkg_file_path =
                entry.path().join(format!("{pkg_name}.pkg.lua"));

            if pkg_file_path.is_file() {
                let pkg_file_path_str =
                    pkg_file_path.to_str().ok_or_else(|| {
                        anyhow::anyhow!(
                            "Package path contains invalid UTF-8: {}",
                            pkg_file_path.display()
                        )
                    })?;
                let mut pkg: zoi_core::types::Package =
                    zoi_lua::parser::parse_lua_package(
                        pkg_file_path_str,
                        None,
                        None,
                        true
                    )?;

                if let Ok(repo_subpath) = entry.path().strip_prefix(&db_root) {
                    let mut repo_path = repo_subpath
                        .to_string_lossy()
                        .to_string()
                        .replace('\\', "/");
                    let pkg_name_suffix = format!("/{}", pkg.name);
                    if repo_path.ends_with(&pkg_name_suffix) {
                        repo_path = repo_path
                            [..repo_path.len() - pkg_name_suffix.len()]
                            .to_string();
                    } else if repo_path == pkg.name {
                        repo_path = String::new();
                    }
                    pkg.repo = repo_path;
                }

                available.push(pkg);
            }
        }
    }

    available.sort_by(|a, b| a.name.cmp(&b.name));
    Ok(available)
}

/// Returns all available packages in the default registry.
///
/// # Errors
///
/// Returns an error if the configuration cannot be read or available packages
/// cannot be retrieved.
pub fn get_all_available_packages() -> Result<Vec<zoi_core::types::Package>> {
    let config = config::read_config()?;
    if let Some(handle) = config
        .default_registry
        .as_ref()
        .map(|r| &r.handle)
        .filter(|h| !h.is_empty())
    {
        let repos_with_handle: Vec<String> = config
            .repos
            .iter()
            .map(|repo| format!("{handle}/{repo}"))
            .collect();
        get_packages_from_repos(&repos_with_handle)
    } else {
        Ok(Vec::new())
    }
}

/// Adds a dependent ID to a package's dependents list.
///
/// # Errors
///
/// Returns an error if the dependent file cannot be written.
pub fn add_dependent(package_dir: &Path, dependent_id: &str) -> Result<()> {
    let dependents_dir = package_dir.join("dependents");
    fs::create_dir_all(&dependents_dir)?;
    let dependent_file = dependents_dir.join(hex::encode(dependent_id));
    fs::write(dependent_file, "")?;
    Ok(())
}

/// Removes a dependent ID from a package's dependents list.
///
/// # Errors
///
/// Returns an error if the dependent file cannot be removed.
pub fn remove_dependent(package_dir: &Path, dependent_id: &str) -> Result<()> {
    let dependents_dir = package_dir.join("dependents");
    if dependents_dir.exists() {
        let dependent_file = dependents_dir.join(hex::encode(dependent_id));
        if dependent_file.exists() {
            fs::remove_file(dependent_file)?;
        } else if let Some(pos) = dependent_id.rfind('@') {
            let legacy_id = &dependent_id[..pos];
            let legacy_file = dependents_dir.join(hex::encode(legacy_id));
            if legacy_file.exists() {
                fs::remove_file(legacy_file)?;
            }
        }
    }
    Ok(())
}

/// Returns a list of all dependent IDs for a package.
///
/// # Errors
///
/// Returns an error if the dependents directory cannot be read.
pub fn get_dependents(package_dir: &Path) -> Result<Vec<String>> {
    let dependents_dir = package_dir.join("dependents");
    let mut dependents = Vec::new();
    if dependents_dir.exists() {
        for entry in fs::read_dir(dependents_dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.is_file()
                && let Some(file_name) =
                    path.file_name().and_then(|s| s.to_str())
                && let Ok(decoded) = hex::decode(file_name)
                && let Ok(dependent_id) = String::from_utf8(decoded)
            {
                dependents.push(dependent_id);
            }
        }
    }
    Ok(dependents)
}

/// Writes an installation manifest to the store and updates the 'latest'
/// symlink.
///
/// # Errors
///
/// Returns an error if the manifest cannot be serialized or written.
pub fn write_manifest(manifest: &InstallManifest) -> Result<()> {
    let version_dir = get_package_version_dir(
        manifest.scope,
        &manifest.registry_handle,
        &manifest.repo,
        &manifest.name,
        &manifest.version
    )?;
    fs::create_dir_all(&version_dir)?;

    let manifest_filename = if let Some(sub) = &manifest.sub_package {
        format!("manifest-{sub}.yaml")
    } else {
        "manifest.yaml".to_string()
    };
    let manifest_path = version_dir.join(manifest_filename);

    let content = serde_yaml::to_string(&manifest)?;
    fs::write(manifest_path, content)?;

    let package_dir = get_package_dir(
        manifest.scope,
        &manifest.registry_handle,
        &manifest.repo,
        &manifest.name
    )?;
    let latest_symlink_path = package_dir.join("latest");
    zoi_core::utils::symlink_dir(&version_dir, &latest_symlink_path)?;

    Ok(())
}

/// Returns the path where the package source file should be stored.
///
/// # Errors
///
/// Returns an error if the source path cannot be constructed.
pub fn get_package_source_path(manifest: &InstallManifest) -> Result<PathBuf> {
    let version_dir = get_package_version_dir(
        manifest.scope,
        &manifest.registry_handle,
        &manifest.repo,
        &manifest.name,
        &manifest.version
    )?;
    Ok(version_dir.join("package.pkg.lua"))
}

/// Persists the package source file to the store.
///
/// # Errors
///
/// Returns an error if the source file cannot be copied.
pub fn persist_package_source(
    manifest: &InstallManifest,
    source_path: &Path
) -> Result<()> {
    let stored_source_path = get_package_source_path(manifest)?;
    if let Some(parent) = stored_source_path.parent() {
        fs::create_dir_all(parent)?;
    }
    fs::copy(source_path, stored_source_path)?;
    Ok(())
}

/// Updates the installation reason in a package's manifest.
///
/// # Errors
///
/// Returns an error if the manifest cannot be updated.
pub fn update_manifest_reason(
    manifest: &InstallManifest,
    new_reason: types::InstallReason
) -> Result<()> {
    let mut updated_manifest = manifest.clone();
    updated_manifest.reason = new_reason;
    write_manifest(&updated_manifest)?;
    Ok(())
}