csd 0.0.15 → 0.0.16

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.
Files changed (256) hide show
  1. data/.gitignore +1 -0
  2. data/COPYING +367 -0
  3. data/Rakefile +10 -10
  4. data/VERSION +1 -1
  5. data/bin/ai +19 -0
  6. data/csd.gemspec +257 -35
  7. data/lib/active_support.rb +75 -0
  8. data/lib/active_support/all.rb +3 -0
  9. data/lib/active_support/backtrace_cleaner.rb +94 -0
  10. data/lib/active_support/base64.rb +42 -0
  11. data/lib/active_support/basic_object.rb +21 -0
  12. data/lib/active_support/benchmarkable.rb +60 -0
  13. data/lib/active_support/buffered_logger.rb +132 -0
  14. data/lib/active_support/builder.rb +6 -0
  15. data/lib/active_support/cache.rb +626 -0
  16. data/lib/active_support/cache/compressed_mem_cache_store.rb +13 -0
  17. data/lib/active_support/cache/file_store.rb +188 -0
  18. data/lib/active_support/cache/mem_cache_store.rb +191 -0
  19. data/lib/active_support/cache/memory_store.rb +159 -0
  20. data/lib/active_support/cache/strategy/local_cache.rb +164 -0
  21. data/lib/active_support/cache/synchronized_memory_store.rb +11 -0
  22. data/lib/active_support/callbacks.rb +600 -0
  23. data/lib/active_support/concern.rb +29 -0
  24. data/lib/active_support/configurable.rb +36 -0
  25. data/lib/active_support/core_ext.rb +3 -0
  26. data/lib/active_support/core_ext/array.rb +7 -0
  27. data/lib/active_support/core_ext/array/access.rb +46 -0
  28. data/lib/active_support/core_ext/array/conversions.rb +164 -0
  29. data/lib/active_support/core_ext/array/extract_options.rb +29 -0
  30. data/lib/active_support/core_ext/array/grouping.rb +100 -0
  31. data/lib/active_support/core_ext/array/random_access.rb +20 -0
  32. data/lib/active_support/core_ext/array/uniq_by.rb +17 -0
  33. data/lib/active_support/core_ext/array/wrap.rb +22 -0
  34. data/lib/active_support/core_ext/benchmark.rb +7 -0
  35. data/lib/active_support/core_ext/big_decimal.rb +1 -0
  36. data/lib/active_support/core_ext/big_decimal/conversions.rb +27 -0
  37. data/lib/active_support/core_ext/cgi.rb +1 -0
  38. data/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb +19 -0
  39. data/lib/active_support/core_ext/class.rb +4 -0
  40. data/lib/active_support/core_ext/class/attribute.rb +67 -0
  41. data/lib/active_support/core_ext/class/attribute_accessors.rb +63 -0
  42. data/lib/active_support/core_ext/class/delegating_attributes.rb +44 -0
  43. data/lib/active_support/core_ext/class/inheritable_attributes.rb +232 -0
  44. data/lib/active_support/core_ext/class/subclasses.rb +55 -0
  45. data/lib/active_support/core_ext/date/acts_like.rb +8 -0
  46. data/lib/active_support/core_ext/date/calculations.rb +240 -0
  47. data/lib/active_support/core_ext/date/conversions.rb +99 -0
  48. data/lib/active_support/core_ext/date/freeze.rb +31 -0
  49. data/lib/active_support/core_ext/date_time/acts_like.rb +13 -0
  50. data/lib/active_support/core_ext/date_time/calculations.rb +113 -0
  51. data/lib/active_support/core_ext/date_time/conversions.rb +102 -0
  52. data/lib/active_support/core_ext/date_time/zones.rb +17 -0
  53. data/lib/active_support/core_ext/enumerable.rb +119 -0
  54. data/lib/active_support/core_ext/exception.rb +3 -0
  55. data/lib/active_support/core_ext/file.rb +2 -0
  56. data/lib/active_support/core_ext/file/atomic.rb +41 -0
  57. data/lib/active_support/core_ext/file/path.rb +5 -0
  58. data/lib/active_support/core_ext/float.rb +1 -0
  59. data/lib/active_support/core_ext/float/rounding.rb +19 -0
  60. data/lib/active_support/core_ext/hash.rb +8 -0
  61. data/lib/active_support/core_ext/hash/conversions.rb +150 -0
  62. data/lib/active_support/core_ext/hash/deep_merge.rb +16 -0
  63. data/lib/active_support/core_ext/hash/diff.rb +13 -0
  64. data/lib/active_support/core_ext/hash/except.rb +24 -0
  65. data/lib/active_support/core_ext/hash/indifferent_access.rb +14 -0
  66. data/lib/active_support/core_ext/hash/keys.rb +45 -0
  67. data/lib/active_support/core_ext/hash/reverse_merge.rb +28 -0
  68. data/lib/active_support/core_ext/hash/slice.rb +38 -0
  69. data/lib/active_support/core_ext/integer.rb +3 -0
  70. data/lib/active_support/core_ext/integer/inflections.rb +14 -0
  71. data/lib/active_support/core_ext/integer/multiple.rb +6 -0
  72. data/lib/active_support/core_ext/integer/time.rb +39 -0
  73. data/lib/active_support/core_ext/kernel.rb +5 -0
  74. data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
  75. data/lib/active_support/core_ext/kernel/debugger.rb +16 -0
  76. data/lib/active_support/core_ext/kernel/reporting.rb +62 -0
  77. data/lib/active_support/core_ext/kernel/requires.rb +26 -0
  78. data/lib/active_support/core_ext/kernel/singleton_class.rb +13 -0
  79. data/lib/active_support/core_ext/load_error.rb +23 -0
  80. data/lib/active_support/core_ext/logger.rb +146 -0
  81. data/lib/active_support/core_ext/module.rb +12 -0
  82. data/lib/active_support/core_ext/module/aliasing.rb +70 -0
  83. data/lib/active_support/core_ext/module/anonymous.rb +24 -0
  84. data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +31 -0
  85. data/lib/active_support/core_ext/module/attr_internal.rb +32 -0
  86. data/lib/active_support/core_ext/module/attribute_accessors.rb +66 -0
  87. data/lib/active_support/core_ext/module/delegation.rb +146 -0
  88. data/lib/active_support/core_ext/module/deprecation.rb +9 -0
  89. data/lib/active_support/core_ext/module/introspection.rb +88 -0
  90. data/lib/active_support/core_ext/module/method_names.rb +14 -0
  91. data/lib/active_support/core_ext/module/reachable.rb +10 -0
  92. data/lib/active_support/core_ext/module/remove_method.rb +6 -0
  93. data/lib/active_support/core_ext/module/synchronization.rb +42 -0
  94. data/lib/active_support/core_ext/name_error.rb +18 -0
  95. data/lib/active_support/core_ext/numeric.rb +2 -0
  96. data/lib/active_support/core_ext/numeric/bytes.rb +44 -0
  97. data/lib/active_support/core_ext/numeric/time.rb +77 -0
  98. data/lib/active_support/core_ext/object.rb +14 -0
  99. data/lib/active_support/core_ext/object/acts_like.rb +10 -0
  100. data/lib/active_support/core_ext/object/blank.rb +76 -0
  101. data/lib/active_support/core_ext/object/conversions.rb +4 -0
  102. data/lib/active_support/core_ext/object/duplicable.rb +65 -0
  103. data/lib/active_support/core_ext/object/extending.rb +11 -0
  104. data/lib/active_support/core_ext/object/instance_variables.rb +67 -0
  105. data/lib/active_support/core_ext/object/misc.rb +2 -0
  106. data/lib/active_support/core_ext/object/returning.rb +42 -0
  107. data/lib/active_support/core_ext/object/to_param.rb +49 -0
  108. data/lib/active_support/core_ext/object/to_query.rb +27 -0
  109. data/lib/active_support/core_ext/object/try.rb +36 -0
  110. data/lib/active_support/core_ext/object/with_options.rb +26 -0
  111. data/lib/active_support/core_ext/proc.rb +14 -0
  112. data/lib/active_support/core_ext/process.rb +1 -0
  113. data/lib/active_support/core_ext/process/daemon.rb +23 -0
  114. data/lib/active_support/core_ext/range.rb +4 -0
  115. data/lib/active_support/core_ext/range/blockless_step.rb +29 -0
  116. data/lib/active_support/core_ext/range/conversions.rb +21 -0
  117. data/lib/active_support/core_ext/range/include_range.rb +21 -0
  118. data/lib/active_support/core_ext/range/overlaps.rb +8 -0
  119. data/lib/active_support/core_ext/regexp.rb +5 -0
  120. data/lib/active_support/core_ext/rexml.rb +46 -0
  121. data/lib/active_support/core_ext/string.rb +12 -0
  122. data/lib/active_support/core_ext/string/access.rb +99 -0
  123. data/lib/active_support/core_ext/string/behavior.rb +7 -0
  124. data/lib/active_support/core_ext/string/conversions.rb +61 -0
  125. data/lib/active_support/core_ext/string/encoding.rb +11 -0
  126. data/lib/active_support/core_ext/string/exclude.rb +6 -0
  127. data/lib/active_support/core_ext/string/filters.rb +49 -0
  128. data/lib/active_support/core_ext/string/inflections.rb +149 -0
  129. data/lib/active_support/core_ext/string/interpolation.rb +2 -0
  130. data/lib/active_support/core_ext/string/multibyte.rb +72 -0
  131. data/lib/active_support/core_ext/string/output_safety.rb +109 -0
  132. data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
  133. data/lib/active_support/core_ext/string/xchar.rb +18 -0
  134. data/lib/active_support/core_ext/time/acts_like.rb +8 -0
  135. data/lib/active_support/core_ext/time/calculations.rb +282 -0
  136. data/lib/active_support/core_ext/time/conversions.rb +85 -0
  137. data/lib/active_support/core_ext/time/marshal.rb +56 -0
  138. data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +10 -0
  139. data/lib/active_support/core_ext/time/zones.rb +78 -0
  140. data/lib/active_support/core_ext/uri.rb +22 -0
  141. data/lib/active_support/dependencies.rb +628 -0
  142. data/lib/active_support/dependencies/autoload.rb +50 -0
  143. data/lib/active_support/deprecation.rb +18 -0
  144. data/lib/active_support/deprecation/behaviors.rb +38 -0
  145. data/lib/active_support/deprecation/method_wrappers.rb +29 -0
  146. data/lib/active_support/deprecation/proxy_wrappers.rb +74 -0
  147. data/lib/active_support/deprecation/reporting.rb +56 -0
  148. data/lib/active_support/duration.rb +105 -0
  149. data/lib/active_support/gzip.rb +25 -0
  150. data/lib/active_support/hash_with_indifferent_access.rb +145 -0
  151. data/lib/active_support/i18n.rb +8 -0
  152. data/lib/active_support/inflections.rb +56 -0
  153. data/lib/active_support/inflector.rb +7 -0
  154. data/lib/active_support/inflector/inflections.rb +211 -0
  155. data/lib/active_support/inflector/methods.rb +141 -0
  156. data/lib/active_support/inflector/transliterate.rb +97 -0
  157. data/lib/active_support/json.rb +2 -0
  158. data/lib/active_support/json/backends/jsongem.rb +43 -0
  159. data/lib/active_support/json/backends/yajl.rb +40 -0
  160. data/lib/active_support/json/backends/yaml.rb +90 -0
  161. data/lib/active_support/json/decoding.rb +51 -0
  162. data/lib/active_support/json/encoding.rb +254 -0
  163. data/lib/active_support/json/variable.rb +11 -0
  164. data/lib/active_support/lazy_load_hooks.rb +27 -0
  165. data/lib/active_support/locale/en.yml +36 -0
  166. data/lib/active_support/memoizable.rb +103 -0
  167. data/lib/active_support/message_encryptor.rb +71 -0
  168. data/lib/active_support/message_verifier.rb +62 -0
  169. data/lib/active_support/multibyte.rb +44 -0
  170. data/lib/active_support/multibyte/chars.rb +480 -0
  171. data/lib/active_support/multibyte/exceptions.rb +8 -0
  172. data/lib/active_support/multibyte/unicode.rb +393 -0
  173. data/lib/active_support/multibyte/utils.rb +60 -0
  174. data/lib/active_support/notifications.rb +81 -0
  175. data/lib/active_support/notifications/fanout.rb +93 -0
  176. data/lib/active_support/notifications/instrumenter.rb +56 -0
  177. data/lib/active_support/option_merger.rb +25 -0
  178. data/lib/active_support/ordered_hash.rb +158 -0
  179. data/lib/active_support/ordered_options.rb +27 -0
  180. data/lib/active_support/railtie.rb +100 -0
  181. data/lib/active_support/rescuable.rb +114 -0
  182. data/lib/active_support/ruby/shim.rb +22 -0
  183. data/lib/active_support/secure_random.rb +199 -0
  184. data/lib/active_support/string_inquirer.rb +21 -0
  185. data/lib/active_support/test_case.rb +42 -0
  186. data/lib/active_support/testing/assertions.rb +82 -0
  187. data/lib/active_support/testing/declarative.rb +40 -0
  188. data/lib/active_support/testing/default.rb +9 -0
  189. data/lib/active_support/testing/deprecation.rb +55 -0
  190. data/lib/active_support/testing/isolation.rb +154 -0
  191. data/lib/active_support/testing/pending.rb +48 -0
  192. data/lib/active_support/testing/performance.rb +455 -0
  193. data/lib/active_support/testing/setup_and_teardown.rb +111 -0
  194. data/lib/active_support/time.rb +34 -0
  195. data/lib/active_support/time/autoload.rb +5 -0
  196. data/lib/active_support/time_with_zone.rb +341 -0
  197. data/lib/active_support/values/time_zone.rb +377 -0
  198. data/lib/active_support/values/unicode_tables.dat +0 -0
  199. data/lib/active_support/version.rb +10 -0
  200. data/lib/active_support/whiny_nil.rb +60 -0
  201. data/lib/active_support/xml_mini.rb +158 -0
  202. data/lib/active_support/xml_mini/jdom.rb +168 -0
  203. data/lib/active_support/xml_mini/libxml.rb +80 -0
  204. data/lib/active_support/xml_mini/libxmlsax.rb +85 -0
  205. data/lib/active_support/xml_mini/nokogiri.rb +78 -0
  206. data/lib/active_support/xml_mini/nokogirisax.rb +83 -0
  207. data/lib/active_support/xml_mini/rexml.rb +129 -0
  208. data/lib/csd.rb +82 -2
  209. data/lib/csd/application.rb +2 -0
  210. data/lib/csd/application/default.rb +51 -0
  211. data/lib/csd/application/default/base.rb +15 -0
  212. data/lib/csd/application/minisip.rb +25 -0
  213. data/lib/csd/application/minisip/about.yml +14 -0
  214. data/lib/csd/application/minisip/base.rb +161 -0
  215. data/lib/csd/application/minisip/error.rb +11 -0
  216. data/lib/csd/application/minisip/options/common.rb +0 -0
  217. data/lib/csd/application/minisip/options/compile.rb +59 -0
  218. data/lib/csd/{applications → application}/minisip/unix/base.rb +10 -11
  219. data/lib/csd/application/opensips/about.yml +2 -0
  220. data/lib/csd/applications.rb +55 -0
  221. data/lib/csd/commands.rb +88 -65
  222. data/lib/csd/error.rb +31 -0
  223. data/lib/csd/extensions.rb +1 -0
  224. data/lib/{extensions → csd/extensions}/core/array.rb +2 -2
  225. data/lib/csd/extensions/core/dir.rb +46 -0
  226. data/lib/{extensions → csd/extensions}/core/file.rb +2 -2
  227. data/lib/{extensions → csd/extensions}/core/object.rb +2 -2
  228. data/lib/csd/extensions/core/option_parser.rb +33 -0
  229. data/lib/{extensions → csd/extensions}/core/pathname.rb +12 -3
  230. data/lib/{extensions → csd/extensions}/core/string.rb +2 -2
  231. data/lib/{extensions → csd/extensions}/gem/platform.rb +6 -2
  232. data/lib/csd/global_open_struct.rb +18 -0
  233. data/lib/csd/options.rb +124 -95
  234. data/lib/csd/path.rb +31 -0
  235. data/lib/csd/ui.rb +1 -0
  236. data/lib/csd/ui/cli.rb +7 -0
  237. data/lib/csd/ui/ui.rb +46 -0
  238. data/lib/csd/version.rb +9 -0
  239. data/lib/term/ansicolor.rb +102 -0
  240. data/lib/term/ansicolor/.keep +0 -0
  241. data/lib/term/ansicolor/version.rb +10 -0
  242. data/test/functional/test_applications.rb +86 -0
  243. data/test/functional/test_commands.rb +42 -29
  244. data/test/functional/test_options.rb +98 -0
  245. data/test/helper.rb +14 -0
  246. data/test/unit/test_dir.rb +38 -0
  247. data/test/unit/test_pathname.rb +32 -0
  248. metadata +253 -40
  249. data/LICENSE +0 -20
  250. data/bin/csd +0 -8
  251. data/lib/csd/applications/base.rb +0 -33
  252. data/lib/csd/applications/minisip/base.rb +0 -125
  253. data/lib/csd/applications/minisip/init.rb +0 -20
  254. data/lib/csd/init.rb +0 -69
  255. data/lib/csd/path_container.rb +0 -15
  256. data/publish +0 -29
@@ -0,0 +1,31 @@
1
+ module CSD
2
+ # In this module we will keep all types of errors in a readable hierarchy
3
+ #
4
+ module Error
5
+
6
+ # All Exceptions raised by CSD must be children of this class.
7
+ #
8
+ class CSDError < StandardError
9
+ def self.status_code(code = nil)
10
+ return @code unless code
11
+ @code = code
12
+ end
13
+
14
+ def status_code
15
+ self.class.status_code
16
+ end
17
+ end
18
+
19
+ # Errors in this module are related to command-line options
20
+ #
21
+ module Argument
22
+ class NoApplication < CSDError; status_code(11); end
23
+ class NoAction < CSDError; status_code(12); end
24
+ end
25
+
26
+ module Application
27
+ class OptionsSyntax < CSDError; status_code(200); end
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1 @@
1
+ Dir.glob(File.join(File.dirname(__FILE__), 'extensions', '**', '*.rb')) { |file| require file }
@@ -1,4 +1,4 @@
1
- module Csd
1
+ module CSD
2
2
  module Extensions
3
3
  module Core
4
4
  module Array
@@ -9,5 +9,5 @@ module Csd
9
9
  end
10
10
 
11
11
  class Array #:nodoc:
12
- include Csd::Extensions::Core::Array
12
+ include CSD::Extensions::Core::Array
13
13
  end
@@ -0,0 +1,46 @@
1
+ require 'pathname'
2
+
3
+ module CSD
4
+ module Extensions
5
+ module Core
6
+ module Dir
7
+
8
+ def directories(path, &block)
9
+ if block_given?
10
+ ::Pathname.new(path).children_directories { |pathname| yield pathname.basename.to_s }
11
+ else
12
+ ::Pathname.new(path).children_directories.map { |pathname| pathname.basename.to_s }
13
+ end
14
+ end
15
+
16
+ # Returns all direct subdirectories of +path+ with their entire path
17
+ #
18
+ #def directories(path, &block)
19
+ # result = []
20
+ # glob(::File.join(path, '*')).each do |dir|
21
+ # if (::File.directory?(dir) and dir != '.' and dir != '..')
22
+ # block_given? ? yield(dir) : result << dir
23
+ # end
24
+ # end
25
+ # result
26
+ #end
27
+
28
+ #def directories(path, absolute=false, &block)
29
+ # result = []
30
+ # entries(path) do |entry|
31
+ # if (::File.directory?(entry) and entry != '.' and entry != '..')
32
+ # dir = absolute? ? File.join(path, entry) : entry
33
+ # block_given? ? yield(dir) : result << dir
34
+ # end
35
+ # end
36
+ # result
37
+ #end
38
+
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ class Dir #:nodoc:
45
+ extend CSD::Extensions::Core::Dir
46
+ end
@@ -1,4 +1,4 @@
1
- module Csd
1
+ module CSD
2
2
  module Extensions
3
3
  module Core
4
4
  module File
@@ -9,5 +9,5 @@ module Csd
9
9
  end
10
10
 
11
11
  class File #:nodoc:
12
- extend Csd::Extensions::Core::File
12
+ extend CSD::Extensions::Core::File
13
13
  end
@@ -1,4 +1,4 @@
1
- module Csd
1
+ module CSD
2
2
  module Extensions
3
3
  module Core
4
4
  module Object
@@ -18,5 +18,5 @@ module Csd
18
18
  end
19
19
 
20
20
  class Object #:nodoc:
21
- include Csd::Extensions::Core::Object
21
+ include CSD::Extensions::Core::Object
22
22
  end
@@ -0,0 +1,33 @@
1
+ require File.join(File.dirname(__FILE__), 'string')
2
+ require 'optparse'
3
+
4
+ module CSD
5
+ module Extensions
6
+ module Core
7
+ module OptionParser
8
+
9
+ def newline
10
+ separator ''
11
+ end
12
+
13
+ def headline(text)
14
+ newline
15
+ separator(text)
16
+ end
17
+
18
+ def subheadline(text)
19
+ separator(@summary_indent + text)
20
+ end
21
+
22
+ def list_item(item='', description='', &block)
23
+ separator(summary_indent + item.ljust(summary_width + 1) + description)
24
+ end
25
+
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ class OptionParser #:nodoc:
32
+ include CSD::Extensions::Core::OptionParser
33
+ end
@@ -1,10 +1,19 @@
1
1
  require File.join(File.dirname(__FILE__), 'string')
2
2
 
3
- module Csd
3
+ module CSD
4
4
  module Extensions
5
5
  module Core
6
6
  module Pathname
7
-
7
+
8
+ def children_directories(&block)
9
+ result = []
10
+ children.map do |child|
11
+ next unless child.directory?
12
+ block_given? ? yield(child) : result << child
13
+ end
14
+ result
15
+ end
16
+
8
17
  def enquote
9
18
  to_s.enquote
10
19
  end
@@ -19,5 +28,5 @@ module Csd
19
28
  end
20
29
 
21
30
  class Pathname #:nodoc:
22
- include Csd::Extensions::Core::Pathname
31
+ include CSD::Extensions::Core::Pathname
23
32
  end
@@ -1,6 +1,6 @@
1
1
  require 'term/ansicolor'
2
2
 
3
- module Csd
3
+ module CSD
4
4
  module Extensions
5
5
  module Core
6
6
  module String
@@ -16,5 +16,5 @@ end
16
16
 
17
17
  class String #:nodoc:
18
18
  include Term::ANSIColor
19
- include Csd::Extensions::Core::String
19
+ include CSD::Extensions::Core::String
20
20
  end
@@ -1,4 +1,4 @@
1
- module Csd
1
+ module CSD
2
2
  module Extensions
3
3
  module Gem
4
4
  module Platform
@@ -7,6 +7,10 @@ module Csd
7
7
  version_string = version ? ", version #{version}" : ''
8
8
  "#{os} (CPU: #{cpu}#{version_string})"
9
9
  end
10
+
11
+ def kernel_version
12
+ Cmd.run('uname -v', :silent => true).chop if os == 'linux'
13
+ end
10
14
 
11
15
  end
12
16
  end
@@ -15,6 +19,6 @@ end
15
19
 
16
20
  module Gem
17
21
  class Platform #:nodoc:
18
- include Csd::Extensions::Gem::Platform
22
+ include CSD::Extensions::Gem::Platform
19
23
  end
20
24
  end
@@ -0,0 +1,18 @@
1
+ module CSD
2
+ class GlobalOpenStruct
3
+
4
+ def self.method_missing(meth, *args, &block)
5
+ if meth.to_s.ends_with?('=')
6
+ class_variable_set("@@#{meth.to_s.chop}".to_sym, *args)
7
+ else
8
+ begin
9
+ class_variable_get("@@#{meth}".to_sym)
10
+ rescue NameError => e
11
+ #UI.debug "The option `#{meth}´ was accessed but not available." # FIXME: This line causes a recursion error :)
12
+ nil
13
+ end
14
+ end
15
+ end
16
+
17
+ end
18
+ end
@@ -1,120 +1,149 @@
1
+ # Ruby standard library
1
2
  require 'optparse'
2
3
  require 'optparse/time'
3
4
  require 'ostruct'
5
+ # Vendor
6
+ require 'active_support/core_ext/string/inflections'
7
+ require 'active_support/core_ext/string/starts_ends_with'
8
+ require 'active_support/core_ext/array'
9
+ # CSD
10
+ require 'csd/extensions'
11
+ Dir.glob(File.join(File.dirname(__FILE__), '*.rb')) { |file| require file }
4
12
 
5
- module Csd
6
- class Options
13
+ module CSD
14
+ # A class that handles the command line option parsing and manipulation
15
+ #
16
+ class Options < GlobalOpenStruct
17
+
18
+ def self.parse!
19
+ clear
20
+ parse_literals
21
+ define_actions_and_scopes
22
+ parse_options
23
+ end
24
+
25
+ def self.valid_action?
26
+ public_actions = actions[:public] ? self.actions[:public].map { |pair| pair.keys.first } : []
27
+ developer_actions = actions[:developer] ? self.actions[:developer].map { |pair| pair.keys.first } : []
28
+ all_actions = public_actions + developer_actions
29
+ all_actions.include?(self.action)
30
+ end
31
+
32
+ def self.define_actions_and_scopes
33
+ if Applications.current
34
+ # Here we overwrite the default supported actions and scopes with the application specific ones
35
+ self.actions = Applications.current.actions
36
+ # At this point we know that the first argument is no option, but *some* action (may it be valid or not)
37
+ self.scopes = Applications.current.scopes(self.action)
38
+ end
39
+ end
40
+
41
+ def self.clear
42
+ # First we define all valid actions and scopes
43
+ self.actions = []
44
+ self.scopes = []
45
+ # Then we define the default literals
46
+ self.help = false
47
+ self.application = nil
48
+ self.action = nil
49
+ # Now we define the default options
50
+ self.yes = false
51
+ self.dry = false
52
+ self.reveal = false
53
+ self.verbose = false
54
+ self.debug = false
55
+ self.silent = false
56
+ self.developer = false
57
+ end
7
58
 
59
+ # Here we check for literals, i.e. "help", ACTION and APPLICATION.
8
60
  #
9
- # Returns a structure describing the options.
61
+ def self.parse_literals
62
+ # First let's see whether we are in help mode, i.e. whether the first argument is `help´.
63
+ # If so, we would like to remove it from the ARGV list.
64
+ if ARGV.first == 'help'
65
+ self.help = true
66
+ ARGV.shift
67
+ end
68
+ # The action and the application name are the other literals we're interested in at this point.
69
+ # Note: If there is no application specified, there is pretty much nothing we can do for the user.
70
+ if Applications.current and Applications.current.name == ARGV.first
71
+ # The application name is the first argument (i.e. there is no action specified at all)
72
+ # Let's store the application name and remove it from the argument line
73
+ self.application = ARGV.shift
74
+ elsif Applications.current and Applications.current.name == ARGV.second
75
+ # The second argument is the application name. This means that the first argument must be the
76
+ # action or happens to be some option. In case it's no option, lets take it as desired action.
77
+ unless ARGV.first.starts_with?('-')
78
+ self.action = ARGV.shift
79
+ self.application = ARGV.shift # Removing the application name from the argument list
80
+ end
81
+ end
82
+ end
83
+
84
+ # Parse all options that the user gave as command parameter. Note that this function strips the options
85
+ # from ARGV and leaves only literal (non-option) parameters (i.e. actions/applications/scopes; strings without -- and -).
10
86
  #
11
- def self.parse
12
- # Default options
13
- options = OpenStruct.new
14
- options.temp = false
15
- options.silent = false
16
- options.dry = false
17
- options.bootstrap = true
18
- options.configure = true
19
- options.make = true
20
- options.make_install = true
21
- options.owner = nil
22
- options.apt_get = true
23
- options.yes = false
24
-
25
- # Parse the command line options
87
+ def self.parse_options
26
88
  OptionParser.new do |opts|
27
- opts.banner = "Usage: csd [action] [application] [options]"
28
- opts.separator ""
29
- opts.separator "Actions:"
30
- opts.separator " install"
31
- opts.separator ""
32
- opts.separator "Applications:"
33
- opts.separator " minisip"
34
- opts.separator ""
35
- opts.separator "Options:"
36
-
37
- opts.on("-t", "--temp",
38
- "Use a subdirectory in the system's temporary directory",
39
- "to download files and not the current directory") do |value|
40
- options.temp = value
41
- end
42
-
43
- opts.on("-d", "--dry","Don't execute any commands, just show them") do |value|
44
- options.dry = value
45
- end
89
+ self.banner = Applications.current ? "ADVANCED HELP FOR #{Applications.current.name}" : "Usage: ".bold + "ai [help] [TASK] APPLICATION [OPTIONS]"
90
+ opts.banner = self.banner.magenta.bold
46
91
 
47
- opts.on("-y", "--yes","Answering all questions with 'yes'") do |value|
48
- options.yes = value
49
- end
50
-
51
- opts.on("-na", "--no-apt-get","Don't run any apt-get commands") do |value|
52
- options.apt_get = value
53
- end
54
-
55
- opts.on("-nb", "--no-bootstrap","Don't run any bootstrap commands") do |value|
56
- options.bootstrap = value
57
- end
58
-
59
- opts.on("-nc", "--no-configure","Don't run any configure commands") do |value|
60
- options.configure = value
61
- end
62
-
63
- opts.on("-nm", "--no-make","Don't run any make commands") do |value|
64
- options.make = value
65
- end
66
-
67
- opts.on("-nmi", "--no-make-install","Don't run any make install commands") do |value|
68
- options.make_install = value
92
+ unless Applications.current
93
+ opts.headline "EXAMPLE COMMANDS".green.bold
94
+ opts.list_item 'ai', 'Lists all available applications'
95
+ opts.list_item 'ai minisip', 'Lists available tasks for the application MiniSIP'
96
+ opts.list_item 'ai minisip --developer', 'Lists advanced AI tasks for the application MiniSIP'
97
+ opts.list_item 'ai compile minisip', 'Downloads MiniSIP and compiles it'
98
+ opts.list_item 'ai help compile minisip', 'Shows more details about the different compiling options'
69
99
  end
100
+
101
+ # Here we load application-specific options file.
102
+ # TODO: There must be a better way for this in general than to eval the raw ruby code
103
+ begin
104
+ unless Applications.current.options(self.action).size.blank?
105
+ opts.headline "#{self.action.upcase} #{Applications.current.name.upcase} OPTIONS".green.bold
106
+ eval Applications.current.options(self.action)
107
+ else
108
+ UI.debug "There were no options to be loaded from #{Applications.current}"
109
+ end
110
+ rescue SyntaxError => e
111
+ raise Error::Application::OptionsSyntax, "The individual options of #{Applications.current.inspect} could not be parsed (SyntaxError)."
112
+ end if Applications.current
70
113
 
71
- opts.on("--only libmcrypto,libmnetuli,etc.", Array, "Include only these libraries") do |list|
72
- options.only = list
114
+ # And here we load all general options
115
+ options_prepend = Applications.current ? 'GENERAL ' : nil
116
+ opts.headline "#{options_prepend}OPTIONS".green.bold
117
+ opts.on("-y", "--yes", "Answer all questions with `yes´ (batch mode)") do |value|
118
+ self.yes = value
73
119
  end
74
-
75
- opts.on("-o", "--owner [OWNER]","Specify OWNER:GROUP for this operation") do |value|
76
- options.owner = value
120
+ opts.on("-p", "--dry","Don't actually execute any commands (preview mode)") do |value|
121
+ self.dry = value
77
122
  end
78
-
79
- opts.on("-p", "--path [PATH]",
80
- "Defines the working directory manually.",
81
- "(This will override the --temp option)") do |value|
82
- options.path = value
123
+ opts.on("-r", "--reveal","List all commands that normally would be executed in this operation") do |value|
124
+ self.reveal = value
83
125
  end
84
-
85
-
86
- opts.on("-d", "--debug","Show debugging information") do |value|
87
- options.quiet = value
126
+ opts.on("-e", "--verbose","Show more elaborate output") do |value|
127
+ self.verbose = value
88
128
  end
89
-
90
- opts.on("--verbose","Show elaborate output") do |value|
91
- options.quiet = value
129
+ opts.on("-d", "--debug","Show more elaborate output and debugging information") do |value|
130
+ self.debug = value
92
131
  end
93
-
94
132
  opts.on("-s", "--silent","Don't show any output") do |value|
95
- options.silent = value
133
+ self.silent = value
96
134
  end
97
-
98
- opts.on_tail("-h", "--help", "Show this message") do
99
- puts opts
100
- exit
135
+ opts.on_tail("-a", "--developer", "Activating advanced AI functionality (developers only)") do |value|
136
+ self.developer = value
137
+ end
138
+ opts.on_tail("-h", "--help", "Show detailed help (regarding the given ACTION and APPLICATION)") do |value|
139
+ self.help = value
101
140
  end
102
-
103
141
  opts.on_tail("-v", "--version", "Show version") do
104
- puts "CSD Gem Version: #{File.read(File.join(PathContainer.new.gem_root, 'VERSION'))}"
142
+ print "CSD Gem Version: #{opts.version}".blue
105
143
  exit
106
144
  end
107
- end
108
-
109
- #.parse!
110
-
111
- if options.owner
112
- chmod = options.owner.split(':')
113
- options.owner = chmod.first
114
- options.group = chmod.last
115
- end
116
-
117
- options
145
+ self.helptext = opts.help
146
+ end.parse!
118
147
  end
119
148
 
120
149
  end