easy_app_helper 0.0.9 → 1.0.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.
@@ -0,0 +1,52 @@
1
+ ################################################################################
2
+ # EasyAppHelper
3
+ #
4
+ # Copyright (c) 2013 L.Briais under MIT license
5
+ # http://opensource.org/licenses/MIT
6
+ ################################################################################
7
+
8
+ # Possible places regarding the OS
9
+ # TODO: Add equivalent for Mac
10
+ class EasyAppHelper::Core::Config::Places
11
+ module Unix
12
+ # Where could be stored admin configuration that rules all EasyAppHelper
13
+ # based applications.
14
+ POSSIBLE_PLACES = {
15
+
16
+ system: ["/etc"],
17
+
18
+ # Where could be stored global wide configuration
19
+ global: ["/etc",
20
+ "/usr/local/etc"],
21
+
22
+ # Where could be stored user configuration
23
+ user: ["#{ENV['HOME']}/.config"]
24
+ }
25
+ end
26
+
27
+ module Windows
28
+ # Where could be stored admin configuration that rules all EasyAppHelper
29
+ # based applications.
30
+ POSSIBLE_PLACES = {
31
+
32
+ system: ["#{ENV['systemRoot']}/Config"],
33
+
34
+ # Where could be stored global configuration
35
+ global: ['C:/Windows/Config',
36
+ "#{ENV['ALLUSERSPROFILE']}/Application Data"],
37
+
38
+ # Where could be stored user configuration
39
+ user: [ENV['APPDATA']]
40
+ }
41
+ end
42
+
43
+ CONF ={
44
+ mingw32: Windows
45
+ }
46
+ DEFAULT = Unix
47
+
48
+ def self.get_OS_module
49
+ conf = CONF[RbConfig::CONFIG['target_os'].to_sym]
50
+ conf.nil? ? DEFAULT : conf
51
+ end
52
+ end
@@ -0,0 +1,61 @@
1
+ ################################################################################
2
+ # EasyAppHelper
3
+ #
4
+ # Copyright (c) 2013 L.Briais under MIT license
5
+ # http://opensource.org/licenses/MIT
6
+ ################################################################################
7
+
8
+ module EasyAppHelper::Core
9
+
10
+ end
11
+
12
+ require 'easy_app_helper/core/logger'
13
+ require 'easy_app_helper/core/base'
14
+ require 'easy_app_helper/core/config'
15
+
16
+ # This module contains the exposed methods of the framework
17
+ # It is included and extended into EasyAppHelper
18
+ module EasyAppHelper::ModuleManager
19
+
20
+ # @return [EasyAppHelper::Core::Logger] The application logger
21
+ def logger
22
+ @@logger
23
+ end
24
+
25
+ # @return [EasyAppHelper::Core::Config] The application config
26
+ def config
27
+ @@config
28
+ end
29
+
30
+ # Convenient method that logs at info level, but also outputs the message to STDOUT if
31
+ # verbose is set in the config.
32
+ # @param [String] msg to be displayed
33
+ def puts_and_logs(msg)
34
+ @@logger.puts_and_logs msg
35
+ end
36
+
37
+ def self.included(base)
38
+ init_core_modules
39
+ base.extend self
40
+ end
41
+
42
+ ################################################################################
43
+ private
44
+
45
+ def self.init_logger
46
+ @@logger ||= EasyAppHelper::Core::Logger.instance
47
+ @@logger
48
+ end
49
+
50
+ def self.init_config
51
+ @@config ||= EasyAppHelper::Core::Config.new @@logger
52
+ @@logger.set_app_config(@@config)
53
+ @@config
54
+ end
55
+
56
+ def self.init_core_modules
57
+ init_logger
58
+ init_config
59
+ end
60
+
61
+ end
@@ -6,6 +6,5 @@
6
6
  ################################################################################
7
7
 
8
8
  module EasyAppHelper
9
- # Framework version
10
- EASY_APP_HELPER_VERSION = "0.0.9"
9
+ EASY_APP_HELPER_VERSION = "1.0.0"
11
10
  end
@@ -5,21 +5,16 @@
5
5
  # http://opensource.org/licenses/MIT
6
6
  ################################################################################
7
7
 
8
- require "easy_app_helper/version"
9
- require "easy_app_helper/common"
10
- require "easy_app_helper/base"
11
- require 'easy_app_helper/places'
12
- require "easy_app_helper/config"
13
- require "easy_app_helper/logger"
8
+ require 'easy_app_helper/version'
14
9
 
15
-
16
- # This module will provide helpers for your applications. Currently supported
17
- # modules are:
18
- # - EasyAppHelper::Base included by default.
19
- # - EasyAppHelper::Logger adds logging capabilities to your scripts.
20
- # - EasyAppHelper::Config provides a consistent configuration framework.
10
+ # When this module is included in any class, it mixes in automatically
11
+ # EasyAppHelper::ModuleManager methods both into the
12
+ # instance and the class of the instance that includes it.
13
+ # Thus to have access to the helper methods, the only requirement is to include
14
+ # this module...
21
15
  module EasyAppHelper
22
- include EasyAppHelper::Base
16
+ require 'easy_app_helper/module_manager'
17
+ include ModuleManager
23
18
  end
24
19
 
25
20
 
data/test/test.yml ADDED
@@ -0,0 +1,7 @@
1
+ stupid_config: true
2
+
3
+ pipo: bimbo
4
+
5
+ hash_example:
6
+ first: one
7
+ second: two
data/test/test2_app.rb ADDED
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ require 'easy_app_helper'
5
+
6
+
7
+ # EasyAppHelper.logger.level = 0
8
+ EasyAppHelper.puts_and_logs "Groovy baby !"
9
+ EasyAppHelper.config[:zboubi] = "Hi shared"
10
+
11
+ class A
12
+ include EasyAppHelper
13
+
14
+ def echo
15
+ puts_and_logs config[:zboubi]
16
+ end
17
+ end
18
+
19
+ A.new.echo
20
+ EasyAppHelper.puts_and_logs EasyAppHelper.config[:zboubi]
21
+
22
+ include EasyAppHelper
23
+ puts_and_logs "ZBOUBI: #{config[:zboubi]}"
24
+ config.reset
25
+ puts_and_logs "ZBOUBI2: #{config[:zboubi]}"
26
+
27
+ puts config.to_yaml
28
+ config.script_filename = 'batch_audio_convert'
29
+ puts 'Internal configs'
30
+ puts config.internal_configs.to_yaml
31
+ puts 'Resulting config'
32
+ puts config.to_yaml
33
+
data/test/test3_app.rb ADDED
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'easy_app_helper'
4
+
5
+ # You can directly access the config or the logger through the EasyAppHelper module
6
+ puts "The application verbose flag is #{EasyAppHelper.config[:verbose]}"
7
+
8
+ # You can directly use the logger according to the command line flags
9
+ # This will do nothing unless --debug is set and --log-level is set to the correct level
10
+ EasyAppHelper.logger.info "Hi guys!"
11
+
12
+ # Fed up with the EasyAppHelper prefix ? Just include the module where you want
13
+ include EasyAppHelper
14
+
15
+ # You can override programmatically any part of the config
16
+ config[:debug] = true
17
+ logger.level = 1
18
+ config[:test] = 'Groovy'
19
+ EasyAppHelper.logger.info "Hi guys!... again"
20
+
21
+ # You can see the internals of the config
22
+ puts config.internal_configs.to_yaml
23
+ # Which will output
24
+ #:modified:
25
+ # :content:
26
+ # :log-level: 1
27
+ # :debug: true
28
+ # :test: cool
29
+ # :source: Changed by code
30
+ #:command_line:
31
+ # :content:
32
+ # :auto:
33
+ # :simulate:
34
+ # :verbose: true
35
+ # :help:
36
+ # :config-file:
37
+ # :config-override:
38
+ # :debug:
39
+ # :debug-on-err:
40
+ # :log-level:
41
+ # :log-file:
42
+ # :source: Command line
43
+ #:system:
44
+ # :content: {}
45
+ # :source:
46
+ # :origin: EasyAppHelper
47
+ #:global:
48
+ # :content: {}
49
+ # :source:
50
+ # :origin: ''
51
+ #:user:
52
+ # :content: {}
53
+ # :source:
54
+ # :origin: ''
55
+ #:specific_file:
56
+ # :content: {}
57
+
58
+ # You see of course that the two modifications we did are in the modified sub-hash
59
+ # And now the merged config
60
+ puts config.to_hash
61
+
62
+ # But you can see the modified part as it is:
63
+ puts config.internal_configs[:modified]
64
+
65
+ # Of course you can access it from any class
66
+ class Dummy
67
+ include EasyAppHelper
68
+
69
+ def initialize
70
+ puts "#{config[:test]} baby !"
71
+ # Back to the original
72
+ config.reset
73
+ puts config.internal_configs[:modified]
74
+ end
75
+ end
76
+
77
+ Dummy.new
78
+
79
+ # Some methods are provided to ease common tasks. For example this one will log at info level
80
+ # (so only displayed if debug mode and log level low enough), but will also puts on the console
81
+ # if verbose if set...
82
+ puts_and_logs "Hi world"
83
+
84
+ # It is actually one of the few methods added to regular Logger class (The added value of this logger
85
+ # is much more to be tightly coupled with the config object). Thus could access it like that:
86
+ logger.puts_and_logs "Hi world"
87
+
88
+ # or even
89
+ EasyAppHelper.logger.puts_and_logs "Hi world... 3 is enough."
90
+
data/test/test4_app.rb ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'easy_app_helper'
4
+
5
+ class MyApp
6
+ include EasyAppHelper
7
+
8
+ APP_NAME = "My super application"
9
+ # SCRIPT_NAME = File.basename($0, '.*')
10
+ VERSION = '0.0.1'
11
+ DESCRIPTION = 'This application is a proof of concept for EasyAppHelper.'
12
+
13
+
14
+ def initialize
15
+ # Providing this data is optional
16
+ config.describes_application(app_name: APP_NAME, app_version: VERSION, app_description: DESCRIPTION)
17
+ end
18
+
19
+
20
+ def run
21
+ if config[:help]
22
+ puts config.help
23
+ exit 0
24
+ end
25
+ puts_and_logs "Application is starting"
26
+ do_some_processing
27
+ end
28
+
29
+ def do_some_processing
30
+ puts_and_logs "Starting some heavy processing"
31
+ end
32
+
33
+ end
34
+
35
+
36
+ MyApp.new.run
data/test/test_app.rb ADDED
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ require 'easy_app_helper'
5
+
6
+
7
+ class Pipo
8
+ include EasyAppHelper
9
+
10
+
11
+ def toto
12
+ puts_and_logs "Now from a second class"
13
+ puts_and_logs config.to_hash
14
+ end
15
+ end
16
+
17
+
18
+
19
+ class TestApp
20
+ include EasyAppHelper
21
+
22
+ def initialize
23
+ #config.script_filename = File.basename $0
24
+ config.app_name = 'Test application'
25
+ #config.app_description = 'Best app to test the framework'
26
+ #config.app_version = '1.0.0'
27
+ puts_and_logs "Hello World"
28
+ p = Pipo.new.toto
29
+ config.script_filename = 'batch_audio_convert'
30
+ puts config.to_hash
31
+ config.internal_configs.each do |layer|
32
+ puts layer
33
+ end
34
+ add_cmd_line_options
35
+ puts config.help
36
+ logger.error "INTARG: #{config[:intarg]}"
37
+
38
+ # puts config.help
39
+ end
40
+
41
+ def add_cmd_line_options
42
+ config.add_command_line_section do |slop|
43
+ slop.on :s, :stupid, 'Stupid option', :argument => false
44
+ slop.on :i, :intarg, 'Stupid option with integer argument', :argument => true, :as => Integer
45
+ end
46
+ end
47
+
48
+ end
49
+
50
+ t = TestApp.new
51
+ include EasyAppHelper
52
+ logger.warn "Yeah"
53
+ EasyAppHelper.logger.error "Groovy baby !"
54
+ puts_and_logs "Hey man"
55
+ #puts config.inspect
56
+ puts "bye"
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: 0.0.9
4
+ version: 1.0.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: 2013-06-06 00:00:00.000000000 Z
11
+ date: 2013-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,12 +80,18 @@ files:
80
80
  - Rakefile
81
81
  - easy_app_helper.gemspec
82
82
  - lib/easy_app_helper.rb
83
- - lib/easy_app_helper/base.rb
84
- - lib/easy_app_helper/common.rb
85
- - lib/easy_app_helper/config.rb
86
- - lib/easy_app_helper/logger.rb
87
- - lib/easy_app_helper/places.rb
83
+ - lib/easy_app_helper/core/base.rb
84
+ - lib/easy_app_helper/core/config.rb
85
+ - lib/easy_app_helper/core/logger.rb
86
+ - lib/easy_app_helper/core/merge_policies.rb
87
+ - lib/easy_app_helper/core/places.rb
88
+ - lib/easy_app_helper/module_manager.rb
88
89
  - lib/easy_app_helper/version.rb
90
+ - test/test.yml
91
+ - test/test2_app.rb
92
+ - test/test3_app.rb
93
+ - test/test4_app.rb
94
+ - test/test_app.rb
89
95
  homepage: https://github.com/lbriais/easy_app_helper
90
96
  licenses:
91
97
  - MIT
@@ -111,5 +117,10 @@ signing_key:
111
117
  specification_version: 4
112
118
  summary: Provides cool helpers to your application, including configuration and logging
113
119
  features
114
- test_files: []
120
+ test_files:
121
+ - test/test.yml
122
+ - test/test2_app.rb
123
+ - test/test3_app.rb
124
+ - test/test4_app.rb
125
+ - test/test_app.rb
115
126
  has_rdoc:
@@ -1,211 +0,0 @@
1
- ################################################################################
2
- # EasyAppHelper
3
- #
4
- # Copyright (c) 2013 L.Briais under MIT license
5
- # http://opensource.org/licenses/MIT
6
- ################################################################################
7
-
8
- require 'slop'
9
-
10
- # Once this module is included in your class which may be in the "application class",
11
- # you probably want to call the init_app_helper method as first statement of your
12
- # initialize method.
13
- #
14
- # This module provides some basic command line options ({using the slop gem}[https://rubygems.org/gems/slop])
15
- # and
16
- # initializes all included EasyAppHelper modules (which may themselves add their
17
- # own command line options). Calling the init_app_helper method is the only thing
18
- # required to instanciate the whole framework.
19
- #
20
- # You can access to any command line option value through the app_config hash attribute.
21
- #
22
- # If you want to add any extra command line option to the one provided by default
23
- # by this module or by any other EasyAppHelper modules, then your class just needs
24
- # to respond to add_specifc_command_line_options(opt).
25
- # The opt object is a Slop object that enables to define any extra command line
26
- # options.
27
- #
28
- # Passing the --help option to the program will cause this module to display the
29
- # inline help and to exit.
30
- module EasyAppHelper::Base
31
-
32
- include EasyAppHelper::Common
33
-
34
-
35
- @app_config = {}
36
-
37
- # Gives access to the application config. This should be the only attribute you will
38
- # use once this module is fully configured.
39
- attr_accessor :app_config
40
-
41
- # Gives access to the initial command line options. Just for information as already
42
- # merged into the app_config.
43
- def app_cmd_line_options
44
- @slop_definition.nil? ? {} : @slop_definition.to_hash
45
- end
46
-
47
- # This method initializes the whole EasyAppHelper helpers framework.
48
- # Even if only the first parameter is mandatory, you may pass all of them if you want
49
- # consistent inline help to be displayed.
50
- def init_app_helper(script_filename,
51
- app_name="Undefined application name",
52
- app_description="No description available",
53
- app_version="unknown")
54
- # A brand new config is created.
55
- @app_config = {}
56
- # Initialize default options, and creates @slop_definition.
57
- build_default_command_line_options script_filename, app_name, app_description, app_version
58
- # Process all actions on EasyAppHelper modules that change app_config or add command
59
- # line options.
60
- process_helpers_instanciators script_filename, app_name, app_description, app_version
61
- # Provides possibility to the script writer to add its own command line options
62
- add_script_specific_cmd_line_options
63
- # Parses command line options, and merges with the app_config
64
- merge_cmd_line_options_into_config
65
- # Performs modules entry points
66
- process_helpers_post_config_actions
67
- # check if help is requested.
68
- check_help_option_invoked
69
- # Log useful debug info
70
- log_debug_info
71
- end
72
-
73
-
74
-
75
- # Returns the text of the inline help. Generated by Slop.
76
- def help
77
- @slop_definition.to_s
78
- end
79
-
80
- # Provides access to a standard logger, fully configured according to command line options
81
- # or config files.
82
- def logger
83
- return @logger unless @logger.nil?
84
- return EasyAppHelper::Common::DummyLogger.instance
85
- end
86
-
87
-
88
- ################################################################################
89
- private
90
-
91
- # Checks EasyAppHelper modules and perform some actions on them.
92
- # - Does it add some command line options? Then add them by calling add_cmd_line_options
93
- # - Does it modify app_config? Then call provides_config.
94
- def process_helpers_instanciators(script_filename, app_name, app_description, app_version)
95
- begin
96
- if (ENV['DEBUG_EASY_MODULES'])
97
- @logger = Logger.new(STDOUT)
98
- @logger.level = Logger::Severity::DEBUG
99
- end
100
- process_helper_modules_instanciators do |mod|
101
- # Command line: Does the module add some command line options ?
102
- if mod.respond_to? :add_cmd_line_options
103
- mod.add_cmd_line_options self, @slop_definition
104
- end
105
- # Does the module modify the global configuration
106
- if mod.respond_to? :provides_config
107
- module_config = mod.provides_config self, script_filename, app_name, app_description, app_version
108
- @app_config = EasyAppHelper::Common.override_config app_config, module_config
109
- end
110
- end
111
- ensure
112
- unless @logger.nil?
113
- @logger = nil
114
- end
115
- end
116
- end
117
-
118
- # Call each post config action for modules if any.
119
- def process_helpers_post_config_actions
120
- process_helper_modules_instanciators do |mod|
121
- if mod.respond_to? :post_config_action
122
- mod.post_config_action self
123
- end
124
- end
125
- end
126
-
127
-
128
-
129
-
130
-
131
- # Utility method to process EasyAppHelper modules in order of priority.
132
- def process_helper_modules_instanciators
133
- self.class.included_modules
134
- .map {|mod| mod if mod.name =~ /^EasyAppHelper::/}
135
- .compact
136
- .map {|mod| mod::Instanciator}
137
- .sort {|a,b| a::MODULE_PRIORITY <=> b::MODULE_PRIORITY }
138
- .each {|mod| logger.debug "Processing helper module: #{mod.name}"; yield mod}
139
- end
140
-
141
- def merge_cmd_line_options_into_config
142
- @slop_definition.parse!
143
- @app_config = EasyAppHelper::Common.override_config app_config, @slop_definition.to_hash
144
- end
145
-
146
- # Builds common used command line options
147
- def build_default_command_line_options(script_filename, app_name, app_description, app_version)
148
- # Default options
149
- @slop_definition = Slop.new do
150
- banner "\nUsage: #{script_filename} [options]\n#{app_name} Version: #{app_version}\n\n#{app_description}"
151
- separator "-- Generic options -------------------------------------------"
152
- on :auto, 'Auto mode. Bypasses questions to user.', :argument => false
153
- on :simulate, 'Do not perform the actual underlying actions.', :argument => false
154
- on :v, :verbose, 'Enable verbose mode.', :argument => false
155
- on :h, :help, 'Displays this help.', :argument => false
156
- end
157
- end
158
-
159
- # Hook for the script to add its own command line options
160
- #
161
- # Just need to implement a add_specifc_command_line_options method that tal=ke a slop object as parameter
162
- def add_script_specific_cmd_line_options
163
- if self.respond_to? :add_specifc_command_line_options
164
- @slop_definition.separator "\n-- Script specific options------------------------------------"
165
- self.add_specifc_command_line_options @slop_definition
166
- end
167
- end
168
-
169
- # Check if the help command line option has been passed and if the case,
170
- # display the inline help and exits the program.
171
- def check_help_option_invoked
172
- return unless app_config[:help]
173
- puts self.help
174
- exit 0
175
- end
176
-
177
- # Displays useful debug info after all EasyAppHelper modules initialization.
178
- def log_debug_info
179
- return unless app_config[:debug] || app_config[:'debug-on-err']
180
- size = ENV.keys.map {|k| k.length }.max + 1
181
- logger.debug '-' * 80
182
- logger.debug " Script environment variables"
183
- logger.debug '-' * 80
184
- # print formatted environment
185
- ENV.sort.each {|k,v| logger.debug "- %-#{size}s %s" % ["#{k}:", v] }
186
- logger.debug '-' * 80
187
- logger.debug " Script Ruby load path"
188
- logger.debug '-' * 80
189
- $:.each {|p| logger.debug p}
190
- logger.debug '-' * 80
191
- logger.debug " Script command line parameters"
192
- logger.debug '-' * 80
193
- size = app_cmd_line_options.to_hash.keys.map {|k| k.length }.max + 1
194
- app_cmd_line_options.to_hash.sort.each {|k,v| logger.debug "- %-#{size}s %s" % ["#{k}:", v] }
195
- logger.debug '-' * 80
196
- logger.debug " Script global configuration"
197
- logger.debug '-' * 80
198
- logger.debug "\n" + app_config.to_yaml
199
- logger.debug '-' * 80
200
- end
201
-
202
- end
203
-
204
-
205
- module EasyAppHelper::Base::Instanciator
206
- extend EasyAppHelper::Common::Instanciator
207
-
208
- # Default module priority
209
- MODULE_PRIORITY = 10
210
-
211
- end