pdb

Simple password manager
Log | Files | Refs | README

merge.lua (1035B)


      1 if not arg[ 1 ] or not arg[ 2 ] then
      2 	print( arg[ 0 ] .. " <source directory> <path to main>" )
      3 	os.exit( 1 )
      4 end
      5 
      6 local lfs = require( "lfs" )
      7 
      8 local merged = { "#! /usr/bin/lua" }
      9 
     10 local root = arg[ 1 ]
     11 local main = arg[ 2 ]
     12 
     13 local function addDir( rel )
     14 	for file in lfs.dir( root .. "/" .. rel ) do
     15 		if file ~= "." and file ~= ".." then
     16 			local full = root .. "/" .. rel .. file
     17 			local attr = lfs.attributes( full )
     18 
     19 			if attr.mode == "directory" then
     20 				addDir( rel .. file .. "/" )
     21 			elseif file:match( "%.lua$" ) and ( rel ~= "" or file ~= main ) then
     22 				local f = io.open( full, "r" )
     23 				local contents = f:read( "*all" )
     24 				f:close()
     25 
     26 				table.insert( merged, ( [[
     27 					package.preload[ "%s" ] = function( ... )
     28 						%s
     29 					end]] ):format(
     30 						( rel .. file ):gsub( "%.lua$", "" ):gsub( "/", "." ),
     31 						contents
     32 					)
     33 				)
     34 			end
     35 		end
     36 	end
     37 end
     38 
     39 addDir( "" )
     40 
     41 local f = io.open( root .. "/" .. main, "r" )
     42 local contents = f:read( "*all" )
     43 f:close()
     44 
     45 print( table.concat( merged, "\n" ) )
     46 print( contents )