easy_app_helper 4.0.3 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c2eb32cf29808bc04cc5f4e6ffbc7973631068bb
4
- data.tar.gz: 6b580342994df7d9bcfb68596dd7cd4ef936f9ae
3
+ metadata.gz: 0912cd94af9f37ef5c11dfd40e433d8930a2e125
4
+ data.tar.gz: 9dba102908c0ffb16315c684e8157381d774d972
5
5
  SHA512:
6
- metadata.gz: 006b0fa47d732de40c9e8522f55a22850cf614accf45f69a21f22f445e4458bb10516eb40ee5aedb35ca3ec1fd47518b28512939df15d03d3d63d3ffde9873aa
7
- data.tar.gz: 39264bf4170be1071a639cabc93d5d49acc4c6a8ba4e43bc4187fc1f1e8e3cdd85e888f4801ba1ef820bb3c15f591c7f653cbff3f5c6c05fd217f9a13b6dda8b
6
+ metadata.gz: ce04f5275a44c191214583c5d4a03dc4ef51a797d235eefa282b2a1763151bb927853ff6548f707d7091c25147509fd38fb130ee6e5c0eada818c3b4addb7566
7
+ data.tar.gz: 8f0e96f694852be154b10167ab73e0def6ef633f067dc1f51b78310c1c430851f6b3c524ea9dce324e0389a1edb2094b6019197118d398a4ef488990a3554176
@@ -0,0 +1,40 @@
1
+ module EasyAppHelper
2
+ module Scripts
3
+
4
+ module Common
5
+
6
+ def extra_parameters
7
+ EasyAppHelper.config.command_line_layer.extra_parameters
8
+ end
9
+
10
+ def pre_process(object=self)
11
+ safe_execution {add_script_options} if object.respond_to? :add_script_options
12
+
13
+ # logging startup configuration
14
+ logger.debug "Config layers ->\n#{config.detailed_layers_info}"
15
+ logger.debug "Merged config -> #{config[].to_yaml}"
16
+ # Displaying (and exiting) command line help
17
+ if config[:help]
18
+ puts display_help
19
+ exit 0
20
+ end
21
+ object.check_config if object.respond_to? :check_config
22
+ logger.info 'Application is starting...'
23
+ end
24
+
25
+ def safe_execution
26
+ yield if block_given?
27
+ rescue => e
28
+ puts "Program ended with message: '#{e.message}'."
29
+ if config[:debug]
30
+ logger.fatal "#{e.message}\nBacktrace:\n#{e.backtrace.join("\n\t")}"
31
+ else
32
+ STDERR.puts ' Use --debug option for more detail (see --help).'
33
+ end
34
+ exit 1
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,44 @@
1
+ require 'fileutils'
2
+ require 'digest/sha1'
3
+
4
+ module EasyAppHelper
5
+ module Scripts
6
+
7
+ module Completion
8
+
9
+ def deploy_zsh_completion_script(script, target)
10
+ FileUtils.mkpath target
11
+ puts <<EOM
12
+
13
+ ------------------------------------------------------------------------------------------
14
+ INFORMATION:
15
+ A new version of the zsh completion for '#{EasyAppHelper.config.app_name} #{EasyAppHelper.config.app_version}' has been installed.
16
+ You may want to restart a terminal to take it account.
17
+ ------------------------------------------------------------------------------------------
18
+
19
+ EOM
20
+ FileUtils.copy script, target
21
+
22
+ end
23
+
24
+ def completion_script_changed?(script, target)
25
+ script_name = File.basename script
26
+ target_script = File.join target, script_name
27
+ return true unless File.exists? target_script
28
+ sha1_source = Digest::SHA1.hexdigest File.read script
29
+ sha1_target = Digest::SHA1.hexdigest File.read target_script
30
+ sha1_source != sha1_target
31
+ end
32
+
33
+ def install_or_update_completion(script, target)
34
+ unless ENV['IGNORE_COMPLETION_UPDATE']
35
+ if File.exists?(script)
36
+ deploy_zsh_completion_script script, target if completion_script_changed? script, target
37
+ end
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,66 @@
1
+ module EasyAppHelper
2
+ module Scripts
3
+
4
+ class Master
5
+
6
+ include EasyAppHelper
7
+ include EasyAppHelper::Scripts::Common
8
+ include EasyAppHelper::Scripts::Completion
9
+
10
+ attr_reader :script_name
11
+
12
+ def initialize(app_name, app_version, app_description, config_file_base_name=nil)
13
+ @script_name = File.basename $0
14
+ config.config_file_base_name = config_file_base_name.nil? ? script_name : config_file_base_name
15
+ config.describes_application app_name: app_name,
16
+ app_version: app_version,
17
+ app_description: app_description
18
+ end
19
+
20
+ def run
21
+ safe_execution do
22
+ if sub_command_mode?
23
+ if ARGV.length == 1
24
+ %w(--help -h --version).each do |option|
25
+ if ARGV.include? option
26
+ if option == '--version'
27
+ puts DeploymentManager::VERSION
28
+ else
29
+ puts display_help
30
+ end
31
+ exit 0
32
+ end
33
+ end
34
+ end
35
+ if ARGV.empty?
36
+ puts display_help
37
+ exit 0
38
+ end
39
+ delegate_to_sub_command
40
+ else
41
+ pre_process
42
+ do_process
43
+ end
44
+ logger.info 'Application terminates successfully...'
45
+ exit 0
46
+ end
47
+ end
48
+
49
+ def display_help
50
+ config.command_line_help
51
+ end
52
+
53
+ def do_process
54
+ raise 'Please implement do_process in your action !'
55
+ end
56
+
57
+ private
58
+
59
+ def sub_command_mode?
60
+ self.class.included_modules.include? EasyAppHelper::Scripts::SubCommandManager
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,22 @@
1
+ module EasyAppHelper
2
+ module Scripts
3
+
4
+ module ParametersHelper
5
+
6
+ # Allows to specify a Ruby Symbol as a string. Supposed to be used to pass
7
+ # symbols from the command line.
8
+ # @param [String] param A string coming normally from the command line.
9
+ # @return [String or Symbol] if param starts with a colon, then it the
10
+ # returns a symbol, ie: ':foo' returns :foo (the Symbol)
11
+ # Else it will return the param itself. 'bar' returns 'bar' (the String)
12
+ def normalize_param(param)
13
+ param.match(/^:(?<param>.+)$/) do |md|
14
+ param = md['param'].to_sym
15
+ end
16
+ param
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,51 @@
1
+ module EasyAppHelper
2
+ module Scripts
3
+
4
+ module SubCommandBase
5
+
6
+ include EasyAppHelper
7
+ include EasyAppHelper::Scripts::Common
8
+
9
+ PROVIDER = 'Core'
10
+ NAME = ''
11
+ DESCRIPTION = ''
12
+ CATEGORY = ''
13
+ ALIASES = []
14
+
15
+ def self.included(base)
16
+ EasyAppHelper::Scripts::SubCommandManager.register base
17
+ base.extend ClassMethods
18
+ end
19
+
20
+ def command_parameters
21
+ params = extra_parameters
22
+ command = params.shift
23
+ raise 'Something weird happened !!' unless command == self.class::NAME or self.class::ALIASES.include? command
24
+ params
25
+ end
26
+
27
+ def do_process
28
+ raise "Process for '#{name}' in '#{self::PROVIDER}' not implemented !"
29
+ end
30
+
31
+ def display_help
32
+ config.app_description = self.class::DESCRIPTION
33
+ config.command_line_help
34
+ end
35
+
36
+ module ClassMethods
37
+
38
+ def help_line
39
+ line = ' * %-10s : %s' % [self::NAME, self::DESCRIPTION]
40
+ unless self::ALIASES.nil? or self::ALIASES.empty?
41
+ line += ' (aliases: %s).' % [ self::ALIASES.join(', ') ]
42
+ end
43
+ line
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,94 @@
1
+ module EasyAppHelper
2
+ module Scripts
3
+
4
+ module SubCommandManager
5
+
6
+ def self.register(sub_command_class)
7
+ raise 'Please specify a sub_command class when registering' if sub_command_class.nil?
8
+ raise "Already registered sub_command '#{sub_command_class.to_s}' !" if sub_command_classes.include? sub_command_class
9
+ EasyAppHelper.logger.debug "Registering handler '#{sub_command_class.to_s}' for sub-command '#{sub_command_class::NAME}'"
10
+ sub_command_classes << sub_command_class
11
+ by_provider[sub_command_class::PROVIDER] ||= []
12
+ raise 'A provider cannot provide the same sub-command multiple times' if by_provider[sub_command_class::PROVIDER].include?(sub_command_class)
13
+ by_provider[sub_command_class::PROVIDER] << sub_command_class
14
+ by_name[sub_command_class::NAME] ||= []
15
+ by_name[sub_command_class::NAME] << sub_command_class
16
+ sub_command_class::ALIASES.each do |command_alias|
17
+ by_name[command_alias] ||= []
18
+ by_name[command_alias] << sub_command_class
19
+ end
20
+ end
21
+
22
+ def self.sub_command_classes
23
+ @sub_command_classes ||= []
24
+ end
25
+
26
+ def self.by_provider
27
+ @by_provider ||= {}
28
+ end
29
+
30
+ def self.by_name
31
+ @by_name ||= {}
32
+ end
33
+
34
+ def self.sub_command_class(command_name_or_alias, provider=EasyAppHelper::Scripts::SubCommandBase::PROVIDER)
35
+ candidates = by_provider[provider]
36
+ raise "There is no provider declared for command '#{command_name_or_alias}'" if candidates.nil?
37
+ candidates.select! do |sub_command_class|
38
+ command_classes_for_command = by_name[command_name_or_alias]
39
+ raise "There is no provider declared for command '#{command_name_or_alias}'" if command_classes_for_command.nil?
40
+ command_classes_for_command.include? sub_command_class
41
+ end
42
+ raise "Cannot determine provider to use for '#{command_name_or_alias}'. Multiple providers exist !" unless candidates.size == 1
43
+ candidates.first
44
+ end
45
+
46
+ def delegate_to_sub_command(provider = EasyAppHelper::Scripts::SubCommandBase::PROVIDER)
47
+ sub_command_name = extra_parameters.shift
48
+ sub_command = EasyAppHelper::Scripts::SubCommandManager.sub_command_class(sub_command_name, provider).new
49
+ sub_command.pre_process
50
+ raise 'You have to implement \'do_process\'' unless sub_command.respond_to? :do_process
51
+ sub_command.do_process
52
+ end
53
+
54
+
55
+ def display_help
56
+ result = [default_header]
57
+
58
+ EasyAppHelper::Scripts::SubCommandManager.sub_command_classes.group_by do |sub_command_classes|
59
+ sub_command_classes::CATEGORY
60
+ end .each_pair do |category, sub_command_classes_for_category|
61
+ result << (' %s:' % category)
62
+ result << ''
63
+ sub_command_classes_for_category.each do |sub_command_class|
64
+ result << sub_command_class.help_line
65
+ end
66
+ result << ''
67
+ end
68
+ result
69
+ end
70
+
71
+ def default_header
72
+ <<EOF
73
+
74
+ This is the '#{script_name}' tool version #{config.app_version} (AKA '#{self.class::NAME}').
75
+ #{self.class::DESCRIPTION}
76
+
77
+ It has some sub-commands, each taking its own options.
78
+
79
+ You can do '#{script_name} <sub-command> --help' for more information on sub-modules.
80
+
81
+ Options
82
+
83
+ --help/-h : This help.
84
+ --version : Echoes dm version
85
+
86
+ Sub-commands:
87
+
88
+ EOF
89
+ end
90
+
91
+ end
92
+
93
+ end
94
+ end
@@ -0,0 +1,6 @@
1
+ require 'easy_app_helper/scripts/common'
2
+ require 'easy_app_helper/scripts/completion'
3
+ require 'easy_app_helper/scripts/parameters_helper'
4
+ require 'easy_app_helper/scripts/sub_command_base'
5
+ require 'easy_app_helper/scripts/sub_command_manager'
6
+ require 'easy_app_helper/scripts/master'
@@ -1,3 +1,3 @@
1
1
  module EasyAppHelper
2
- VERSION = '4.0.3'
2
+ VERSION = '4.1.0'
3
3
  end
@@ -7,65 +7,40 @@ require 'rubygems'
7
7
  require 'bundler/setup'
8
8
 
9
9
  require 'easy_app_helper'
10
+ require 'easy_app_helper/scripts'
10
11
  require '<%= gem_name %>'
11
12
 
12
13
  module <%= gem_module %>
13
- class <%= script_class %>
14
+ class <%= script_class %> < EasyAppHelper::Scripts::Master
15
+
16
+ # If you want to manage sub-commands a-la-git, include following
17
+ # include EasyAppHelper::Scripts::SubCommandManager
14
18
 
15
19
  # Inserted here, but you may probably have already defined this somewhere else...
16
20
  NAME = '<%= executable_name.titleize %>'
17
21
  DESCRIPTION = 'Description of <%= executable_name %>'
18
22
 
19
- include EasyAppHelper
20
-
21
23
  def initialize
22
- <% unless executable_name == gem_name -%>config.config_file_base_name = '<%= executable_name %>'
23
- <% end -%>config.describes_application app_name: NAME,
24
- app_version: <%= gem_module %>::VERSION,
25
- app_description: DESCRIPTION
26
- add_script_options
27
- end
28
-
29
- def run
30
- # logging startup configuration
31
- logger.debug "Config layers ->\n#{config.detailed_layers_info}"
32
- logger.debug "Merged config -> #{config[].to_yaml}"
33
- # Displaying (and exiting) command line help
34
- if config[:help]
35
- puts config.command_line_help
36
- exit 0
37
- end
38
- check_config
39
- logger.info 'Application is starting...'
40
- do_process
41
- rescue => e
42
- puts "Program aborted with message: '#{e.message}'."
43
- if config[:debug]
44
- logger.fatal "#{e.message}\nBacktrace:\n#{e.backtrace.join("\n\t")}"
45
- else
46
- puts ' Use --debug option for more detail (see --help).'
47
- end
48
- end
49
-
50
- private
51
-
52
- def add_script_options
53
- ## Create here your extra command-line options
54
- ## Here under are examples using potentially gem config layer to display default option value...
55
- ## Check Slop documentation for further info.
56
- # config.add_command_line_section do |slop|
57
- # slop.on :p, :port, "Specify port to bind to. Default #{config.executable_gem_layer[:port]}.", argument: true, as: Integer
58
- # slop.on :b, :bind, "Specify address to bind to. Default #{config.executable_gem_layer[:bind]}.", argument: true, as: String
59
- # end
24
+ super(NAME, <%= gem_module %>::VERSION, DESCRIPTION<% unless executable_name == gem_name -%>, '<%= executable_name %>'<% end -%>)
60
25
  end
61
26
 
62
- def do_process
63
- # Your code here.
64
- end
65
-
66
- def check_config
67
- # Check the config and raise an exception if incorrect.
68
- end
27
+ # def add_script_options
28
+ # ## Create here your extra command-line options
29
+ # ## Here under are examples using potentially gem config layer to display default option value...
30
+ # ## Check Slop documentation for further info.
31
+ # # config.add_command_line_section do |slop|
32
+ # # slop.on :p, :port, "Specify port to bind to. Default #{config.executable_gem_layer[:port]}.", argument: true, as: Integer
33
+ # # slop.on :b, :bind, "Specify address to bind to. Default #{config.executable_gem_layer[:bind]}.", argument: true, as: String
34
+ # # end
35
+ # end
36
+
37
+ # def do_process
38
+ # # Your code here.
39
+ # end
40
+
41
+ # def check_config
42
+ # # Check the config and raise an exception if incorrect.
43
+ # end
69
44
 
70
45
  end
71
46
  end
@@ -29,7 +29,7 @@ module EasyAppHelper
29
29
  @gem_module = @gem_name.camelize
30
30
  @current_date = Time.now.strftime('%c')
31
31
  @script_class = executable_name == current_gem_spec.name ? '' : executable_name.camelize
32
- @script_class << 'Script'
32
+ @script_class << 'MasterScript'
33
33
  renderer = ERB.new(File.read(TEMPLATE), nil, '-')
34
34
  renderer.result binding
35
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_app_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.3
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - L.Briais
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-12 00:00:00.000000000 Z
11
+ date: 2016-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,6 +122,13 @@ files:
122
122
  - lib/easy_app_helper/processes/command.rb
123
123
  - lib/easy_app_helper/processes/synchronous.rb
124
124
  - lib/easy_app_helper/processes/time_management.rb
125
+ - lib/easy_app_helper/scripts.rb
126
+ - lib/easy_app_helper/scripts/common.rb
127
+ - lib/easy_app_helper/scripts/completion.rb
128
+ - lib/easy_app_helper/scripts/master.rb
129
+ - lib/easy_app_helper/scripts/parameters_helper.rb
130
+ - lib/easy_app_helper/scripts/sub_command_base.rb
131
+ - lib/easy_app_helper/scripts/sub_command_manager.rb
125
132
  - lib/easy_app_helper/tasks.rb
126
133
  - lib/easy_app_helper/version.rb
127
134
  - lib/tasks/easy_app_helper_tasks.rake