Skip to content

Instantly share code, notes, and snippets.

@ReFreezed
Last active Sep 24, 2019
Embed
What would you like to do?
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"
end
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
end
if OUTPUT_ZIP then
-- Output a .zip file.
os.execute("some_program_that_creates_a_zip_file.exe")
else
-- Execute LÖVE in the top/current folder to start the game.
os.execute("love.exe .")
end
-- src/main.lua2p
-- require() should look in the top folder or the srcgen/ folder.
package.path = "?.lua;srcgen/?.lua"
function love.load()
!if DEVELOPER_MODE then
doDevStuff()
!end
require("menus")
showMainMenu()
end
-- src/menus.lua2p
function _G.showMainMenu()
!if not DEVELOPER_MODE then
showSplashScreen()
!end
end
@flamendless

This comment has been minimized.

Copy link

@flamendless flamendless commented May 21, 2019

Where is this function getLuaFilesInDirectory found?

@ReFreezed

This comment has been minimized.

Copy link
Owner Author

@ReFreezed 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.

@flamendless

This comment has been minimized.

Copy link

@flamendless flamendless commented May 22, 2019

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)
		end
		return files
	end
	return nil
end
@ReFreezed

This comment has been minimized.

Copy link
Owner Author

@ReFreezed ReFreezed commented May 22, 2019

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).

@flamendless

This comment has been minimized.

Copy link

@flamendless flamendless commented May 22, 2019

Oh yeah, got it! Thanks!

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