calcit 0.12.32

Interpreter and js codegen for Calcit
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
//! CLI tool to download packages from github,
//! packages are defined in `deps.cirru` file
//!
//! files are stored in `~/.config/calcit/modules/`.

mod git;

use argh::{self, FromArgs};

use cirru_edn::Edn;
use colored::*;
use git::*;
use semver::Version;
use std::{
  collections::HashMap,
  fs,
  io::Write,
  path::{Path, PathBuf},
  sync::Arc,
  thread,
};

#[derive(Debug, Clone, PartialEq, Eq)]
struct PackageDeps {
  calcit_version: Option<String>,
  dependencies: HashMap<Arc<str>, Arc<str>>,
}

impl TryFrom<Edn> for PackageDeps {
  type Error = String;

  fn try_from(value: Edn) -> Result<Self, Self::Error> {
    let deps_info = value.view_map()?;
    #[allow(clippy::mutable_key_type)]
    let dict = deps_info.get_or_nil("dependencies").view_map()?.0;

    let mut deps: HashMap<Arc<str>, Arc<str>> = HashMap::new();
    for (k, v) in &dict {
      match (k, v) {
        (Edn::Str(k), Edn::Str(v)) => {
          deps.insert(k.to_owned(), v.to_owned());
        }
        _ => {
          return Err(format!("invalid dependency: {k} {v}"));
        }
      }
    }
    let expected_version: Option<String> = match deps_info.get_or_nil("calcit-version") {
      Edn::Str(s) => Some((*s).to_owned()),
      Edn::Nil => None,
      v => return Err(format!("invalid calcit-version: {v}")),
    };
    Ok(PackageDeps {
      calcit_version: expected_version,
      dependencies: deps,
    })
  }
}

pub fn main() -> Result<(), String> {
  // parse deps.cirru

  let cli_args: TopLevelCaps = argh::from_env();
  if let Some(SubCommand::Download(dep_names)) = &cli_args.subcommand {
    if dep_names.packages.is_empty() {
      eprintln!("Error: no packages to download!");
      std::process::exit(1);
    }
    let dict: HashMap<Arc<str>, Arc<str>> = dep_names
      .packages
      .iter()
      .map(|s| {
        let (org_and_folder, version) = s.split_once('@').ok_or("invalid name")?;
        Ok((org_and_folder.to_owned().into(), version.to_owned().into()))
      })
      .collect::<Result<_, String>>()?;
    download_deps(dict, cli_args)?;
    return Ok(());
  }

  // if file exists

  if Path::new(&cli_args.input).exists() {
    let content = fs::read_to_string(&cli_args.input).map_err(|e| e.to_string())?;
    let parsed = cirru_edn::parse(&content).map_err(|e| {
      eprintln!("\nFailed to parse '{}':", cli_args.input);
      eprintln!("{e}");
      format!("Failed to parse '{}'", cli_args.input)
    })?;
    let deps: PackageDeps = parsed.try_into()?;

    if let Some(version) = &deps.calcit_version
      && version != CALCIT_VERSION
    {
      eprintln!("[Warn] calcit version mismatch, deps.cirru expected {version}, running {CALCIT_VERSION}");
    }

    match &cli_args.subcommand {
      Some(SubCommand::Outdated(opts)) => {
        let updated = outdated_tags(deps, &cli_args.input, opts.yes)?;
        if updated {
          // Re-read deps.cirru and download updated dependencies
          println!("\nDownloading updated dependencies...");
          let content = fs::read_to_string(&cli_args.input).map_err(|e| e.to_string())?;
          let parsed = cirru_edn::parse(&content).map_err(|e| {
            eprintln!("\nFailed to parse '{}':", cli_args.input);
            eprintln!("{e}");
            format!("Failed to parse '{}'", cli_args.input)
          })?;
          let updated_deps: PackageDeps = parsed.try_into()?;
          download_deps(updated_deps.dependencies, cli_args)?;
        }
      }
      Some(SubCommand::Add(opts)) => {
        if opts.packages.is_empty() {
          return Err("no packages to add".to_string());
        }

        let mut updated_deps = deps;
        for raw in &opts.packages {
          let org_and_folder = normalize_package_name(raw)?;
          updated_deps
            .dependencies
            .insert(org_and_folder.into(), opts.version.to_owned().into());
        }

        write_deps_file(&cli_args.input, &updated_deps)?;
        println!("updated {}", cli_args.input.green());
        download_deps(updated_deps.dependencies, cli_args)?;
      }
      Some(SubCommand::Remove(opts)) => {
        if opts.packages.is_empty() {
          return Err("no packages to remove".to_string());
        }

        let mut updated_deps = deps;
        for raw in &opts.packages {
          let org_and_folder = normalize_package_name(raw)?;
          updated_deps.dependencies.remove(org_and_folder.as_str());
        }

        write_deps_file(&cli_args.input, &updated_deps)?;
        println!("updated {}", cli_args.input.green());
        download_deps(updated_deps.dependencies, cli_args)?;
      }
      Some(SubCommand::Download(dep_names)) => {
        unreachable!("already handled: {:?}", dep_names);
      }
      None => {
        download_deps(deps.dependencies, cli_args)?;
      }
    }

    Ok(())
  } else if Path::new("package.cirru").exists() {
    eprintln!("{}", "Error: 'package.cirru' is deprecated!".red().bold());
    eprintln!("Please rename it to 'deps.cirru':");
    eprintln!("  {}", "mv package.cirru deps.cirru".yellow());
    std::process::exit(1);
  } else {
    eprintln!("Error: no {} found!", cli_args.input);
    std::process::exit(1);
  }
}

fn download_deps(deps: HashMap<Arc<str>, Arc<str>>, options: TopLevelCaps) -> Result<(), String> {
  // ~/.config/calcit/modules/
  let clone_target = if options.local_debug {
    println!("{}", "  [DEBUG] local debug mode, cloning to test-modules/".yellow());
    ".config/calcit/test-modules"
  } else {
    ".config/calcit/modules"
  };
  let modules_dir = dirs::home_dir().ok_or("no config dir")?.join(clone_target);

  if !modules_dir.exists() {
    fs::create_dir_all(&modules_dir).map_err(|e| e.to_string())?;
    dim_println(format!("created dir: {modules_dir:?}"));
  }

  let mut children = vec![];

  for (org_and_folder, version) in deps {
    // cloned

    let org_and_folder = org_and_folder.clone();
    let options = options.to_owned();
    let modules_dir = modules_dir.clone();

    // TODO too many threads do not make it faster though
    let options2 = options.clone();
    let ret = thread::spawn(move || {
      let ret = handle_path(modules_dir, version, &options2, org_and_folder);
      if let Err(e) = ret {
        err_println(format!("{e}\n"));
      }
    });
    children.push(ret);
  }
  for child in children {
    child.join().unwrap();
  }

  Ok(())
}

fn handle_path(modules_dir: PathBuf, version: Arc<str>, options: &TopLevelCaps, org_and_folder: Arc<str>) -> Result<(), String> {
  // check if exists
  let (_org, folder) = org_and_folder.split_once('/').ok_or("invalid name")?;
  // split with / into (org,folder)

  let folder_path = modules_dir.join(folder);
  let build_file = folder_path.join("build.sh");
  let git_repo = GitRepo { dir: folder_path.clone() };
  if folder_path.exists() {
    // println!("module {} exists", folder);
    // check branch
    let current_head = git_repo.current_head()?;

    if current_head.get_name() == *version {
      dim_println(format!("√ found {} of {}", gray(&version), gray(folder)));
      if let GitHead::Branch(branch) = current_head
        && options.pull_branch
      {
        dim_println(format!("↺ pulling {} at version {}", gray(&org_and_folder), gray(&version)));
        git_repo.pull(&branch)?;
        dim_println(format!("pulled {} at {}", gray(folder), gray(&version)));

        // if there's a build.sh file in the folder, run it
        if build_file.exists() {
          let build_msg = call_build_script(&folder_path)?;
          dim_println(format!("ran build script for {}", gray(&org_and_folder)));
          dim_println(build_msg);
        }
      }
      return Ok(());
    }
    // let msg = format!("module {} is at version {:?}, but required {}", folder, current_head, version);
    // println!("  {}", msg.yellow());

    // load latest tags
    git_repo.fetch()?;
    // try if tag or branch exists in git history
    let has_target = git_repo.check_branch_or_tag(&version, folder)?;
    if !has_target {
      dim_println(format!("↺ fetching {} at version {}", gray(&org_and_folder), gray(&version)));
      git_repo.fetch()?;
      dim_println(format!("fetched {} at version {}", gray(&org_and_folder), gray(&version)));
      // fetch git repo and checkout target version
    }
    git_repo.checkout(&version)?;
    dim_println(format!("√ checked out {} of {}", gray(&version), gray(&org_and_folder)));

    let current_head = git_repo.current_head()?;
    if let GitHead::Branch(branch) = current_head
      && options.pull_branch
    {
      dim_println(format!("↺ pulling {} at version {}", gray(&org_and_folder), gray(&version)));
      git_repo.pull(&branch)?;
      dim_println(format!("pulled {} at {}", gray(folder), gray(&version)));
    }

    // if there's a build.sh file in the folder, run it
    if build_file.exists() {
      let build_msg = call_build_script(&folder_path)?;
      dim_println(format!("ran build script for {}", gray(&org_and_folder)));
      dim_println(build_msg);
    }
  } else {
    let url = if options.ci {
      format!("https://github.com/{org_and_folder}.git")
    } else {
      format!("git@github.com:{org_and_folder}.git")
    };
    dim_println(format!("↺ cloning {} at version {}", gray(&org_and_folder), gray(&version)));
    GitRepo::clone_to(&modules_dir, &url, &version, options.ci)?;
    // println!("downloading {} at version {}", url, version);
    dim_println(format!("downloaded {} at version {}", gray(&org_and_folder), gray(&version)));

    if !options.ci {
      // if there's a build.sh file in the folder, run it
      if build_file.exists() {
        let build_msg = call_build_script(&folder_path)?;
        dim_println(format!("ran build script for {}", gray(&org_and_folder)));
        dim_println(build_msg);
      }
    }
  }
  Ok(())
}

pub const CALCIT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(FromArgs, PartialEq, Debug, Clone)]
/// Top-level command.
struct TopLevelCaps {
  /// verbose mode
  #[argh(switch, short = 'v')]
  verbose: bool,

  /// outdated command
  #[argh(subcommand)]
  subcommand: Option<SubCommand>,

  /// pull branch in the repo
  #[argh(switch)]
  pull_branch: bool,
  /// CI mode loads shallow repo via HTTPS
  #[argh(switch)]
  ci: bool,
  /// debug mode, clone to test-modules/
  #[argh(switch)]
  local_debug: bool,

  /// input file
  #[argh(positional, default = "\"deps.cirru\".to_owned()")]
  input: String,
}

#[derive(FromArgs, PartialEq, Debug, Clone)]
#[argh(subcommand)]
enum SubCommand {
  /// show outdated versions
  Outdated(OutdatedCaps),
  Download(DownloadCaps),
  Add(AddCaps),
  Remove(RemoveCaps),
}

#[derive(FromArgs, PartialEq, Debug, Clone)]
/// show outdated versions
#[argh(subcommand, name = "outdated")]
struct OutdatedCaps {
  /// update deps.cirru directly without interactive confirmation
  #[argh(switch, short = 'y', long = "yes")]
  yes: bool,
}

#[derive(FromArgs, PartialEq, Debug, Clone)]
/// download named packages with org/repo@branch
#[argh(subcommand, name = "download")]
struct DownloadCaps {
  /// packages to download, in format of `org/repo@branch`
  #[argh(positional)]
  packages: Vec<String>,
}

#[derive(FromArgs, PartialEq, Debug, Clone)]
/// add dependencies to deps.cirru then run default download flow
#[argh(subcommand, name = "add")]
struct AddCaps {
  /// packages in format `org/repo` or github URL
  #[argh(positional)]
  packages: Vec<String>,
  /// version/branch written to deps.cirru
  #[argh(option, short = 'r', default = "\"main\".to_string()")]
  version: String,
}

#[derive(FromArgs, PartialEq, Debug, Clone)]
/// remove dependencies from deps.cirru then run default download flow
#[argh(subcommand, name = "remove")]
struct RemoveCaps {
  /// packages in format `org/repo` or github URL
  #[argh(positional)]
  packages: Vec<String>,
}

fn dim_println(msg: String) {
  if msg.chars().nth(1) == Some(' ') {
    println!("{}", msg.truecolor(128, 128, 128));
  } else {
    println!("  {}", msg.truecolor(128, 128, 128));
  }
}

fn err_println(msg: String) {
  if msg.chars().nth(1) == Some(' ') {
    println!("{}", msg.truecolor(255, 80, 80));
  } else {
    println!("  {}", msg.replace('\n', "\n  ").truecolor(255, 80, 80));
  }
}

fn gray(msg: &str) -> ColoredString {
  msg.truecolor(172, 172, 172)
}

fn indent4(msg: &str) -> String {
  let ret = msg
    .trim()
    .lines()
    .map(|line| format!("    {line}"))
    .collect::<Vec<String>>()
    .join("\n");
  format!("\n{ret}\n")
}

fn normalize_package_name(raw: &str) -> Result<String, String> {
  let mut s = raw.trim().to_string();

  if let Some(rest) = s.strip_prefix("https://github.com/") {
    s = rest.to_string();
  } else if let Some(rest) = s.strip_prefix("http://github.com/") {
    s = rest.to_string();
  } else if let Some(rest) = s.strip_prefix("git@github.com:") {
    s = rest.to_string();
  }

  if s.ends_with(".git") {
    s.truncate(s.len() - 4);
  }
  s = s.trim_end_matches('/').to_string();

  let segments: Vec<&str> = s.split('/').filter(|x| !x.is_empty()).collect();
  if segments.len() < 2 {
    return Err(format!("invalid package '{raw}', expected org/repo or github URL"));
  }

  Ok(format!("{}/{}", segments[0], segments[1]))
}

fn write_deps_file(deps_file: &str, deps: &PackageDeps) -> Result<(), String> {
  let mut updated_edn = Edn::Map(cirru_edn::EdnMapView::default());

  if let Edn::Map(ref mut map) = updated_edn {
    if let Some(ref version) = deps.calcit_version {
      map.insert(Edn::tag("calcit-version"), Edn::str(version.as_str()));
    }

    let mut deps_map = cirru_edn::EdnMapView::default();
    for (k, v) in &deps.dependencies {
      deps_map.insert(Edn::str(&**k), Edn::str(&**v));
    }
    map.insert(Edn::tag("dependencies"), Edn::Map(deps_map));
  }

  let updated_content = cirru_edn::format(&updated_edn, false)?;
  fs::write(deps_file, updated_content).map_err(|e| e.to_string())?;
  Ok(())
}

/// calcit dynamic libs uses a `build.sh` script to build Rust `.so` files
fn call_build_script(folder_path: &Path) -> Result<String, String> {
  let output = std::process::Command::new("sh")
    .arg("build.sh")
    .current_dir(folder_path)
    .output()
    .map_err(|e| e.to_string())?;
  if output.status.success() {
    let msg = std::str::from_utf8(&output.stdout).unwrap_or("");
    Ok(indent4(msg))
  } else {
    let msg = std::str::from_utf8(&output.stderr).unwrap_or("");
    err_println(indent4(msg));
    Err(format!("failed to build module {}", folder_path.display()))
  }
}

/// read packages from deps, find tag(or sha) and committed date,
/// also git fetch to read latest tag from remote,
/// then we can compare, get outdated version printed
/// Returns true if deps.cirru was updated
fn outdated_tags(deps: PackageDeps, deps_file: &str, auto_yes: bool) -> Result<bool, String> {
  print_column("package".dimmed(), "expected".dimmed(), "latest".dimmed(), "hint".dimmed());
  println!();

  let mut outdated_packages = Vec::new();
  let mut children = vec![];

  for (org_and_folder, version) in &deps.dependencies {
    let org_and_folder_clone = org_and_folder.clone();
    let version_clone = version.clone();
    let ret = thread::spawn(move || {
      let ret = show_package_versions(org_and_folder_clone, version_clone);
      if let Err(e) = ret {
        err_println(format!("{e}\n"));
        return None;
      }
      ret.ok()
    });
    children.push((org_and_folder.clone(), version.clone(), ret));
  }

  for (org_and_folder, version, child) in children {
    if let Ok(Some(Some(latest_tag))) = child.join() {
      if latest_tag != *version {
        outdated_packages.push((org_and_folder.to_owned(), version.to_owned(), latest_tag));
      }
    }
  }

  let calcit_version_upgrade = deps.calcit_version.as_ref().and_then(|version| {
    let expected = Version::parse(version).ok()?;
    let current = Version::parse(CALCIT_VERSION).ok()?;
    if expected < current { Some(version.to_owned()) } else { None }
  });

  if !outdated_packages.is_empty() || calcit_version_upgrade.is_some() {
    if auto_yes {
      update_deps_file(&outdated_packages, calcit_version_upgrade.as_deref(), deps_file)?;
      println!("deps.cirru updated successfully!");
      return Ok(true);
    }

    println!();
    let mut changes = Vec::new();
    if !outdated_packages.is_empty() {
      changes.push(format!("{} outdated package(s)", outdated_packages.len()));
    }
    if let Some(version) = &calcit_version_upgrade {
      changes.push(format!("calcit-version {version} -> {CALCIT_VERSION}"));
    }
    print!("Found {}. Update deps.cirru? (y/N): ", changes.join(", "));
    std::io::stdout().flush().map_err(|e| e.to_string())?;

    let mut input = String::new();
    std::io::stdin().read_line(&mut input).map_err(|e| e.to_string())?;
    let input = input.trim();

    if input.is_empty() || input.to_lowercase() == "y" || input.to_lowercase() == "yes" {
      update_deps_file(&outdated_packages, calcit_version_upgrade.as_deref(), deps_file)?;
      println!("deps.cirru updated successfully!");
      return Ok(true);
    }
  }

  Ok(false)
}

fn show_package_versions(org_and_folder: Arc<str>, version: Arc<str>) -> Result<Option<String>, String> {
  let (_org, folder) = org_and_folder.split_once('/').ok_or("invalid name")?;
  let folder_path = dirs::home_dir().ok_or("no config dir")?.join(".config/calcit/modules").join(folder);
  let git_repo = GitRepo { dir: folder_path.clone() };
  if folder_path.exists() {
    git_repo.fetch()?;

    // get latest tag and timestamp
    let latest_tag = git_repo.latest_tag()?;
    let latest_timestamp = git_repo.timestamp(&latest_tag)?;

    // get expected tag and timestamp
    let expected_timestamp = git_repo.timestamp(&version)?;

    let outdated = expected_timestamp < latest_timestamp;

    if outdated {
      print_column(org_and_folder.yellow(), version.yellow(), latest_tag.yellow(), "Outdated".yellow());
      Ok(Some(latest_tag))
    } else {
      print_column(org_and_folder.dimmed(), version.dimmed(), latest_tag.dimmed(), "".dimmed());
      Ok(None)
    }
  } else {
    print_column(org_and_folder.red(), version.red(), "not found".red(), "-".red());
    Ok(None)
  }
}

fn update_deps_file(
  outdated_packages: &[(Arc<str>, Arc<str>, String)],
  calcit_version_upgrade: Option<&str>,
  deps_file: &str,
) -> Result<(), String> {
  if !Path::new(deps_file).exists() {
    return Err("deps.cirru file not found".to_string());
  }

  let content = fs::read_to_string(deps_file).map_err(|e| e.to_string())?;
  let parsed = cirru_edn::parse(&content).map_err(|e| {
    eprintln!("\nFailed to parse '{deps_file}':");
    eprintln!("{e}");
    format!("Failed to parse '{deps_file}'")
  })?;
  let mut deps: PackageDeps = parsed.try_into()?;

  if let Some(version) = calcit_version_upgrade {
    deps.calcit_version = Some(version.to_string());
  }

  // Update the dependencies in the parsed structure
  for (org_and_folder, _old_version, new_version) in outdated_packages {
    deps.dependencies.insert(org_and_folder.clone(), new_version.clone().into());
  }

  write_deps_file(deps_file, &deps)
}

fn print_column(pkg: ColoredString, expected: ColoredString, latest: ColoredString, hint: ColoredString) {
  println!("{pkg:<32} {expected:<12} {latest:<12} {hint:<12}");
}