linguo 1.2.0

Cross-platform, multi-language runtime, package, and project manager
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
mod config;
mod exec;
mod fetch;
mod go;
mod node;
mod php;
mod python;
mod ruby;
mod rust;
mod shell;
mod status;
mod store;
mod terraform;
mod versions;
mod workspace;
mod zig;

use clap::{Parser, Subcommand};

use shell::Shell;

#[derive(Parser)]
#[command(
    name = "linguo",
    version,
    about = "Multi-language runtime, package, and project manager"
)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Manage Python toolchains and projects
    Python {
        #[command(subcommand)]
        command: PythonCommand,
    },
    /// Manage Node.js toolchains and projects
    Node {
        #[command(subcommand)]
        command: NodeCommand,
    },
    /// Manage Go toolchains and projects
    Go {
        #[command(subcommand)]
        command: GoCommand,
    },
    /// Manage Ruby toolchains and projects
    Ruby {
        #[command(subcommand)]
        command: RubyCommand,
    },
    /// Manage Rust toolchains and projects
    Rust {
        #[command(subcommand)]
        command: RustCommand,
    },
    /// Manage PHP toolchains and projects
    Php {
        #[command(subcommand)]
        command: PhpCommand,
    },
    /// Manage Zig toolchains and projects
    Zig {
        #[command(subcommand)]
        command: ZigCommand,
    },
    /// Manage Terraform toolchains
    #[command(alias = "tf")]
    Terraform {
        #[command(subcommand)]
        command: TerraformCommand,
    },
    /// Overview of all languages: installed toolchains and active pins
    #[command(alias = "list")]
    Status,
    /// Sync every workspace member: install missing pinned toolchains, then
    /// run each member project's dependency sync
    Sync,
    /// Upgrade every language pinned in this directory
    Upgrade {
        /// Bump each pin to the newest stable release (same granularity)
        #[arg(long)]
        latest: bool,
        /// Uninstall older toolchains the pins previously matched
        #[arg(long)]
        prune: bool,
    },
    /// Print the shell hook (add `eval "$(linguo activate zsh)"` to your rc file)
    Activate { shell: Shell },
    /// Print PATH updates for the current directory (used by the shell hook)
    #[command(hide = true)]
    Env {
        #[arg(long)]
        shell: Shell,
    },
}

#[derive(Subcommand)]
enum PythonCommand {
    /// Download and install a toolchain (latest if no version is given)
    Install { version: Option<String> },
    /// Remove an installed toolchain
    Uninstall { version: String },
    /// List installed toolchains
    List {
        /// List versions available for download instead
        #[arg(long)]
        available: bool,
    },
    /// Pin a version for this directory (or globally)
    Use {
        version: String,
        #[arg(long)]
        global: bool,
    },
    /// Upgrade the pinned toolchain: newest release within the pin, or bump
    /// the pin itself with --latest
    Upgrade {
        /// Bump the pin to the newest stable release (same granularity)
        #[arg(long)]
        latest: bool,
        /// Uninstall older toolchains the pin previously matched
        #[arg(long)]
        prune: bool,
    },
    /// Create a new project: pyproject.toml, version pin, and venv
    Init { name: Option<String> },
    /// Install packages into the project venv and add them to pyproject.toml
    Add { packages: Vec<String> },
    /// Uninstall packages and remove them from pyproject.toml
    Remove { packages: Vec<String> },
    /// Install everything pyproject.toml declares into the project venv
    Sync,
    /// Show which executable a command resolves to (default: python)
    Which { command: Option<String> },
    /// Run a command with the project venv and pinned toolchain on PATH
    Run {
        #[arg(trailing_var_arg = true, required = true)]
        args: Vec<String>,
    },
}

#[derive(Subcommand)]
enum NodeCommand {
    /// Download and install a toolchain (latest LTS if no version is given)
    Install { version: Option<String> },
    /// Remove an installed toolchain
    Uninstall { version: String },
    /// List installed toolchains
    List {
        /// List versions available for download instead
        #[arg(long)]
        available: bool,
    },
    /// Pin a version for this directory (or globally)
    Use {
        version: String,
        #[arg(long)]
        global: bool,
    },
    /// Upgrade the pinned toolchain: newest release within the pin, or bump
    /// the pin itself with --latest
    Upgrade {
        /// Bump the pin to the newest stable release (same granularity)
        #[arg(long)]
        latest: bool,
        /// Uninstall older toolchains the pin previously matched
        #[arg(long)]
        prune: bool,
    },
    /// Create a new project: package.json and version pin
    Init { name: Option<String> },
    /// npm install packages into the project
    Add { packages: Vec<String> },
    /// npm uninstall packages from the project
    Remove { packages: Vec<String> },
    /// Install everything package.json declares
    Sync,
    /// Show which executable a command resolves to (default: node)
    Which { command: Option<String> },
    /// Run a command with node_modules/.bin and the toolchain on PATH
    Run {
        #[arg(trailing_var_arg = true, required = true)]
        args: Vec<String>,
    },
}

#[derive(Subcommand)]
enum GoCommand {
    /// Download and install a toolchain (latest stable if no version is given)
    Install { version: Option<String> },
    /// Remove an installed toolchain
    Uninstall { version: String },
    /// List installed toolchains
    List {
        /// List versions available for download instead
        #[arg(long)]
        available: bool,
    },
    /// Pin a version for this directory (or globally)
    Use {
        version: String,
        #[arg(long)]
        global: bool,
    },
    /// Upgrade the pinned toolchain: newest release within the pin, or bump
    /// the pin itself with --latest
    Upgrade {
        /// Bump the pin to the newest stable release (same granularity)
        #[arg(long)]
        latest: bool,
        /// Uninstall older toolchains the pin previously matched
        #[arg(long)]
        prune: bool,
    },
    /// Create a new module: go mod init and version pin
    Init { module: Option<String> },
    /// go get packages into the module
    Add { packages: Vec<String> },
    /// Drop packages from the module (go get pkg@none)
    Remove { packages: Vec<String> },
    /// Download everything go.mod declares
    Sync,
    /// Show which executable a command resolves to (default: go)
    Which { command: Option<String> },
    /// Run a command with the pinned toolchain on PATH
    Run {
        #[arg(trailing_var_arg = true, required = true)]
        args: Vec<String>,
    },
}

#[derive(Subcommand)]
enum RubyCommand {
    /// Download and install a toolchain (latest if no version is given)
    Install { version: Option<String> },
    /// Remove an installed toolchain
    Uninstall { version: String },
    /// List installed toolchains
    List {
        /// List versions available for download instead
        #[arg(long)]
        available: bool,
    },
    /// Pin a version for this directory (or globally)
    Use {
        version: String,
        #[arg(long)]
        global: bool,
    },
    /// Upgrade the pinned toolchain: newest release within the pin, or bump
    /// the pin itself with --latest
    Upgrade {
        /// Bump the pin to the newest stable release (same granularity)
        #[arg(long)]
        latest: bool,
        /// Uninstall older toolchains the pin previously matched
        #[arg(long)]
        prune: bool,
    },
    /// Create a new project: Gemfile and version pin
    Init,
    /// bundle add gems to the project
    Add { gems: Vec<String> },
    /// bundle remove gems from the project
    Remove { gems: Vec<String> },
    /// Install everything the Gemfile declares (bundle install)
    Sync,
    /// Show which executable a command resolves to (default: ruby)
    Which { command: Option<String> },
    /// Run a command with the pinned toolchain and its gems on PATH
    Run {
        #[arg(trailing_var_arg = true, required = true)]
        args: Vec<String>,
    },
}

#[derive(Subcommand)]
enum RustCommand {
    /// Download and install a toolchain (latest stable if no version is given)
    Install { version: Option<String> },
    /// Remove an installed toolchain
    Uninstall { version: String },
    /// List installed toolchains
    List {
        /// List versions available for download instead
        #[arg(long)]
        available: bool,
    },
    /// Pin a version for this directory (or globally)
    Use {
        version: String,
        #[arg(long)]
        global: bool,
    },
    /// Upgrade the pinned toolchain: newest release within the pin, or bump
    /// the pin itself with --latest
    Upgrade {
        /// Bump the pin to the newest stable release (same granularity)
        #[arg(long)]
        latest: bool,
        /// Uninstall older toolchains the pin previously matched
        #[arg(long)]
        prune: bool,
    },
    /// Create a new project: cargo init and version pin
    Init { name: Option<String> },
    /// cargo add crates to the project
    Add { crates: Vec<String> },
    /// cargo remove crates from the project
    Remove { crates: Vec<String> },
    /// Download everything Cargo.toml declares (cargo fetch)
    Sync,
    /// Show which executable a command resolves to (default: cargo)
    Which { command: Option<String> },
    /// Run a command with the pinned toolchain on PATH
    Run {
        #[arg(trailing_var_arg = true, required = true)]
        args: Vec<String>,
    },
    /// Manage the active toolchain's components (rust-analyzer, rust-src, ...)
    Component {
        #[command(subcommand)]
        command: RustComponentCommand,
    },
    /// Manage the active toolchain's cross-compilation targets
    Target {
        #[command(subcommand)]
        command: RustTargetCommand,
    },
}

#[derive(Subcommand)]
enum RustComponentCommand {
    /// Install components into the active toolchain
    Add { names: Vec<String> },
}

#[derive(Subcommand)]
enum RustTargetCommand {
    /// Install rust-std for extra target triples into the active toolchain
    Add { triples: Vec<String> },
}

#[derive(Subcommand)]
enum PhpCommand {
    /// Download and install a toolchain (latest stable if no version is given)
    Install { version: Option<String> },
    /// Remove an installed toolchain
    Uninstall { version: String },
    /// List installed toolchains
    List {
        /// List versions available for download instead
        #[arg(long)]
        available: bool,
    },
    /// Pin a version for this directory (or globally)
    Use {
        version: String,
        #[arg(long)]
        global: bool,
    },
    /// Upgrade the pinned toolchain: newest release within the pin, or bump
    /// the pin itself with --latest
    Upgrade {
        /// Bump the pin to the newest stable release (same granularity)
        #[arg(long)]
        latest: bool,
        /// Uninstall older toolchains the pin previously matched
        #[arg(long)]
        prune: bool,
    },
    /// Create a new project: composer.json and version pin
    Init,
    /// composer require packages into the project
    Add { packages: Vec<String> },
    /// composer remove packages from the project
    Remove { packages: Vec<String> },
    /// Install everything composer.json declares
    Sync,
    /// Show which executable a command resolves to (default: php)
    Which { command: Option<String> },
    /// Run a command with vendor/bin and the toolchain on PATH
    Run {
        #[arg(trailing_var_arg = true, required = true)]
        args: Vec<String>,
    },
}

#[derive(Subcommand)]
enum ZigCommand {
    /// Download and install a toolchain (latest stable if no version is given)
    Install { version: Option<String> },
    /// Remove an installed toolchain
    Uninstall { version: String },
    /// List installed toolchains
    List {
        /// List versions available for download instead
        #[arg(long)]
        available: bool,
    },
    /// Pin a version for this directory (or globally)
    Use {
        version: String,
        #[arg(long)]
        global: bool,
    },
    /// Upgrade the pinned toolchain: newest release within the pin, or bump
    /// the pin itself with --latest
    Upgrade {
        /// Bump the pin to the newest stable release (same granularity)
        #[arg(long)]
        latest: bool,
        /// Uninstall older toolchains the pin previously matched
        #[arg(long)]
        prune: bool,
    },
    /// Create a new project: zig init and version pin
    Init,
    /// zig fetch --save packages (archive URLs or paths) into the project
    Add { packages: Vec<String> },
    /// Fetch everything build.zig.zon declares (zig build --fetch)
    Sync,
    /// Show which executable a command resolves to (default: zig)
    Which { command: Option<String> },
    /// Run a command with the pinned toolchain on PATH
    Run {
        #[arg(trailing_var_arg = true, required = true)]
        args: Vec<String>,
    },
}

#[derive(Subcommand)]
enum TerraformCommand {
    /// Download and install a toolchain (latest stable if no version is given)
    Install { version: Option<String> },
    /// Remove an installed toolchain
    Uninstall { version: String },
    /// List installed toolchains
    List {
        /// List versions available for download instead
        #[arg(long)]
        available: bool,
    },
    /// Pin a version for this directory (or globally)
    Use {
        version: String,
        #[arg(long)]
        global: bool,
    },
    /// Upgrade the pinned toolchain: newest release within the pin, or bump
    /// the pin itself with --latest
    Upgrade {
        /// Bump the pin to the newest stable release (same granularity)
        #[arg(long)]
        latest: bool,
        /// Uninstall older toolchains the pin previously matched
        #[arg(long)]
        prune: bool,
    },
    /// Show which executable a command resolves to (default: terraform)
    Which { command: Option<String> },
    /// Run a command with the pinned toolchain on PATH
    Run {
        #[arg(trailing_var_arg = true, required = true)]
        args: Vec<String>,
    },
}

/// Upgrade every language with a resolvable pin in the current directory.
fn upgrade_all(latest: bool, prune: bool) -> anyhow::Result<()> {
    let cwd = std::env::current_dir()?;
    let mut failures: Vec<String> = Vec::new();
    let mut any = false;
    type UpgradeFn = fn(bool, bool) -> anyhow::Result<()>;
    let languages: [(&str, UpgradeFn); 7] = [
        (python::LANGUAGE, python::upgrade),
        (node::LANGUAGE, node::upgrade),
        (ruby::LANGUAGE, ruby::upgrade),
        (go::LANGUAGE, go::upgrade),
        (rust::LANGUAGE, rust::upgrade),
        (zig::LANGUAGE, zig::upgrade),
        (php::LANGUAGE, php::upgrade),
    ];
    for (language, upgrade) in languages {
        if store::resolve_pin(language, &cwd)?.is_none() {
            continue;
        }
        any = true;
        println!("{language}:");
        if let Err(err) = upgrade(latest, prune) {
            eprintln!("  {err:#}");
            failures.push(language.to_string());
        }
    }
    if config::resolve_pin(terraform::LANGUAGE, &cwd)?.is_some() {
        any = true;
        println!("terraform:");
        if let Err(err) = terraform::upgrade(latest, prune) {
            eprintln!("  {err:#}");
            failures.push("terraform".to_string());
        }
    }
    if !any {
        println!("nothing pinned here (run `linguo <language> use <version>` first)");
    }
    if !failures.is_empty() {
        anyhow::bail!("upgrade failed for: {}", failures.join(", "));
    }
    Ok(())
}

fn main() -> anyhow::Result<()> {
    let cli = Cli::parse();
    match cli.command {
        Command::Python { command } => match command {
            PythonCommand::Install { version } => python::install(version),
            PythonCommand::Uninstall { version } => store::uninstall(python::LANGUAGE, &version),
            PythonCommand::List { available } => python::list(available),
            PythonCommand::Use { version, global } => {
                store::use_version(python::LANGUAGE, &version, global)
            }
            PythonCommand::Upgrade { latest, prune } => python::upgrade(latest, prune),
            PythonCommand::Init { name } => python::project::init(name),
            PythonCommand::Add { packages } => python::project::add(&packages),
            PythonCommand::Remove { packages } => python::project::remove(&packages),
            PythonCommand::Sync => python::project::sync(),
            PythonCommand::Which { command } => python::project::which(command),
            PythonCommand::Run { args } => python::project::run(&args),
        },
        Command::Node { command } => match command {
            NodeCommand::Install { version } => node::install(version),
            NodeCommand::Uninstall { version } => store::uninstall(node::LANGUAGE, &version),
            NodeCommand::List { available } => node::list(available),
            NodeCommand::Use { version, global } => {
                store::use_version(node::LANGUAGE, &version, global)
            }
            NodeCommand::Upgrade { latest, prune } => node::upgrade(latest, prune),
            NodeCommand::Init { name } => node::project::init(name),
            NodeCommand::Add { packages } => node::project::add(&packages),
            NodeCommand::Remove { packages } => node::project::remove(&packages),
            NodeCommand::Sync => node::project::sync(),
            NodeCommand::Which { command } => node::project::which(command),
            NodeCommand::Run { args } => node::project::run(&args),
        },
        Command::Go { command } => match command {
            GoCommand::Install { version } => go::install(version),
            GoCommand::Uninstall { version } => store::uninstall(go::LANGUAGE, &version),
            GoCommand::List { available } => go::list(available),
            GoCommand::Use { version, global } => {
                store::use_version(go::LANGUAGE, &version, global)
            }
            GoCommand::Upgrade { latest, prune } => go::upgrade(latest, prune),
            GoCommand::Init { module } => go::project::init(module),
            GoCommand::Add { packages } => go::project::add(&packages),
            GoCommand::Remove { packages } => go::project::remove(&packages),
            GoCommand::Sync => go::project::sync(),
            GoCommand::Which { command } => go::project::which(command),
            GoCommand::Run { args } => go::project::run(&args),
        },
        Command::Ruby { command } => match command {
            RubyCommand::Install { version } => ruby::install(version),
            RubyCommand::Uninstall { version } => store::uninstall(ruby::LANGUAGE, &version),
            RubyCommand::List { available } => ruby::list(available),
            RubyCommand::Use { version, global } => {
                store::use_version(ruby::LANGUAGE, &version, global)
            }
            RubyCommand::Upgrade { latest, prune } => ruby::upgrade(latest, prune),
            RubyCommand::Init => ruby::project::init(),
            RubyCommand::Add { gems } => ruby::project::add(&gems),
            RubyCommand::Remove { gems } => ruby::project::remove(&gems),
            RubyCommand::Sync => ruby::project::sync(),
            RubyCommand::Which { command } => ruby::project::which(command),
            RubyCommand::Run { args } => ruby::project::run(&args),
        },
        Command::Rust { command } => match command {
            RustCommand::Install { version } => rust::install(version),
            RustCommand::Uninstall { version } => rust::uninstall(&version),
            RustCommand::List { available } => rust::list(available),
            RustCommand::Use { version, global } => rust::use_version(&version, global),
            RustCommand::Upgrade { latest, prune } => rust::upgrade(latest, prune),
            RustCommand::Init { name } => rust::project::init(name),
            RustCommand::Add { crates } => rust::project::add(&crates),
            RustCommand::Remove { crates } => rust::project::remove(&crates),
            RustCommand::Sync => rust::project::sync(),
            RustCommand::Which { command } => rust::project::which(command),
            RustCommand::Run { args } => rust::project::run(&args),
            RustCommand::Component { command } => match command {
                RustComponentCommand::Add { names } => rust::component_add(&names),
            },
            RustCommand::Target { command } => match command {
                RustTargetCommand::Add { triples } => rust::target_add(&triples),
            },
        },
        Command::Php { command } => match command {
            PhpCommand::Install { version } => php::install(version),
            PhpCommand::Uninstall { version } => store::uninstall(php::LANGUAGE, &version),
            PhpCommand::List { available } => php::list(available),
            PhpCommand::Use { version, global } => {
                store::use_version(php::LANGUAGE, &version, global)
            }
            PhpCommand::Upgrade { latest, prune } => php::upgrade(latest, prune),
            PhpCommand::Init => php::project::init(),
            PhpCommand::Add { packages } => php::project::add(&packages),
            PhpCommand::Remove { packages } => php::project::remove(&packages),
            PhpCommand::Sync => php::project::sync(),
            PhpCommand::Which { command } => php::project::which(command),
            PhpCommand::Run { args } => php::project::run(&args),
        },
        Command::Zig { command } => match command {
            ZigCommand::Install { version } => zig::install(version),
            ZigCommand::Uninstall { version } => store::uninstall(zig::LANGUAGE, &version),
            ZigCommand::List { available } => zig::list(available),
            ZigCommand::Use { version, global } => {
                store::use_version(zig::LANGUAGE, &version, global)
            }
            ZigCommand::Upgrade { latest, prune } => zig::upgrade(latest, prune),
            ZigCommand::Init => zig::project::init(),
            ZigCommand::Add { packages } => zig::project::add(&packages),
            ZigCommand::Sync => zig::project::sync(),
            ZigCommand::Which { command } => zig::project::which(command),
            ZigCommand::Run { args } => zig::project::run(&args),
        },
        Command::Terraform { command } => match command {
            TerraformCommand::Install { version } => terraform::install(version),
            TerraformCommand::Uninstall { version } => terraform::uninstall(&version),
            TerraformCommand::List { available } => terraform::list(available),
            TerraformCommand::Use { version, global } => terraform::use_version(&version, global),
            TerraformCommand::Upgrade { latest, prune } => terraform::upgrade(latest, prune),
            TerraformCommand::Which { command } => terraform::which(command),
            TerraformCommand::Run { args } => terraform::run(&args),
        },
        Command::Sync => workspace::sync(),
        Command::Status => status::status(),
        Command::Upgrade { latest, prune } => upgrade_all(latest, prune),
        Command::Activate { shell } => {
            shell::activate(shell);
            Ok(())
        }
        Command::Env { shell } => shell::env(shell),
    }
}