lib/xolti.rb in xolti-0.1.1 vs lib/xolti.rb in xolti-0.2.0

- old
+ new

@@ -8,11 +8,11 @@ # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Xolti 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 +# 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 Xolti. If not, see <http://www.gnu.org/licenses/>. @@ -23,103 +23,84 @@ require_relative "core" require_relative "config" require_relative "file_finder" require_relative "resources" require_relative "version" +require_relative "print_utils" Signal.trap("INT") do puts "\nCancelling..." exit 1 end class XoltiCLI < Thor desc "add [FILE|FOLDER]", "Add a header to FILE or to all files in FOLDER" def add(file) + config = self.load_config {|e| puts e.message; exit 1 } if File.file?(file) - puts "Adding header to #{file}" - config = self.load_config {|e| puts e.message; exit 1 } + PrintUtils.puts_single "Adding header to #{file}" Core.licensify(file, config) if !Core.has_header(file, config) else - config = self.load_config {|e| puts e.message; exit 1 } FileFinder.explore_folder(file) .reject{|source_file| Core.has_header(source_file, config)} .each do |source_file| - puts "Adding header to #{source_file}" + PrintUtils.puts_single "Adding header to #{source_file}" Core.licensify(source_file, config) end end end - desc "check FILE", "Check the header of FILE" - def check(file) + desc "status [FILE|FOLDER]", "Check the header of FILE or to all files in FOLDER; FOLDER default to current one" + def status(file = ".") config = self.load_config {|e| puts e; exit 1 } - diffs = Core.validate_header(file, config) - if diffs.length > 0 - diffs.each do |diff| - if diff[:type] && diff[:type] == "no_header_found" - puts "No header found." - else - puts "Line #{diff[:line]}: expected \"#{diff[:expected]}\" but got \"#{diff[:actual]}\"." - end - end + if File.file?(file) + PrintUtils.puts self.check_file(file, config) || "Correct header" else - puts "Correct header." + FileFinder.explore_folder(file) + .each do |source_file| + message = self.check_file(source_file, config) + if (message) + PrintUtils.puts_single "#{source_file}" + PrintUtils.puts(message, 1) + PrintUtils.puts_single "" + end + end end end desc "list-missing", "Print a list of files missing (proper) header" def list_missing() dir = Dir.pwd config = self.load_config {|e| puts e.message; exit 1 } missing_headers = FileFinder.explore_folder(dir) .reject{|file| Core.has_header(file, config)} - return puts "All files OK" if missing_headers.empty? - puts "Files missing (proper) header:" - missing_headers.each{|file| puts file[dir.length + 1..-1]} + return PrintUtils.puts_single "All files OK" if missing_headers.empty? + PrintUtils.puts_single "Files missing (proper) header:" + PrintUtils.puts(missing_headers.map{|file| file.sub(dir + "/", "")}, 1) end desc "delete FILE", "Delete the header in FILE" def delete(file) - puts "Deleting header in #{file}" + PrintUtils.puts_single "Deleting header in #{file}" config = self.load_config {|e| puts e.message; exit 1 } Core.delete_header(file, config) end desc "generate-license", "Generate a LICENSE file containing a full license" def generate_license() config = self.load_config {|e| puts e.message; exit 1 } filename = "LICENSE" if File.exists?(File.join(Dir.pwd, filename)) then - puts "There is already a LICENSE file. Abort generation." + PrintUtils.puts_single "There is already a #{filename} file. Abort generation." else - FileUtils.cp(Resources.get_full_license_path(config.license), filename) - puts "Created the LICENSE file (#{config.license})" + full_license = IO.binread(Resources.get_full_license_path(config.license)) + File.write(filename, full_license % config.project_info) + PrintUtils.puts_single "Created the #{filename} file (#{config.license})" end end - desc "status", "Check all files in current folder" - def status() - config = self.load_config {|e| puts e.message; exit 1 } - FileFinder.explore_folder() - .each do |source_file| - puts "-- .#{source_file[Dir.pwd.length..-1]}" - diffs = Core.validate_header(source_file, config) - if diffs.length > 0 - diffs.each do |diff| - if diff[:type] && diff[:type] == "no_header_found" - puts "No header found." - else - puts "Line #{diff[:line]}: expected \"#{diff[:expected]}\" but got \"#{diff[:actual]}\"." - end - end - else - puts "Correct header." - end - end - end - map ["--version", "-v"] => :__print_version desc "--version, -v", "Print version of xolti" def __print_version() puts XoltiVersion.get @@ -163,16 +144,31 @@ return XoltiConfig.load_config rescue Exception => e yield e if block_given? end end - } + def check_file(file, config) + diffs = Core.validate_header(file, config) + if diffs.length > 0 + result = [] + diffs.each do |diff| + if diff[:type] && diff[:type] == :no_header_found + return ["No header found."] + else + result << "Line #{diff[:line]}: expected \"#{diff[:expected]}\" but got \"#{diff[:actual]}\"." + end + end + return result.join("\n") + end + end +} + desc "init", "Create xolti.yml" def init() config = self.load_config - return puts "Xolti is already initialiazed" if config != nil - puts "Initiating xolti project" + return PrintUtils.puts_single "Xolti is already initialiazed" if config != nil + PrintUtils.puts_single "Initializing xolti project" config = {"project_info" => {}} self.ask_for_name(config) self.ask_for_author(config) self.ask_for_license(config) File.write("xolti.yml", config.to_yaml)