Skip to content

Instantly share code, notes, and snippets.

Last active September 24, 2019 23:24
Show Gist options
  • Save ReFreezed/be97dce6b67496b0f0c5275bf2a96d51 to your computer and use it in GitHub Desktop.
Save ReFreezed/be97dce6b67496b0f0c5275bf2a96d51 to your computer and use it in GitHub Desktop.
Example usage of LuaPreprocess+LÖVE.
I have a separate installation of Lua that I use to build the game (though you could use LÖVE too).
I run build.lua which processes all Lua files in the project and starts the game.
> lua buildSystem/build.lua
This example uses 3 folders:
- buildSystem/ for things used to build the game.
- src/ for unprocessed files.
- srcgen/ for processed/generated files.
I use a different extension for unprocessed files (.lua2p) so I don't get confused while working on the game.
-- buildSystem/build.lua
local pp = require("buildSystem.preprocess")
-- Settings for the build system.
local OUTPUT_ZIP = false
-- Add values that all files use. The values in pp.metaEnvironment
-- will be globals in the files we're about to process.
pp.metaEnvironment.DEVELOPER_MODE = true
pp.metaEnvironment.DISABLE_SOUNDS = false
pp.metaEnvironment.GAME_VERSION_NAME = "v1.0.8 (Tomato)"
-- Get paths to all Lua files in the src/ folder.
local paths = getLuaFilesInDirectory("src")
for _, pathIn in ipairs(paths) do
local pathOut = pathIn
-- Output the files into the srcgen/ folder.
pathOut = pathOut:gsub("^src/", "srcgen/")
-- The unprocessed files have a different extension (.lua2p)
-- so I don't get confused.
pathOut = pathOut:gsub("%.lua2p$", ".lua")
-- Output main.lua into the top folder instead of srcgen/.
if pathOut == "srcgen/main.lua" then
pathOut = "main.lua"
local info, err = pp.processFile{ pathIn=pathIn, pathOut=pathOut }
-- Just exit if there was an error while processing the file.
-- An error message should already have been printed to the console.
if not info then os.exit(1) end
if OUTPUT_ZIP then
-- Output a .zip file.
-- Execute LÖVE in the top/current folder to start the game.
os.execute("love.exe .")
-- src/main.lua2p
-- require() should look in the top folder or the srcgen/ folder.
package.path = "?.lua;srcgen/?.lua"
function love.load()
-- src/menus.lua2p
function _G.showMainMenu()
!if not DEVELOPER_MODE then
Copy link

Where is this function getLuaFilesInDirectory found?

Copy link

ReFreezed commented May 22, 2019

You'll have to define it yourself. Since Lua has no way of retrieving files in a directory by default you'll have to use something like LuaFileSystem.

Copy link

I suggest you also provide one?
here's a working one on my case

local getLuaFilesInDirectory = function(str)
	local f = io.popen("ls " .. str)
	if f then
		local files = {}
		for line in f:lines() do
			local path = str .. "/" .. line
			table.insert(files, path)
		return files
	return nil

Copy link

I left out implementation details involving the operative system on purpose because things work differently everywhere. For Windows you'd use something like io.popen("dir /b "..str) instead of io.popen("ls "..str).

Copy link

Oh yeah, got it! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment