kamaze-project 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +9 -9
  3. data/lib/kamaze-project.rb +1 -31
  4. data/lib/kamaze/project.rb +37 -31
  5. data/lib/kamaze/project/autoload.rb +38 -0
  6. data/lib/kamaze/project/boot/core_ext.rb +8 -8
  7. data/lib/kamaze/project/boot/listen.rb +11 -13
  8. data/lib/kamaze/project/bundled.rb +79 -0
  9. data/lib/kamaze/project/concern.rb +17 -2
  10. data/lib/kamaze/project/concern/cli.rb +3 -1
  11. data/lib/kamaze/project/concern/cli/with_exit_on_failure.rb +2 -6
  12. data/lib/kamaze/project/concern/env.rb +14 -12
  13. data/lib/kamaze/project/concern/helper.rb +1 -2
  14. data/lib/kamaze/project/concern/mode.rb +1 -1
  15. data/lib/kamaze/project/concern/observable.rb +12 -11
  16. data/lib/kamaze/project/concern/sh.rb +7 -7
  17. data/lib/kamaze/project/concern/tasks.rb +36 -10
  18. data/lib/kamaze/project/concern/tools.rb +2 -3
  19. data/lib/kamaze/project/config.rb +1 -1
  20. data/lib/kamaze/project/core_ext/object.rb +17 -8
  21. data/lib/kamaze/project/core_ext/pp.rb +14 -16
  22. data/lib/kamaze/project/debug.rb +47 -48
  23. data/lib/kamaze/project/dsl.rb +9 -5
  24. data/lib/kamaze/project/dsl/definition.rb +6 -12
  25. data/lib/kamaze/project/dsl/setup.rb +1 -3
  26. data/lib/kamaze/project/helper.rb +1 -1
  27. data/lib/kamaze/project/helper/inflector.rb +9 -27
  28. data/lib/kamaze/project/helper/project.rb +1 -1
  29. data/lib/kamaze/project/helper/project/config.rb +1 -1
  30. data/lib/kamaze/project/inflector.rb +15 -0
  31. data/lib/kamaze/project/observable.rb +1 -4
  32. data/lib/kamaze/project/observer.rb +1 -1
  33. data/lib/kamaze/project/resources/Vagrantfile +1 -1
  34. data/lib/kamaze/project/struct.rb +1 -1
  35. data/lib/kamaze/project/tasks/cs.rb +1 -1
  36. data/lib/kamaze/project/tasks/cs/control.rb +1 -1
  37. data/lib/kamaze/project/tasks/cs/correct.rb +1 -1
  38. data/lib/kamaze/project/tasks/cs/pre_commit.rb +4 -6
  39. data/lib/kamaze/project/tasks/doc.rb +2 -4
  40. data/lib/kamaze/project/tasks/doc/watch.rb +4 -6
  41. data/lib/kamaze/project/tasks/gem.rb +15 -6
  42. data/lib/kamaze/project/tasks/gem/build.rb +33 -17
  43. data/lib/kamaze/project/tasks/gem/compile.rb +1 -1
  44. data/lib/kamaze/project/tasks/gem/gemspec.rb +12 -11
  45. data/lib/kamaze/project/tasks/gem/install.rb +1 -1
  46. data/lib/kamaze/project/tasks/gem/push.rb +16 -17
  47. data/lib/kamaze/project/tasks/misc/gitignore.rb +1 -1
  48. data/lib/kamaze/project/tasks/shell.rb +1 -1
  49. data/lib/kamaze/project/tasks/sources.rb +1 -1
  50. data/lib/kamaze/project/tasks/sources/license.rb +1 -1
  51. data/lib/kamaze/project/tasks/test.rb +3 -5
  52. data/lib/kamaze/project/tasks/vagrant.rb +1 -1
  53. data/lib/kamaze/project/tasks/version/edit.rb +1 -1
  54. data/lib/kamaze/project/tools.rb +18 -6
  55. data/lib/kamaze/project/tools/base_tool.rb +6 -2
  56. data/lib/kamaze/project/tools/console.rb +4 -13
  57. data/lib/kamaze/project/tools/console/output.rb +14 -6
  58. data/lib/kamaze/project/tools/console/output/buffer.rb +8 -3
  59. data/lib/kamaze/project/tools/gemspec.rb +11 -21
  60. data/lib/kamaze/project/tools/gemspec/builder.rb +43 -31
  61. data/lib/kamaze/project/tools/gemspec/concern.rb +6 -1
  62. data/lib/kamaze/project/tools/gemspec/concern/reading.rb +2 -5
  63. data/lib/kamaze/project/tools/gemspec/packager.rb +10 -10
  64. data/lib/kamaze/project/tools/gemspec/packer.rb +11 -15
  65. data/lib/kamaze/project/tools/gemspec/packer/command.rb +1 -1
  66. data/lib/kamaze/project/tools/gemspec/reader.rb +22 -14
  67. data/lib/kamaze/project/tools/gemspec/reader/decorator.rb +3 -6
  68. data/lib/kamaze/project/tools/gemspec/writer.rb +43 -19
  69. data/lib/kamaze/project/tools/gemspec/writer/dep_gen.rb +9 -6
  70. data/lib/kamaze/project/tools/gemspec/writer/dependency.rb +1 -1
  71. data/lib/kamaze/project/tools/git.rb +13 -31
  72. data/lib/kamaze/project/tools/git/hooks.rb +12 -6
  73. data/lib/kamaze/project/tools/git/hooks/base_hook.rb +3 -11
  74. data/lib/kamaze/project/tools/git/hooks/pre_commit.rb +3 -7
  75. data/lib/kamaze/project/tools/git/status.rb +13 -9
  76. data/lib/kamaze/project/tools/git/status/decorator.rb +1 -1
  77. data/lib/kamaze/project/tools/git/status/file.rb +2 -1
  78. data/lib/kamaze/project/tools/git/status/files_array.rb +1 -1
  79. data/lib/kamaze/project/tools/git/status/index.rb +1 -1
  80. data/lib/kamaze/project/tools/git/status/worktree.rb +1 -1
  81. data/lib/kamaze/project/tools/git/util.rb +1 -1
  82. data/lib/kamaze/project/tools/licenser.rb +2 -2
  83. data/lib/kamaze/project/tools/packager.rb +12 -22
  84. data/lib/kamaze/project/tools/packager/filesystem.rb +11 -3
  85. data/lib/kamaze/project/tools/packager/filesystem/operator.rb +19 -11
  86. data/lib/kamaze/project/tools/packager/filesystem/operator/utils.rb +22 -16
  87. data/lib/kamaze/project/tools/process_locker.rb +67 -36
  88. data/lib/kamaze/project/tools/rspec.rb +5 -3
  89. data/lib/kamaze/project/tools/rubocop.rb +30 -40
  90. data/lib/kamaze/project/tools/rubocop/arguments.rb +1 -1
  91. data/lib/kamaze/project/tools/rubocop/config.rb +2 -4
  92. data/lib/kamaze/project/tools/shell.rb +7 -8
  93. data/lib/kamaze/project/tools/vagrant.rb +6 -16
  94. data/lib/kamaze/project/tools/vagrant/composer.rb +1 -1
  95. data/lib/kamaze/project/tools/vagrant/composer/file.rb +1 -1
  96. data/lib/kamaze/project/tools/vagrant/remote.rb +1 -1
  97. data/lib/kamaze/project/tools/vagrant/shell.rb +1 -1
  98. data/lib/kamaze/project/tools/vagrant/writer.rb +1 -1
  99. data/lib/kamaze/project/tools/yardoc.rb +14 -17
  100. data/lib/kamaze/project/tools/yardoc/file.rb +7 -7
  101. data/lib/kamaze/project/tools/yardoc/watchable.rb +9 -15
  102. data/lib/kamaze/project/tools/yardoc/watcher.rb +28 -35
  103. data/lib/kamaze/project/tools_provider.rb +28 -40
  104. data/lib/kamaze/project/tools_provider/resolver.rb +1 -1
  105. data/lib/kamaze/project/version.rb +5 -94
  106. data/lib/kamaze/project/version.yml +5 -5
  107. metadata +23 -64
  108. data/bin/plop +0 -5
@@ -1,14 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (C) 2017-2018 Dimitri Arrigoni <dimitri@arrigoni.me>
3
+ # Copyright (C) 2017-2021 Dimitri Arrigoni <dimitri@arrigoni.me>
4
4
  # License GPLv3+: GNU GPL version 3 or later
5
5
  # <http://www.gnu.org/licenses/gpl.html>.
6
6
  # This is free software: you are free to change and redistribute it.
7
7
  # There is NO WARRANTY, to the extent permitted by law.
8
8
 
9
9
  require_relative '../concern'
10
- require 'dotenv'
11
- require 'pathname'
12
10
 
13
11
  # Load dotenv file
14
12
  #
@@ -21,6 +19,9 @@ require 'pathname'
21
19
  # @see https://github.com/bkeepers/dotenv
22
20
  # @see http://12factor.net/config
23
21
  module Kamaze::Project::Concern::Env
22
+ autoload(:Dotenv, 'dotenv')
23
+ autoload(:Pathanme, 'pathname')
24
+
24
25
  # @!attribute [r] env_loaded
25
26
  # @return [Hash] loaded environment
26
27
 
@@ -39,23 +40,24 @@ module Kamaze::Project::Concern::Env
39
40
  # @return [Hash]
40
41
  def env_loaded
41
42
  @env_loaded ||= {}
42
-
43
- @env_loaded
44
43
  end
45
44
 
46
45
  protected
47
46
 
48
47
  # Load ``.env`` file (and store result)
49
48
  #
50
- # @todo load different (or additionnal) files depending on env/mode
51
- #
52
49
  # @return [self]
53
- def env_load(options = {})
54
- options[:pwd] = ::Pathname.new(options[:pwd] || Dir.pwd).realpath
55
- options[:file] ||= '.env'
50
+ def env_load(**options)
51
+ options = {
52
+ # @formatter:off
53
+ pwd: Pathname.new(options[:pwd] || Dir.pwd).realpath,
54
+ file: '.env',
55
+ # @formatter:on
56
+ }.merge(options)
56
57
 
57
- [options.fetch(:pwd).join(options.fetch(:file))].each do |file|
58
- env_loaded.merge!(::Dotenv.load(file))
58
+ # @todo load different (or additionnal) files depending on env/mode
59
+ [Pathname.new(options.fetch(:pwd)).join(options.fetch(:file))].each do |file|
60
+ env_loaded.merge!(Dotenv.load(file))
59
61
  end
60
62
 
61
63
  self
@@ -1,13 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (C) 2017-2018 Dimitri Arrigoni <dimitri@arrigoni.me>
3
+ # Copyright (C) 2017-2021 Dimitri Arrigoni <dimitri@arrigoni.me>
4
4
  # License GPLv3+: GNU GPL version 3 or later
5
5
  # <http://www.gnu.org/licenses/gpl.html>.
6
6
  # This is free software: you are free to change and redistribute it.
7
7
  # There is NO WARRANTY, to the extent permitted by law.
8
8
 
9
9
  require_relative '../concern'
10
- require_relative '../helper'
11
10
 
12
11
  # Provides access to helpers
13
12
  module Kamaze::Project::Concern::Helper
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (C) 2017-2018 Dimitri Arrigoni <dimitri@arrigoni.me>
3
+ # Copyright (C) 2017-2021 Dimitri Arrigoni <dimitri@arrigoni.me>
4
4
  # License GPLv3+: GNU GPL version 3 or later
5
5
  # <http://www.gnu.org/licenses/gpl.html>.
6
6
  # This is free software: you are free to change and redistribute it.
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (C) 2017-2018 Dimitri Arrigoni <dimitri@arrigoni.me>
3
+ # Copyright (C) 2017-2021 Dimitri Arrigoni <dimitri@arrigoni.me>
4
4
  # License GPLv3+: GNU GPL version 3 or later
5
5
  # <http://www.gnu.org/licenses/gpl.html>.
6
6
  # This is free software: you are free to change and redistribute it.
@@ -54,6 +54,7 @@ module Kamaze::Project::Concern::Observable
54
54
  # Add observer.
55
55
  #
56
56
  # @param [Class] observer_class
57
+ #
57
58
  # @return [self]
58
59
  def add_observer(observer_class, func = :handle_event)
59
60
  func = func.to_sym
@@ -71,20 +72,19 @@ module Kamaze::Project::Concern::Observable
71
72
  # Remove observer, so that it will no longer receive notifications.
72
73
  #
73
74
  # @param [Class] observer_class
75
+ #
74
76
  # @return [self]
75
77
  def delete_observer(observer_class)
76
- observer_peers.delete(observer_class)
77
-
78
- return self
78
+ self.tap do
79
+ observer_peers.delete(observer_class)
80
+ end
79
81
  end
80
82
 
81
83
  # Remove all observers.
82
84
  #
83
85
  # @return [self]
84
86
  def delete_observers
85
- observers.clear
86
-
87
- self
87
+ self.tap { observers.clear }
88
88
  end
89
89
 
90
90
  protected
@@ -121,12 +121,13 @@ module Kamaze::Project::Concern::Observable
121
121
  #
122
122
  # @param [Symbol|String] event
123
123
  # @param [Array<Object>] args
124
+ #
124
125
  # @return [self]
125
126
  def dispatch_event(event, *args)
126
- observer_peers.to_h.each do |k, v|
127
- k.__send__(v, *[event, self].concat(args))
127
+ self.tap do
128
+ observer_peers.to_h.each do |k, v|
129
+ k.__send__(v, *[event, self].concat(args))
130
+ end
128
131
  end
129
-
130
- self
131
132
  end
132
133
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (C) 2017-2018 Dimitri Arrigoni <dimitri@arrigoni.me>
3
+ # Copyright (C) 2017-2021 Dimitri Arrigoni <dimitri@arrigoni.me>
4
4
  # License GPLv3+: GNU GPL version 3 or later
5
5
  # <http://www.gnu.org/licenses/gpl.html>.
6
6
  # This is free software: you are free to change and redistribute it.
@@ -61,12 +61,12 @@ module Kamaze::Project::Concern::Sh
61
61
  self.shell_runner_last_status = status
62
62
 
63
63
  if !ok and shell_runner_debug?
64
- # rubocop:disable Layout/IndentHash
65
- warn("Command failed with status (%<retcode>s):\n# %<command>s" % {
66
- retcode: status.exitstatus,
67
- command: debug_cmd(cmd.clone).gsub(/\{\}$/, '')
68
- })
69
- # rubocop:enable Layout/IndentHash
64
+ # @formatter:off
65
+ "Command failed with status (%<retcode>s):\n# %<command>s" % {
66
+ retcode: status.exitstatus,
67
+ command: debug_cmd(cmd.clone).gsub(/\{\}$/, '')
68
+ }.tap { |message| warn(message) }
69
+ # @formatter:on
70
70
  end
71
71
  end
72
72
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (C) 2017-2018 Dimitri Arrigoni <dimitri@arrigoni.me>
3
+ # Copyright (C) 2017-2021 Dimitri Arrigoni <dimitri@arrigoni.me>
4
4
  # License GPLv3+: GNU GPL version 3 or later
5
5
  # <http://www.gnu.org/licenses/gpl.html>.
6
6
  # This is free software: you are free to change and redistribute it.
@@ -10,7 +10,11 @@ require_relative '../concern'
10
10
 
11
11
  # Concern tasks
12
12
  #
13
- # Stores tasks to enable and provides method to load them
13
+ # Stores tasks to enable and provides method to load them.
14
+ #
15
+ # Tasks are deignated by a string, preprocessing is applied on this strings
16
+ # before each ``task`` is required. Task prefixd by a ``@`` character are
17
+ # required without preprocessing, except first char removal.
14
18
  module Kamaze::Project::Concern::Tasks
15
19
  # Get tasks
16
20
  #
@@ -24,11 +28,16 @@ module Kamaze::Project::Concern::Tasks
24
28
  # Set tasks
25
29
  #
26
30
  # @param [Array] tasks
31
+ # @return [Array<String>]
27
32
  def tasks=(tasks)
28
33
  @tasks = (@tasks.to_a + tasks.to_a).map(&:to_s).map do |tn|
29
- { ':': '/', '-': '_' }.each { |k, v| tn = tn.tr(k.to_s, v) }
34
+ if tn[0] != '@'
35
+ { ':': '/', '-': '_' }.each do |k, v|
36
+ tn = tn.tr(k.to_s, v)
37
+ end
38
+ end
30
39
 
31
- tn
40
+ tn.to_s
32
41
  end.map(&:to_sym).uniq
33
42
  end
34
43
 
@@ -42,14 +51,31 @@ module Kamaze::Project::Concern::Tasks
42
51
  # @see Kamaze::Project::DSL::Definition
43
52
  # @return [self]
44
53
  def tasks_load!
45
- if Kernel.const_defined?('Rake::DSL')
46
- ns = Pathname.new('kamaze/project')
54
+ return self unless Kernel.const_defined?('Rake::DSL')
55
+
56
+ require tasks_ns.join('dsl/setup').to_s
47
57
 
48
- [ns.join('dsl/setup'),
49
- tasks.map { |task| ns.join("tasks/#{task}") }]
50
- .flatten.map(&:to_s).each { |req| require req }
51
- end
58
+ tasks.map { |task| load_task!(task) }
52
59
 
53
60
  self
54
61
  end
62
+
63
+ # Load task by given name
64
+ #
65
+ # @param [String] task
66
+ # @return [String]
67
+ def load_task!(task)
68
+ req = task[0] != '@' ? tasks_ns.join("tasks/#{task}") : task[1..-1]
69
+
70
+ require req
71
+
72
+ req
73
+ end
74
+
75
+ # Get namespace for default tasks
76
+ #
77
+ # @return [Pathname]
78
+ def tasks_ns
79
+ Pathname.new('kamaze/project')
80
+ end
55
81
  end
@@ -1,13 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (C) 2017-2018 Dimitri Arrigoni <dimitri@arrigoni.me>
3
+ # Copyright (C) 2017-2021 Dimitri Arrigoni <dimitri@arrigoni.me>
4
4
  # License GPLv3+: GNU GPL version 3 or later
5
5
  # <http://www.gnu.org/licenses/gpl.html>.
6
6
  # This is free software: you are free to change and redistribute it.
7
7
  # There is NO WARRANTY, to the extent permitted by law.
8
8
 
9
9
  require_relative '../concern'
10
- require_relative '../../project/tools_provider'
11
10
 
12
11
  # Provides access to tools
13
12
  #
@@ -15,7 +14,7 @@ require_relative '../../project/tools_provider'
15
14
  module Kamaze::Project::Concern::Tools
16
15
  # Get tools
17
16
  #
18
- # @return [Hash]
17
+ # @return [ToolsProvider|Hash]
19
18
  def tools
20
19
  @tools ||= Kamaze::Project::ToolsProvider.new
21
20
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (C) 2017-2018 Dimitri Arrigoni <dimitri@arrigoni.me>
3
+ # Copyright (C) 2017-2021 Dimitri Arrigoni <dimitri@arrigoni.me>
4
4
  # License GPLv3+: GNU GPL version 3 or later
5
5
  # <http://www.gnu.org/licenses/gpl.html>.
6
6
  # This is free software: you are free to change and redistribute it.
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (C) 2017-2018 Dimitri Arrigoni <dimitri@arrigoni.me>
3
+ # Copyright (C) 2017-2021 Dimitri Arrigoni <dimitri@arrigoni.me>
4
4
  # License GPLv3+: GNU GPL version 3 or later
5
5
  # <http://www.gnu.org/licenses/gpl.html>.
6
6
  # This is free software: you are free to change and redistribute it.
@@ -15,13 +15,22 @@
15
15
  #
16
16
  # @see https://ruby-doc.org/core-2.5.0/Object.html
17
17
  class Object
18
- if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5.0')
19
- # Yields self to the block and returns the result of the block.
20
- #
21
- # @return [Object]
22
- # @see https://ruby-doc.org/core-2.5.0/Object.html#method-i-yield_self
23
- def yield_self
24
- yield(self)
18
+ autoload(:RbConfig, 'rbconfig')
19
+ # noinspection RubyEmptyRescueBlockInspection
20
+ begin
21
+ autoload(:Gem, 'rubygems')
22
+ rescue LoadError # rubocop:disable Lint/SuppressedException
23
+ end
24
+
25
+ RbConfig::CONFIG['ruby_version']&.tap do |ruby_version|
26
+ if defined?(Gem) and Gem::Version.new(ruby_version) < Gem::Version.new('2.5.0')
27
+ # Yields self to the block and returns the result of the block.
28
+ #
29
+ # @return [Object]
30
+ # @see https://ruby-doc.org/core-2.5.0/Object.html#method-i-yield_self
31
+ def yield_self
32
+ yield(self)
33
+ end
25
34
  end
26
35
  end
27
36
  end
@@ -1,28 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (C) 2017-2018 Dimitri Arrigoni <dimitri@arrigoni.me>
3
+ # Copyright (C) 2017-2021 Dimitri Arrigoni <dimitri@arrigoni.me>
4
4
  # License GPLv3+: GNU GPL version 3 or later
5
5
  # <http://www.gnu.org/licenses/gpl.html>.
6
6
  # This is free software: you are free to change and redistribute it.
7
7
  # There is NO WARRANTY, to the extent permitted by law.
8
8
 
9
- require_relative '../debug'
9
+ require_relative '../../project'
10
10
 
11
- # rubocop:disable Style/Documentation
12
-
13
- class Object
14
- private
15
-
16
- # Print arguments in pretty form
17
- #
11
+ Object.tap do |c|
18
12
  # @see https://ruby-doc.org/stdlib-2.4.0/libdoc/pp/rdoc/Kernel.html
19
- # @see https://github.com/topazproject/topaz/blob/master/lib-ruby/pp.rb
20
- def pp(*objs)
21
- debug = Kamaze::Project::Debug.new
22
- objs.each { |obj| debug.dump(obj) }
13
+ # @see https://github.com/topazproject/topaz/blob/bf4a56adbe03ae9ab4984729c733fcbc64a164c4/lib-ruby/pp.rb#L58
14
+ # @see https://github.com/ruby/ruby/blob/a8dc5156e183489c5121fb1759bda5d9406d9175/lib/pp.rb#L602
15
+ :pp.tap do |defined_method|
16
+ c.define_method(defined_method) do |*objs|
17
+ Kamaze::Project::Debug.new.yield_self do |printer|
18
+ objs.each { |obj| printer.dump(obj) }
23
19
 
24
- objs.size <= 1 ? objs.first : objs
20
+ objs.size <= 1 ? objs.first : objs
21
+ end
22
+ end
23
+
24
+ [:private].each { |method_name| c.__send__(method_name, defined_method) }
25
25
  end
26
26
  end
27
-
28
- # rubocop:enable Style/Documentation
@@ -1,14 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (C) 2017-2018 Dimitri Arrigoni <dimitri@arrigoni.me>
3
+ # Copyright (C) 2017-2021 Dimitri Arrigoni <dimitri@arrigoni.me>
4
4
  # License GPLv3+: GNU GPL version 3 or later
5
5
  # <http://www.gnu.org/licenses/gpl.html>.
6
6
  # This is free software: you are free to change and redistribute it.
7
7
  # There is NO WARRANTY, to the extent permitted by law.
8
8
 
9
9
  require_relative '../project'
10
- require 'tty/screen'
11
- require 'active_support/inflector'
12
10
 
13
11
  # Provides colored pretty-printer automagically
14
12
  #
@@ -16,8 +14,9 @@ require 'active_support/inflector'
16
14
  # @see https://github.com/pry/pry
17
15
  class Kamaze::Project::Debug
18
16
  def initialize
19
- @inflector ||= ActiveSupport::Inflector
20
- @printers = available_printers
17
+ self.tap do
18
+ @printers = load_printers.yield_self { available_printers }.freeze
19
+ end.freeze
21
20
  end
22
21
 
23
22
  class << self
@@ -50,7 +49,11 @@ class Kamaze::Project::Debug
50
49
  # @param [Fixnum] width
51
50
  # @see http://ruby-doc.org/stdlib-2.2.0/libdoc/pp/rdoc/PP.html
52
51
  def dump(obj, out = $stdout, width = nil)
53
- width ||= TTY::Screen.width || 79
52
+ width ||= screen_width || 79
53
+
54
+ unless out.respond_to?(:isatty)
55
+ out.singleton_class.define_method(:isatty) { false }
56
+ end
54
57
 
55
58
  printer_for(out).pp(obj, out, width)
56
59
  end
@@ -60,9 +63,7 @@ class Kamaze::Project::Debug
60
63
  # @param [IO] out
61
64
  # @return [PP]
62
65
  def printer_for(out)
63
- out_tty = out.respond_to?(:isatty) and out.isatty
64
-
65
- printers[out_tty ? 0 : 1]
66
+ printers.fetch(out.isatty ? 0 : 1)
66
67
  end
67
68
 
68
69
  # Get printers
@@ -71,52 +72,53 @@ class Kamaze::Project::Debug
71
72
  #
72
73
  # @return [Array<PP>]
73
74
  def available_printers
74
- load_printers
75
-
76
- default = '::PP'
77
-
78
- [
79
- proc do
80
- target = '::Pry::ColorPrinter'
81
-
82
- Kernel.const_defined?(target) ? target : default
83
- end.call,
84
- default
85
- ].map { |n| inflector.constantize(n) }.freeze
75
+ require 'dry/inflector'
76
+
77
+ '::PP'.yield_self do |default|
78
+ # @formatter:off
79
+ [
80
+ 'Pry::ColorPrinter'.yield_self do |cp|
81
+ Kernel.const_defined?(cp) ? cp : default
82
+ end,
83
+ default
84
+ ].map { |n| Dry::Inflector.new.constantize(n) }.freeze
85
+ # @formatter:on
86
+ end
86
87
  end
87
88
 
88
89
  protected
89
90
 
90
- # @return [Boolean|nil]
91
- attr_reader :warned
91
+ # @return [Integer]
92
+ def screen_width
93
+ require 'tty/screen'
92
94
 
93
- # @return [Class]
94
- attr_reader :inflector
95
+ TTY::Screen.width
96
+ end
95
97
 
96
- # Load printers requirements (on demand)
98
+ # Load printers requirements.
97
99
  #
98
100
  # @return [self]
99
101
  def load_printers
100
- Object.const_set('Pry', Class.new) unless Kernel.const_defined?('::Pry')
101
-
102
- begin
103
- load_printer_requirements
104
- rescue LoadError => e
105
- self.class.__send__('warned=', !!warn_error(e)) unless warned?
102
+ self.tap do
103
+ Object.const_set('Pry', Class.new) unless Kernel.const_defined?('::Pry')
104
+
105
+ begin
106
+ load_requirements
107
+ rescue LoadError => e
108
+ self.class.__send__('warned=', !!warn_error(e)) unless warned?
109
+ end
106
110
  end
107
-
108
- self
109
111
  end
110
112
 
113
+ # Load requirements.
114
+ #
111
115
  # @raise [LoadError]
112
116
  # @return [self]
113
- def load_printer_requirements
114
- ['pp',
115
- 'coderay',
116
- 'pry/pager',
117
- 'pry/color_printer'].each { |req| require req }
118
-
119
- self
117
+ def load_requirements
118
+ self.tap do
119
+ # noinspection RubyLiteralArrayInspection,RubyResolve
120
+ ['pp', 'coderay', 'pry'].each { |req| require req }
121
+ end
120
122
  end
121
123
 
122
124
  # Display the given exception message (followed by a newline) on STDERR
@@ -124,13 +126,10 @@ class Kamaze::Project::Debug
124
126
  # unless warnings are disabled (for example with the -W0 flag).
125
127
  #
126
128
  # @param [Exception] error
127
- # @return [Array<String>]
129
+ # @return [nil]
128
130
  def warn_error(error)
129
- formats = { from: caller(1..1).first, mssg: error.message }
130
- message = '%<from>s: %<mssg>s' % formats
131
-
132
- warn(message)
133
-
134
- formats.values
131
+ { from: caller(1..1).first, mssg: error.message }.tap do |formats|
132
+ return warn('%<from>s: %<mssg>s' % formats)
133
+ end
135
134
  end
136
135
  end