Skip to content

Instantly share code, notes, and snippets.

@exerro
Created February 20, 2017 18:20
Show Gist options
  • Save exerro/9c3d53f0bd91a6c3756cb4ffd408844a to your computer and use it in GitHub Desktop.
Save exerro/9c3d53f0bd91a6c3756cb4ffd408844a to your computer and use it in GitHub Desktop.
-- create a state to use when compiling the package
local substate = preprocess.create_state( file:match "(.+)/" or "" )
-- names that the package localises (used to add these to the package table)
local localised_names = {}
local localised = {}
-- copy minify status across
substate.microminify = state.microminify
substate.minify.active = state.minify.active
-- copy errors table so contents don't need to be copied back later
substate.errors = state.errors
-- copy environment variables starting with package name prefix across
for k, v in pairs( state.environment ) do
if k:find( "^" .. name:upper() .. "_" ) then
substate.environment[k:sub( #name + 2 )] = v
end
end
-- parse package contents
local sublines = preprocess.process_file( file, substate, true )
or error( "failed to find file '" .. file .. "' on line " .. line .. " of '" .. src .. "'", 0 )
-- copy package environment back into current environment with a prefix
for k, v in pairs( substate.environment ) do
state.environment[name:upper() .. "_" .. k] = v
end
-- copy error data back into current state with a prefix
for k, v in pairs( substate.error_data ) do
state.error_data[name:lower() .. "_" .. k] = v
end
-- calculate stuff from the package locals
for k, v in pairs( substate.localised ) do
localised[#localised + 1] = name .. "." .. k .. " = " .. k
localised_names[#localised_names + 1] = k
end
-- localise the package
state.localised[name] = true
-- copy package content into current build
local len = #sublines + 1
lines[line].content = "do " .. name .. " = {}" .. (#localised_names > 0 and " local " .. table.concat( localised_names, ", " ) or "")
sublines[#sublines + 1] = { content = table.concat( localised, ";" ) .. " end", source = "<preprocessor>", line = 0 }
for i = #lines, line + 1, -1 do
lines[i + len] = lines[i]
end
for i = 1, len do
lines[line + i] = sublines[i]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment