This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
dragonlord's profile picture

depends.lua - A simple (and powerful) dependency management API

Started by dragonlord, 03 June 2014 - 06:38 AM
dragonlord #1
Posted 03 June 2014 - 08:38 AM
We all started small. Then our programs got bigger. And bigger. AND BIGGER.

At some point, maintaining all that code you've written can be such a pain.

Then you start messing around with os.loadAPI(). Sure, it makes your code modular and easier to read. But now you have to worry about how to distribute your program. How do you ensure that users will install it properly?

I've created a simple dependency API/Program called depends.lua. It's a dependency management API built on top of ComputerCraft's existing API loading mechanism, with a few extras.

Features:
  • Simple syntax - No need to specify the path to the API you want to load. All managed API's are located in just one folder. One line of code takes care of both os.unloadAPI() and os.loadAPI().
  • Pastebin integration - Want your program to download a missing API? Not a problem. Just declare the pastebin code for the dependency, and depends.lua will download it if it's not yet installed.
  • Simple configuration - Dependencies are declared in a very simple config file (it's just another lua script!). Forgot to include a dependency? Don't worry, it'll figure itself out and update the config file.
Usage:

You can get depends.lua by running the following on any computer:


pastebin get Heu45RKs depends

One of the nice things about this is you can name it any way you like.

Before starting, you will need to generate a configuration file. Just run the program and you're ready to go.

There are 2 ways to use depends.lua:

As an API

You can use depends.lua like any normal API.

Let's make one called example


os.unloadAPI("depends")
os.loadAPI("depends")

depends.on("stack")

stack.push("world")
stack.push("hello")
print(stack.pop())
print(stack.pop())

This basically says your program requires the API "stack" loaded. It will then search for the API in /lib (configurable).

That's fine and good, but stack does not exist in /lib yet, and it will throw an error. Let's fix that:


os.unloadAPI("depends")
os.loadAPI("depends")

depends.on("stack", "HZQqMVbH")

stack.push("world")
stack.push("hello")
print(stack.pop())
print(stack.pop())

Run your program again, and it'll download the stack API from pastebin and run the program.


As your program's entry point

For some people, they would be happy to just use depends.lua as just another API. But to take full advantage of what it can do, you can use it as your program's main entry point.

When using depends.lua as the entry point, our example program becomes like this:


depends.on("stack", "HZQqMVbH")

stack.push("world")
stack.push("hello")
print(stack.pop())
print(stack.pop())

You could still keep the calls to os.loadAPI() if you so wish.

Run your program by executing the following:


depends --run example

This will search for example in the root directory. If it does not exist, it will look at the lib directory.

Running this will simulate an os.loadAPI() call to setup depends.lua as an API, register example as a startup program, and run it.

Running this:


depends

Will run the last startup program.


Publish your API's

Once you're ready to publish your new API's, just run the following:


depends --publish

It will check the config file for any dependencies without any pastebin key, and post it. The config file gets updated with the keys.

The config file should now contain all the dependencies and startup configuration your program needs. Just hand out your depends.lua config file, tell them to download depends.lua, and your program is ready to go.

This is part of an ongoing project of mine (semi-related to RFCC).
BlockSmith #2
Posted 03 June 2014 - 10:10 AM
Very neat idea. I know my programs are starting to get wild. Might have to look into this.
sEi #3
Posted 03 June 2014 - 11:20 AM
Interesting and seems very usefull.

Could you explain the use of…
local apiKey = "0ec2eb25b6166c0c27a394ae118ad829"
… and why it is there?

Is it not 'dangerous' to give away your pastebin apiKey?

/sEi
dragonlord #4
Posted 03 June 2014 - 11:32 AM
Interesting and seems very usefull.

Could you explain the use of…
local apiKey = "0ec2eb25b6166c0c27a394ae118ad829"
… and why it is there?

Is it not 'dangerous' to give away your pastebin apiKey?

/sEi

That's the same API key used in the stock pastebin program.

In other words, we're all sharing the same key anyway.
skwerlman #5
Posted 03 June 2014 - 04:20 PM
Interesting and seems very usefull.

Could you explain the use of…
local apiKey = "0ec2eb25b6166c0c27a394ae118ad829"
… and why it is there?

Is it not 'dangerous' to give away your pastebin apiKey?

/sEi

That's the same API key used in the stock pastebin program.

In other words, we're all sharing the same key anyway.
Could you add a way to specify a custom API key via the command line? It'd be nice to be able to post as myself…
dragonlord #6
Posted 03 June 2014 - 05:53 PM
Interesting and seems very usefull.

Could you explain the use of…
local apiKey = "0ec2eb25b6166c0c27a394ae118ad829"
… and why it is there?

Is it not 'dangerous' to give away your pastebin apiKey?

/sEi

That's the same API key used in the stock pastebin program.

In other words, we're all sharing the same key anyway.
Could you add a way to specify a custom API key via the command line? It'd be nice to be able to post as myself…

I'll add one, as I need it myself too.
skwerlman #7
Posted 03 June 2014 - 07:22 PM
–snip–

I'll add one, as I need it myself too.