argsfilesindir

function argsfilesindir( appname [, dir] ) --> defargs, filepaths

Description

Function that helps to integrate args files in the recommended behaviour.

  • able to detect the default args file

  • detects all other relevant args files in the working directory

The optional value dir allows to use this function to locate the args file in a special directory.

Parameters

appname

Defines the current application name that should be used for the scanning.

dir

The directory that will be scanned. By default uses the function the current working directory.

Return Values

defargs

The default args file for this application in the directory if existing, otherwise nil.

filepaths

Filepaths to the args file that can be read if existing, otherwise nil.

Code

local lfs = require( "lfs" ) --ZREQ-lfs

--ZFUNC-argsfilesindir-v1
local function argsfilesindir( appname, dir ) --> defargs, filepaths
   --ZFUNC-dirfiles-v1
   local function dirfiles( path ) --> iter
      local function yielddir( path )
         for entry in lfs.dir( path ) do
            local entrypath = path.."/"..entry
            local mode = lfs.attributes( entrypath, "mode" )
            if mode == "file" then
               coroutine.yield( entry )
            end
         end
      end

      return coroutine.wrap( function() yielddir( path ) end )
   end
   --ZFUNC-endswith-v1
   local function endswith( str, suffix ) --> res
      return string.sub( str, -string.len( suffix ) ) == suffix
   end
   --ZFUNC-startswith-v1
   local function startswith( str, prefix ) --> res
      return string.sub( str, 1, string.len( prefix ) ) == prefix
   end

   dir = dir or "."

   local defargs = nil
   local filepaths = {}
   -- check for .args files in the working directory
   for filename in dirfiles( dir ) do
      if filename == appname..".auto.args" then
         defargs = filename
      elseif startswith( filename, appname ) and
         endswith( filename, ".args" ) then
         table.insert( filepaths, filename )
      end
   end
   if #filepaths > 0 then
      return defargs, filepaths
   end

   return defargs, nil
end

return argsfilesindir

Examples

local t = require( "taptest" )
local argsfilesindir = require( "argsfilesindir" )
local like = require( "like" )
local mkdirtree = require( "mkdirtree" )
local rmdirtree = require( "rmdirtree" )
local same = require( "same" )

-- setup
res, err = mkdirtree{
   [ "app1.auto.args" ] = "",
   [ "subfolder" ] = {
      [ "app1.auto.args" ] = "",
      [ "app1.args" ] = "",
      [ "app1.var1.args" ] = "",
      [ "aaa.var.args" ] = "",
      [ "zzz.auto.args" ] = ""
   }
}

t( res,true )

-- test in the working dir
defargs, filepaths = argsfilesindir( "app1" )
t( defargs, "app1.auto.args" )
t( filepaths, nil )

defargs, filepaths = argsfilesindir( "xxx" )
t( defargs, nil )
t( filepaths, nil )

-- test subfolder
defargs, filepaths = argsfilesindir( "app1", "subfolder" )
t( defargs, "app1.auto.args" )
t( like( filepaths, { "app1.args", "app1.var1.args" } ), true )

defargs, filepaths = argsfilesindir( "aaa", "subfolder" )
t( defargs, nil )
t( same( filepaths, { "aaa.var.args" } ), true )

defargs, filepaths = argsfilesindir( "zzz", "subfolder" )
t( defargs, "zzz.auto.args" )
t( filepaths, nil )

-- teardown
os.remove( "app1.auto.args" )
t( rmdirtree( "subfolder" ), true )

t()

Inspired by