Grabby - No more downloads or complicated package management

Started by sigma-octantis, Jul 23, 2023, 12:44 AM

Previous topic - Next topic

sigma-octantis

Grabby The simplest library dependency solution

Have you ever found out that splitting APIs, reusing code and using your own (Or other's) libraries gets tedious?

Usually goes this way: You wget a bunch of random lua files, you find out that you're missing some more. Then you get angry and tired, and realize that, you need them also in another computer. You end up with a disk that contain all the stuff, but you lose it...

Solutions? Bundling the files (Good luck...), use intricate package managers (Version, downloads? my head gets dizzy)

Why not just require them from the internet?


Here is where grabby comes to the rescue.

Okay cool, but gimme the installer

As of now (22nd of July, 2023), the grabby module depends on the monody package, which is also available at my ComputerCraft Scripts Monorepo, so you should download two scripts for now, but I simplified the process with a pastebin installer, just run:
pastebin run sWRBUZtcAnd you're set!

Now go ahead and grab some libraries.

Pro tip: Grab libraries that use Grabby, and they'll resolve their own dependencies, without headaches!

Based on Deno's package philosophy, grabby will act as your
require function, but with multiple schemas.

If your require looks like
let mod = grab("http://my-cdn.com/my-mod");, it will download and load it from the internet, with a get request.

If your require looks like
let mod = grab("pastebin:iex4Tgeb");, it grabs it from pastebin, and so on.

Supported schemas and examples:
  • Local import
    grab("your/module"), like the usual require.
  • HTTP/HTTPS import, just plug in a plain URL
  • pastebin import
    grab("pastebin:iex4Tgeb");, just
    pastebin: followed by the code
  • Github scheme:
    grab("github:sigmasoldi3r/cc-scripts/main/enquirer.saturn");, which is just
    github: followed by
    username/repository/branch/path-to-the-file



Source

You can take a look at the source file at the repositories' file cc-scripts/grabby.saturn.

It looks like this:

// ... More code ...
@trait()
class Solver {
  fn resolve(self, source) {
    if self.cache[source] <> () {
      return self.cache[source];
    }
    let url = self.resolve_url(source);
    let src = http::get(url)::readAll();
    let mod = load(src, source, "bt", { require });
    self.cache[source] = mod();
    return self.cache[source];
  }
  @abstract() fn resolve_url(self, source) {}
  @abstract() fn is_valid(self, source) {}
}

@mixin(Solver)
class HttpSolver {
  fn new() {
    return HttpSolver { cache: {} };
  }
  @param("source", is_string)
  fn is_valid(self, source) {
    return source.find("^https?://") <> ();
  }
  @param("source", is_string)
  fn resolve_url(self, source) {
    return source;
  }
}
// ... More code ...
Check out Saturnus - a language that compiles to lua
You'll be surprised how modern languages can simplify development!

Also check out my ComputerCraft scripts repository!