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( "tapered" )
local argsfilesindir = require( "argsfilesindir" )
-- util functions
local like = require( "like" )
local mkdirtree = require( "mkdirtree" )
local rmdirtree = require( "rmdirtree" )

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

t.ok( res )

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

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

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

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

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

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

t.done()

Inspired by