Class: Rocket_starter_options

Inherits:
Hash
  • Object
show all
Defined in:
lib/rocketstarter/rocket_starter_options.rb

Constant Summary collapse

@@version =
Rocketstarter::VERSION::STRING
@@product_name =
"rocket_starter"
@@command_line_params =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRocket_starter_options

Returns a new instance of Rocket_starter_options.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rocketstarter/rocket_starter_options.rb', line 12

def initialize
  super()
  
  # default values
  self[:project] = ""
  self[:rocket_starter_conf_path] = ENV["HOME"] + "/.rocket_starter"
  self[:rocket_starter_conf_path] = ENV['ROCKET_STARTER_CONF'] unless ENV['ROCKET_STARTER_CONF'].nil?
  self[:init] = false
  self.merge!(Rocket_starter_options::static_default_values)
  
end

Class Method Details

.error_with_show_usage(message) ⇒ Object



228
229
230
231
232
# File 'lib/rocketstarter/rocket_starter_options.rb', line 228

def self.error_with_show_usage(message)
  puts "Error:" + message
  Rocket_starter_options::show_usage
  exit(1)
end

.show_usageObject



234
235
236
# File 'lib/rocketstarter/rocket_starter_options.rb', line 234

def self.show_usage
  puts @@opts
end

.static_default_valuesObject

default values if not use .rocket_starter file



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/rocketstarter/rocket_starter_options.rb', line 248

def Rocket_starter_options.static_default_values
  defaults = {}
  defaults[:scmtype] = "svn"
  defaults[:scmuri] = ""
  defaults[:scmpassword] = ""
  defaults[:database] = ""
  defaults[:ignoredatabase] = false
  defaults[:verbose] = false
  defaults[:sudo] = false
  defaults[:logging] = false
  defaults[:log_file] = ENV["HOME"] + "/rocket_starter.log"
  defaults[:skip_netbeans] = false
  defaults[:pluginlist_path] = ENV["HOME"] + "/.rocket_starter_pluginlist"
  defaults[:skip_plugins] = false
  defaults[:skip_commit_for_plugins] = false
  defaults[:rapt] = false
  defaults[:external] = false
  defaults[:createdb] = false
  defaults[:dbpassword] = ""
  defaults[:emulate] = false
  defaults
end

Instance Method Details

#marge_valuesObject

priority static < conf < command line params



272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/rocketstarter/rocket_starter_options.rb', line 272

def marge_values
  conf_params = {}
  # can open?
  begin
    File::open(self[:rocket_starter_conf_path]) do |conf_file|
      temp = YAML.load(conf_file.read)
      conf_params = temp if temp
    end
  rescue
  end
  self.merge!(conf_params.merge!(@@command_line_params))
end

#parseObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
83
84
85
86
87
88
89
90
91
92
93
94
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rocketstarter/rocket_starter_options.rb', line 25

def parse
  
  
  @@opts = OptionParser.new do |opts|
    opts.banner = "Usage: #{@@product_name} project-name [options]"
    opts.separator "Usage: #{@@product_name} --init [options]"
    opts.separator "Usage: #{@@product_name} --check [options]"
    opts.separator "version #{@@version}"
    
    opts.separator ""
    opts.separator "Useful Options:"
    opts.separator ""
    
    opts.on( '--conf=/path/to/conffile', 'change conf file path(static < conf < command line params)' ) do |p|
      self[:rocket_starter_conf_path] = p
    end
    
    opts.on( '--scmtype=[svn|git]', 'choose SCM type. default is svn' ) do |p|
      @@command_line_params[:scmtype] = p
    end
    
    opts.on( '--scmuri=URI', 'use provided URI for SCM(if need, use with "username@")' ) do |p|
      @@command_line_params[:scmuri] = p
    end
    
    opts.on( '--scmpassword=PASSWORD', 'use supplied password for SCM' ) do |p|
      @@command_line_params[:scmpassword] = p
    end

    opts.on( '--database=name', 'paramater for rails. default is ""' ) do |p|
      @@command_line_params[:database] = p
    end
    
    opts.on( '--ignoredatabase', 'set ingore for database.yml') do
      @@command_line_params[:ignoredatabase] = true
    end

    opts.on( '--verbose', 'provide extra output while running') do
      @@command_line_params[:verbose] = true
    end
    
    opts.on( '--sudo', 'use sudo command') do
      @@command_line_params[:sudo] = true
    end

    opts.on( '--log', 'create a log of command activity' ) do
      @@command_line_params[:logging] = true
    end
    
    opts.on( '--logfile=/path/to/logfile', 'Set path to logfile' ) do |p|
      @@command_line_params[:log_file] = p
    end

    opts.on( '--skip-netbeans', 'skip to setup propset for netbeans ide' ) do
      @@command_line_params[:skip_netbeans] = true
    end

    opts.on( '--listpath=/path/to/listfile', 'plugin url list path for setup' ) do |p|
      @@command_line_params[:pluginlist_path] = p
    end

    opts.on( '--skip-commit-plugins', "don't commit files for plugins" ) do
      @@command_line_params[:skip_commit_for_plugins] = true
    end
    
    opts.on( '--skip-plugins', 'skip to setup plugins' ) do
      @@command_line_params[:skip_plugins] = true
    end
    
          
    opts.on( '--rapt', 'use RaPT gem for plugins' ) do
      @@command_line_params[:rapt] = true
    end

    opts.on( '--externals', 'use svn:externals for plugins' ) do
      @@command_line_params[:external] = true
    end

    opts.on( '--createdb', 'execute rake db:create:all at last' ) do
      @@command_line_params[:createdb] = true
    end

    opts.on( '--dbpassword=password', 'write password to config/database.yml file' ) do |p|
      @@command_line_params[:dbpassword] = p
    end

    opts.on( '--emulate', 'enable emulation mode' ) do
      @@command_line_params[:emulate] = true
    end

    opts.separator ""
    opts.separator "General Options:"
    opts.separator ""
    
    opts.on( '-h', '--help', 'display this information' ) do
      puts opts
      exit
    end
    
    opts.on( '-v', '--version', 'display version information'  ) do
      puts "Ruby on Rails environment setup tool: #{@@product_name} version #{@@version}"
      exit
    end

    opts.on( '--init', 'setup a template of conf file and plugin list file'  ) do
      self[:init] = true
    end

    self[:check] = false
    opts.on( '--check', 'check current variables'  ) do
      self[:check] = true
    end

    opts.separator ""
    opts.separator "Notice:"
    opts.separator ""
    opts.separator "Paramater priority: static < conf < command line"
    opts.separator "You can use a environment variable ROCKET_STARTER_CONF for a rocket_starter_conf_file"
    opts.separator "conf path priority: env < command line"

  end
  
  validate_project_name
  @@opts.parse!
  marge_values
  if self[:check] or "--check" == self[:project]
    puts "current variables"
    self.each_pair do |key, value| puts "#{key}:#{value}" end
    exit
  end 
  if self[:init] or "--init" == self[:project] 
    put_conf_file
    put_pluginlist_file
    exit
  end
  self
end

#put_conf_fileObject

write a template conf file to rocket_starter_conf_path



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rocketstarter/rocket_starter_options.rb', line 164

def put_conf_file
  # check put path
  Rocket_starter_options::error_with_show_usage "conf path is empty. set ROCKET_STARTER_CONF environment variable or use --conf paramater." if self[:rocket_starter_conf_path].empty?
  
  unless "y" == Readline.readline("would a template file put to #{self[:rocket_starter_conf_path]}? [y/N] > ").downcase
    puts "Set your conf path to ROCKET_STARTER_CONF environment variable or use --conf paramater."
    exit
  end
      
  begin
    File.open(self[:rocket_starter_conf_path]) do |yamlfile|
      # check overwrite?
      exit unless "y" == Readline.readline("Overwrite? [y/N]").downcase
    end
  rescue
  end
  
  begin
    File.open(self[:rocket_starter_conf_path], "w") do |yamlfile|
      yamlfile.puts "# Rocket_starter conf file"
      yamlfile.puts ""
      output = Rocket_starter_options::static_default_values.merge(@@command_line_params)
      yamlfile.puts output.to_yaml
    end
    puts "Put template yaml file to #{self[:rocket_starter_conf_path]}"
  rescue
    puts "Warning:Could not open for writing. check parmitions."
  end
end

#put_pluginlist_fileObject

write a template plugin list file to :pluginlist_path



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
# File 'lib/rocketstarter/rocket_starter_options.rb', line 195

def put_pluginlist_file
  # check put path
  Rocket_starter_options::error_with_show_usage "plugin list path is empty. set --pluginlist_path paramater." if "" == self[:pluginlist_path]
  
  unless "y" == Readline.readline("Would a template of plugin list file put to #{self[:pluginlist_path]}? [y/N] > ").downcase
    puts "Set your plugin list path to --listpath paramater."
    exit
  end
      
  begin
    File.open(self[:pluginlist_path]) do |textfile|
      # check overwrite?
      exit unless "y" == Readline.readline("Overwrite? [y/N]").downcase
    end
  rescue
  end
  
  begin
    File.open(self[:pluginlist_path], "w") do |textfile|
      textfile.puts "# command list file for executing it before finish"
      textfile.puts ""
      textfile.puts "# <- this line is comment."
      textfile.puts "# next line is plugin URI:"
      textfile.puts "http://svn.cardboardrocket.com/paginating_find"
      textfile.puts "http://svn.techno-weenie.net/projects/plugins/attachment_fu/"
      textfile.puts "http://svn.s21g.com/public/rails/plugins/annotate_models_with_index/"
    end
    puts "Put template of plugin list file to #{self[:pluginlist_path]}"
  rescue
    puts "Warning:Could not open for writing. check parmitions."
  end
end

#validate_project_nameObject



238
239
240
241
242
243
244
245
# File 'lib/rocketstarter/rocket_starter_options.rb', line 238

def validate_project_name
  temp  = ARGV.first
  unless temp.nil? or temp.empty?
    @@command_line_params[:project] = temp.chomp
  else
    Rocket_starter_options::error_with_show_usage("Need project name.")
  end
end