zoi-rs 1.8.7

Universal Package Manager & Environment Setup Tool
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
use crate::pkg::types;
use anyhow::Result;
use rusqlite::{Connection, params};
use std::path::PathBuf;

pub fn get_db_path(registry_handle: &str) -> Result<PathBuf> {
    let db_root = crate::pkg::resolve::get_db_root()?;
    Ok(db_root.join(format!("{}.db", registry_handle)))
}

pub fn open_connection(registry_handle: &str) -> Result<Connection> {
    let conn = open_connection_no_setup(registry_handle)?;
    setup_schema(&conn)?;
    Ok(conn)
}

pub fn open_connection_no_setup(registry_handle: &str) -> Result<Connection> {
    let db_path = get_db_path(registry_handle)?;
    if let Some(parent) = db_path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let conn = Connection::open(db_path)?;
    conn.busy_timeout(std::time::Duration::from_secs(5))?;

    conn.execute_batch(
        "
        PRAGMA foreign_keys = ON;
        PRAGMA journal_mode = WAL;
        PRAGMA synchronous = NORMAL;
    ",
    )?;

    Ok(conn)
}

fn setup_schema(conn: &Connection) -> Result<()> {
    conn.execute(
        "CREATE TABLE IF NOT EXISTS packages (
            id INTEGER PRIMARY KEY,
            name TEXT NOT NULL,
            sub_package TEXT,
            repo TEXT NOT NULL,
            version TEXT,
            description TEXT,
            package_type TEXT,
            tags TEXT,
            bins TEXT,
            license TEXT,
            registry TEXT,
            scope TEXT,
            reason TEXT,
            dependencies TEXT,
            UNIQUE(name, sub_package, repo, scope)
        )",
        [],
    )?;

    let has_deps: bool = conn
        .query_row(
            "SELECT count(*) FROM pragma_table_info('packages') WHERE name='dependencies'",
            [],
            |row| row.get(0),
        )
        .unwrap_or(0)
        > 0;

    if !has_deps {
        let _ = conn.execute("ALTER TABLE packages ADD COLUMN dependencies TEXT", []);
    }

    let column_exists: bool = conn
        .query_row(
            "SELECT count(*) FROM pragma_table_info('packages') WHERE name='bins'",
            [],
            |row| row.get(0),
        )
        .unwrap_or(0)
        > 0;

    if !column_exists {
        let _ = conn.execute("ALTER TABLE packages ADD COLUMN bins TEXT", []);
    }

    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_packages_name ON packages(name)",
        [],
    )?;
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_packages_repo ON packages(repo)",
        [],
    )?;

    conn.execute(
        "CREATE TABLE IF NOT EXISTS package_files (
            id INTEGER PRIMARY KEY,
            package_id INTEGER,
            path TEXT NOT NULL,
            FOREIGN KEY(package_id) REFERENCES packages(id) ON DELETE CASCADE
        )",
        [],
    )?;

    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_package_files_path ON package_files(path)",
        [],
    )?;

    let mut fts_needs_rebuild = false;
    let fts_exists: bool = conn
        .query_row(
            "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='packages_fts'",
            [],
            |row| row.get(0),
        )
        .unwrap_or(0)
        > 0;

    if fts_exists {
        let has_bins_fts: bool = conn
            .query_row(
                "SELECT count(*) FROM pragma_table_info('packages_fts') WHERE name='bins'",
                [],
                |row| row.get(0),
            )
            .unwrap_or(0)
            > 0;
        if !has_bins_fts {
            fts_needs_rebuild = true;
        }
    } else {
        fts_needs_rebuild = true;
    }

    if fts_needs_rebuild {
        let _ = conn.execute("DROP TABLE IF EXISTS packages_fts", []);
        let _ = conn.execute("DROP TRIGGER IF EXISTS packages_ai", []);
        let _ = conn.execute("DROP TRIGGER IF EXISTS packages_ad", []);
        let _ = conn.execute("DROP TRIGGER IF EXISTS packages_au", []);

        let _ = conn.execute(
            "CREATE VIRTUAL TABLE packages_fts USING fts5(name, description, tags, bins, content='packages', content_rowid='id')",
            [],
        );

        let _ = conn.execute(
            "INSERT INTO packages_fts(rowid, name, description, tags, bins) 
             SELECT id, name, description, tags, bins FROM packages",
            [],
        );

        let _ = conn.execute(
            "CREATE TRIGGER packages_ai AFTER INSERT ON packages BEGIN
                INSERT INTO packages_fts(rowid, name, description, tags, bins) VALUES (new.id, new.name, new.description, new.tags, new.bins);
            END",
            [],
        );
        let _ = conn.execute(
            "CREATE TRIGGER packages_ad AFTER DELETE ON packages BEGIN
                INSERT INTO packages_fts(packages_fts, rowid, name, description, tags, bins) VALUES('delete', old.id, old.name, old.description, old.tags, old.bins);
            END",
            [],
        );
        let _ = conn.execute(
            "CREATE TRIGGER packages_au AFTER UPDATE ON packages BEGIN
                INSERT INTO packages_fts(packages_fts, rowid, name, description, tags, bins) VALUES('delete', old.id, old.name, old.description, old.tags, old.bins);
                INSERT INTO packages_fts(rowid, name, description, tags, bins) VALUES (new.id, new.name, new.description, new.tags, new.bins);
            END",
            [],
        );
    }

    let files_fts_exists: bool = conn
        .query_row(
            "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='package_files_fts'",
            [],
            |row| row.get(0),
        )
        .unwrap_or(0)
        > 0;

    if !files_fts_exists {
        let _ = conn.execute(
            "CREATE VIRTUAL TABLE package_files_fts USING fts5(path, content='package_files', content_rowid='id')",
            [],
        );

        let _ = conn.execute(
            "INSERT INTO package_files_fts(rowid, path) SELECT id, path FROM package_files",
            [],
        );

        let _ = conn.execute(
            "CREATE TRIGGER package_files_ai AFTER INSERT ON package_files BEGIN
                INSERT INTO package_files_fts(rowid, path) VALUES (new.id, new.path);
            END",
            [],
        );
        let _ = conn.execute(
            "CREATE TRIGGER package_files_ad AFTER DELETE ON package_files BEGIN
                INSERT INTO package_files_fts(package_files_fts, rowid, path) VALUES('delete', old.id, old.path);
            END",
            [],
        );
        let _ = conn.execute(
            "CREATE TRIGGER package_files_au AFTER UPDATE ON package_files BEGIN
                INSERT INTO package_files_fts(package_files_fts, rowid, path) VALUES('delete', old.id, old.path);
                INSERT INTO package_files_fts(rowid, path) VALUES (new.id, new.path);
            END",
            [],
        );
    }

    Ok(())
}

pub fn update_package(
    conn: &Connection,
    pkg: &types::Package,
    registry: &str,
    scope: Option<types::Scope>,
    sub_package: Option<&str>,
    reason: Option<&types::InstallReason>,
) -> Result<i64> {
    let tags_json = serde_json::to_string(&pkg.tags)?;
    let bins_json =
        serde_json::to_string(&pkg.bins.as_ref().unwrap_or(&vec![])).unwrap_or_default();
    let pkg_type = format!("{:?}", pkg.package_type).to_lowercase();
    let scope_str = scope.map(|s| format!("{:?}", s).to_lowercase());
    let reason_str = reason.map(|r| match r {
        types::InstallReason::Direct => "direct".to_string(),
        types::InstallReason::Dependency { parent } => format!("dependency:{}", parent),
        types::InstallReason::Declarative => "declarative".to_string(),
    });

    let deps_json = if let Some(deps) = &pkg.dependencies {
        serde_json::to_string(deps).unwrap_or_default()
    } else {
        String::new()
    };

    conn.execute(
        "INSERT INTO packages (name, sub_package, repo, version, description, package_type, tags, bins, license, registry, scope, reason, dependencies)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)
         ON CONFLICT(name, sub_package, repo, scope) DO UPDATE SET
            version = excluded.version,
            description = excluded.description,
            package_type = excluded.package_type,
            tags = excluded.tags,
            bins = excluded.bins,
            license = excluded.license,
            registry = excluded.registry,
            reason = COALESCE(excluded.reason, packages.reason),
            dependencies = excluded.dependencies",
        params![
            pkg.name,
            sub_package,
            pkg.repo,
            pkg.version,
            pkg.description,
            pkg_type,
            tags_json,
            bins_json,
            pkg.license,
            registry,
            scope_str,
            reason_str,
            deps_json,
        ],
    )?;

    let row_id = conn.query_row(
        "SELECT id FROM packages WHERE name = ?1 AND (sub_package IS ?2) AND repo = ?3 AND (scope IS ?4 OR (scope IS NULL AND ?4 IS NULL))",
        params![pkg.name, sub_package, pkg.repo, scope_str],
        |row| row.get(0),
    )?;

    Ok(row_id)
}

#[derive(Debug)]
pub struct CompletionEntry {
    pub name: String,
    pub repo: String,
    pub description: String,
    pub sub_package: Option<String>,
}

pub fn get_packages_for_completion(registry_handle: &str) -> Result<Vec<CompletionEntry>> {
    let conn = open_connection(registry_handle)?;
    let mut stmt =
        conn.prepare("SELECT name, repo, description, sub_package FROM packages ORDER BY name")?;

    let rows = stmt.query_map([], |row| {
        Ok(CompletionEntry {
            name: row.get(0)?,
            repo: row.get(1)?,
            description: row.get(2).unwrap_or_default(),
            sub_package: row.get(3)?,
        })
    })?;

    let mut entries = Vec::new();
    for row in rows {
        entries.push(row?);
    }
    Ok(entries)
}

pub fn index_package_files(conn: &Connection, package_id: i64, files: &[String]) -> Result<()> {
    let mut stmt = conn.prepare("INSERT INTO package_files (package_id, path) VALUES (?1, ?2)")?;
    for file in files {
        stmt.execute(params![package_id, file])?;
    }
    Ok(())
}

pub fn delete_package(
    conn: &Connection,
    name: &str,
    sub_package: Option<&str>,
    repo: &str,
    scope: Option<types::Scope>,
) -> Result<()> {
    let scope_str = scope.map(|s| format!("{:?}", s).to_lowercase());
    conn.execute(
        "DELETE FROM packages WHERE name = ?1 AND (sub_package IS ?2) AND repo = ?3 AND (scope IS ?4 OR scope IS NULL)",
        params![name, sub_package, repo, scope_str],
    )?;
    Ok(())
}

pub fn clear_registry(conn: &Connection) -> Result<()> {
    conn.execute("DELETE FROM packages", [])?;
    Ok(())
}

pub fn find_provides(registry_handle: &str, term: &str) -> Result<Vec<(types::Package, String)>> {
    let conn = open_connection(registry_handle)?;

    let mut stmt = conn.prepare(
        "SELECT name, repo, version, description, package_type, tags, bins, license, sub_package 
         FROM packages 
         WHERE name = ?1 OR bins LIKE ?2",
    )?;

    let bins_like_query = format!("%\"{}\"%", term);
    let rows = stmt.query_map(params![term, bins_like_query], |row| {
        let tags_raw: String = row.get(5)?;
        let tags: Vec<String> = serde_json::from_str(&tags_raw).unwrap_or_default();
        let bins_raw: String = row.get(6)?;
        let bins: Vec<String> = serde_json::from_str(&bins_raw).unwrap_or_default();
        let type_raw: String = row.get(4)?;

        let package_type = match type_raw.as_str() {
            "collection" => types::PackageType::Collection,
            "app" => types::PackageType::App,
            "extension" => types::PackageType::Extension,
            _ => types::PackageType::Package,
        };

        Ok(types::Package {
            name: row.get(0)?,
            repo: row.get(1)?,
            version: row.get(2)?,
            description: row.get(3)?,
            package_type,
            tags,
            bins: Some(bins),
            license: row.get(7)?,
            sub_package: row.get(8)?,
            maintainer: types::Maintainer::default(),
            ..Default::default()
        })
    })?;

    let mut results = Vec::new();
    for row in rows {
        let pkg = row?;
        results.push((pkg, format!("bin/{}", term)));
    }

    let mut stmt = conn.prepare(
        "SELECT p.name, p.repo, p.version, p.description, p.package_type, p.tags, p.bins, p.license, p.sub_package, pf.path 
         FROM packages p
         JOIN package_files pf ON p.id = pf.package_id
         WHERE pf.path LIKE ?1 OR pf.path LIKE ?2",
    )?;

    let path_like_query = format!("%/{}", term);
    let exact_path_query = term.to_string();

    let rows = stmt.query_map(params![path_like_query, exact_path_query], |row| {
        let tags_raw: String = row.get(5)?;
        let tags: Vec<String> = serde_json::from_str(&tags_raw).unwrap_or_default();
        let bins_raw: String = row.get(6)?;
        let bins: Vec<String> = serde_json::from_str(&bins_raw).unwrap_or_default();
        let type_raw: String = row.get(4)?;

        let package_type = match type_raw.as_str() {
            "collection" => types::PackageType::Collection,
            "app" => types::PackageType::App,
            "extension" => types::PackageType::Extension,
            _ => types::PackageType::Package,
        };

        let pkg = types::Package {
            name: row.get(0)?,
            repo: row.get(1)?,
            version: row.get(2)?,
            description: row.get(3)?,
            package_type,
            tags,
            bins: Some(bins),
            license: row.get(7)?,
            sub_package: row.get(8)?,
            maintainer: types::Maintainer::default(),
            ..Default::default()
        };
        let path: String = row.get(9)?;
        Ok((pkg, path))
    })?;

    for row in rows {
        let (pkg, mut path): (types::Package, String) = row?;
        if let Some(stripped) = path.strip_prefix("data/pkgstore/") {
            path = stripped.to_string();
        } else if let Some(stripped) = path.strip_prefix("data/usrroot/") {
            path = format!("/{}", stripped);
        } else if let Some(stripped) = path.strip_prefix("data/usrhome/") {
            path = format!("~/{}", stripped);
        }

        results.push((pkg, path));
    }

    results.sort_by(|a, b| {
        a.0.name
            .cmp(&b.0.name)
            .then(a.0.repo.cmp(&b.0.repo))
            .then(a.1.cmp(&b.1))
    });
    results.dedup_by(|a, b| a.0.name == b.0.name && a.0.repo == b.0.repo && a.1 == b.1);

    Ok(results)
}

pub fn search_packages(registry_handle: &str, term: &str) -> Result<Vec<types::Package>> {
    let conn = open_connection(registry_handle)?;
    let mut stmt = conn.prepare(
        "SELECT name, repo, version, description, package_type, tags, license, sub_package 
         FROM packages 
         WHERE id IN (SELECT rowid FROM packages_fts WHERE packages_fts MATCH ?1)
         OR name LIKE ?2",
    )?;

    let search_query = format!("{}*", term);
    let like_query = format!("%{}%", term);

    let rows = stmt.query_map(params![search_query, like_query], |row| {
        let tags_raw: String = row.get(5)?;
        let tags: Vec<String> = serde_json::from_str(&tags_raw).unwrap_or_default();
        let type_raw: String = row.get(4)?;

        let package_type = match type_raw.as_str() {
            "collection" => types::PackageType::Collection,
            "app" => types::PackageType::App,
            "extension" => types::PackageType::Extension,
            _ => types::PackageType::Package,
        };

        Ok(types::Package {
            name: row.get(0)?,
            repo: row.get(1)?,
            version: row.get(2)?,
            description: row.get(3)?,
            package_type,
            tags,
            license: row.get(6)?,
            sub_package: row.get(7)?,
            maintainer: types::Maintainer {
                name: String::new(),
                email: String::new(),
                website: None,
            },
            ..Default::default()
        })
    })?;

    let mut pkgs = Vec::new();
    for row in rows {
        pkgs.push(row?);
    }
    Ok(pkgs)
}

pub fn search_files(registry_handle: &str, term: &str) -> Result<Vec<(types::Package, String)>> {
    let conn = open_connection(registry_handle)?;
    let mut stmt = conn.prepare(
        "SELECT p.name, p.repo, p.version, p.description, p.package_type, p.tags, p.license, p.sub_package, pf.path 
         FROM packages p
         JOIN package_files pf ON p.id = pf.package_id
         WHERE pf.id IN (SELECT rowid FROM package_files_fts WHERE package_files_fts MATCH ?1)
         OR pf.path LIKE ?2",
    )?;

    let search_query = format!("*{}*", term.replace('/', " "));
    let like_query = format!("%{}%", term);

    let rows = stmt.query_map(params![search_query, like_query], |row| {
        let tags_raw: String = row.get(5)?;
        let tags: Vec<String> = serde_json::from_str(&tags_raw).unwrap_or_default();
        let type_raw: String = row.get(4)?;

        let package_type = match type_raw.as_str() {
            "collection" => types::PackageType::Collection,
            "app" => types::PackageType::App,
            "extension" => types::PackageType::Extension,
            _ => types::PackageType::Package,
        };

        let pkg = types::Package {
            name: row.get(0)?,
            repo: row.get(1)?,
            version: row.get(2)?,
            description: row.get(3)?,
            package_type,
            tags,
            license: row.get(6)?,
            sub_package: row.get(7)?,
            maintainer: types::Maintainer {
                name: String::new(),
                email: String::new(),
                website: None,
            },
            ..Default::default()
        };
        let path: String = row.get(8)?;
        Ok((pkg, path))
    })?;

    let mut results = Vec::new();
    for row in rows {
        results.push(row?);
    }
    Ok(results)
}

pub fn list_all_packages(registry_handle: &str) -> Result<Vec<types::Package>> {
    let conn = open_connection(registry_handle)?;
    let mut stmt = conn.prepare(
        "SELECT name, repo, version, description, package_type, tags, license, sub_package, scope, registry, reason FROM packages ORDER BY name"
    )?;

    let rows = stmt.query_map([], |row| {
        let tags_raw: String = row.get(5)?;
        let tags: Vec<String> = serde_json::from_str(&tags_raw).unwrap_or_default();
        let type_raw: String = row.get(4)?;

        let package_type = match type_raw.as_str() {
            "collection" => types::PackageType::Collection,
            "app" => types::PackageType::App,
            "extension" => types::PackageType::Extension,
            _ => types::PackageType::Package,
        };

        let sub_package: Option<String> = row.get(7)?;
        let scope_raw: Option<String> = row.get(8)?;
        let registry: Option<String> = row.get(9)?;
        let reason_raw: Option<String> = row.get(10)?;

        let scope = match scope_raw.as_deref() {
            Some("system") => types::Scope::System,
            Some("project") => types::Scope::Project,
            _ => types::Scope::User,
        };

        let reason = reason_raw.map(|r| {
            if r == "direct" {
                types::InstallReason::Direct
            } else if r == "declarative" {
                types::InstallReason::Declarative
            } else if let Some(parent) = r.strip_prefix("dependency:") {
                types::InstallReason::Dependency {
                    parent: parent.to_string(),
                }
            } else {
                types::InstallReason::Direct
            }
        });

        let pkg = types::Package {
            name: row.get(0)?,
            repo: row.get(1)?,
            version: row.get(2)?,
            description: row.get(3)?,
            package_type,
            tags,
            license: row.get(6)?,
            scope,
            registry_handle: registry,
            sub_package,
            reason,
            maintainer: types::Maintainer {
                name: String::new(),
                email: String::new(),
                website: None,
            },
            ..Default::default()
        };

        Ok(pkg)
    })?;

    let mut pkgs = Vec::new();
    for row in rows {
        pkgs.push(row?);
    }
    Ok(pkgs)
}

pub fn get_all_package_names(registry_handle: &str) -> Result<Vec<String>> {
    let conn = open_connection(registry_handle)?;
    let mut stmt = conn.prepare("SELECT DISTINCT name FROM packages")?;
    let rows = stmt.query_map([], |row| row.get(0))?;
    let mut names = Vec::new();
    for name in rows {
        names.push(name?);
    }
    Ok(names)
}

pub fn get_all_versions(registry_handle: &str, name: &str, repo: &str) -> Result<Vec<String>> {
    let conn = open_connection(registry_handle)?;
    let mut stmt = conn.prepare("SELECT version FROM packages WHERE name = ?1 AND repo = ?2")?;
    let rows = stmt.query_map(params![name, repo], |row| row.get(0))?;
    let mut versions = Vec::new();
    for v in rows.flatten() {
        versions.push(v);
    }
    Ok(versions)
}

pub fn get_package_dependencies(
    registry_handle: &str,
    name: &str,
    version: &str,
    sub_package: Option<&str>,
    repo: &str,
) -> Result<Option<String>> {
    let conn = open_connection(registry_handle)?;
    let mut stmt = conn.prepare(
        "SELECT dependencies FROM packages 
         WHERE name = ?1 AND version = ?2 AND (sub_package IS ?3) AND repo = ?4",
    )?;
    let mut rows = stmt.query(params![name, version, sub_package, repo])?;
    if let Some(row) = rows.next()? {
        let deps: Option<String> = row.get(0)?;
        Ok(deps)
    } else {
        Ok(None)
    }
}