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

[API] Pastebin version control

Started by Orginalet, 01 March 2014 - 09:24 PM
Orginalet #1
Posted 01 March 2014 - 10:24 PM
I've made a little silly API for those of us who can't be bothered to use GitHub or similar for small Lua scripts but still want to keep them up to date with the latest version of the paste at Pastebin. So I came up with the "PasteVersion" API that you can load and use for some nifty things like autoupdating files, checking version numbers in a paste, etc.

How do I use it in my project(s)?
You need to denote the tag "@version: <version numbers>" somewhere in a comment in your file. I highly recommend to denote it early in the file since the file is read line-by-line until it stumbles upon the tag or the file ends. For example:


-- @version: 1.0

local function fancyFunc()
	return math.random(1,6)
end

It also works with multiline comments, like this for example:

--[[
"PasteVersion", keep your programs up to date with Pastebin.

@author: Orginalet for http://mustachebrigade.enjin.com
@license: GNU GPLv3, https://gnu.org/licenses/gpl.html
@version: 1.0
]]

local function fancyFunc()
	return math.random(1,6)
end

Now that you've tagged your file you can call the API from another file to make use of the functions.

Functions
getPasteVersion(paste key)
This function downloads the file from Pastebin using the "paste key" parameter, it then reads through it and finally returns the version number if one was found. Otherwise it returns false.

versionCheck(paste key, filename)
This function is meant to be used to check the version of a file then compare to the version of a Pastebin paste. If the version numbers match it returns true, otherwise it returns false.

update(filename, paste key)
This function can update a file with the code from a Pastebin paste. It removes all code in the file and replaces it with the code from the Pastebin paste. It returns true if everything went well, otherwise it returns false.


Tips
- You can enable "verbose mode" inside the API-file to get all error/success codes. Highly recommended to find bugs and to see that the file works as it should.

- This API could easily be used to create a script that automatically updates a file after every computer reboot if the version number doesn't match the one on Pastebin, this was my initial thought with the API and is how I use it myself.

- The API unfortunately doesn't check if the Pastebin version is a newer version, it simply checks if the version number is equal to that in the file.

- Be very strict when using the @version-tag. It requires the you have a space between the "@version:" and the actual version number. The amount of spaces doesn't matter though.

- You can use this API with the common "major.minor.patch" system of noting versions. It doesn't support any other format, but since the code is completely free to modify you can add another pattern that the functions check for. :)/>


Download
Sourcecode @ Pastebin
SherlockHolmes #2
Posted 03 March 2014 - 03:54 AM
Very cool! I use github primarily for my code, but pastebin sounds like a lightweight solution for small projects. I'll have a go with your scripts!
LDShadowLord #3
Posted 03 March 2014 - 05:39 PM
Will it only work with Pastebin or can I configure it to work with Pastebay or something similar?
Orginalet #4
Posted 06 March 2014 - 11:46 AM
Will it only work with Pastebin or can I configure it to work with Pastebay or something similar?

The Pastebin url is "hardcoded" in the "getPaste"-function so it doesn't support any other paste-site out of the box. If you modify the URL on line 45 it might work. I haven't tried it but as long as you can get a raw text output similar to what Pastebin provides it could be done.