Go Back   Cockos Incorporated Forums > REAPER Forums > JSFX and ReaScript Discussion

Reply
 
Thread Tools Display Modes
Old 12-21-2016, 08:32 AM   #1
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: The vast, frozen wasteland of western Canadia.
Posts: 4,212
Default A compiler that writes external Lua libraries into your script

Isn't it a pain having to distribute multiple files with your scripts? Yeah, I thought so too. But then I realized that Lua can read/write to files, so why not make use of it?



https://github.com/ReaTeam/ReaScript...20Compiler.lua

Nothing super fancy; it lets you load a .lua script, looks through it for any external scripts you're calling (i.e. with loadfile or require), and copy/pastes those libraries into your script. Boom! No more zip files, special folders for your script, etc.

To use:

1. Install and run this script in Reaper just like you would any other.
2. Your base script has to have a few specific lines in it:
Code:
    --!!REQUIRES START

    --!!REQUIRE "my other cool script.lua"

    your existing require/loadfile code here

    --!!REQUIRES END
The first and last lines are optional, and anything between them will be overwritten. This way your existing code for external libraries can be left in while you're developing the script, but will be removed automatically when the script is compiled.

The second line is what my script is really looking for - it picks up that filename and does its thing. Multiple filenames are fine, relative and absolute paths are both fine, starting your path with a "" is NOT fine, and I think it should work on Macs but I'm not positive.

Script Compiler will also accept filenames passed to a function (you'll have to define it yourself) called req(); I hate dealing with requires, so I personally find this easier:

Code:
--!!REQUIRES START
local function req(file)
	
	if missing_lib then return function () end end
	
	local ret, err = loadfile(script_path .. file)
	if not ret then
		reaper.ShowMessageBox("Couldn't load "..file.."\n\nError: "..tostring(err), "Library error", 0)
		missing_lib = true		
		return function () end

	else 
		return ret
	end	

end


req("Core.lua")()

--!!REQUIRES END

3. Once a script is loaded, you can choose which of the detected libraries to include. You probably don't need to change this, unless you've got comments in the code somewhere that also say --!!REQUIRE "filename.lua".

4. Choose a name for the output file.

5. Click "Go". Your new, completely self-sufficient script should be waiting for you in the same folder as the original, or wherever you chose to put it.

The only downside is an increase in file size for obvious reason - this script is only 2kb by itself, but the graphics library added roughly 60kb. I mean, it's still only 62kb, and you'd be distributing the library file with your script anyway.

Enjoy.
__________________
Currently working away from home; don't expect timely responses from me.
Default 5.0 Nitpicky Edition / GUI tutorial for Lua / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate / ReaPack

Last edited by Lokasenna; 06-09-2017 at 02:49 PM.
Lokasenna is offline   Reply With Quote
Old 06-09-2017, 11:54 AM   #2
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: The vast, frozen wasteland of western Canadia.
Posts: 4,212
Default

Small update, some bug fixes and an alternate syntax. This makes it compatible with the new release of my GUI library that will be available... shortly after this post is posted, I would imagine.
__________________
Currently working away from home; don't expect timely responses from me.
Default 5.0 Nitpicky Edition / GUI tutorial for Lua / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate / ReaPack
Lokasenna is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 11:37 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2017, vBulletin Solutions Inc.