class Object
Constants
- CRUSTY_PATH
- FILES_TO_UNCRUST
- FILE_SPECS
- UNCRUSTIFY_BIN
Location of uncrustify
- UNCRUSTIFY_CONFIG_FILE
Location of uncrustify config. A default one will be created here if none exists
- VALID_EXTENSIONS
Valid extensions of file to uncrust
Public Instance Methods
Ensures the uncrustify.cfg exists, or creates it if not
# File bin/crusty, line 168 def ensure_config_file if not File.exist? UNCRUSTIFY_CONFIG_FILE File.open(UNCRUSTIFY_CONFIG_FILE, 'w') {|f| DATA.each_line { |l| f.write l } } end end
Can’t find uncrustify, exit with an error
# File bin/crusty, line 177 def error_no_uncrustify $stderr.puts %{} $stderr.puts %{Could not locate the uncrustify executable.} $stderr.puts %{} if find_binary('brew') != nil $stderr.puts %{You can install it via homebrew:} $stderr.puts %{ brew install uncrustify} else $stderr.puts %{You can install uncrustify via homebrew package manager.} $stderr.puts %{} $stderr.puts %{To install homebrew, see:} $stderr.puts %{ http://mxcl.github.com/homebrew/} end exit 1 end
Exit with an error message, but show usage
# File bin/crusty, line 196 def error_show_usage_and_exit(message) $stderr.puts %[#{message}] $stderr.puts %[] show_usage exit 1 end
Given /foo/bar/thing.bob, it will return “/foo/bar”, “thing.bob”, “thing”, “.bob”
# File bin/crusty, line 76 def explode_filepath_to_dir_basename_name_ext(filepath) dir = File.dirname(filepath) basename = File.basename(filepath) ext = File.extname(basename) name = File.basename(basename, ext) return dir, basename, name, ext end
Searches some likely locations for an executable
# File bin/crusty, line 147 def find_binary(name) fullpath = nil; locations = [ '/usr/local/bin', '/opt/local/bin', '/usr/bin', '/bin', '/usr/local/sbin', '/usr/sbin', '/sbin' ].any? { |path| searchpath = File.join(path, name) fullpath = searchpath if File.exist? searchpath } return fullpath end
Recursively gathers all files from the specified path who have one of the specifed extensions
# File bin/crusty, line 89 def gather_all_sourcefiles(path, *extensions) extension_glob = "*.{#{extensions.join(',')}}" return Dir.glob(File.join(path, '**', extension_glob), File::FNM_CASEFOLD) end
Generates a array of uncrustfy specs from an array of file paths.
A spec is simply a hash containing :source which is the source file full path and :forcetype which can optionally be used to force the filetype. If :forcetype is nil, leave it up to uncrustify to decide
# File bin/crusty, line 104 def generate_uncrustify_specs(filelist) file_specs = []; filelist.each { |file| dir, filename, name, ext = explode_filepath_to_dir_basename_name_ext(file) # If it's a header file, do some craziness to work out the file type sourcetype = nil if ext.downcase == '.h' if filelist.any? { |companion| companion =~ /\/#{name}\.mm$/i } sourcetype = "OC+" elsif filelist.any? { |companion| companion =~ /\/#{name}\.(cc|cp|cpp)$/i } sourcetype = "CPP" elsif filelist.any? { |companion| companion =~ /\/#{name}\.c$/i } sourcetype = "C" else # Assume Objective-C sourcetype = "OC" end end file_specs << { :source => file, :forcetype => sourcetype } } return file_specs end
Shows usage instructions
# File bin/crusty, line 208 def show_usage $stdout.puts "Usage" $stdout.puts "=====" $stdout.puts "" $stdout.puts "WARNING: Files are editied WITHOUT backup. You should protect" $stdout.puts "the files with your version control system of choice before" $stdout.puts "running this script!" $stdout.puts "" $stdout.puts "Examples:" $stdout.puts "" $stdout.puts " Uncrust from the current directory:" $stdout.puts " #{File.basename(__FILE__)} ." $stdout.puts "" $stdout.puts " Uncrust files in directory './foo':" $stdout.puts " #{File.basename(__FILE__)} foo" $stdout.puts "" $stdout.puts " Uncrust files in directory '/User/bob/dirtyproject':" $stdout.puts " #{File.basename(__FILE__)} /User/bob/dirtyproject" $stdout.puts "" end
Runs the uncrustify command for the given spec dictionary. See generate_uncrustify_specs
for a description of the spec.
# File bin/crusty, line 138 def uncrust(spec) config_path = File.join CRUSTY_PATH, "../", "uncrustify.cfg" forcetype = spec[:forcetype] ? "-l #{spec[:forcetype]}" : "" %x[#{UNCRUSTIFY_BIN} #{forcetype} -c #{config_path} --no-backup '#{spec[:source]}'] end