Class: PinfoRails::PinfoRails
- Inherits:
-
Object
- Object
- PinfoRails::PinfoRails
- Defined in:
- lib/pinfo-rails.rb
Overview
pinfo-rails: A gem to collect informations from a Rails project
Class Method Summary collapse
- .cat(file) ⇒ Object
-
.check_cache ⇒ Object
support methods.
- .check_database ⇒ Object
- .check_deploy ⇒ Object
- .check_rails ⇒ Object
- .check_requirements ⇒ Object
- .check_ruby ⇒ Object
- .grep(file, expression) ⇒ Object
-
.info(args) ⇒ Object
main method.
- .optparse(args) ⇒ Object
- .param(k, v) ⇒ Object
- .printline(intro, styles, *strings) ⇒ Object
Class Method Details
.cat(file) ⇒ Object
160 161 162 163 164 165 166 167 168 169 |
# 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 ⇒ Object
support methods
86 87 88 89 90 91 92 93 |
# File 'lib/pinfo-rails.rb', line 86 def self.check_cache if .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 ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/pinfo-rails.rb', line 95 def self.check_database if .info[:database] && File.exist?( FILES[:conf_db] ) @output += "\n" if [: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 ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/pinfo-rails.rb', line 125 def self.check_deploy if .info[:deploy] @output += "\n" printline( 'Deploy tool', :light_green, grep( FILES[:gemfile], PATTERNS[:deploy_tool] ) ) if [: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 ⇒ Object
146 147 148 |
# File 'lib/pinfo-rails.rb', line 146 def self.check_rails printline( 'Rails', :light_green, grep( FILES[:gemfile], PATTERNS[:rails] ) ) end |
.check_requirements ⇒ Object
140 141 142 143 144 |
# File 'lib/pinfo-rails.rb', line 140 def self.check_requirements .reqs.split( ',' ).each do |req| printline( 'Required', :green, grep( FILES[:gemfile], Regexp.new( "'[^']*#{req}[^']*'.*|\"[^\"]*#{req}[^\"]*\".*" ) ) ) end end |
.check_ruby ⇒ Object
150 151 152 153 154 155 156 |
# 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) ⇒ Object
171 172 173 174 175 176 177 178 179 |
# 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) ⇒ Object
main method
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/pinfo-rails.rb', line 58 def self.info( args ) @conf = {} = optparse( args ) @output = '' @output += "[verbose mode]\n" if [:verbose] if [:conf] @output += "[with config: #{@options[:conf]}]\n" if File.exist? [:conf] lines = File.read( [:conf] ).split( "\n" ).reject { |l| l =~ /^\s*$|^\s*#.*$/ }.map { |l| "--#{l.strip}" } = 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) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/pinfo-rails.rb', line 195 def self.optparse( args ) = OpenStruct.new .library = [] .inplace = false .encoding = 'utf8' .transfer_type = :auto .conf = nil .reqs = '' .styles = true .verbose = false .info = { database: true, cache: true, deploy: true } begin opt_parser = OptionParser.new do |opts| opts. = 'Usage: pinfo [options]' opts.separator '' opts.separator 'Specific options:' opts.on('-cCONF', '--config=CONF', 'Config file') do |v| .conf = v end opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v| .verbose = v end opts.on('-rREQS', '--required=REQS', 'Search for specific gems') do |v| .reqs = v end opts.on('-s', '--[no-]styles', 'With styles and colors (default)') do |v| .styles = v end opts.separator '' opts.on('--[no-]cache', 'Show cache info') do |v| .info[:cache] = v end opts.on('--[no-]database', 'Show database info') do |v| .info[:database] = v end opts.on('--[no-]deploy', 'Show deploy info') do |v| .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. '~/.pinfo-rails.conf' # global configuration lines = File.read( File.( '~/.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. exit rescue OptionParser::InvalidOption => e puts 'ERR: ' + e. exit end end |
.param(k, v) ⇒ Object
181 182 183 |
# File 'lib/pinfo-rails.rb', line 181 def self.param( k, v ) !v.nil? ? ( k + ' = ' + v ) : '' end |
.printline(intro, styles, *strings) ⇒ Object
185 186 187 188 189 190 191 192 193 |
# 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 += [:styles] ? strings.map( &:to_s ).join( '; ' ).color( styles ) : strings.map( &:to_s ).join( '; ' ) @output += "\n" end |