[latex3-commits] [git/LaTeX3-latex3-l3build] master: Refactor aux (88f92ae)

Joseph Wright joseph.wright at morningstar2.co.uk
Tue Jan 26 12:56:37 CET 2021


Repository : https://github.com/latex3/l3build
On branch  : master
Link       : https://github.com/latex3/l3build/commit/88f92ae7a9ffb8d96847b5a1fa1c44caf860ef0c

>---------------------------------------------------------------

commit 88f92ae7a9ffb8d96847b5a1fa1c44caf860ef0c
Author: LAURENS Jérôme <jerome.laurens at u-bourgogne.fr>
Date:   Sun Jan 24 20:34:40 2021 +0100

    Refactor aux
    
    Minimize reference to global variables (turn epoch to an argument)
    Some names are "more explicit"
    snake_case cosmetic change.
    Inline documentation added.


>---------------------------------------------------------------

88f92ae7a9ffb8d96847b5a1fa1c44caf860ef0c
 l3build-aux.lua         | 126 +++++++++++++++++++++++++++++++-----------------
 l3build-check.lua       |   4 +-
 l3build-stdmain.lua     |   2 +-
 l3build-typesetting.lua |   6 +--
 l3build-unpack.lua      |   2 +-
 l3build.lua             |   2 +-
 6 files changed, 90 insertions(+), 52 deletions(-)

diff --git a/l3build-aux.lua b/l3build-aux.lua
index 8fa4b0c..2b8a67b 100644
--- a/l3build-aux.lua
+++ b/l3build-aux.lua
@@ -22,6 +22,8 @@ for those people who are interested.
 
 --]]
 
+-- local safety guards and shortcuts
+
 local match = string.match
 
 local pairs = pairs
@@ -29,25 +31,41 @@ local print = print
 
 local lookup = kpse.lookup
 
+local os_time = os_time
 --
 -- Auxiliary functions which are used by more than one main function
 --
 
-function normalise_epoch()
+---Convert the given `epoch` to a number.
+--- at param epoch string
+--- at return number
+--- at see l3build.lua
+--- at usage private?
+function normalise_epoch(epoch)
+  assert(epoch, 'normalize_epoch argument must not be nil')
   -- If given as an ISO date, turn into an epoch number
   local y, m, d = match(epoch, "^(%d%d%d%d)-(%d%d)-(%d%d)$")
   if y then
-    epoch =
-      os_time({year = y, month = m, day = d, hour = 0, sec = 0, isdst = nil}) -
-      os_time({year = 1970, month = 1, day = 1, hour = 0, sec = 0, isdst = nil})
+    return os_time({
+        year = y, month = m, day   = d,
+        hour = 0, sec = 0, isdst = nil
+      }) - os_time({
+        year = 1970, month = 1, day = 1,
+        hour = 0, sec = 0, isdst = nil
+      })
   elseif match(epoch, "^%d+$") then
-    epoch = tonumber(epoch)
+    return tonumber(epoch)
   else
-    epoch = 0
+    return 0
   end
 end
 
-function setepoch()
+---CLI command to set the epoch, will be run while checking or typesetting
+--- at param epoch string
+--- at return string
+--- at see check, typesetting
+--- at usage private?
+function set_epoch_cmd(epoch)
   return
     os_setenv .. " SOURCE_DATE_EPOCH=" .. epoch
       .. os_concat ..
@@ -57,69 +75,89 @@ function setepoch()
       .. os_concat
 end
 
-local function getscriptname()
+---Returns the script name depending on the calling sequence.
+---`l3build ...` -> full path of `l3build.lua` in the TDS
+---When called via `texlua l3build.lua ...`, `l3build.lua` is resolved to either 
+---`./l3build.lua` or the full path of `l3build.lua` in the TDS.
+---`texlua l3build.lua` -> `/Library/TeX/texbin/l3build.lua` or `./l3build.lua`
+--- at return string
+local function get_script_name()
   if match(arg[0], "l3build$") or match(arg[0], "l3build%.lua$") then
     return lookup("l3build.lua")
   else
-    return arg[0]
+    return arg[0] -- Why no lookup here? 
   end
 end
 
--- Do some subtarget for all modules in a bundle
-function call(dirs, target, opts)
-  -- Turn the option table into a string
-  local opts = opts or options
-  local s = ""
+-- Performs the task named target given modules in a bundle.
+---A module is the path of a directory relative to the main one.
+---Uses `run` to launch a command.
+--- at param modules table List of modules.
+--- at param target string
+--- at param opts table
+--- at return number 0 on proper termination, a non 0 error code otherwise.
+--- at see many places, including latex2e/build.lua
+--- at usage Public
+function call(modules, target, opts)
+  -- Turn the option table into a CLI option string
+  opts = opts or options
+  local cli_opts = ""
   for k,v in pairs(opts) do
-    if k ~= "names" and k ~= "target" then -- Special cases
-      local t = option_list[k] or { }
-      local arg = ""
+    if k ~= "names" and k ~= "target" then -- Special cases, to be removed!!!
+      local t = option_list[k] or {}
+      local value = ""
       if t["type"] == "string" then
-        arg = arg .. "=" .. v
-      end
-      if t["type"] == "table" then
+        value = value .. "=" .. v
+      elseif t["type"] == "table" then
         for _,a in pairs(v) do
-          if arg == "" then
-            arg = "=" .. a -- Add the initial "=" here
+          if value == "" then
+            value = "=" .. a -- Add the initial "=" here
           else
-            arg = arg .. "," .. a
+            value = value .. "," .. a
           end
         end
       end
-      s = s .. " --" .. k .. arg
+      cli_opts = cli_opts .. " --" .. k .. value
     end
   end
-  if opts["names"] then
-    for _,v in pairs(opts["names"]) do
-      s = s .. " " .. v
+  if opts.names then
+    for _, name in pairs(opts.names) do
+      cli_opts = cli_opts .. " " .. name
     end
   end
-  local scriptname = getscriptname()
-  for _,i in ipairs(dirs) do
-    local text = " for module " .. i
-    if i == "." and opts["config"] then
+  local script_name = get_script_name()
+  for _, module in ipairs(modules) do
+    local text
+    if module == "." and opts["config"] and #opts["config"] then
       text = " with configuration " .. opts["config"][1]
+    else
+      text = " for module " .. module
     end
     print("Running l3build with target \"" .. target .. "\"" .. text )
-    local errorlevel = run(
-      i,
-      "texlua " .. scriptname .. " " .. target .. s
+    local error_level = run(
+      module,
+      "texlua " .. script_name .. " " .. target .. cli_opts
     )
-    if errorlevel ~= 0 then
-      return errorlevel
+    if error_level ~= 0 then
+      return error_level
     end
   end
   return 0
 end
 
--- Unpack files needed to support testing/typesetting/unpacking
-function depinstall(deps)
-  local errorlevel
-  for _,i in ipairs(deps) do
-    print("Installing dependency: " .. i)
-    errorlevel = run(i, "texlua " .. getscriptname() .. " unpack -q")
-    if errorlevel ~= 0 then
-      return errorlevel
+---Unpack the given dependencies.
+---A dependency is the path of a directory relative to the main one.
+--- at param deps table regular array of dependencies.
+--- at return number 0 on proper termination, a non 0 error code otherwise.
+--- at see stdmain, check, unpack, typesetting
+--- at usage Private?
+function dep_install(deps)
+  local error_level
+  for _, dep in ipairs(deps) do
+    print("Installing dependency: " .. dep)
+    error_level = run(dep, "texlua " .. get_script_name() .. " unpack -q")
+    if error_level ~= 0 then
+      return error_level
     end
   end
   return 0
diff --git a/l3build-check.lua b/l3build-check.lua
index 34da922..e9c8141 100644
--- a/l3build-check.lua
+++ b/l3build-check.lua
@@ -59,7 +59,7 @@ function checkinit()
     cleandir(testdir)
     cleandir(resultdir)
   end
-  depinstall(checkdeps)
+  dep_install(checkdeps)
   -- Copy dependencies to the test directory itself: this makes the paths
   -- a lot easier to manage, and is important for dealing with the log and
   -- with file input/output tests
@@ -764,7 +764,7 @@ function runtest(name, engine, hide, ext, pdfmode, breakout)
       -- Allow for local texmf files
       os_setenv .. " TEXMFCNF=." .. os_pathsep
         .. os_concat ..
-      (forcecheckepoch and setepoch() or "") ..
+      (forcecheckepoch and set_epoch_cmd(epoch) or "") ..
       -- Ensure lines are of a known length
       os_setenv .. " max_print_line=" .. maxprintline
         .. os_concat ..
diff --git a/l3build-stdmain.lua b/l3build-stdmain.lua
index 10303b7..c12c29d 100644
--- a/l3build-stdmain.lua
+++ b/l3build-stdmain.lua
@@ -65,7 +65,7 @@ target_list =
     bundleunpack =
       {
         func = bundleunpack,
-        pre  = function() return(depinstall(unpackdeps)) end
+        pre  = function() return(dep_install(unpackdeps)) end
       },
     -- Public targets
     check =
diff --git a/l3build-typesetting.lua b/l3build-typesetting.lua
index 540ac3f..b58a87b 100644
--- a/l3build-typesetting.lua
+++ b/l3build-typesetting.lua
@@ -38,7 +38,7 @@ local os_type = os.type
 function dvitopdf(name, dir, engine, hide)
   run(
     dir,
-    (forcecheckepoch and setepoch() or "") ..
+    (forcecheckepoch and set_epoch_cmd(epoch) or "") ..
     "dvips " .. name .. dviext
       .. (hide and (" > " .. os_null) or "")
       .. os_concat ..
@@ -68,7 +68,7 @@ function runcmd(cmd,dir,vars)
   for _,var in pairs(vars) do
     env = env .. os_concat .. os_setenv .. " " .. var .. "=" .. envpaths
   end
-  return run(dir,(forcedocepoch and setepoch() or "") .. env .. os_concat . cmd)
+  return run(dir,(forcedocepoch and set_epoch_cmd(epoch) or "") .. env .. os_concat .. cmd)
 end
 
 function biber(name,dir)
@@ -188,7 +188,7 @@ local function docinit()
   for _,file in pairs(typesetsuppfiles) do
     cp(file, supportdir, typesetdir)
   end
-  depinstall(typesetdeps)
+  dep_install(typesetdeps)
   unpack({sourcefiles, typesetsourcefiles}, {sourcefiledir, docfiledir})
   -- Main loop for doc creation
   local errorlevel = typeset_demo_tasks()
diff --git a/l3build-unpack.lua b/l3build-unpack.lua
index 412b5e8..4f16f75 100644
--- a/l3build-unpack.lua
+++ b/l3build-unpack.lua
@@ -27,7 +27,7 @@ local execute          = os.execute
 -- Unpack the package files using an 'isolated' system: this requires
 -- a copy of the 'basic' DocStrip program, which is used then removed
 function unpack(sources, sourcedirs)
-  local errorlevel = depinstall(unpackdeps)
+  local errorlevel = dep_install(unpackdeps)
   if errorlevel ~= 0 then
     return errorlevel
   end
diff --git a/l3build.lua b/l3build.lua
index e7acd47..7d5d56a 100755
--- a/l3build.lua
+++ b/l3build.lua
@@ -120,7 +120,7 @@ if options["epoch"] then
   forcecheckepoch = true
   forcedocepoch   = true
 end
-normalise_epoch()
+epoch = normalise_epoch(epoch)
 
 -- Sanity check
 check_engines()





More information about the latex3-commits mailing list.