Skip to content

Instantly share code, notes, and snippets.

@nikolamin
Created June 10, 2014 01:40
Show Gist options
  • Save nikolamin/476567e9060f105b8dd2 to your computer and use it in GitHub Desktop.
Save nikolamin/476567e9060f105b8dd2 to your computer and use it in GitHub Desktop.
CountLinesOfCode
------------------------------------
-- Author: Nikola Minoski - June 2014
-- Done in competition with Kex
-- Competition: Swift vs Lua
------------------------------------
if #arg < 2 then
print "Usange: count_src_lines.lua <path_to_project_src_dir> <src_code_ext1> [<src_code_ext2 [...]]"
os.exit()
end
local dst = arg[1]
local allowedFiles = {}
for i=2,#arg do
allowedFiles[arg[i]:lower()] = true
end
require("lfs")
if not lfs.chdir(dst) then
print("Wrong src directory: '" .. dst .. "'")
os.exit()
end
local sourceLinesCount, sourceFiles = 0, 0
function countLines(file)
sourceFiles = sourceFiles + 1
local fd = io.open(file)
local count = 1
while true do
local line = fd:read()
if line == nil then break end
if string.len(line:gsub("%s+", "")) > 0 then
sourceLinesCount = sourceLinesCount + 1
end
end
fd:close()
end
function checkDir(folder)
for file in lfs.dir(folder) do
if file:sub(1, 1) ~= "." then
local path = folder .. "/" .. file
if lfs.attributes(path,"mode") == "file" then
local ext = file:match(".(%w-)$"):lower()
if allowedFiles[ext] then
countLines(path)
end
elseif lfs.attributes(path,"mode")== "directory" then
checkDir(path)
end
end
end
end
checkDir(dst)
print("Files: ", sourceFiles)
print("Source Lines: ", sourceLinesCount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment