Skip to content

Commit

Permalink
Reworked and cleaned up some scripting code and glue.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Jan 20, 2008
1 parent 0882e54 commit 3b36520
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 147 deletions.
13 changes: 0 additions & 13 deletions lua_glue.c
Expand Up @@ -1623,23 +1623,10 @@ boolean MojoLua_initLua(void)
free(binarypath);
free(homedir);

// Set up localization tables, if possible.
MojoLua_runFile("localization");
MojoLua_runFile("app_localization");

// Transfer control to Lua to setup some APIs and state...
if (!MojoLua_runFile("mojosetup_init"))
return false;

// ...and run the installer-specific config file.
if (!MojoLua_runFile("config"))
return false;

// We don't need the "Setup" namespace anymore. Make it
// eligible for garbage collection.
lua_pushnil(luaState);
lua_setglobal(luaState, "Setup");

MojoLua_collectGarbage(); // get rid of old init crap we don't need.

return true;
Expand Down
250 changes: 132 additions & 118 deletions scripts/mojosetup_init.lua
Expand Up @@ -21,25 +21,6 @@

local _ = MojoSetup.translate

MojoSetup.loginfo("MojoSetup Lua initialization at " .. MojoSetup.date())
MojoSetup.loginfo("buildver: " .. MojoSetup.info.buildver)
MojoSetup.loginfo("locale: " .. MojoSetup.info.locale)
MojoSetup.loginfo("platform: " .. MojoSetup.info.platform)
MojoSetup.loginfo("arch: " .. MojoSetup.info.arch)
MojoSetup.loginfo("ostype: " .. MojoSetup.info.ostype)
MojoSetup.loginfo("osversion: " .. MojoSetup.info.osversion)
MojoSetup.loginfo("ui: " .. MojoSetup.info.ui)
MojoSetup.loginfo("loglevel: " .. MojoSetup.info.loglevel)

MojoSetup.loginfo("command line:")
for i,v in ipairs(MojoSetup.info.argv) do
MojoSetup.loginfo(" " .. i .. ": " .. v)
end

--MojoSetup.loginfo(MojoSetup.info.license)
--MojoSetup.loginfo(MojoSetup.info.lualicense)


-- Returns three elements: protocol, host, path
function MojoSetup.spliturl(url)
return string.match(url, "^(.+://)(.-)/(.*)")
Expand Down Expand Up @@ -88,106 +69,8 @@ function MojoSetup.dumptable(tabname, tab, depth)
end
end

-- This table gets filled by the config file. Just create an empty one for now.
MojoSetup.installs = {}


local function sanity_check_localization_entry(str, translations)
local maxval = -1;

for val in string.gmatch(str, "%%.") do
val = string.sub(val, 2)
if string.match(val, "^[^%%0-9]$") ~= nil then
MojoSetup.fatal("BUG: localization key ['" .. str .. "'] has invalid escape sequence.")
end
if val ~= "%" then
local num = tonumber(val)
if num > maxval then
maxval = num
end
end
end

for k,v in pairs(translations) do
for val in string.gmatch(v, "%%.") do
val = string.sub(val, 2)
if string.match(val, "^[^%%0-9]$") ~= nil then
MojoSetup.fatal("BUG: '" .. k .. "' localization ['" .. v .. "'] has invalid escape sequence.")
end
if val ~= "%" then
if tonumber(val) > maxval then
MojoSetup.fatal("BUG: '" .. k .. "' localization ['" .. v .. "'] has escape sequence > max for translation.")
end
end
end
end
end


-- Build the translations table from the localizations table supplied in
-- localizations.lua...
if type(MojoSetup.localization) ~= "table" then
MojoSetup.localization = nil
end

-- Merge the applocalization table into localization.
if type(MojoSetup.applocalization) == "table" then
for k,v in pairs(MojoSetup.applocalization) do
if MojoSetup.localization[k] == nil then
MojoSetup.localization[k] = v -- just take the whole table as-is.
else
-- This can add or overwrite entries...
for lang,str in pairs(MojoSetup.applocalization) do
MojoSetup.localization[k][lang] = str
end
end
end
end
MojoSetup.applocalization = nil -- done with this; garbage collect it.

-- Map some legacy language identifiers into updated equivalents.
local lang_remap =
{
no = "nb", -- "Norwegian" split into "Bokmal" (nb) and "Nynorsk" (nn)
}

if MojoSetup.localization ~= nil then
local at_least_one = false
local locale = MojoSetup.info.locale
local lang = string.gsub(locale, "_%w+", "", 1) -- make "en_US" into "en"

if lang_remap[lang] ~= nil then
lang = lang_remap[lang]
end

MojoSetup.translations = {}
for k,v in pairs(MojoSetup.localization) do
if MojoSetup.translations[k] ~= nil then
MojoSetup.fatal("BUG: Duplicate localization key ['" .. k .. "']")
end
if type(v) == "table" then
sanity_check_localization_entry(k, v)
if v[locale] ~= nil then
MojoSetup.translations[k] = v[locale]
at_least_one = true
elseif v[lang] ~= nil then
MojoSetup.translations[k] = v[lang]
at_least_one = true
end
end
end

-- Delete the table if there's no actual useful translations for this run.
if (not at_least_one) then
MojoSetup.translations = nil
end

-- This is eligible for garbage collection now. We're done with it.
MojoSetup.localization = nil
end


-- Our namespace for this API...this is filled in with the rest of this file.
-- Our namespace for config API...
Setup = {}

local function schema_assert(test, fnname, elem, errstr)
Expand Down Expand Up @@ -377,6 +260,10 @@ function Setup.Package(tab)
{ "superuser", false, mustBeBool },
})

if MojoSetup.installs == nil then
MojoSetup.installs = {}
end

tab._type_ = nil
tab = reform_schema_table(tab)
table.insert(MojoSetup.installs, tab)
Expand Down Expand Up @@ -512,5 +399,132 @@ function Setup.OptionGroup(tab)
})
end


local function prepare_localization()
-- Map some legacy language identifiers into updated equivalents.
local lang_remap =
{
no = "nb", -- "Norwegian" split into "Bokmal" (nb) and "Nynorsk" (nn)
}

local function sanity_check_localization_entry(str, translations)
local maxval = -1;

for val in string.gmatch(str, "%%.") do
val = string.sub(val, 2)
if string.match(val, "^[^%%0-9]$") ~= nil then
MojoSetup.fatal("BUG: localization key ['" .. str .. "'] has invalid escape sequence.")
end
if val ~= "%" then
local num = tonumber(val)
if num > maxval then
maxval = num
end
end
end

for k,v in pairs(translations) do
for val in string.gmatch(v, "%%.") do
val = string.sub(val, 2)
if string.match(val, "^[^%%0-9]$") ~= nil then
MojoSetup.fatal("BUG: '" .. k .. "' localization ['" .. v .. "'] has invalid escape sequence.")
end
if val ~= "%" then
if tonumber(val) > maxval then
MojoSetup.fatal("BUG: '" .. k .. "' localization ['" .. v .. "'] has escape sequence > max for translation.")
end
end
end
end
end

-- Build the translations table from the localizations table supplied in
-- localizations.lua...
if type(MojoSetup.localization) ~= "table" then
MojoSetup.localization = nil
end

-- Merge the applocalization table into localization.
if type(MojoSetup.applocalization) == "table" then
if MojoSetup.localization == nil then
MojoSetup.localization = {}
end
for k,v in pairs(MojoSetup.applocalization) do
if MojoSetup.localization[k] == nil then
MojoSetup.localization[k] = v -- just take the whole table as-is.
else
-- This can add or overwrite entries...
for lang,str in pairs(MojoSetup.applocalization) do
MojoSetup.localization[k][lang] = str
end
end
end
end
MojoSetup.applocalization = nil -- done with this; garbage collect it.

if MojoSetup.localization == nil then
local at_least_one = false
local locale = MojoSetup.info.locale
local lang = string.gsub(locale, "_%w+", "", 1) -- make "en_US" into "en"

if lang_remap[lang] ~= nil then
lang = lang_remap[lang]
end

MojoSetup.translations = {}
for k,v in pairs(MojoSetup.localization) do
if MojoSetup.translations[k] ~= nil then
MojoSetup.fatal("BUG: Duplicate localization key ['" .. k .. "']")
end
if type(v) == "table" then
sanity_check_localization_entry(k, v)
if v[locale] ~= nil then
MojoSetup.translations[k] = v[locale]
at_least_one = true
elseif v[lang] ~= nil then
MojoSetup.translations[k] = v[lang]
at_least_one = true
end
end
end

-- Delete the table if there's no actual useful translations for this run.
if (not at_least_one) then
MojoSetup.translations = nil
end

-- This is eligible for garbage collection now. We're done with it.
MojoSetup.localization = nil
end
end


-- Mainline...

MojoSetup.loginfo("MojoSetup Lua initialization at " .. MojoSetup.date())
MojoSetup.loginfo("buildver: " .. MojoSetup.info.buildver)
MojoSetup.loginfo("locale: " .. MojoSetup.info.locale)
MojoSetup.loginfo("platform: " .. MojoSetup.info.platform)
MojoSetup.loginfo("arch: " .. MojoSetup.info.arch)
MojoSetup.loginfo("ostype: " .. MojoSetup.info.ostype)
MojoSetup.loginfo("osversion: " .. MojoSetup.info.osversion)
MojoSetup.loginfo("ui: " .. MojoSetup.info.ui)
MojoSetup.loginfo("loglevel: " .. MojoSetup.info.loglevel)

MojoSetup.loginfo("command line:")
for i,v in ipairs(MojoSetup.info.argv) do
MojoSetup.loginfo(" " .. i .. ": " .. v)
end

--MojoSetup.loginfo(MojoSetup.info.license)
--MojoSetup.loginfo(MojoSetup.info.lualicense)

-- These scripts are optional, but hopefully exist...
MojoSetup.runFile("localization")
MojoSetup.runFile("app_localization")
prepare_localization()

-- okay, we're initialized!

-- end of mojosetup_init.lua ...

49 changes: 33 additions & 16 deletions scripts/mojosetup_mainline.lua
Expand Up @@ -1357,26 +1357,43 @@ local function do_install(install)
MojoSetup.totaldownload = 0
end

local function installer()
-- This builds the MojoSetup.installs table.
MojoSetup.runFile("config")

-- We don't need the "Setup" namespace anymore. Make it eligible
-- for garbage collection.
Setup = nil

-- This dumps the table built from the user's config script using logdebug,
-- so it only spits out crap if debug-level logging is enabled.
MojoSetup.dumptable("MojoSetup.installs", MojoSetup.installs)

local saw_an_installer = false
for installkey,install in pairs(MojoSetup.installs) do
if not install.disabled then
saw_an_installer = true
do_install(install)
MojoSetup.collectgarbage() -- nuke all the tables we threw around...
end
end


-- Mainline.


-- This dumps the table built from the user's config script using logdebug,
-- so it only spits out crap if debug-level logging is enabled.
MojoSetup.dumptable("MojoSetup.installs", MojoSetup.installs)

local saw_an_installer = false
for installkey,install in pairs(MojoSetup.installs) do
if not install.disabled then
saw_an_installer = true
do_install(install)
MojoSetup.collectgarbage() -- nuke all the tables we threw around...
if not saw_an_installer then
MojoSetup.fatal(_("Nothing to do!"))
end
end

if not saw_an_installer then
MojoSetup.fatal(_("Nothing to do!"))

-- Mainline.

if MojoSetup.cmdline("manifest") then
Setup = nil -- don't need this after all.
MojoSetup.fatal("Not implemented yet.")
elseif MojoSetup.cmdline("uninstall") then
Setup = nil -- don't need this after all.
MojoSetup.fatal("Not implemented yet.")
else
installer()
end

-- end of mojosetup_mainline.lua ...
Expand Down

0 comments on commit 3b36520

Please sign in to comment.