slox-core 0.9.0

Package Manager written in rust
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
mod activate;
mod env;
mod package;
mod store;
mod term;

use slox_cli::{Commands, EnvCommand, PkgCommand};
use store::StorePaths;
use term::{Progress, Report};
use std::io;
use std::fs;
use std::path::PathBuf;

pub fn report_error(error: &str) {
    term::report_error(error);
}

pub fn run(command: Commands) -> Result<(), String> {
    run_with_store(command, &StorePaths::default())
}

fn run_with_store(command: Commands, store: &StorePaths) -> Result<(), String> {
    match command {
        Commands::Env { command } => {
            let progress = Progress::start();
            match handle_env(store, command) {
                Ok(report) => {
                    progress.finish(report);
                    Ok(())
                }
                Err(error) => {
                    progress.fail();
                    Err(error)
                }
            }
        }
        Commands::Activate { command } => {
            println!("{}", activate::activation_script(store, command)?);
            Ok(())
        }
        Commands::Pkg { command } => {
            let progress = Progress::start();
            match handle_pkg(store, command) {
                Ok(report) => {
                    progress.finish(report);
                    Ok(())
                }
                Err(error) => {
                    progress.fail();
                    Err(error)
                }
            }
        }
    }
}


fn get_files_in_dir(dir_path: &str) -> io::Result<Vec<PathBuf>> {
    let mut files = Vec::new();
    
    for entry in fs::read_dir(dir_path)? {
        let entry = entry?;
        let path = entry.path();
        
        if path.is_file() {
            files.push(path);
        }
    }
    
    Ok(files)
}

fn handle_env(store: &StorePaths, cmd: EnvCommand) -> Result<Report, String> {
    match cmd {
        EnvCommand::Add { path } => {
            let env_dir = env::add_env(store, &path)?;
            Ok(Report::new(format!("added env `{path}`"))
                .detail(format!("path: {}", env_dir.display()))
                .detail(format!("bin: {}", store.env_bin_dir(&path).display()))
                .detail(format!("shims: {}", store.shim_bin_dir().display())))
        }
        EnvCommand::Remove { path } => {
            let result = env::remove_env(store, &path)?;
            let summary = if result.removed {
                format!("removed env `{path}`")
            } else {
                format!("env `{path}` was already absent")
            };
            let mut report =
                Report::new(summary).detail(format!("path: {}", result.env_dir.display()));
            if result.cleared_active {
                report = report.detail("active env cleared; shims now point to root/bin");
            }
            Ok(report)
        }
        EnvCommand::Set { path } => {
            let active_bin = env::set_env(store, &path)?;
            Ok(Report::new(format!("activated env `{path}`"))
                .detail(format!("bin: {}", active_bin.display()))
                .detail(format!("shims: {}", store.shim_bin_dir().display())))
        }
        EnvCommand::Fetch { path } => {
            let active_bin = env::get_env(store, &path)?;
            

            let bin_files_vec = get_files_in_dir(&active_bin.display().to_string())
                .unwrap_or_else(|_| vec![]);
                
            let shim_files_vec = get_files_in_dir(&store.shim_bin_dir().display().to_string())
                .unwrap_or_else(|_| vec![]);


            let bin_files = bin_files_vec
                .iter()
                .map(|p| p.file_name().unwrap_or_default().to_string_lossy())
                .collect::<Vec<_>>()
                .join(", ");

            let shim_files = shim_files_vec
                .iter()
                .map(|p| p.file_name().unwrap_or_default().to_string_lossy())
                .collect::<Vec<_>>()
                .join(", ");


            Ok(Report::new(format!("fetch"))
                .detail(format!("bins: {}", bin_files))
                .detail(format!("shims: {}", shim_files)))
        }
    }
}

fn handle_pkg(store: &StorePaths, cmd: PkgCommand) -> Result<Report, String> {
    match cmd {
        PkgCommand::Add { path } => {
            let install = package::download(store, &package::parse_pkg(&path)?)?;
            Ok(
                Report::new(format!("added package `{}`", install.package_name))
                    .detail(format!("binary: {}", install.installed_binary.display()))
                    .detail(format!("shim: {}", install.shim_binary.display())),
            )
        }
        PkgCommand::Remove { path } => {
            let removed = package::remove(store, &path)?;
            let summary = if removed.removed {
                format!("removed package `{}`", removed.package_name)
            } else {
                format!("package `{}` was not installed", removed.package_name)
            };
            Ok(Report::new(summary)
                .detail(format!("binary: {}", removed.removed_binary.display()))
                .detail(format!("shim: {}", removed.shim_binary.display())))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{
        activate::activation_script,
        env::{active_bin_dir, active_env_name, add_env, remove_env, set_env},
        package::{
            BuildConfig, Pkg, install_built_binary, parse_pkg, remove as remove_pkg,
            run_build_script,
        },
        store::StorePaths,
    };
    use crate::activate;
    use slox_cli::ActivateCommand;
    use std::fs;
    use std::path::PathBuf;
    use std::time::{SystemTime, UNIX_EPOCH};

    fn make_temp_dir(name: &str) -> PathBuf {
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("time went backwards")
            .as_nanos();
        let dir = std::env::temp_dir().join(format!("slox-{name}-{unique}"));
        fs::create_dir_all(&dir).expect("failed to create temp dir");
        dir
    }

    fn make_store() -> StorePaths {
        StorePaths {
            base: make_temp_dir("store"),
        }
    }

    #[test]
    fn parse_github_package() {
        let pkg = parse_pkg("devwon/slox@github").expect("github package should parse");
        match pkg {
            Pkg::GitHub { user, repo } => {
                assert_eq!(user, "devwon");
                assert_eq!(repo, "slox");
            }
            Pkg::Regit { .. } => panic!("expected github package"),
        }
    }

    #[test]
    fn parse_regit_package() {
        let pkg = parse_pkg("plur@sloxpkgs").expect("sloxpkgs package should parse");
        match pkg {
            Pkg::Regit { pkg } => assert_eq!(pkg, "plur"),
            Pkg::GitHub { .. } => panic!("expected sloxpkgs package"),
        }
    }

    #[test]
    fn reject_invalid_package_format() {
        let error = parse_pkg("invalid-package").expect_err("package parse should fail");
        assert!(error.contains("user/repo@github"));
    }

    #[test]
    fn activation_script_uses_root_bin_path() {
        let store = StorePaths {
            base: PathBuf::from("/tmp/slox-store"),
        };

        let script = activation_script(&store, ActivateCommand::Zsh)
            .expect("root activation script should render");
        assert_eq!(
            script,
            concat!(
                "_slox_store='/tmp/slox-store'\n",
                "_slox_bin='/tmp/slox-store/bin'\n",
                "_slox_filtered_path=\n",
                "_slox_old_ifs=$IFS\n",
                "IFS=:\n",
                "for _slox_entry in $PATH; do\n",
                "  case \"$_slox_entry\" in\n",
                "    \"$_slox_store\"/bin|\"$_slox_store\"/bin/)\n",
                "      ;;\n",
                "    '')\n",
                "      ;;\n",
                "    *)\n",
                "      if [ -n \"$_slox_filtered_path\" ]; then\n",
                "        _slox_filtered_path=\"$_slox_filtered_path:$_slox_entry\"\n",
                "      else\n",
                "        _slox_filtered_path=\"$_slox_entry\"\n",
                "      fi\n",
                "      ;;\n",
                "  esac\n",
                "done\n",
                "IFS=$_slox_old_ifs\n",
                "if [ -n \"$_slox_filtered_path\" ]; then\n",
                "  export PATH=\"$_slox_bin:$_slox_filtered_path\"\n",
                "else\n",
                "  export PATH=\"$_slox_bin\"\n",
                "fi\n",
                "unset _slox_store _slox_bin _slox_filtered_path _slox_old_ifs _slox_entry"
            )
        );
    }

    #[test]
    fn add_and_remove_env_under_store() {
        let store = make_store();
        let env_dir = store.env_dir("demo");

        add_env(&store, "demo").expect("env should be created");
        assert!(env_dir.is_dir());
        assert!(store.env_bin_dir("demo").is_dir());

        remove_env(&store, "demo").expect("env should be removed");
        assert!(!env_dir.exists());

        fs::remove_dir_all(store.base).expect("failed to clean store dir");
    }

    #[test]
    fn set_env_changes_active_bin_dir_and_activation_script() {
        let store = make_store();
        add_env(&store, "demo").expect("env should be created");

        set_env(&store, "demo").expect("env should be set");
        assert_eq!(
            active_env_name(&store).expect("active env should be readable"),
            Some("demo".to_string())
        );
        assert_eq!(
            active_bin_dir(&store).expect("active bin dir should resolve"),
            store.env_bin_dir("demo")
        );

        let script = activation_script(&store, ActivateCommand::Bash)
            .expect("env activation script should render");
        assert_eq!(
            script,
            format!(
                concat!(
                    "_slox_store={store}\n",
                    "_slox_bin={bin}\n",
                    "_slox_filtered_path=\n",
                    "_slox_old_ifs=$IFS\n",
                    "IFS=:\n",
                    "for _slox_entry in $PATH; do\n",
                    "  case \"$_slox_entry\" in\n",
                    "    \"$_slox_store\"/bin|\"$_slox_store\"/bin/)\n",
                    "      ;;\n",
                    "    '')\n",
                    "      ;;\n",
                    "    *)\n",
                    "      if [ -n \"$_slox_filtered_path\" ]; then\n",
                    "        _slox_filtered_path=\"$_slox_filtered_path:$_slox_entry\"\n",
                    "      else\n",
                    "        _slox_filtered_path=\"$_slox_entry\"\n",
                    "      fi\n",
                    "      ;;\n",
                    "  esac\n",
                    "done\n",
                    "IFS=$_slox_old_ifs\n",
                    "if [ -n \"$_slox_filtered_path\" ]; then\n",
                    "  export PATH=\"$_slox_bin:$_slox_filtered_path\"\n",
                    "else\n",
                    "  export PATH=\"$_slox_bin\"\n",
                    "fi\n",
                    "unset _slox_store _slox_bin _slox_filtered_path _slox_old_ifs _slox_entry"
                ),
                store = activate::shell_single_quote(&store.base.display().to_string()),
                bin = activate::shell_single_quote(&store.shim_bin_dir().display().to_string())
            )
        );

        fs::remove_dir_all(store.base).expect("failed to clean store dir");
    }

    #[test]
    fn removing_active_env_clears_active_env_file() {
        let store = make_store();
        add_env(&store, "demo").expect("env should be created");
        set_env(&store, "demo").expect("env should be set");

        remove_env(&store, "demo").expect("env should be removed");
        assert_eq!(
            active_env_name(&store).expect("active env should be readable"),
            None
        );
        assert_eq!(
            active_bin_dir(&store).expect("root bin should be used"),
            store.root_bin_dir()
        );

        fs::remove_dir_all(store.base).expect("failed to clean store dir");
    }

    #[test]
    fn build_and_install_github_package_layout() {
        let repo_name = "demo";
        let repo_dir = make_temp_dir("repo");
        let root_bin_dir = make_temp_dir("root-bin");

        fs::write(
            repo_dir.join("build"),
            format!(
                "#!/bin/sh\nmkdir -p bin\nprintf '#!/bin/sh\\necho installed\\n' > bin/{repo_name}\nchmod +x bin/{repo_name}\n"
            ),
        )
        .expect("failed to write build script");

        let build_config = BuildConfig {
            name: repo_name.to_string(),
            script: PathBuf::from("build"),
            binary: PathBuf::from("bin").join(repo_name),
        };
        run_build_script(&repo_dir, &build_config).expect("build script should succeed");
        let installed_binary =
            install_built_binary(&repo_dir, &build_config.binary, repo_name, &root_bin_dir)
                .expect("install should succeed");

        assert!(installed_binary.is_file());
        let contents =
            fs::read_to_string(installed_binary).expect("failed to read installed binary");
        assert!(contents.contains("installed"));

        fs::remove_dir_all(repo_dir).expect("failed to clean repo dir");
        fs::remove_dir_all(root_bin_dir).expect("failed to clean root bin dir");
    }

    #[test]
    fn install_target_follows_active_env_bin() {
        let store = make_store();
        let repo_name = "demo";
        let repo_dir = make_temp_dir("repo");

        add_env(&store, "demo-env").expect("env should be created");
        set_env(&store, "demo-env").expect("env should be set");

        fs::write(
            repo_dir.join("build"),
            format!(
                "#!/bin/sh\nmkdir -p bin\nprintf '#!/bin/sh\\necho active-env\\n' > bin/{repo_name}\nchmod +x bin/{repo_name}\n"
            ),
        )
        .expect("failed to write build script");

        let build_config = BuildConfig {
            name: repo_name.to_string(),
            script: PathBuf::from("build"),
            binary: PathBuf::from("bin").join(repo_name),
        };
        run_build_script(&repo_dir, &build_config).expect("build script should succeed");
        let install_dir = active_bin_dir(&store).expect("active bin dir should resolve");
        let installed_binary =
            install_built_binary(&repo_dir, &build_config.binary, repo_name, &install_dir)
                .expect("install should succeed");

        assert_eq!(
            installed_binary,
            store.env_bin_dir("demo-env").join(repo_name)
        );
        assert!(installed_binary.is_file());

        fs::remove_dir_all(repo_dir).expect("failed to clean repo dir");
        fs::remove_dir_all(store.base).expect("failed to clean store dir");
    }

    #[test]
    fn sync_active_shims_points_to_active_env_bins() {
        let store = make_store();
        add_env(&store, "demo").expect("env should be created");

        fs::create_dir_all(store.env_bin_dir("demo")).expect("env bin dir should exist");
        fs::write(
            store.env_bin_dir("demo").join("demo"),
            "#!/bin/sh\necho env\n",
        )
        .expect("binary should be written");

        set_env(&store, "demo").expect("env should be set");

        let shim_path = store.shim_bin_dir().join("demo");
        assert!(shim_path.is_file());
        let shim_contents = fs::read_to_string(&shim_path).expect("shim should be readable");
        assert!(
            shim_contents.contains(&store.env_bin_dir("demo").join("demo").display().to_string())
        );

        fs::remove_dir_all(store.base).expect("failed to clean store dir");
    }

    #[test]
    fn clearing_active_env_refreshes_shims_to_root_bin() {
        let store = make_store();
        add_env(&store, "demo").expect("env should be created");
        fs::create_dir_all(store.root_bin_dir()).expect("root bin dir should exist");
        fs::write(
            store.root_bin_dir().join("root-tool"),
            "#!/bin/sh\necho root\n",
        )
        .expect("root binary should be written");
        fs::write(
            store.env_bin_dir("demo").join("env-tool"),
            "#!/bin/sh\necho env\n",
        )
        .expect("env binary should be written");

        set_env(&store, "demo").expect("env should be set");
        remove_env(&store, "demo").expect("env should be removed");

        let shim_dir = store.shim_bin_dir();
        assert!(shim_dir.join("root-tool").is_file());
        assert!(!shim_dir.join("env-tool").exists());

        fs::remove_dir_all(store.base).expect("failed to clean store dir");
    }

    #[test]
    fn remove_package_deletes_binary_and_refreshes_shim() {
        let store = make_store();
        fs::create_dir_all(store.root_bin_dir()).expect("root bin dir should exist");
        fs::write(store.root_bin_dir().join("demo"), "#!/bin/sh\necho root\n")
            .expect("package binary should be written");
        super::env::sync_active_shims(&store).expect("shims should be created");

        let removed = remove_pkg(&store, "demo").expect("package should be removable");

        assert!(removed.removed);
        assert!(!store.root_bin_dir().join("demo").exists());
        assert!(!store.shim_bin_dir().join("demo").exists());

        fs::remove_dir_all(store.base).expect("failed to clean store dir");
    }

    #[test]
    fn build_toml_can_override_script_and_binary_location() {
        let repo_dir = make_temp_dir("repo");
        let root_bin_dir = make_temp_dir("root-bin");

        fs::create_dir_all(repo_dir.join("scripts")).expect("scripts dir should exist");
        fs::write(
            repo_dir.join("scripts").join("custom-build.sh"),
            "#!/bin/sh\nmkdir -p dist\nprintf '#!/bin/sh\\necho custom\\n' > dist/custom-bin\nchmod +x dist/custom-bin\n",
        )
        .expect("build script should be written");
        fs::write(
            repo_dir.join("build.toml"),
            "script = \"scripts/custom-build.sh\"\nbinary = \"dist/custom-bin\"\n",
        )
        .expect("build config should be written");

        let build_config =
            super::package::load_build_config(&repo_dir, "demo").expect("config should load");
        run_build_script(&repo_dir, &build_config).expect("custom build should succeed");
        let installed_binary =
            install_built_binary(&repo_dir, &build_config.binary, "demo", &root_bin_dir)
                .expect("custom binary should install");

        assert_eq!(installed_binary, root_bin_dir.join("demo"));
        let contents =
            fs::read_to_string(installed_binary).expect("failed to read installed binary");
        assert!(contents.contains("custom"));

        fs::remove_dir_all(repo_dir).expect("failed to clean repo dir");
        fs::remove_dir_all(root_bin_dir).expect("failed to clean root bin dir");
    }

    #[test]
    fn build_toml_can_override_installed_name() {
        let repo_dir = make_temp_dir("repo");
        let root_bin_dir = make_temp_dir("root-bin");

        fs::write(
            repo_dir.join("build"),
            "#!/bin/sh\nmkdir -p bin\nprintf '#!/bin/sh\\necho renamed\\n' > bin/original\nchmod +x bin/original\n",
        )
        .expect("build script should be written");
        fs::write(
            repo_dir.join("build.toml"),
            "name = \"renamed\"\nbinary = \"bin/original\"\n",
        )
        .expect("build config should be written");

        let build_config =
            super::package::load_build_config(&repo_dir, "demo").expect("config should load");
        run_build_script(&repo_dir, &build_config).expect("custom build should succeed");
        let installed_binary = install_built_binary(
            &repo_dir,
            &build_config.binary,
            &build_config.name,
            &root_bin_dir,
        )
        .expect("custom binary should install");

        assert_eq!(build_config.name, "renamed");
        assert_eq!(installed_binary, root_bin_dir.join("renamed"));

        fs::remove_dir_all(repo_dir).expect("failed to clean repo dir");
        fs::remove_dir_all(root_bin_dir).expect("failed to clean root bin dir");
    }
}