Skip to content

Instantly share code, notes, and snippets.

@kexoth
Last active August 29, 2015 14:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kexoth/73613630cfea6f71db01 to your computer and use it in GitHub Desktop.
Save kexoth/73613630cfea6f71db01 to your computer and use it in GitHub Desktop.
Started this gist as comparison implementation between scripts for counting number of files & lines of code in projects in Swift (me) & Lua(@nikolamin). Added Python (@whoeverest) & Haskell (@bor0).
import System.Directory
import Control.Monad (filterM, mapM, liftM)
import System.FilePath ((</>))
getDirsRec :: FilePath -> IO [FilePath]
getDirsRec d = do
dirContents <- getDirectoryContents d
let dirContents' = [ d </> x | x <- dirContents, x /= ".", x /= ".." ]
dirs' <- mapM dirRec dirContents'
return (concat dirs' ++ [d])
where
dirRec n = do
isDir <- doesDirectoryExist n
if isDir then getDirsRec n
else return []
getFiles :: FilePath -> IO [FilePath]
getFiles d = do
dirContents <- getDirectoryContents d
filterM doesFileExist (map (d </>) dirContents)
getLOC :: FilePath -> IO Int
getLOC f = (length . lines) `fmap` readFile f
main = liftM sum (getDirsRec "." >>= mapM getFiles >>= mapM getLOC . concat)
------------------------------------
-- Author: Nikola Minoski - June 2014
-- Done in competition with Kex
-- Competition: Swift vs Lua
------------------------------------
require("lfs")
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, allowedFiles, sourceLinesCount, sourceFiles = arg[1], {}, 0, 0
for i=2,#arg do allowedFiles[arg[i]:lower()] = true end
if not lfs.chdir(dst) then print("Wrong src directory: '" .. dst .. "'") os.exit() 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
if allowedFiles[file:match(".(%w-)$"):lower()] then --check for allowed extensions
sourceFiles = sourceFiles + 1
for line in io.lines(path) do
if line:gsub("%s+", ""):len() > 0 then sourceLinesCount = sourceLinesCount + 1 end
end
end
elseif lfs.attributes(path,"mode")== "directory" then checkDir(path) end
end
end
end
checkDir(dst)
print("Files: ", sourceFiles)
print("Source Lines: ", sourceLinesCount)
import os
def generate_file_paths(path):
for dirpath, dirlist, filelist in os.walk(path):
for f in filelist:
yield os.path.join(dirpath, f)
def create_filter(extension):
def filter_f(path):
return path.endswith(extension)
return filter_f
def lines(file_handle):
return sum(1 for line in file_handle)
paths = generate_file_paths('..') # sys.argv[1]
filt = create_filter('.js') # sys.argv[2]
s = 0
for f_path in paths:
if not filt(f_path):
continue
s += lines(open(f_path))
print s
// Playground - noun: a place where people can play
// Done in competition with Nikola
// Competition: Swift vs Lua
import Cocoa
let fileManager:NSFileManager = NSFileManager.defaultManager()
let currentFilePath = "/Users/aleksandartrpeski/Documents/TMP/smashicons/BadIcons/BadIcons"
let paths:AnyObject[] = fileManager.subpathsAtPath(currentFilePath)
var linesOfCode = 0;
var files = 0;
for path:AnyObject in paths {
let filePath = currentFilePath.stringByAppendingPathComponent(path as String)
if filePath.pathExtension == "h" ||
filePath.pathExtension == "m" ||
filePath.pathExtension == "plist"{
let text:AnyObject = NSString.stringWithContentsOfFile(filePath)
let count = text.componentsSeparatedByString("\n").count
linesOfCode += count
files++
// println("\(count)\t\(text)")
}
}
println("\nLines of Code: \(linesOfCode)\n Files:\(files)")
@whoeverest
Copy link

Во zsh:

➜ bitkix git:(refactor) ✗ wc -l */.js | tail -1
wc: lib/client/app/scripts/vendor/handlebars.js: Is a directory
wc: lib/client/app/scripts/vendor/intro.js: Is a directory
wc: lib/client/app/scripts/vendor/retina.js: Is a directory
1918805 total

@Zlate87
Copy link

Zlate87 commented Jun 13, 2014

In groovy:

def numberOfFiles = 0
def numberOfLines = 0
new File("$pathToDir").eachFileRecurse (groovy.io.FileType.FILES) { file ->
    if (file.name.endsWith('.groovy')) {
        numberOfFiles++
        file.eachLine{ line ->
            numberOfLines++
        }
    }
}
println "Number of files:$numberOfFiles, number of lines:$numberOfLines"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment