-
Notifications
You must be signed in to change notification settings - Fork 60
/
parser.rb
262 lines (234 loc) · 7.43 KB
/
parser.rb
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#
# Copyright Header - A utility to manipulate copyright headers on source code files
# Copyright (C) 2012 Erik Osterman <e@osterman.com>
#
# This file is part of Copyright Header.
#
# Copyright Header is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Copyright Header is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Copyright Header. If not, see <http://www.gnu.org/licenses/>.
#
require 'fileutils'
require 'yaml'
require 'erb'
require 'ostruct'
require 'linguist'
module CopyrightHeader
class FileNotFoundException < Exception; end
class ExistingLicenseException < Exception; end
class License
@lines = []
def initialize(options)
@options = options
@lines = load_template.split(/\n/).map { |line| line += "\n" }
end
def word_wrap(text, max_width = nil)
max_width ||= @options[:word_wrap]
text.gsub(/(.{1,#{max_width}})(\s|\Z)/, "\\1\n")
end
def load_template
if File.exists?(@options[:license_file])
template = ::ERB.new File.new(@options[:license_file]).read, 0, '%'
license = template.result(OpenStruct.new(@options).instance_eval { binding })
license = word_wrap(license)
license
else
raise FileNotFoundException.new("Unable to open #{file}")
end
end
def format(comment_open = nil, comment_close = nil, comment_prefix = nil)
comment_open ||= ''
comment_close ||= ''
comment_prefix ||= ''
license = comment_open + @lines.map { |line| (comment_prefix + line).gsub(/\s+\n$/, "\n") }.join() + comment_close
license.gsub!(/\\n/, "\n")
license
end
end
class Header
@file = nil
@contents = nil
@config = nil
def initialize(file, config)
@file = file
@contents = File.read(@file)
@config = config
end
def format(license)
license.format(@config[:comment]['open'], @config[:comment]['close'], @config[:comment]['prefix'])
end
def add(license)
if has_copyright?
raise ExistingLicenseException.new("detected existing license")
end
copyright = self.format(license)
if copyright.nil?
STDERR.puts "Copyright is nil"
return nil
end
text = ""
if @config.has_key?(:after) && @config[:after].instance_of?(Array)
copyright_written = false
lines = @contents.split(/\n/)
head = lines.shift(10)
while(head.size > 0)
line = head.shift
text += line + "\n"
@config[:after].each do |regex|
pattern = Regexp.new(regex)
if pattern.match(line)
text += copyright
copyright_written = true
break
end
end
end
if copyright_written
text += lines.join("\n")
else
text = copyright + text + lines.join("\n")
end
else
# Simply prepend text
text = copyright + @contents
end
return text
end
def remove(license)
if has_copyright?
text = self.format(license)
@contents.gsub!(/#{Regexp.escape(text)}/, '')
@contents
else
STDERR.puts "SKIP #{@file}; copyright not detected"
return nil
end
end
def has_copyright?(lines = 10)
@contents.split(/\n/)[0..lines].select { |line| line =~ /(?!class\s+)([Cc]opyright|[Ll]icense)\s/ }.length > 0
end
end
class Syntax
attr_accessor :guess_extension
def initialize(config, guess_extension = false)
@guess_extension = guess_extension
@config = {}
syntax = YAML.load_file(config)
syntax.each_value do |format|
format['ext'].each do |ext|
@config[ext] = {
:before => format['before'],
:after => format['after'],
:comment => format['comment']
}
end
end
end
def ext(file)
extension = File.extname(file)
if @guess_extension && (extension.nil? || extension.empty?)
extension = Linguist::FileBlob.new(file).language.primary_extension
end
return extension
end
def supported?(file)
@config.has_key? ext(file)
end
def header(file)
Header.new(file, @config[ext(file)])
end
end
class Parser
attr_accessor :options
@syntax = nil
@license = nil
def initialize(options = {})
@options = options
@exclude = [ /^LICENSE(|\.txt)$/i, /^holders(|\.txt)$/i, /^README/, /^\./]
@license = License.new(:license_file => @options[:license_file],
:copyright_software => @options[:copyright_software],
:copyright_software_description => @options[:copyright_software_description],
:copyright_years => @options[:copyright_years],
:copyright_holders => @options[:copyright_holders],
:word_wrap => @options[:word_wrap])
@syntax = Syntax.new(@options[:syntax], @options[:guess_extension])
end
def execute
if @options.has_key?(:add_path)
@options[:add_path].split(File::PATH_SEPARATOR).each { |path| add(path) }
end
if @options.has_key?(:remove_path)
@options[:remove_path].split(File::PATH_SEPARATOR).each { |path| remove(path) }
end
end
def transform(method, path)
paths = []
if File.file?(path)
paths << path
else
paths << Dir.glob("#{path}/**/*")
end
paths.flatten!
paths.each do |path|
begin
if File.file?(path)
if @exclude.include? File.basename(path)
STDERR.puts "SKIP #{path}; excluded"
next
end
else
STDERR.puts "SKIP #{path}; not file"
next
end
if @syntax.supported?(path)
header = @syntax.header(path)
contents = header.send(method, @license)
if contents.nil?
STDERR.puts "SKIP #{path}; failed to generate license"
else
write(path, contents)
end
else
STDERR.puts "SKIP #{path}; unsupported"
end
rescue Exception => e
STDERR.puts "SKIP #{path}; #{e.message}"
end
end
end
# Add copyright header recursively
def add(dir)
transform(:add, dir)
end
# Remove copyright header recursively
def remove(dir)
transform(:remove, dir)
end
def write(file, contents)
if @options[:dry_run]
STDERR.puts "UPDATE #{file} [dry-run]"
STDERR.puts contents
elsif @options[:output_dir].nil?
STDERR.puts "UPDATE #{file} [no output-dir]"
STDERR.puts contents
else
dir = "#{@options[:output_dir]}/#{File.dirname(file).gsub(/^\/+/, '')}"
STDERR.puts "UPDATE #{file} [output-dir #{dir}]"
FileUtils.mkpath dir unless File.directory?(dir)
output_path = @options[:output_dir] + file
f =File.new(output_path, 'w')
f.write(contents)
f.close
end
end
end
end