Skip to content

Instantly share code, notes, and snippets.

@ReFreezed
Last active September 24, 2019 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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"
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
@ReFreezed
Copy link
Author

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