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:
You can get depends.lua by running the following on any computer:
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
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:
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:
You could still keep the calls to os.loadAPI() if you so wish.
Run your program by executing the following:
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:
Will run the last startup program.
Publish your API's
Once you're ready to publish your new API's, just run the following:
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).
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.
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).