module Maximus::Helper
Methods used in more than one place @since 0.1.0
Public Instance Methods
Default paths to check for lints and some stats @since 0.1.6.1 Note: is_rails? must be defined second-to-last if more frameworks are added @param root [String] base directory @param folder [String] nested folder to search for for Rails or Middleman @param extension [String] file glob type to search for if neither @return [String] path to desired files
# File lib/maximus/helper.rb, line 130 def discover_path(root = @config.working_dir, folder = '', extension = '') return @path unless @path.blank? if is_middleman? File.join(root, 'source', folder) elsif is_rails? File.join(root, 'app', 'assets', folder) else extension.blank? ? File.join(root) : File.join(root, '/**', "/*.#{extension}") end end
Edit and save a YAML file @param yaml_location [String] YAML absolute file path @return [void]
# File lib/maximus/helper.rb, line 86 def edit_yaml(yaml_location, &block) d = YAML.load_file(yaml_location) block.call(d) File.open(yaml_location, 'w') {|f| f.write d.to_yaml } end
Count how many files were linted
@param path [String] path to folders @param ext [String] file extension to search for @return [Integer] number of files matched by the path
# File lib/maximus/helper.rb, line 71 def file_count(path, ext = 'scss') file_list(path, ext).length end
Find all files that were linted by extension
@param path [String] path to folders @param ext [String] file extension to search for @return [Array<String>] list of file paths
# File lib/maximus/helper.rb, line 59 def file_list(path, ext = 'scss', remover = '') # Necessary so that directories aren't counted collect_path = path.include?("*") ? path : "#{path}/**/*.#{ext}" # Remove first slash from path if present. probably a better way to do this. Dir[collect_path].collect { |file| file.gsub(remover, '').gsub(/^\/app\//, 'app/') if File.file?(file) } end
See if project is a Middleman app @since 0.1.6.1 @return [Boolean]
# File lib/maximus/helper.rb, line 23 def is_middleman? (@config.settings[:framework] == 'middleman' unless @config.blank?) || Gem::Specification::find_all_by_name('middleman').any? end
See if project linted is a Rails app This will usually be stored as a class variable in the inherited class @return [Boolean]
# File lib/maximus/helper.rb, line 16 def is_rails? (@config.settings[:framework] == 'rails' unless @config.blank?) || defined?(Rails) end
Verify that command is available on the box before continuing
@param command [String] command to check @param install_instructions [String] how to install the missing command @return [void] aborts the action if command not found
# File lib/maximus/helper.rb, line 38 def node_module_exists(command, install_instructions = 'npm install -g') cmd = `if hash #{command} 2>/dev/null; then echo "true"; else echo "false"; fi` if cmd.include? "false" command_msg = "Missing command #{command}" abort "#{command_msg}: Please run `#{install_instructions} #{command}` And try again\n" exit 1 end end
Ensure path exists @param path [String, Array] path to files can be directory or glob @return [Boolean]
# File lib/maximus/helper.rb, line 103 def path_exists?(path = @path) path = path.split(' ') if path.is_a?(String) && path.include?(' ') if path.is_a?(Array) path.each do |p| unless File.exist?(p) puts "#{p} does not exist" return false end end else path = path.gsub('/**', '').gsub('/*', '').gsub(/\/\.*/, '') if path.include?('*') if File.exist?(path) return true else puts "#{path} does not exist" return false end end end
Request user input @param args [Array<String>] prompts to request @return [String] user input to use elsewhere
# File lib/maximus/helper.rb, line 95 def prompt(*args) print(*args) STDIN.gets end
Grab the absolute path of the reporter file @param filename [String] @return [String] absolute path to the reporter file
# File lib/maximus/helper.rb, line 50 def reporter_path(filename) File.join(File.dirname(__FILE__), 'reporter', filename) end
Get root directory of file being called @return [String] absolute path to root directory
# File lib/maximus/helper.rb, line 29 def root_dir is_rails? ? Rails.root.to_s : Dir.pwd.to_s end
Convert string to boolean @param str [String] the string to evaluate @return [Boolean] whether or not the string is true
# File lib/maximus/helper.rb, line 78 def truthy?(str) return true if str == true || str =~ (/^(true|t|yes|y|1)$/i) return false if str == false || str.blank? || str =~ (/^(false|f|no|n|0)$/i) end