[latex3-commits] [git/LaTeX3-latex3-l3build] master: Use a table-based approach to targets (fixes #23) (47f450f)

Joseph Wright joseph.wright at morningstar2.co.uk
Sun Jul 29 12:38:43 CEST 2018


Repository : https://github.com/latex3/l3build
On branch  : master
Link       : https://github.com/latex3/l3build/commit/47f450f7e1ad2169904cc10b0a97d22602612a9c

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

commit 47f450f7e1ad2169904cc10b0a97d22602612a9c
Author: Joseph Wright <joseph.wright at morningstar2.co.uk>
Date:   Sun Jul 29 11:38:43 2018 +0100

    Use a table-based approach to targets (fixes #23)
    
    At present, stdmain() is retained to allow a transition. That was
    never documented, so once this code is 'out' we should look to
    drop stdmain() entirely.


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

47f450f7e1ad2169904cc10b0a97d22602612a9c
 CHANGELOG.md        |    2 +
 l3build-stdmain.lua |  167 +++++++++++++++++++++++++--------------------------
 l3build.dtx         |   18 ++++++
 3 files changed, 102 insertions(+), 85 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index f4265ad..50815d1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,8 @@ this project uses date-based 'snapshot' version identifiers.
 
 ### Added 
 - `CHANGELOG.md`
+- `target_list` table to allow control of targets without redefining
+  `main()`
 
 ## [2018-05-10]
 
diff --git a/l3build-stdmain.lua b/l3build-stdmain.lua
index 26dbd5e..e8f7b19 100644
--- a/l3build-stdmain.lua
+++ b/l3build-stdmain.lua
@@ -45,45 +45,95 @@ end
 
 target_list =
   {
+    -- Some hidden targets
+    bundlecheck =
+      {
+        func = check,
+        pre  = function()
+            if names then
+              print("Bundle checks should not list test names")
+              help()
+              exit(1)
+            end
+            return 0
+          end
+      },
+    bundleunpack =
+      {
+        func = bundleunpack,
+        pre  = function() return(depinstall(unpackdeps)) end
+      },
+    -- Public targets
     check =
       {
-        desc = "Run all automated tests"
+        bundle_target = true,
+        desc = "Run all automated tests",
+        func = check,
       },
     clean =
       {
-        desc = "Clean out directory tree"
+        bundle_func = bundleclean,
+        desc = "Clean out directory tree",
+        func = clean
       },
     ctan =
       {
-        desc = "Create CTAN-ready archive"
+        bundle_func = ctan,
+        desc = "Create CTAN-ready archive",
+        func = ctan
       },
     doc =
       {
-        desc = "Typesets all documentation files"
+        desc = "Typesets all documentation files",
+        func = doc
       },
     install =
       {
-        desc = "Installs files into the local textmf tree"
+        desc = "Installs files into the local textmf tree",
+        func = install
       },
     manifest =
       {
-        desc = "Creates a manifest file"
+        desc = "Creates a manifest file",
+        func = manifest
       },
     save =
       {
-        desc = "Saves test validation log"
+        desc = "Saves test validation log",
+        func = save
       },
     tag =
       {
-        desc = "Updates release tags in files"
+        bundle_func = function(names)
+          local modules = modules or listmodules()
+          local errorlevel = call(modules,"tag")
+          -- Deal with any files in the bundle dir itself
+          if errorlevel == 0 then
+            errorlevel = tag(names[1])
+          end
+          return errorlevel
+        end
+        desc = "Updates release tags in files",
+        func = tag,
+        pre  = function(names)
+           if not names or #names ~=1 then
+             print("Tag name required")
+             help()
+             exit(1)
+           end
+           return 0
+         end
       },
     uninstall =
       {
-        desc = "Uninstalls files from the local textmf tree"
+        desc = "Uninstalls files from the local textmf tree",
+        func = uninstall
       },
     unpack=
       {
-        desc = "Unpacks the source files into the build tree"
+        bundle_target = true,
+        desc = "Unpacks the source files into the build tree",
+        func = unpack
       }
   }
 
@@ -91,87 +141,34 @@ target_list =
 -- The overall main function
 --
 
-function stdmain(target, names)
-  local errorlevel
-  -- If the module name is empty, the script is running in a bundle:
-  -- apart from ctan all of the targets are then just mappings
+function stdmain(target,names)
+  -- Deal with unknown targets up-front
+  if not target_list[target] then
+    help()
+    exit(1)
+  end
+  local errorlevel = 0
   if module == "" then
-    -- Detect all of the modules
-    modules = modules or listmodules()
-    if target == "doc" then
-      errorlevel = call(modules, "doc")
-    elseif target == "check" then
-      errorlevel = call(modules, "bundlecheck")
-      if errorlevel ~=0 then
-        print("There were errors: checks halted!\n")
-      end
-    elseif target == "clean" then
-      errorlevel = bundleclean()
-    elseif target == "ctan" then
-      errorlevel = ctan()
-    elseif target == "install" then
-      errorlevel = call(modules, "install")
-    elseif target == "tag" then
-      if options["names"] and #options["names"] == 1 then
-        errorlevel = call(modules,"tag")
-        -- Deal with any files in the bundle dir itself
-        if errorlevel == 0 then
-          errorlevel = tag(options["names"][1])
-        end
-      else
-        print("Tag name required")
-        help()
-        exit(1)
-      end
-    elseif target == "uninstall" then
-      errorlevel = call(modules, "uninstall")
-    elseif target == "unpack" then
-      errorlevel = call(modules, "bundleunpack")
+    if target_list[target].bundle_func then
+      errorlevel = target_list[target].bundle_func(names)
     else
-      help()
+      -- Detect all of the modules
+      modules = modules or listmodules()
+      if target_list[target].bundle_target then
+        target = "bundle" .. target
+      end
+      errorlevel = call(modules,target)
     end
   else
-    if target == "bundleunpack" then -- 'Hidden' as only needed 'higher up'
-      depinstall(unpackdeps)
-      errorlevel = bundleunpack()
-    elseif target == "bundlecheck" then
-      errorlevel = check()
-    elseif target == "bundlectan" then
-      errorlevel = bundlectan()
-    elseif target == "doc" then
-      errorlevel = doc(names)
-    elseif target == "check" then
-      errorlevel = check(names)
-    elseif target == "clean" then
-      errorlevel = clean()
-    elseif target == "ctan" then
-      errorlevel = ctan()
-    elseif target == "install" then
-      errorlevel = install()
-    elseif target == "manifest" then
-      errorlevel = manifest()
-    elseif target == "save" then
-      if next(names) then
-        errorlevel = save(names)
-      else
-        help()
-      end
-    elseif target == "tag" then
-      if options["names"] and #options["names"] == 1 then
-        errorlevel = tag(options["names"][1])
-      else
-        print("Tag name required")
-        help()
-        exit(1)
-      end
-    elseif target == "uninstall" then
-      errorlevel = uninstall()
-    elseif target == "unpack" then
-      errorlevel = unpack()
-    else
-      help()
+    if target_list[target].pre then
+     errorlevel = target_list[target].pre(names)
+     if errorlevel ~= 0 then
+       exit(1)
+     end
     end
+    errorlevel = target_list[target].func(names)
   end
+  -- All done, finish up
   if errorlevel ~= 0 then
     exit(1)
   else
diff --git a/l3build.dtx b/l3build.dtx
index 2c4cd5a..307b2a1 100644
--- a/l3build.dtx
+++ b/l3build.dtx
@@ -1432,6 +1432,24 @@
 %   |target| in this table is ignored.
 % \end{function}
 %
+% \subsection{Customising the target list}
+%
+% The targets known to \pkg{l3build} are stored in the global table
+% |target_list|. Each entry should have at least a |func|, pointing to the
+% function used to implement the target. This function will receive the
+% list of names given at the command line as a table argument.
+% In most cases, targets will also have a |desc|, used to construct |help()|
+% automatically. In addition, the following may also be used:
+% \begin{itemize}
+%   \item |bundle_func| A variant of |func| used when at the top level of
+%     a bundle
+%   \item |bundle_target| A boolean to specify that when passing the target
+%     name in a bundle, it should have |bundle| prepended.
+%   \item |pre| A function executed before the main function, and receiving
+%     the |names| as an argument; this allows checking of the |name| data
+%     without impact on the main |func|.
+% \end{itemize}
+%
 % \subsection{Customising the manifest file}
 % \label{sec:manifest}
 %





More information about the latex3-commits mailing list