forked from kucherenko/jscpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.coffee
53 lines (39 loc) · 1.55 KB
/
report.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
fs = require 'fs'
logger = require 'winston'
class Report
constructor: (@options) ->
generate: (@map) ->
result = ""
xmlDoc = "<?xml version='1.0' 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
xmlDoc = "#{xmlDoc}
<duplication lines='#{clone.linesCount}' tokens='#{clone.tokensCount}'>
<file path='#{clone.firstFile}' line='#{clone.firstFileStart}'/>
<file path='#{clone.secondFile}' line='#{clone.secondFileStart}'/>
<codefragment>#{htmlspecialchars(clone.getLines())}</codefragment>
</duplication>"
xmlDoc = xmlDoc + "</pmd-cpd>"
fs.writeFileSync(@options.output, xmlDoc) if @options.output
result = "Found #{@map.clones.length} exact clones with
#{@map.numberOfDuplication} duplicated lines in
#{@map.numberOfFiles} files\n #{result}"
logger.info "#{result}\n\n
#{@map.getPercentage()}% (#{@map.numberOfDuplication} lines)
duplicated lines out of
#{@map.numberOfLines} total lines of code.\n"
return xmlDoc
htmlspecialchars = (str) ->
if (typeof(str) == "string")
str = str.replace(/&/g, "&")
str = str.replace(/"/g, """)
str = str.replace(/'/g, "'")
str = str.replace(/</g, "<")
str = str.replace(/>/g, ">")
str
exports.Report = Report