class PinfoRails::PinfoRails
pinfo-rails: A gem to collect informations from a Rails project
Public Class Methods
cat( file )
click to toggle source
# File lib/pinfo-rails.rb, line 160 def self.cat( file ) lines = [] if File.exist? file File.read( file ).each_line do |line| lines.push( line.rstrip ) unless line.strip =~ /^$|^#.*$/ end lines.push( '' ) end lines.join( "\n" ) end
check_cache()
click to toggle source
support methods
# File lib/pinfo-rails.rb, line 86 def self.check_cache if @options.info[:cache] @output += "\n" printline( 'Cache development', :cyan, grep( FILES[:conf_env_dev], PATTERNS[:cache] ) ) printline( 'Cache staging ', :yellow, grep( FILES[:conf_env_stag], PATTERNS[:cache] ) ) printline( 'Cache production ', :red, grep( FILES[:conf_env_prod], PATTERNS[:cache] ) ) end end
check_database()
click to toggle source
# File lib/pinfo-rails.rb, line 95 def self.check_database if @options.info[:database] && File.exist?( FILES[:conf_db] ) @output += "\n" if @options[:verbose] printline FILES[:conf_db], {}, ' ' @output += cat FILES[:conf_db] else content = YAML.load_file( FILES[:conf_db] ) rescue nil if content.nil? @output += "ERR: invalid YAML file: #{FILES[:conf_db]}" else content.sort.each do |env, _data| color = case env when 'staging' :yellow when 'production' :red when 'test' :blue else :cyan end printline( "Database #{env}", color, param( 'adapter', content[env]['adapter'] ), param( 'host', content[env]['host'] ), param( 'database', content[env]['database'] ), param( 'username', content[env]['username'] ), param( 'password', content[env]['password'] ) ) end end end end end
check_deploy()
click to toggle source
# File lib/pinfo-rails.rb, line 125 def self.check_deploy if @options.info[:deploy] @output += "\n" printline( 'Deploy tool', :light_green, grep( FILES[:gemfile], PATTERNS[:deploy_tool] ) ) if @options[:verbose] printline FILES[:conf_dep], {}, ' ' @output += cat FILES[:conf_dep] else printline( 'Deploy user', :green, grep( FILES[:conf_dep], PATTERNS[:deploy_user] ) ) end printline( 'Staging ', :yellow, grep( FILES[:conf_dep_stag], PATTERNS[:deploy_info] ) ) printline( 'Production ', :red, grep( FILES[:conf_dep_prod], PATTERNS[:deploy_info] ) ) end end
check_rails()
click to toggle source
# File lib/pinfo-rails.rb, line 146 def self.check_rails printline( 'Rails', :light_green, grep( FILES[:gemfile], PATTERNS[:rails] ) ) end
check_requirements()
click to toggle source
# File lib/pinfo-rails.rb, line 140 def self.check_requirements @options.reqs.split( ',' ).each do |req| printline( 'Required', :green, grep( FILES[:gemfile], Regexp.new( "'[^']*#{req}[^']*'.*|\"[^\"]*#{req}[^\"]*\".*" ) ) ) end end
check_ruby()
click to toggle source
# File lib/pinfo-rails.rb, line 150 def self.check_ruby printline( 'Ruby (current)', :light_green, RUBY_VERSION + ' p' + RUBY_PATCHLEVEL.to_s ) printline( 'Ruby (.rvmrc)', :green, grep( FILES[:rvmrc], PATTERNS[:rvmrc] ) ) ruby_ver = cat( FILES[:ruby_ver] ).strip printline( 'Ruby (.ruby-version)', :green, ruby_ver ) printline( 'Ruby (Gemfile)', :green, grep( FILES[:gemfile], PATTERNS[:ruby] ) ) end
grep( file, expression )
click to toggle source
# File lib/pinfo-rails.rb, line 171 def self.grep( file, expression ) lines = [] if File.exist? file File.read( file ).each_line do |line| lines.push( Regexp.last_match[1].nil? ? Regexp.last_match.to_s.strip : Regexp.last_match[1].to_s.strip ) if !( line.strip =~ /^#.*$/ ) && line =~ expression end end ( lines.length > 1 ? "\n " : '' ) + lines.join( "\n " ) end
info( args )
click to toggle source
main method
# File lib/pinfo-rails.rb, line 58 def self.info( args ) @conf = {} @options = optparse( args ) @output = '' @output += "[verbose mode]\n" if @options[:verbose] if @options[:conf] @output += "[with config: #{@options[:conf]}]\n" if File.exist? @options[:conf] lines = File.read( @options[:conf] ).split( "\n" ).reject { |l| l =~ /^\s*$|^\s*#.*$/ }.map { |l| "--#{l.strip}" } @options = optparse( lines ) else puts 'ERR: file not found' exit end end check_ruby check_rails check_requirements check_database check_cache check_deploy @output end
optparse( args )
click to toggle source
# File lib/pinfo-rails.rb, line 195 def self.optparse( args ) options = OpenStruct.new options.library = [] options.inplace = false options.encoding = 'utf8' options.transfer_type = :auto options.conf = nil options.reqs = '' options.styles = true options.verbose = false options.info = { database: true, cache: true, deploy: true } begin opt_parser = OptionParser.new do |opts| opts.banner = 'Usage: pinfo [options]' opts.separator '' opts.separator 'Specific options:' opts.on('-cCONF', '--config=CONF', 'Config file') do |v| options.conf = v end opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v| options.verbose = v end opts.on('-rREQS', '--required=REQS', 'Search for specific gems') do |v| options.reqs = v end opts.on('-s', '--[no-]styles', 'With styles and colors (default)') do |v| options.styles = v end opts.separator '' opts.on('--[no-]cache', 'Show cache info') do |v| options.info[:cache] = v end opts.on('--[no-]database', 'Show database info') do |v| options.info[:database] = v end opts.on('--[no-]deploy', 'Show deploy info') do |v| options.info[:deploy] = v end opts.separator '' opts.separator 'Common options:' opts.on_tail('-h', '--help', 'Show this message') do puts opts exit end opts.on_tail('-A', '--about', 'Show about') do puts INFO + ' v' + VERSION.join('.') + "\n" + DESC + "\nby " + AUTHORS.first.join( ', ' ) exit end opts.on_tail('-V', '--version', 'Show version') do puts VERSION.join('.') exit end end if File.exist? File.expand_path '~/.pinfo-rails.conf' # global configuration lines = File.read( File.expand_path( '~/.pinfo-rails.conf' ) ).split( "\n" ).reject { |l| l =~ /^\s*$|^\s*#.*$/ }.map { |l| "--#{l.strip}" } opt_parser.parse!( lines ) end opt_parser.parse!( args ) rescue OptionParser::MissingArgument => e puts 'ERR: ' + e.message exit rescue OptionParser::InvalidOption => e puts 'ERR: ' + e.message exit end options end
param( k, v )
click to toggle source
# File lib/pinfo-rails.rb, line 181 def self.param( k, v ) !v.nil? ? ( k + ' = ' + v ) : '' end
printline( intro, styles, *strings )
click to toggle source
# File lib/pinfo-rails.rb, line 185 def self.printline( intro, styles, *strings ) strings = strings.reject { |s| s.nil? || s.empty? } cnt = strings.length return unless cnt > 0 @output += '- ' + intro + ': ' # @output += "\n " if cnt > 1 @output += @options[:styles] ? strings.map( &:to_s ).join( '; ' ).color( styles ) : strings.map( &:to_s ).join( '; ' ) @output += "\n" end