elba 0.3.0

elba is a package manager for Idris
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
use crate::{
    build::{
        context::{BuildContext, Compiler},
        job::{Job, JobQueue},
        Target, Targets,
    },
    package::{
        lockfile::LockfileToml,
        manifest::{BinTarget, Manifest},
        PackageId, Spec, Summary,
    },
    remote::resolution::{DirectRes, IndexRes, Resolution},
    resolve::Resolver,
    retrieve::{
        cache::{Cache, Layout, OutputLayout},
        Retriever,
    },
    util::{
        config::Backend,
        errors::Res,
        fmt_output,
        graph::Graph,
        lock::DirLock,
        shell::{Shell, Verbosity},
    },
};
use console::style;
use crossbeam::queue::MsQueue;
use failure::{bail, format_err, ResultExt};
use indexmap::IndexMap;
use itertools::Either::{self, Left, Right};
use petgraph::{graph::NodeIndex, visit::Dfs};
use scoped_threadpool::Pool;
use slog::Logger;
use std::{
    env, fs,
    io::{prelude::*, Seek, SeekFrom},
    path::{Path, PathBuf},
    process::Command,
    str::FromStr,
};
use toml;
use toml_edit;

pub struct BuildCtx {
    pub compiler: String,
    pub indices: IndexMap<String, IndexRes>,
    pub global_cache: Layout,
    pub logger: Logger,
    pub threads: u32,
    pub shell: Shell,
    pub offline: bool,
    pub opts: Vec<String>,
}

pub fn test(
    ctx: &BuildCtx,
    project: &Path,
    targets: &[&str],
    backend: &Backend,
    test_threads: u32,
) -> Res<String> {
    let mut contents = String::new();
    let project = find_manifest_root(project)?;
    let mut manifest = fs::File::open(project.join("elba.toml"))
        .context(format_err!("failed to read manifest file (elba.toml)"))?;
    manifest.read_to_string(&mut contents)?;
    let manifest = Manifest::from_str(&contents)?;

    if manifest.targets.test.is_empty() {
        bail!("at least one test must be defined")
    }

    solve_local(ctx, &project, 3, None, |cache, mut retriever, solve| {
        let sources = retriever
            .retrieve_packages(&solve)
            .context(format_err!("package retrieval failed"))?;

        // We drop the Retriever because we want to release our lock on the Indices as soon as we
        // can to avoid stopping other instances of elba from downloading and resolving (even
        // though we don't even need the Retriever anymore).
        drop(retriever);

        let bctx = BuildContext {
            backend,
            codegen: true,
            compiler: Compiler::new(&ctx.compiler)?,
            opts: &ctx.opts,
            cache,
            threads: ctx.threads,
        };

        ctx.shell.println(
            style("[2/3]").dim().bold(),
            "Building targets...",
            Verbosity::Quiet,
        );

        // We want to store the outputs of our labor in a local target directory.
        let lock = DirLock::acquire(&project.join("target"))?;
        let layout = OutputLayout::new(lock).context("could not create local target directory")?;

        let bin_dir = layout.bin.clone();

        let mut root = vec![];
        if manifest.targets.lib.is_some() {
            root.push(Target::Lib(false));
        } else {
            ctx.shell.println(
                style("[warn]").yellow().bold(),
                "No lib target for tests to import",
                Verbosity::Normal,
            );
        }
        let emp = targets.is_empty();
        for (ix, bt) in manifest.targets.test.iter().enumerate() {
            let bt: BinTarget = bt.clone().into();
            if emp || targets.contains(&bt.name.as_str()) {
                root.push(Target::Test(ix));
            }
        }

        let root = Targets::new(root);
        let q = JobQueue::new(sources, &root, Some(layout), &bctx, &ctx.logger, ctx.shell)?;
        q.exec(&bctx)?;

        ctx.shell.println(
            style("[3/3]").dim().bold(),
            "Running tests...",
            Verbosity::Quiet,
        );

        let root: Vec<BinTarget> = root
            .0
            .into_iter()
            .filter_map(|t| {
                if let Target::Test(ix) = t {
                    Some(manifest.targets.test[ix].clone().into())
                } else {
                    None
                }
            })
            .collect();

        // Until pb.println gets added, we can't use progress bars
        // let pb = ProgressBar::new(root.len() as u64);
        // pb.set_style(ProgressStyle::default_bar().template("  [-->] {bar} {pos}/{len}"));

        let results = &MsQueue::new();
        let mut pool = Pool::new(test_threads);

        pool.scoped(|scope| {
            // let mut prg = 0;
            let shell = ctx.shell;
            for test in &root {
                let bin_dir = &bin_dir;
                // let pb = &pb;
                scope.execute(move || {
                    shell.println(style("Running").cyan(), &test.name, Verbosity::Normal);
                    let out = if let Some(r) = &backend.runner {
                        Command::new(r).arg(bin_dir.join(&test.name)).output()
                    } else {
                        Command::new(bin_dir.join(&test.name)).output()
                    };
                    if out.is_err() {
                        shell.println(
                            style("[error]").red().bold(),
                            format!(
                                "Test binary {} could not be executed",
                                bin_dir.join(&test.name).display()
                            ),
                            Verbosity::Quiet,
                        );
                    }
                    results.push(out.map(|x| (&test.name, x)));
                    // prg += 1;
                    // pb.set_position(prg);
                });
            }

            // pb.finish_and_clear();
        });

        let mut errs = 0;
        while let Some(res) = results.try_pop() {
            match res {
                Ok((test, out)) => {
                    ctx.shell.println(
                        if out.status.success() {
                            style("Passed").green()
                        } else {
                            style("Failed").red()
                        },
                        &test,
                        Verbosity::Quiet,
                    );

                    ctx.shell.println_plain(fmt_output(&out), Verbosity::Quiet);

                    if !out.status.success() {
                        errs += 1;
                    }
                }
                Err(e) => bail!("not all tests executed:\n{}", e),
            }
        }

        if errs != 0 {
            Err(format_err!(
                "{} test binaries executed with {} failures",
                root.len(),
                errs
            ))
        } else {
            Ok(format!("{} test binaries executed", root.len()))
        }
    })
}

pub fn install(
    ctx: &BuildCtx,
    name: Either<Spec, PathBuf>,
    targets: &[&str],
    backend: &Backend,
    force: bool,
) -> Res<String> {
    let f = |cache: &Cache, mut retriever: Retriever, solve| -> Res<String> {
        let sources = retriever
            .retrieve_packages(&solve)
            .context(format_err!("package retrieval failed"))?;

        // We drop the Retriever because we want to release our lock on the Indices as soon as we
        // can to avoid stopping other instances of elba from downloading and resolving (even
        // though we don't even need the Retriever anymore).
        drop(retriever);

        let mut contents = String::new();
        let mut manifest = fs::File::open(sources[NodeIndex::new(0)].path().join("elba.toml"))
            .context(format_err!("failed to read manifest file (elba.toml)"))?;
        manifest.read_to_string(&mut contents)?;
        let manifest = Manifest::from_str(&contents)?;

        // By default, we build all bin targets.
        let mut root = vec![];
        let emp = targets.is_empty();
        for (ix, bt) in manifest.targets.bin.iter().enumerate() {
            if emp || targets.contains(&bt.name.as_str()) {
                root.push(Target::Bin(ix));
            }
        }
        let root = Targets::new(root);

        let bctx = BuildContext {
            backend,
            codegen: true,
            compiler: Compiler::new(&ctx.compiler)?,
            opts: &ctx.opts,
            cache,
            threads: ctx.threads,
        };

        ctx.shell.println(
            style("[2/3]").dim().bold(),
            "Building targets...",
            Verbosity::Quiet,
        );

        // We unconditionally use a global OutputLayout to force rebuilding of root packages
        // and to avoid dealing with making our own for global/remote packages

        let q = JobQueue::new(sources, &root, None, &bctx, &ctx.logger, ctx.shell)?;
        // Because we're just building, we don't need to do anything after executing the build
        // process. Yay abstraction!
        let bins = q.exec(&bctx)?.1;
        let binc = bins.len();

        ctx.shell.println(
            style("[3/3]").dim().bold(),
            "Installing binaries...",
            Verbosity::Quiet,
        );
        cache.store_bins(&bins, force)?;

        Ok(format!(
            "{} binaries installed into {}",
            binc,
            cache.layout.bin.display()
        ))
    };

    match name {
        Left(name) => solve_remote(ctx, &name, 3, f),
        Right(path) => solve_local(ctx, &path, 3, None, f),
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Interactivity {
    Normal,
    IDE,
    Socket,
}

pub fn repl(
    ctx: &BuildCtx,
    project: &Path,
    targets: &(bool, Option<Vec<&str>>),
    backend: &Backend,
    interactivity: Interactivity,
) -> Res<String> {
    let mut contents = String::new();
    let project = find_manifest_root(project)?;
    let mut manifest = fs::File::open(project.join("elba.toml"))
        .context(format_err!("failed to read manifest file (elba.toml)"))?;
    manifest.read_to_string(&mut contents)?;
    let manifest = Manifest::from_str(&contents)?;

    env::set_current_dir(&project)?;

    let mut parents = vec![];
    let mut paths = vec![];

    if let Some(lib) = manifest.targets.lib {
        if targets.1.is_none() || targets.0 {
            let src_path = lib.path.0.clone();
            let new_paths = lib
                .mods
                .iter()
                .map(|mod_name| {
                    let path: PathBuf = mod_name.trim_matches('.').replace(".", "/").into();
                    if src_path.join(&path).with_extension("idr").exists() {
                        Ok((parents.len(), path.with_extension("idr")))
                    } else if src_path.join(&path).with_extension("lidr").exists() {
                        Ok((parents.len(), path.with_extension("lidr")))
                    } else {
                        Err(format_err!(
                            "Module at path {} doesn't exist",
                            path.display()
                        ))
                    }
                })
                .collect::<Result<Vec<_>, _>>()?;
            parents.push(src_path);
            paths.extend(new_paths);
        }
    }

    for bin in manifest.targets.bin {
        if let Some(v) = targets.1.as_ref() {
            if v.contains(&bin.name.as_ref()) {
                let resolved = bin.resolve_bin(Path::new(".")).ok_or_else(|| {
                    format_err!(
                        "module {} isn't a subpath and doesn't exist under path {}",
                        bin.main,
                        bin.path.0.display()
                    )
                })?;
                parents.push(resolved.0);
                paths.push((parents.len() - 1, resolved.1));
            }
        } else if !targets.0 {
            let resolved = bin.resolve_bin(Path::new(".")).ok_or_else(|| {
                format_err!(
                    "module {} isn't a subpath and doesn't exist under path {}",
                    bin.main,
                    bin.path.0.display()
                )
            })?;
            parents.push(resolved.0);
            paths.push((parents.len() - 1, resolved.1));
        }
    }

    solve_local(ctx, &project, 3, None, |cache, mut retriever, solve| {
        let sources = retriever
            .retrieve_packages(&solve)
            .context(format_err!("package retrieval failed"))?;

        // We drop the Retriever because we want to release our lock on the Indices as soon as we
        // can to avoid stopping other instances of elba from downloading and resolving (even
        // though we don't even need the Retriever anymore).
        drop(retriever);

        // We add no targets because we're going to directly add the paths of the files (so we can
        // interactively edit).
        let root = vec![];
        let root = Targets::new(root);

        let bctx = BuildContext {
            backend,
            codegen: true,
            compiler: Compiler::new(&ctx.compiler)?,
            opts: &ctx.opts,
            cache,
            threads: ctx.threads,
        };

        ctx.shell.println(
            style("[2/3]").dim().bold(),
            "Building targets...",
            Verbosity::Quiet,
        );

        let mut q = JobQueue::new(sources, &root, None, &bctx, &ctx.logger, ctx.shell)?;

        // We only want to build the dependencies; we expressly do NOT want to generate anything
        // for the root package, because we're gonna manually add the files ourselves.
        // The reason we do this is because the repl is often used for interactive development.
        q.graph.inner[NodeIndex::new(0)] = Job::default();

        let deps = q.exec(&bctx)?.0;

        // From here, we basically manually build a CompileInvocation, but tailor-made for the
        // repl command.
        ctx.shell.println(
            style("[3/3]").dim().bold(),
            "Launching REPL...",
            Verbosity::Quiet,
        );

        if bctx.compiler.flavor().is_idris2() {
            bail!("The Idris 2 compiler doesn't currently support custom source paths, needed for the REPL.")
        }

        let mut process = bctx.compiler.process();
        for binary in deps {
            // We assume that deps have already been compiled
            process.arg("-i").arg(binary);
        }

        for path in &parents {
            process.arg("--sourcepath").arg(path);
            process.arg("-i").arg(path);
        }

        // We add the arguments in the build context at the end so that any
        // conflicting flags will be ignored (idris chooses the earliest flags first)
        process.args(&ctx.opts);

        // Add the files we want to make available for the repl
        for target in &paths {
            process.arg(&target.1);
        }

        match interactivity {
            // In ide-mode, we only want to pass the current file as the arg.
            // An editor should be in charge of dealing with this.
            Interactivity::IDE => { process.arg("--ide-mode"); },
            Interactivity::Socket => { process.arg("--ide-mode-socket"); },
            _ => {},
        };

        // The moment of truth:
        process
            .spawn()
            .with_context(|e| format_err!("couldn't launch the repl:\n{}", e))?
            .wait_with_output()
            .with_context(|e| format_err!("misc. repl failure:\n{}", e))?;

        // Clean up after ourselves
        for target in &paths {
            let src_path = &parents[target.0];
            let bin = src_path.join(&target.1).with_extension("ibc");
            if bin.exists() {
                fs::remove_file(&bin).with_context(|e| {
                    format_err!("couldn't remove ibc file {}:\n{}", bin.display(), e)
                })?;
            }
        }

        Ok("finished repl session".to_string())
    })
}

pub fn doc(ctx: &BuildCtx, project: &Path) -> Res<String> {
    let mut contents = String::new();
    let project = find_manifest_root(project)?;
    let mut manifest = fs::File::open(project.join("elba.toml"))
        .context(format_err!("failed to read manifest file (elba.toml)"))?;
    manifest.read_to_string(&mut contents)?;
    let manifest = Manifest::from_str(&contents)?;

    // By default, we build all lib and bin targets.
    let mut root = vec![];
    if manifest.targets.lib.is_some() {
        root.push(Target::Lib(false));
        root.push(Target::Doc);
    } else {
        // The user specifically asked for a lib target but there wasn't any. Error.
        bail!("the package doesn't have a library target. add one before proceeding")
    }
    let root = Targets::new(root);

    solve_local(ctx, &project, 2, None, |cache, mut retriever, solve| {
        let sources = retriever
            .retrieve_packages(&solve)
            .context(format_err!("package retrieval failed"))?;

        // We drop the Retriever because we want to release our lock on the Indices as soon as we
        // can to avoid stopping other instances of elba from downloading and resolving (even
        // though we don't even need the Retriever anymore).
        drop(retriever);

        let backend = Backend::default();

        let bctx = BuildContext {
            // We just use the default backend cause it doesn't matter for this case
            backend: &backend,
            codegen: true,
            compiler: Compiler::new(&ctx.compiler)?,
            opts: &ctx.opts,
            cache,
            threads: ctx.threads,
        };

        ctx.shell.println(
            style("[2/2]").dim().bold(),
            "Building targets + root docs...",
            Verbosity::Quiet,
        );

        // We want to store the outputs of our labor in a local target directory.
        let lock = DirLock::acquire(&project.join("target"))?;
        let layout = OutputLayout::new(lock).context("could not create local target directory")?;

        let q = JobQueue::new(sources, &root, Some(layout), &bctx, &ctx.logger, ctx.shell)?;
        // Because we're just building, we don't need to do anything after executing the build
        // process. Yay abstraction!
        q.exec(&bctx)?;

        Ok("docs output available at `./target/docs`".to_string())
    })
}

pub fn build(
    ctx: &BuildCtx,
    project: &Path,
    targets: &(bool, bool, Option<Vec<&str>>, Option<Vec<&str>>),
    codegen: bool,
    backend: &Backend,
) -> Res<String> {
    let mut contents = String::new();
    let project = find_manifest_root(project)?;
    let mut manifest = fs::File::open(project.join("elba.toml"))
        .context(format_err!("failed to read manifest file (elba.toml)"))?;
    manifest.read_to_string(&mut contents)?;
    let manifest = Manifest::from_str(&contents)?;

    // By default, we build all lib and bin targets.
    let mut root = vec![];
    if (targets.2.is_none() || targets.0 || targets.1) && manifest.targets.lib.is_some() {
        root.push(Target::Lib(targets.1));
    } else if targets.0 || targets.1 {
        // The user specifically asked for a lib target but there wasn't any. Error.
        bail!("the package doesn't have a library target. add one before proceeding")
    }

    if targets.2.as_ref().is_some() && manifest.targets.bin.is_empty() {
        // The user specifically asked for a bin target(s) but there wasn't any. Error.
        bail!("the package doesn't have any binary targets. add one before proceeding")
    }

    for (ix, bt) in manifest.targets.bin.iter().enumerate() {
        // Case 1: If the --bin flag is passed by itself, we assume the user wants all binaries.
        //         Or, the --bin flag might come with the name of a binary which we should build.
        let target_specified = targets
            .2
            .as_ref()
            .map(|v| v.is_empty() || v.contains(&bt.name.as_str()))
            .unwrap_or(false);
        // Case 2: Neither --bin nor --lib are specified. We're fine with --lib-cg.
        let neither_specified = !targets.0 && targets.2.is_none();
        if target_specified || neither_specified {
            root.push(Target::Bin(ix));
        }
    }

    // We only build test targets if the user asks for them.
    if let Some(ts) = &targets.3 {
        for (ix, bt) in manifest.targets.test.iter().enumerate() {
            let bt: BinTarget = bt.clone().into();
            let target_specified = ts.is_empty() || ts.contains(&bt.name.as_str());
            if target_specified {
                root.push(Target::Test(ix));
            }
        }
    }

    let root = Targets::new(root);
    solve_local(ctx, &project, 2, None, |cache, mut retriever, solve| {
        let sources = retriever
            .retrieve_packages(&solve)
            .context(format_err!("package retrieval failed"))?;

        // We drop the Retriever because we want to release our lock on the Indices as soon as we
        // can to avoid stopping other instances of elba from downloading and resolving (even
        // though we don't even need the Retriever anymore).
        drop(retriever);

        let bctx = BuildContext {
            backend,
            codegen,
            compiler: Compiler::new(&ctx.compiler)?,
            opts: &ctx.opts,
            cache,
            threads: ctx.threads,
        };

        ctx.shell.println(
            style("[2/2]").dim().bold(),
            "Building targets...",
            Verbosity::Quiet,
        );

        // We want to store the outputs of our labor in a local target directory.
        let lock = DirLock::acquire(&project.join("target"))?;
        let layout = OutputLayout::new(lock).context("could not create local target directory")?;

        let q = JobQueue::new(sources, &root, Some(layout), &bctx, &ctx.logger, ctx.shell)?;
        // Because we're just building, we don't need to do anything after executing the build
        // process. Yay abstraction!
        q.exec(&bctx)?;

        Ok("build output available at `./target`".to_string())
    })
}

pub fn update(ctx: &BuildCtx, project: &Path, ignore: Option<&[Spec]>) -> Res<String> {
    let project = find_manifest_root(project)?;

    let op = || -> Res<Graph<Summary>> {
        let mut f = fs::File::open(&project.join("elba.lock"))?;
        let mut contents = String::new();
        f.read_to_string(&mut contents)?;
        let toml = LockfileToml::from_str(&contents)?;

        Ok(toml.into())
    };

    let prev = op().ok();

    solve_local(ctx, &project, 1, ignore, |_, _, solve| {
        if let Some(prev) = prev.as_ref() {
            for (_, old) in prev.sub_tree(NodeIndex::new(0)) {
                if let Some(new) = solve.find_by(|sum| sum.id().lowkey_eq(old.id())) {
                    if old.id() != new.id() {
                        // This is a git repo or something
                        ctx.shell.println(
                            style("Updating").cyan(),
                            format!("{} -> {}", old, new),
                            Verbosity::Normal,
                        );
                    } else if old != new {
                        ctx.shell.println(
                            style("Updating").cyan(),
                            format!(
                                "{} ({}) {} -> {}",
                                old.name(),
                                old.resolution(),
                                old.version(),
                                new.version()
                            ),
                            Verbosity::Normal,
                        );
                    }
                // Otherwise, the packages are exactly the same in both, and we don't do
                // anything
                } else {
                    // It's in the old, but not the new. It was removed!
                    ctx.shell
                        .println(style("Removing").red(), old, Verbosity::Normal);
                }
            }

            for (_, new) in prev.sub_tree(NodeIndex::new(0)) {
                // At this point we just want to find packages which were added in the new lockfile
                if solve.find_by(|sum| new.id().lowkey_eq(sum.id())).is_none() {
                    ctx.shell
                        .println(style("Adding").green(), new, Verbosity::Normal);
                }
            }

            Ok("lockfile at ./elba.lock updated".to_string())
        } else {
            Ok("lockfile created at `./elba.lock`".to_string())
        }
    })
}

pub fn add(ctx: &BuildCtx, project: &Path, spec: &Spec, dev: bool) -> Res<String> {
    let mut contents = String::new();
    let project = find_manifest_root(project)?;
    let mut mf = fs::OpenOptions::new()
        .read(true)
        .write(true)
        .open(project.join("elba.toml"))
        .context(format_err!("failed to read manifest file (elba.toml)"))?;
    mf.read_to_string(&mut contents)?;
    let mut manifest = contents.parse::<toml_edit::Document>().with_context(|e| format!("invalid manifest toml format: {}", e))?;

    let cache = Cache::from_disk(&ctx.logger, ctx.global_cache.clone(), ctx.shell)?;
    let indices = ctx
        .indices
        .values()
        .cloned()
        .map(|x| x.res)
        .collect::<Vec<_>>();
    let indices = cache.get_indices(&indices, false, false);
    let target = indices.select_by_spec(&spec)?;
    let target_s = target.to_string();

    let res = match target.id.resolution() {
        Resolution::Index(IndexRes { res }) => res.clone(),
        _ => unreachable!(),
    };

    if !dev {
        manifest["dependencies"][target.id.name().to_string()]["version"] = toml_edit::value(target.version.to_string());
        manifest["dependencies"][target.id.name().to_string()]["index"] = toml_edit::value(res.to_string());
        manifest["dependencies"][target.id.name().to_string()].as_inline_table_mut().map(|t| t.fmt());
    } else {
        manifest["dev_dependencies"][target.id.name().to_string()]["version"] = toml_edit::value(target.version.to_string());
        manifest["dev_dependencies"][target.id.name().to_string()]["index"] = toml_edit::value(res.to_string());
        manifest["dev_dependencies"][target.id.name().to_string()].as_inline_table_mut().map(|t| t.fmt());
    }

    mf.seek(SeekFrom::Start(0))?;
    mf.write_all(manifest.to_string().as_bytes())?;


    Ok(format!("added package {} to manifest", target_s))
}

pub fn solve_local<F: FnMut(&Cache, Retriever, Graph<Summary>) -> Res<String>>(
    ctx: &BuildCtx,
    project: &Path,
    total: u8,
    ignore: Option<&[Spec]>,
    mut f: F,
) -> Res<String> {
    let project = find_manifest_root(project)?;
    let mut manifest = fs::File::open(project.join("elba.toml"))
        .context(format_err!("failed to read manifest file (elba.toml)"))?;
    let mut contents = String::new();
    manifest.read_to_string(&mut contents)?;

    let manifest = Manifest::from_str(&contents)?;

    let op = || -> Res<Graph<Summary>> {
        let mut f = fs::File::open(&project.join("elba.lock"))?;
        let mut contents = String::new();
        f.read_to_string(&mut contents)?;
        let toml = LockfileToml::from_str(&contents)?;

        Ok(toml.into())
    };

    let lock = match ignore {
        None => {
            if let Ok(solve) = op() {
                solve
            } else {
                Graph::default()
            }
        }
        Some(i) => {
            if i.is_empty() {
                Graph::default()
            } else if let Ok(mut solve) = op() {
                for spec in i {
                    let mut chosen: Option<Summary> = None;
                    let mut dfs = Dfs::new(&solve.inner, NodeIndex::new(0));
                    while let Some(ix) = dfs.next(&solve.inner) {
                        if spec.matches(&solve[ix]) {
                            if let Some(already_chosen) = chosen {
                                return Err(format_err!(
                                    "spec {} is ambiguous: both {} and {} match",
                                    spec,
                                    &solve[ix],
                                    already_chosen
                                ));
                            } else {
                                chosen = Some(solve.inner.remove_node(ix).unwrap());
                            }
                        }
                    }
                    if chosen.is_none() {
                        return Err(format_err!("spec {} not in lockfile", spec));
                    }
                }
                solve
            } else {
                Graph::default()
            }
        }
    };

    let root = {
        let cur = project.clone();
        let pid = PackageId::new(manifest.name().clone(), DirectRes::Dir { path: cur }.into());
        Summary::new(pid, manifest.version().clone())
    };

    let deps = manifest
        .deps(&ctx.indices, true)?
        .into_iter()
        .collect::<Vec<_>>();

    let dreses = deps
        .iter()
        .filter_map(|(p, _)| {
            if let Resolution::Index(IndexRes { res }) = p.resolution() {
                Some(res.clone())
            } else {
                None
            }
        })
        .collect::<Vec<_>>();

    let cache = Cache::from_disk(&ctx.logger, ctx.global_cache.clone(), ctx.shell)?;

    ctx.shell.println(
        style(format!("[1/{}]", total)).dim().bold(),
        "Resolving dependencies...",
        Verbosity::Quiet,
    );

    let mut retriever = Retriever::new(
        &cache.logger,
        &cache,
        root,
        deps,
        Left(dreses),
        lock,
        &ctx.indices,
        ctx.shell,
        ctx.offline,
    );
    let solver = Resolver::new(&retriever.logger.clone(), &mut retriever);
    let solve = solver.solve()?;
    ctx.shell.println(
        style("Writing").dim(),
        "lockfile at elba.lock",
        Verbosity::Verbose,
    );

    let mut lockfile = fs::OpenOptions::new()
        .write(true)
        .create(true)
        .open(project.join("elba.lock"))
        .context(format_err!("could not open elba.lock for writing"))?;

    let lf_contents: LockfileToml = solve.clone().into();
    let lf_contents = toml::to_string_pretty(&lf_contents)?;

    lockfile
        .write_all(lf_contents.as_bytes())
        .context(format_err!("could not write to elba.lock"))?;

    f(&cache, retriever, solve)
}

pub fn solve_remote<F: FnMut(&Cache, Retriever, Graph<Summary>) -> Res<String>>(
    ctx: &BuildCtx,
    name: &Spec,
    total: u8,
    mut f: F,
) -> Res<String> {
    let cache = Cache::from_disk(&ctx.logger, ctx.global_cache.clone(), ctx.shell)?;
    ctx.shell.println(
        style(format!("[1/{}]", total)).dim().bold(),
        "Resolving dependencies...",
        Verbosity::Quiet,
    );
    // For remote packages, we check the config for the indices we can load from
    let indices = ctx
        .indices
        .values()
        .cloned()
        .map(|x| x.res)
        .collect::<Vec<_>>();
    let mut indices = cache.get_indices(&indices, true, ctx.offline);
    ctx.shell.println(
        style("Cached").dim(),
        format!("indices at {}", cache.layout.indices.display()),
        Verbosity::Verbose,
    );
    let root = indices.select_by_spec(&name)?;

    let deps = indices
        .select(&root)
        .unwrap()
        .dependencies
        .iter()
        .cloned()
        .map(|d| (PackageId::new(d.name, d.index.into()), d.req))
        .collect::<Vec<_>>();

    let lock = Graph::default();

    let mut retriever = Retriever::new(
        &cache.logger,
        &cache,
        root,
        deps,
        Right(indices),
        lock,
        &ctx.indices,
        ctx.shell,
        ctx.offline,
    );
    let solve = Resolver::new(&retriever.logger.clone(), &mut retriever).solve()?;

    f(&cache, retriever, solve)
}

pub fn find_manifest_root(path: &Path) -> Res<PathBuf> {
    for p in path.ancestors() {
        if p.join("elba.toml").exists() {
            return Ok(p.to_path_buf());
        }
    }

    Err(format_err!(
        "no manifest file (elba.toml) exists in any parent directory"
    ))
}