Skip to content

Commit

Permalink
Added xml report
Browse files Browse the repository at this point in the history
  • Loading branch information
kucherenko committed Jun 3, 2013
1 parent ca40bf2 commit e231a64
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 20 deletions.
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
"coffee-script": "1.6.x",
"underscore": "1.4.x",
"shelljs": "0.1.x",
"crypto": "0.0.x"
},

"devDependencies": {
"jshint": "*",
"cli": "*",
"glob": "*"
"crypto": "0.0.x",
"jshint": "2.1.x",
"cli": "*",
"glob": "*",
"jade": "*"
}
}
}
20 changes: 14 additions & 6 deletions src/cli/cli.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,33 @@ Strategy = require('./../strategy').Strategy
Report = require('./../report').Report

cli.setUsage "jscpd [OPTIONS]"
cli.setApp('jscpd', '0.1.1')
cli.setApp('package.json')
cli.parse {
"min-lines": ['m', "min size of duplication in code lines", "number", 5]
"min-tokens": ['t', "mim size of duplication in code tokens", "number", 70]
"path": ['p', "path to JavaScript code", "path", process.cwd()]
"ignore": ['i', "directory to ignore", "path"]
"log": ['l', "path to log file", "path"]
"ignore": ['i', "directory to ignore", "path"],
"log": ['l', "path to log file", "path"],
"output": ['o', "path to report xml file", "path"],
"verbose": [false, "show full info about copies"]
}

cli.main (args, options) ->
console.log "\njscpd - copy/paste detector for JavaScript, developed by Andrey Kucherenko\n"
files = []
pattern = "#{options.path}/**/*.js"
exclude = process.cwd() + '/' + options.ignore if options.ignore
console.log 'Scaning...'
files = glob.sync(pattern, {})
files = (file for file in files when file.indexOf(exclude) is -1) if exclude

strategy = new Strategy()
detector = new Detector(strategy)
report = new Report({})
report.generate detector.start(files, options['min-lines'], options['min-tokens'])
report = new Report({
verbose: options.verbose,
output: options.output
})
codeMap = detector.start(files, options['min-lines'], options['min-tokens'])
console.log 'Scaning... done!\n'
report.generate codeMap


9 changes: 7 additions & 2 deletions src/clone.coffee
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
shjs = require "shelljs"

class Clone
constructor: (@firstFile, @secondFile, @firstFileStart, @secondFileStart, @linesCount, @tokensCount)->

getLines: ->
console.log "getLines"
code = shjs.cat(@firstFile)
lines = code.split '\n'
start = @firstFileStart
end = start + @linesCount
lines[start..end].join("\n")

exports.Clone = Clone
exports.Clone = Clone
8 changes: 7 additions & 1 deletion src/map.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@ class Map
@clonesByFile[clone.secondFile] = [clone]
@numberOfFiles++

getPercentage: ->
result = 100
if @numberOfLines > 0
result = @numberOfDuplication / @numberOfLines * 100
result.toFixed 2

exports.Map = Map

exports.Map = Map
26 changes: 23 additions & 3 deletions src/report.coffee
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@

jade = require('jade').runtime
fs = require 'fs'
class Report

constructor: (@options) ->

generate: (@map) ->
result = ""
xmlDoc = false
if @options.output
xmlDoc = "<?xml version=\"1.1\" encoding=\"UTF-8\" ?><pmd-cpd>"
verbose = @options.verbose
for clone in @map.clones
do (clone) ->
result = result + "\n\t- #{clone.firstFile}:#{clone.firstFileStart}-#{clone.firstFileStart + clone.linesCount}\n\t" +
" #{clone.secondFile}:#{clone.secondFileStart}-#{clone.secondFileStart + clone.linesCount}\n\t"
result = "#{result}\n#{clone.getLines()}" if verbose

if xmlDoc
xmlDoc = xmlDoc +
"<duplication lines='" + clone.linesCount + "' tokens='" + clone.tokensCount + "'>" +
"<file path='" + clone.firstFile + "' line='" + clone.firstFileStart + "'/>" +
"<file path='" + clone.secondFile + "' line='" + clone.secondFileStart + "'/>" +
"<codefragment>" + jade.escape(clone.getLines()) + "</codefragment></duplication>"

if xmlDoc
xmlDoc = xmlDoc + "</pmd-cpd>";
fs.writeFileSync(@options.output, xmlDoc)

result = "Found #{@map.clones.length} exact clones with #{@map.numberOfDuplication} duplicated lines in #{@map.numberOfFiles} files\n #{result}"
console.log result

exports.Report = Report
console.log "#{result}\n\n #{@map.getPercentage()}% (#{@map.numberOfDuplication} lines) duplicated lines out of #{@map.numberOfLines} total lines of code.\n"


exports.Report = Report

0 comments on commit e231a64

Please sign in to comment.