cantor 0.1 → 0.2
raw patch · 5 files changed
+79/−22 lines, 5 filesdep +bytestringdep +containers
Dependencies added: bytestring, containers
Files
- CHANGES +5/−1
- cantor.cabal +16/−4
- src/Main.hs +28/−13
- src/Utils/Folder.hs +22/−1
- test-src/Spec.hs +8/−3
CHANGES view
@@ -1,1 +1,5 @@-Version 0.1+Version 0.2+-----------++* Added default command to scan project for class paths. Works only with Java projects+* Added LOC metric
cantor.cabal view
@@ -1,5 +1,5 @@ name: cantor-version: 0.1+version: 0.2 cabal-version: >= 1.10 build-type: Simple author: Krzysztof Langner@@ -14,7 +14,15 @@ Extra-Source-Files: CHANGES description: - Application for analyzing Java source code+ Application for analyzing Java source code.+ .+ Currently implemented:+ .+ * Finding project class paths.+ .+ * Line Of Code metric.+ .+ Check <https://github.com/klangner/cantor/blob/master/doc/usage.md documentation> for usage patterns. source-repository head type: git@@ -31,7 +39,9 @@ hxt-xpath >=9.1.2 && <9.2, gtk >=0.12.4 && <0.13, filepath >=1.3.0 && <1.4,- parsec >=3.1.3 && <3.2+ parsec >=3.1.3 && <3.2,+ containers >=0.5.0 && <0.6,+ bytestring >=0.10.0 && <0.11 ghc-options: -Wall other-modules: @@ -51,7 +61,9 @@ QuickCheck >=2.6 && <2.7, hxt >= 9.3 && < 9.4, hxt-xpath >=9.1.2 && <9.2,- parsec >=3.1.3 && <3.2+ parsec >=3.1.3 && <3.2,+ containers >=0.5.0 && <0.6,+ bytestring >=0.10.0 && <0.11 other-modules: hs-source-dirs:
src/Main.hs view
@@ -14,8 +14,11 @@ import System.Environment import System.Console.GetOpt import System.Exit-import Project.Maven as Maven+import Paths_cantor (version)+import Data.Version (showVersion)+import Project.Sources (findJavaClassPaths) import Project.Model+import Metric.Basic data Flag = Version -- -v@@ -28,7 +31,7 @@ argv <- getArgs case getOpt Permute flags argv of ([], cmd:src:_, []) -> commandAction cmd src- ([], [src], []) -> analyzeAction src+ ([], [src], []) -> printPathsAction src ([Version], _, []) -> printVersion ([Help], _, []) -> printUsageInfo (_, _, []) -> printUsageInfo@@ -49,7 +52,7 @@ -- | Print application version number printVersion :: IO () printVersion = - putStrLn "mavex version 0.1"+ putStrLn $ "cantor version " ++ showVersion version -- | This action prints errors@@ -62,9 +65,10 @@ -- | Print usage info printUsageInfo :: IO () printUsageInfo = do- putStrLn "Usage: mavex [command] <project_path>"+ putStrLn "Usage: cantor [command] <project_path>" putStrLn " commands:"- putStrLn " create - Create maven POM file."+ putStrLn " <none> - Print source class paths."+ putStrLn " loc - Print Line of code metric." putStrLn (usageInfo "" flags) @@ -73,16 +77,27 @@ printProjectInfo prj = putStrLn (getProjectInfo prj) --- | Print information about project-analyzeAction :: FilePath -> IO ()-analyzeAction src = do- pom <- Maven.loadProject src- case pom of- Just prj -> printProjectInfo prj- Nothing -> putStrLn "Maven POM file not found"+-- | Print information about class paths+printPathsAction :: FilePath -> IO ()+printPathsAction src = do+ paths <- findJavaClassPaths src+ putStrLn (if null paths then "Can't find any valid Java files in: " ++ src + else "Found Java class paths:")+ mapM_ putStrLn paths -- | Execute command commandAction :: String -> FilePath -> IO ()-commandAction _ _ = putStrLn "Commands not implemented yet."+commandAction xs src | xs == "loc" = locCommand src+ | otherwise = putStrLn "Commands not implemented yet." +-- | Print LOC metric+locCommand :: FilePath -> IO ()+locCommand src = do + loc <- lineOfCode src + let loc1 = filter (\(_, a) -> a > 0) loc+ mapM_ f loc1 + where f (ext, count) = putStrLn $ ext ++ " lines: " ++ fmt count+ fmt :: Int -> String+ fmt a = if a > 1000 then show (a `div` 1000) ++ "K" else show a+
src/Utils/Folder.hs view
@@ -6,13 +6,16 @@ Helper module with functions operating on IO -} module Utils.Folder- ( listFiles+ ( isJavaFile , listDirs+ , listFiles+ , listFilesR )where import System.Directory (canonicalizePath, getDirectoryContents, doesDirectoryExist) import Data.List import Control.Monad+import System.FilePath (takeExtension) -- | list files@@ -36,4 +39,22 @@ path <- canonicalizePath p return $ map ((path++"/")++) filtered where f x = (x /= ".") && (x /= "..") && (not . isPrefixOf ".") x+ + +-- | List all files in given directory and all subdirectories.+-- Returns files with absolute path +listFilesR :: (FilePath -> Bool) -- Predicate to filter files+ -> FilePath -- Directory path+ -> IO [FilePath] -- Found file paths+listFilesR p path = do+ files <- listFiles path+ let filtered = filter p files+ dirs <- listDirs path+ children <- mapM (listFilesR p) dirs + return $ filtered ++ concat children++-- | Predicate to check if given source is Java file+isJavaFile :: FilePath -> Bool+isJavaFile src = takeExtension src == ".java"+
test-src/Spec.hs view
@@ -3,10 +3,15 @@ import qualified Utils.FolderSpec import qualified Project.MavenSpec import qualified AST.JavaParserSpec+import qualified Project.SourcesSpec+import qualified Metric.BasicSpec main :: IO () main = hspec $ do- describe "Folder" Utils.FolderSpec.spec- describe "Maven" Project.MavenSpec.spec- describe "JavaParser" AST.JavaParserSpec.spec+ describe "Utils.Folder" Utils.FolderSpec.spec+ describe "Project.Maven" Project.MavenSpec.spec+ describe "ProjectSources" Project.SourcesSpec.spec+ describe "AST.JavaParser" AST.JavaParserSpec.spec+ describe "Metric.Basic" Metric.BasicSpec.spec+