bin_script 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  log
2
+ TODO
2
3
  locks
3
- *.gem
4
+ *.gem
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bin_script (0.1.5)
4
+ bin_script (0.1.6)
5
5
  activesupport (>= 2.3.2)
6
6
 
7
7
  GEM
data/README.markdown CHANGED
@@ -21,7 +21,7 @@ gem 'bin_script'
21
21
 
22
22
  Call like:
23
23
 
24
- $ cd project && ./bin/bla.rb -e production -t -d "2012-04-07" -u "asdf"
24
+ $ cd project && ./bin/bla.rb -e production --test -d "2012-04-07" -u "asdf"
25
25
 
26
26
  Features by default:
27
27
 
@@ -32,7 +32,6 @@ Features by default:
32
32
  $ ./bin/bla.rb -e production --daemonize --pidfile=./tmp/bla.pid
33
33
 
34
34
 
35
-
36
35
  Example Bin
37
36
  -----------
38
37
  app/models/bin/bla_script.rb
@@ -40,8 +39,10 @@ app/models/bin/bla_script.rb
40
39
  ``` ruby
41
40
  class BlaScript < BinScript
42
41
  optional :u, "Update string"
43
- required :d, "Date in format YYYY-MM-DD or YYYY-MM"
44
- noarg :t, "Test run"
42
+ required :d, :description => "Date in format YYYY-MM-DD or YYYY-MM", :default => "2012-04-01"
43
+ noarg :t, :decription => "Test run", :alias => 'test'
44
+
45
+ self.description = "Super Bla script"
45
46
 
46
47
  def test?
47
48
  params(:t)
data/bin/bin_helper CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  APP_ROOT ||= File.expand_path(File.dirname($0)) + "/../"
5
5
 
6
- if defined?(DAEMONIZE) || ARGV.include?('--daemonize') # для демонизации бина
6
+ if defined?(DAEMONIZE) || ARGV.include?('--daemonize') # for daemonize bin
7
7
  ARGV.delete('--daemonize')
8
8
 
9
9
  # Stolen from activesupport
data/bin_script.gemspec CHANGED
@@ -5,7 +5,7 @@ Gem::Specification.new do |s|
5
5
  s.name = %q{bin_script}
6
6
  s.version = BinScript::VERSION
7
7
 
8
- s.authors = ["Lifshits Dmitry", "Makarchev Konstantin"]
8
+ s.authors = ["Makarchev Konstantin", "Lifshits Dmitry"]
9
9
  s.autorequire = %q{init}
10
10
 
11
11
  s.description = %q{Easy writing and executing bins(executable scripts) in Rails Application (especially for crontab or god)}
@@ -10,14 +10,14 @@ require File.dirname(__FILE__) + '/class_inheritable_attributes'
10
10
 
11
11
  class BinScript
12
12
  include ClassLevelInheritableAttributes
13
- class_inheritable_attributes :parameters, :log_level, :enable_locking, :enable_logging, :date_log_postfix, :disable_puts_for_tests
13
+ class_inheritable_attributes :parameters, :log_level, :enable_locking, :enable_logging, :date_log_postfix, :disable_puts_for_tests, :description
14
14
 
15
15
  # Default parameters
16
16
  @parameters = [
17
- {:key => :e, :type => :optional, :description => "Rails environment ID (default - development)"},
17
+ {:key => :e, :type => :required, :description => "Rails environment ID (default - development)"},
18
18
  {:key => :h, :type => :noarg, :description => "Print usage message", :alias => [:H, :help]},
19
- {:key => :l, :type => :optional, :description => "Path to log file (default \#{Rails.root}/log/[script_name].log"},
20
- {:key => :L, :type => :optional, :description => "Path to lock file (default \#{Rails.root}/lock/[script_name].lock"}
19
+ {:key => :l, :type => :required, :description => "Path to log file (default \#{Rails.root}/log/[script_name].log)"},
20
+ {:key => :L, :type => :required, :description => "Path to lock file (default \#{Rails.root}/locks/[script_name].lock)"}
21
21
  ]
22
22
 
23
23
  # Enable locking by default
@@ -28,16 +28,16 @@ class BinScript
28
28
 
29
29
  # Default log level INFO or DEBUG for test env
30
30
  @log_level = (RailsStub.env == 'development' ? XLogger::DEBUG : XLogger::INFO)
31
+
32
+ # Bin Description
33
+ @description = nil
31
34
 
32
- # По умолчанию мы при каждом запуске скрипта продолжаем писать в основной лог. Но можно записать сюда формат, который
33
- # можно скормить time.strftime(format) и результат подставится в конец имени файла лога. Таким образом можно делать
34
- # отдельные лог-файлы для каждого запуска, для каждого дня/месяца/года/часа и т.д...
35
- # Например "_%Y-%m-%d_%H-%M-%S" - каждый запуск новый лог
36
- # "_%Y-%m-%d" - каждый день новый лог
35
+ # By default, each bin logging into main log. Possible to specify log name for date.
36
+ # Examples: "_%Y-%m-%d_%H-%M-%S" - each execute, new log
37
+ # "_%Y-%m-%d" - each day, new log
37
38
  @date_log_postfix = ''
38
39
 
39
- # По умолчанию puts работает как обычно, но BinScript может ничего не выводить если RAILS_ENV=test
40
- # Если скрипт что-то выводит, это попадает в лог спеков, что не красиво. Так что для таких скриптов лучше включать эту опцию
40
+ # BinScript can output with puts, for specs puts is not good, this option disable puts in test env
41
41
  @disable_puts_for_tests = false
42
42
 
43
43
  # Allowed parameter types. Equivalence aliases with GetoptLong constants.
@@ -180,9 +180,17 @@ class BinScript
180
180
  def usage(message = nil)
181
181
  usage_msg = ''
182
182
  usage_msg += "Error: #{message}\n\n" unless message.nil?
183
- usage_msg += "Use: ./bin/#{script_name}.rb [OPTIONS]\n\nAvailible options:\n\n"
183
+ usage_msg += "Use: ./bin/#{script_name}.rb [OPTIONS]\n\n"
184
+ usage_msg += "\"#{self.description}\"\n\n" if message.nil? && self.description.present?
185
+ usage_msg += "Availible options:\n\n"
186
+
184
187
  @parameters.each do |param|
185
- usage_msg += " #{prefix_key param[:key]} #{param[:description]}\n"
188
+ arg = case param[:type]
189
+ when :required then " v "
190
+ when :optional then "[v]"
191
+ when :noarg then " "
192
+ end
193
+ usage_msg += " #{prefix_key param[:key]}#{arg} #{param[:description]}\n"
186
194
  end
187
195
  usage_msg += "\n"
188
196
  usage_msg
@@ -230,6 +238,17 @@ class BinScript
230
238
  usage_exit e.message
231
239
  end
232
240
  end
241
+
242
+ def check_required_params
243
+ self.class.parameters.each do |param|
244
+ if param[:type] == :required && @params_values.has_key?(param[:key])
245
+ if @params_values[param[:key]].nil?
246
+ error "Param #{param[:key]} require value, but not present"
247
+ usage_exit
248
+ end
249
+ end
250
+ end
251
+ end
233
252
 
234
253
  # Create lock file, call script code and unlock file even if error happend.
235
254
  def run!
@@ -237,6 +256,8 @@ class BinScript
237
256
  # Print usage and exit if asked
238
257
  usage_exit if params(:h)
239
258
 
259
+ check_required_params
260
+
240
261
  info "====================="
241
262
 
242
263
  # Create and check lock file if enabled
@@ -320,7 +341,6 @@ class BinScript
320
341
  when :noarg
321
342
  return (@overrided_parameters.has_key?(key) && @overrided_parameters[key]) || !@params_values[key].nil?
322
343
  when :optional
323
- #return nil unless @overrided_parameters.has_key?(key) || @params_values.has_key?(key)
324
344
  return @overrided_parameters[key] || @params_values[key] || param[:default]
325
345
  when :required
326
346
  value = @overrided_parameters[key] || @params_values[key] || param[:default]
@@ -1,4 +1,4 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  class BinScript
3
- VERSION = "0.1.5"
3
+ VERSION = "0.1.6"
4
4
  end
@@ -48,7 +48,7 @@ class XLogger < Logger
48
48
  end
49
49
 
50
50
  def prod_env?
51
- defined?(Rails) && Rails.env.production?
51
+ defined?(Rails) && Rails.env == 'production'
52
52
  end
53
53
  end
54
54
 
@@ -111,13 +111,13 @@ Use: ./bin/test_script.rb [OPTIONS]
111
111
 
112
112
  Availible options:
113
113
 
114
- -e Rails environment ID (default - development)
115
- -h Print usage message
116
- -l Path to log file (default \#{Rails.root}/log/[script_name].log
117
- -L Path to lock file (default \#{Rails.root}/lock/[script_name].lock
118
- -n Test parameter that can't have argument
119
- -o Test parameter that can have argument
120
- -r Test parameter that should have argument
114
+ -e v Rails environment ID (default - development)
115
+ -h Print usage message
116
+ -l v Path to log file (default \#{Rails.root}/log/[script_name].log)
117
+ -L v Path to lock file (default \#{Rails.root}/locks/[script_name].lock)
118
+ -n Test parameter that can't have argument
119
+ -o[v] Test parameter that can have argument
120
+ -r v Test parameter that should have argument
121
121
 
122
122
  USAGE
123
123
  TestScript.usage.should == USAGE
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bin_script
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 5
10
- version: 0.1.5
9
+ - 6
10
+ version: 0.1.6
11
11
  platform: ruby
12
12
  authors:
13
- - Lifshits Dmitry
14
13
  - Makarchev Konstantin
14
+ - Lifshits Dmitry
15
15
  autorequire: init
16
16
  bindir: bin
17
17
  cert_chain: []