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

Packaging API

Started by Exerro, 31 May 2014 - 11:26 PM
Exerro #1
Posted 01 June 2014 - 01:26 AM
This API adds in functions for creating and loading packages.
A package is a file containing other files that can depend on other packages and run internal code.


Features:
  • Single file packages with multiple files inside.
  • Ability to run internal files from the package.
  • Package dependencies (load other packages automatically no matter where they are in the filesystem and use them in the package).
​Code example:

pack = package.create( "MyPackage" ) -- create a package called MyPackage
pack:addFile( "test", "test.lua" ) -- add a file from the filesystem
pack:addFile( "test2", "print( \"hey\" )" ) -- add a file with custom content
pack:addDependency( "MyOtherPackage" ) -- make it load the other package
pack:addFile( "main", [[
  require "MyOtherPackage.main" -- load files from other packages
  require "test2" -- load internal files
  print( "Done!" ) -- normal Lua
  local content = package.read( "test" ) -- access the self package object
  print( content )
]] )
pack:saveExecutable( "run_me", "main" ) -- create an executable package that will run "main" when it is called
pack:save( "path" ) -- save it as a normal package, for example an extension to something that won't run on it's own


Why would I use this?
Ever created a big project? Ever wanted to split it from 1 big file into a folder of smaller files? Ever wanted to get rid of the hassle of making an installer to download all the files neccessary for your project?
This handles all of that for you. You can easily pack an entire folder of files into a single file, and make minimal modifications to your code for it to work.
In addition, this is great if you want to add modularity to a project. For example, if your project uses a GUI API you made, normally you would have to include that API with the project. But what if you have another project that uses that same API. The user would have the API installed twice, taking up un-necessary space. With this, you can export the GUI API as a package, add it as a dependency in both projects, and voila! Both projects will work fine, and it will only be installed once. You can then create unlimited numbers of packages that all use this GUI API and not even have to worry about making sure it is in the project folder.

Finally, you don't need the API to use packages. Once you have created a package, it is completely independent.

Documentation
Spoiler
pack = package.create( string name )
— Create a new package object
pack:addFile( string filename, string content / string path )
— Add a file to the package
pack:addDependency( string name )
— Add a dependency to the package
pack:save( string path )
— Save the package
pack:saveExecutable( string path, string mainFile )
— Save the package in a way that it will run mainFile when it is run
pack = package.load( path )
— Get the data from a package (not recommended to use)
path = package.find( name )
— Find a package called [name] anywhere in the filesystem (returns nil if it can't find the package)



Download:
pastebin get ngTdqDMS package
Edited on 01 June 2014 - 02:26 PM
SquidDev #2
Posted 01 June 2014 - 03:59 PM
This feels rather familiar?

Sorry, I do really like this different way of approaching the same problem. I do really like:
The user would have the API installed twice, taking up un-necessary space. With this, you can export the GUI API as a package, add it as a dependency in both projects, and voila! Both projects will work fine, and it will only be installed once.

PPI would include the same file in multiple projects, so this is a much nicer way. Is there a way to minimise the 'commonPackageData' code? Having 100+ lines at the top of every program does not seem that appealing.

This is an interesting project though, I will watch with interest.

Please though, make the font smaller..
Edited on 01 June 2014 - 02:01 PM
Exerro #3
Posted 01 June 2014 - 04:31 PM
This feels rather familiar?

Sorry, I do really like this different way of approaching the same problem. I do really like:
The user would have the API installed twice, taking up un-necessary space. With this, you can export the GUI API as a package, add it as a dependency in both projects, and voila! Both projects will work fine, and it will only be installed once.

PPI would include the same file in multiple projects, so this is a much nicer way. Is there a way to minimise the 'commonPackageData' code? Having 100+ lines at the top of every program does not seem that appealing.

This is an interesting project though, I will watch with interest.

Please though, make the font smaller..
Haha, honestly I made this as soon as I saw that program :P/>. As for the commonPackageData string, there probably is a way to make it smaller, although when you create a package it is only really intended for distribution, not editing. I might give it a go (condensing it) but the user isn't supposed to see it so it's not high on my priority list.

On a side note: I made a package upload/download/create program here (pastebin get hPdKfKkV packmanager).
It has a small GUI that lets you upload packages, download them, and create them from folders.
When creating a package, if you toggle the "executable?" button to true, the output file will try to run a "main" or "main.lua" file when it is run, and will crash without it.
Also, please don't judge the website too much right now, it is very basic, prone to attacks, and doesn't really do much. I have never really got into websites and this was my first attempt at PHP and HTML.

Edit:
When creating a package, you cannot add dependencies from the GUI. If you need to add in dependencies and want to use the GUI, create a package, then open the file and change "local dependencies = {}" to a table containing the package names of the packages you need.

Also, I changed the font size :)/>

Edit2:
I will be putting some packages I make up on that website soon. For example, a class API, function overloading API, and config API all in one awesome package! (Edit3: uploaded as NovaCore)
Edited on 01 June 2014 - 03:04 PM