1use crate::pkg::{config, types};
2use anyhow::{Result, anyhow};
3use colored::*;
4use std::collections::HashMap;
5
6pub fn run(packages: Vec<String>, global: bool) -> Result<()> {
7 if global {
8 run_global(packages)
9 } else {
10 run_project(packages)
11 }
12}
13
14fn run_global(packages: Vec<String>) -> Result<()> {
15 println!(
16 "{} Adding packages to global configuration...",
17 "::".bold().blue()
18 );
19
20 let mut versions_to_add = HashMap::new();
21 for pkg_spec in &packages {
22 let request = crate::pkg::resolve::parse_source_string(pkg_spec)?;
23 let version = request.version_spec.unwrap_or_else(|| "latest".to_string());
24 versions_to_add.insert(request.name, version);
25 }
26
27 config::update_global_versions(versions_to_add)?;
28
29 println!("{} Installing global packages...", "::".bold().blue());
30 let options = crate::SourceInstallOptions {
31 scope_override: Some(types::Scope::User),
32 yes: true,
33 ..Default::default()
34 };
35
36 crate::install_sources(&packages, &options)?;
37
38 println!("\n{}", "Global packages updated and installed.".green());
39 Ok(())
40}
41
42fn run_project(packages: Vec<String>) -> Result<()> {
43 if !std::path::Path::new("zoi.lua").exists() {
44 return Err(anyhow!(
45 "No 'zoi.lua' found in the current directory. Run 'zoi use --global' or initialize a project first."
46 ));
47 }
48
49 println!(
50 "{} Project uses zoi.lua. Automatic saving is not supported for Lua configurations.",
51 "Note:".yellow().bold()
52 );
53 println!(" Please add the following to your packages() block in zoi.lua:");
54 for pkg in &packages {
55 println!(" - \"{}\"", pkg);
56 }
57
58 println!("{} Installing project packages...", "::".bold().blue());
59 let options = crate::SourceInstallOptions {
60 scope_override: Some(types::Scope::Project),
61 yes: true,
62 ..Default::default()
63 };
64
65 crate::install_sources(&packages, &options)?;
66
67 println!("\n{}", "Project packages updated and installed.".green());
68 Ok(())
69}