xolti 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/exe/xolti CHANGED
@@ -1,4 +1,4 @@
1
- #!/user/bin/env ruby
1
+ #!/usr/bin/env ruby
2
2
 
3
3
  # xolti
4
4
  # Copyright (C) Rémi Even 2016
@@ -17,29 +17,44 @@
17
17
  # along with Xolti. If not, see <http://www.gnu.org/licenses/>.
18
18
  require "yaml"
19
19
  require "pathname"
20
+ require "date"
20
21
 
21
- def extract_project_info(raw_project_info)
22
- {
23
- author: raw_project_info["author"],
24
- project_name: raw_project_info["project_name"],
25
- year: raw_project_info["year"] || Date.today().year
26
- }
27
- end
22
+ require_relative "default_comment_tokens"
23
+ require_relative "resources"
24
+
25
+ class XoltiConfig
28
26
 
29
- module XoltiConfig
30
- def XoltiConfig.find_config_file(path = Pathname.getwd)
31
- return nil if path.root?
27
+ def self.find_config_file(path = Pathname.getwd)
32
28
  potential_config_file = (path + "xolti.yml")
33
- potential_config_file.file? ? potential_config_file.to_s : find_config_file(path.parent)
29
+ return potential_config_file.to_s if potential_config_file.file?
30
+ raise "No xolti.yml found" if path.root?
31
+ find_config_file(path.parent)
34
32
  end
35
33
 
36
- def XoltiConfig.load_config()
34
+ def self.load_config()
37
35
  raw_config = YAML.load(IO.binread(find_config_file()))
36
+ XoltiConfig.new(raw_config)
37
+ end
38
+
39
+ attr_reader :project_info, :template, :offset, :license
40
+
41
+ def initialize(raw_config)
42
+ @project_info = extract_project_info(raw_config["project_info"])
43
+ @comment = DefaultComment::HASH.merge!(raw_config["comment"] || {})
44
+ @license = raw_config["license"]
45
+ @template = raw_config.include?("template") ? raw_config["template"] : IO.binread(Resources.get_template_path(@license))
46
+ @offset = raw_config["offset"] || 0
47
+ end
48
+
49
+ def get_comment(ext)
50
+ @comment[ext.delete('.')]
51
+ end
52
+
53
+ private def extract_project_info(raw_project_info)
38
54
  {
39
- project_info: extract_project_info(raw_config["project_info"]),
40
- comment: raw_config["comment"] || ["/*", " * ", " */"],
41
- template: raw_config["template"] || "header.txt",
42
- offset: raw_config["offset"] || 0
55
+ author: raw_project_info["author"],
56
+ project_name: raw_project_info["project_name"],
57
+ year: raw_project_info["year"] || Date.today().year.to_s
43
58
  }
44
59
  end
45
60
  end
@@ -17,85 +17,35 @@
17
17
  # along with Xolti. If not, see <http://www.gnu.org/licenses/>.
18
18
  require "tempfile"
19
19
 
20
- require_relative "header_detection"
21
- require_relative "comment"
22
-
23
- def complete_template(path, info, template)
24
- template %= info.merge({:file_name => File.basename(path)})
25
- template
26
- end
27
-
28
- def create_header_for(path, config)
29
- bare_header = complete_template(path, config[:project_info], config[:template])
30
- Comment.comment(bare_header, config[:comment])
31
- end
32
-
33
- def insert_with_offset(path, text, offset)
34
- file = Tempfile.new("xolti")
35
- begin
36
- File.open(path, "r") do |source_file|
37
- i = 0
38
- source_file.each_line do |line|
39
- file.write(text) if i == offset
40
- i += 1
41
- file.write(line)
42
- end
43
- end
44
- file.close()
45
- FileUtils.cp(file, path)
46
- ensure
47
- file.close()
48
- file.unlink()
49
- end
50
- end
51
-
52
- def delete_lines_from_file(path, start, length)
53
- file = Tempfile.new("xolti")
54
- begin
55
- File.open(path, "r").each_with_index do |line, index|
56
- file.write(line) if index < start || index >= start + length
57
- end
58
- file.close()
59
- FileUtils.cp(file, path)
60
- ensure
61
- file.close()
62
- file.unlink()
63
- end
64
- end
65
-
66
- def detect_header_position(path, template, comment_tokens)
67
- template_lines = Comment.comment(template, comment_tokens).lines("\n")
68
- template_regexp_lines = template_lines.map do |line|
69
- HeaderDetection.create_detection_regexp_for_line(line)
70
- end
71
- potential_header_start = 0
72
- i = 0
73
- File.open(path, "r").each do |line|
74
- if template_regexp_lines[i].match(line)
75
- i += 1
76
- return potential_header_start if i == template_regexp_lines.length
77
- else
78
- potential_header_start += i + 1
79
- i = 0
80
- end
81
- end
82
- -1
83
- end
20
+ require_relative "file_modification"
21
+ require_relative "header_detector"
22
+ require_relative "header_generator"
23
+ require_relative "header_validator"
84
24
 
85
25
  module Core
86
26
  def Core.licensify(path, config)
87
- header = create_header_for(path, config)
88
- insert_with_offset(path, header, config[:offset])
27
+ header = HeaderGenerator.create_for(path, config)
28
+ FileModification.insert_lines_with_offset(path, header, config.offset)
89
29
  end
90
30
 
91
31
  def Core.delete_header(path, config)
92
- template = config[:template]
93
- start = detect_header_position(path, template, config[:comment])
94
- delete_lines_from_file(path, start, Comment.comment(template, config[:comment]).lines.length) if start >= 0
32
+ template = config.template
33
+ ext = File.extname(path)
34
+ detected = HeaderDetector.detect(path, template, config.get_comment(ext))
35
+ FileModification.delete_lines(path, detected[:start], detected[:matches].length) if detected
95
36
  end
96
37
 
97
38
  def Core.has_header(path, config)
98
- template = config[:template]
99
- detect_header_position(path, template, config[:comment]) >= 0
39
+ template = config.template
40
+ ext = File.extname(path)
41
+ HeaderDetector.detect(path, template, config.get_comment(ext))
42
+ end
43
+
44
+ def Core.validate_header(path, config)
45
+ template = config.template
46
+ ext = File.extname(path)
47
+ detected = HeaderDetector.detect(path, template, config.get_comment(ext))
48
+ return [{type: "no_header_found"}] if !detected
49
+ HeaderValidator.diff(detected, config.project_info.merge({file_name: File.basename(path)}))
100
50
  end
101
51
  end
@@ -0,0 +1,72 @@
1
+ # default_comment_tokens.rb
2
+ # Copyright (C) Rémi Even 2016
3
+ #
4
+ # This file is part of Xolti.
5
+ #
6
+ # Xolti is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Xolti is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Xolti. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ class DefaultComment
20
+ HASH = Hash.new(["/*", " * ", " */"]).merge!({
21
+ "adb" => ["--", "-- ", "--"],
22
+ "ads" => ["--", "-- ", "--"],
23
+ "apt" => "~~ ",
24
+ "asm" => [";", "; ", ";"],
25
+ "asp" => ["<%", "' ", "%>"],
26
+ "bas" => ["'", "' ", "'"],
27
+ "bat" => "@REM",
28
+ "cfc" => ["<!---", " ", "--->"],
29
+ "cfm" => ["<!---", " ", "--->"],
30
+ "cls" => "% ",
31
+ "cmd" => "@REM",
32
+ "dtd" => ["<!--", " ", "-->"],
33
+ "e" => ["--", "-- ", "--"],
34
+ "el" => ["!!!", "!!! ", "!!!"],
35
+ "erl" => ["%%%", "%%% ", "%%%"],
36
+ "f" => ["!", "! ", "!"],
37
+ "fml" => ["<!--", " ", "-->"],
38
+ "ftl" => ["<#--", " ", "-->"],
39
+ "ftl" => ["<#--", " ", "-->"],
40
+ "gsp" => ["<!--", " ", "-->"],
41
+ "haml" => "-# ",
42
+ "hrl" => ["%%%", "%%% ", "%%%"],
43
+ "htm" => ["<!--", " ", "-->"],
44
+ "html" => ["<!--", " ", "-->"],
45
+ "jsp" => ["<%--", " ", "--%>"],
46
+ "jspx" => ["<!--", " ", "-->"],
47
+ "kml" => ["<!--", " ", "-->"],
48
+ "lol" => ["OBTW", "", "TLDR"],
49
+ "lua" => ["--[[", "", "]]"],
50
+ "mxml" => ["<!--", " ", "-->"],
51
+ "pas" => ["{*", " * ", " *}"],
52
+ "pl" => "# ",
53
+ "pm" => "# ",
54
+ "pom" => ["<!--", " ", "-->"],
55
+ "properties" => "# ",
56
+ "py" => "# ",
57
+ "rb" => "# ",
58
+ "sh" => "# ",
59
+ "sql" => ["--", "-- ", "--"],
60
+ "sty" => "% ",
61
+ "tex" => "% ",
62
+ "tld" => ["<!--", " ", "-->"],
63
+ "txt" => ["====", "\t", "===="],
64
+ "vm" => ["#*", " ", "*#"],
65
+ "xhtml" => ["<!--", " ", "-->"],
66
+ "xml" => ["<!--", " ", "-->"],
67
+ "xsd" => ["<!--", " ", "-->"],
68
+ "xsl" => ["<!--", " ", "-->"],
69
+ "yaml" => "# ",
70
+ "yml" => "# "
71
+ });
72
+ end
@@ -23,7 +23,7 @@ def parse_xoltignore(path)
23
23
  end
24
24
 
25
25
  module FileFinder
26
- def FileFinder.explore_folder(folder, ignoreRules=[])
26
+ def FileFinder.explore_folder(folder=Dir.pwd, ignoreRules=[])
27
27
  fileAcc = []
28
28
  ignoredPaths = [".", "..", ".git", ".xoltignore", "xolti.yml", "header.txt"]
29
29
  ignoreRules += parse_xoltignore(folder)
@@ -0,0 +1,51 @@
1
+ # file_modification.rb
2
+ # Copyright (C) Rémi Even 2016
3
+ #
4
+ # This file is part of Xolti.
5
+ #
6
+ # Xolti is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Xolti is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Xolti. If not, see <http://www.gnu.org/licenses/>.
18
+ module FileModification
19
+ def FileModification.insert_lines_with_offset(path, text, offset)
20
+ file = Tempfile.new("xolti")
21
+ begin
22
+ File.open(path, "r") do |source_file|
23
+ i = 0
24
+ source_file.each_line do |line|
25
+ file.write(text) if i == offset
26
+ i += 1
27
+ file.write(line)
28
+ end
29
+ end
30
+ file.close()
31
+ FileUtils.cp(file, path)
32
+ ensure
33
+ file.close()
34
+ file.unlink()
35
+ end
36
+ end
37
+
38
+ def FileModification.delete_lines(path, start, length)
39
+ file = Tempfile.new("xolti")
40
+ begin
41
+ File.open(path, "r").each_with_index do |line, index|
42
+ file.write(line) if index < start || index >= start + length
43
+ end
44
+ file.close()
45
+ FileUtils.cp(file, path)
46
+ ensure
47
+ file.close()
48
+ file.unlink()
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,46 @@
1
+ # header_detector.rb
2
+ # Copyright (C) Rémi Even 2016
3
+ #
4
+ # This file is part of Xolti.
5
+ #
6
+ # Xolti is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Xolti is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Xolti. If not, see <http://www.gnu.org/licenses/>.
18
+ require_relative "comment"
19
+ require_relative "template_utils"
20
+
21
+ module HeaderDetector
22
+ def HeaderDetector.detect(path, template, comment_tokens)
23
+ template_lines = Comment.comment(template, comment_tokens).lines("\n")
24
+ template_regexp_lines = template_lines.map do |line|
25
+ TemplateUtils.create_detection_regexp_for_line(line)
26
+ end
27
+ potential_header_start = 0
28
+ matches = []
29
+ File.open(path, "r").each do |line|
30
+ match = template_regexp_lines[matches.length].match(line)
31
+ if match
32
+ matches << match
33
+ if matches.length == template_regexp_lines.length
34
+ return {
35
+ start: potential_header_start,
36
+ matches: matches
37
+ }
38
+ end
39
+ else
40
+ potential_header_start += matches.length + 1
41
+ matches = []
42
+ end
43
+ end
44
+ nil
45
+ end
46
+ end
@@ -0,0 +1,30 @@
1
+ # header_generator.rb
2
+ # Copyright (C) Rémi Even 2016
3
+ #
4
+ # This file is part of Xolti.
5
+ #
6
+ # Xolti is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Xolti is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Xolti. If not, see <http://www.gnu.org/licenses/>.
18
+ require_relative "comment"
19
+
20
+ def complete_template(path, info, template)
21
+ template %= info.merge({file_name: File.basename(path)})
22
+ template
23
+ end
24
+
25
+ module HeaderGenerator
26
+ def HeaderGenerator.create_for(path, config)
27
+ bare_header = complete_template(path, config.project_info, config.template)
28
+ Comment.comment(bare_header, config.get_comment(File.extname(path)))
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ # header_validator.rb
2
+ # Copyright (C) Rémi Even 2016
3
+ #
4
+ # This file is part of Xolti.
5
+ #
6
+ # Xolti is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Xolti is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Xolti. If not, see <http://www.gnu.org/licenses/>.
18
+ module HeaderValidator
19
+ def HeaderValidator.diff(detected, info)
20
+ diff = []
21
+ detected[:matches].each_with_index do |match, i|
22
+ line = detected[:start] + i + 1
23
+ match.names.each do |name|
24
+ name_sym = name.to_sym
25
+ diff << {
26
+ line: line,
27
+ expected: info[name_sym],
28
+ actual: match[name_sym]
29
+ } if info[name_sym] != match[name_sym]
30
+ end
31
+ end
32
+ diff
33
+ end
34
+ end
@@ -0,0 +1,33 @@
1
+ # resources.rb
2
+ # Copyright (C) Rémi Even 2016
3
+ #
4
+ # This file is part of Xolti.
5
+ #
6
+ # Xolti is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Xolti is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Xolti. If not, see <http://www.gnu.org/licenses/>.
18
+ module Resources
19
+
20
+ # If xolti is not installed as a gem, assume that pwd is the root of the project
21
+ # Needed to fix travis integration.
22
+ Resources_dir = Gem::Specification.all_names.select { |name| name.match(/xolti/) }.length >= 1 ?
23
+ File.join(Gem::Specification.find_by_name("xolti").gem_dir, "resources") :
24
+ File.join(Dir.pwd, "resources")
25
+
26
+ def Resources.get_template_path(license)
27
+ File.join(Resources_dir, "headers", license)
28
+ end
29
+
30
+ def Resources.get_full_license_path(license)
31
+ File.join(Resources_dir, "licenses", license)
32
+ end
33
+ end