lib/core.rb in xolti-0.0.0 vs lib/core.rb in xolti-0.1.0

- old
+ new

@@ -15,87 +15,37 @@ # # You should have received a copy of the GNU General Public License # along with Xolti. If not, see <http://www.gnu.org/licenses/>. require "tempfile" -require_relative "header_detection" -require_relative "comment" +require_relative "file_modification" +require_relative "header_detector" +require_relative "header_generator" +require_relative "header_validator" -def complete_template(path, info, template) - template %= info.merge({:file_name => File.basename(path)}) - template -end - -def create_header_for(path, config) - bare_header = complete_template(path, config[:project_info], config[:template]) - Comment.comment(bare_header, config[:comment]) -end - -def insert_with_offset(path, text, offset) - file = Tempfile.new("xolti") - begin - File.open(path, "r") do |source_file| - i = 0 - source_file.each_line do |line| - file.write(text) if i == offset - i += 1 - file.write(line) - end - end - file.close() - FileUtils.cp(file, path) - ensure - file.close() - file.unlink() - end -end - -def delete_lines_from_file(path, start, length) - file = Tempfile.new("xolti") - begin - File.open(path, "r").each_with_index do |line, index| - file.write(line) if index < start || index >= start + length - end - file.close() - FileUtils.cp(file, path) - ensure - file.close() - file.unlink() - end -end - -def detect_header_position(path, template, comment_tokens) - template_lines = Comment.comment(template, comment_tokens).lines("\n") - template_regexp_lines = template_lines.map do |line| - HeaderDetection.create_detection_regexp_for_line(line) - end - potential_header_start = 0 - i = 0 - File.open(path, "r").each do |line| - if template_regexp_lines[i].match(line) - i += 1 - return potential_header_start if i == template_regexp_lines.length - else - potential_header_start += i + 1 - i = 0 - end - end - -1 -end - module Core def Core.licensify(path, config) - header = create_header_for(path, config) - insert_with_offset(path, header, config[:offset]) + header = HeaderGenerator.create_for(path, config) + FileModification.insert_lines_with_offset(path, header, config.offset) end def Core.delete_header(path, config) - template = config[:template] - start = detect_header_position(path, template, config[:comment]) - delete_lines_from_file(path, start, Comment.comment(template, config[:comment]).lines.length) if start >= 0 + template = config.template + ext = File.extname(path) + detected = HeaderDetector.detect(path, template, config.get_comment(ext)) + FileModification.delete_lines(path, detected[:start], detected[:matches].length) if detected end def Core.has_header(path, config) - template = config[:template] - detect_header_position(path, template, config[:comment]) >= 0 + template = config.template + ext = File.extname(path) + HeaderDetector.detect(path, template, config.get_comment(ext)) + end + + def Core.validate_header(path, config) + template = config.template + ext = File.extname(path) + detected = HeaderDetector.detect(path, template, config.get_comment(ext)) + return [{type: "no_header_found"}] if !detected + HeaderValidator.diff(detected, config.project_info.merge({file_name: File.basename(path)})) end end