toys 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 562abaf505246d6b3a84c800908b19cfe9acbc6a57d5432bceccac31eaa107ef
4
- data.tar.gz: 39efb6120be94eeb4479e8ae4fe782d796a49e5dcfd4e837735dd69986e3cfb4
3
+ metadata.gz: 4a2d413fba0285f718e4d586832a78010060e68e52ad2f0ef7df54e4295a71b6
4
+ data.tar.gz: 431cbcb0425cf2bcde91c145a574238293d83c47b81668abf9c67de6cc16d6cc
5
5
  SHA512:
6
- metadata.gz: fe6196e43402bce7c7e916dcc75906e5b420ce1882ed0ca6104b6f0a54c6263feba8bf2284c1e8b8d2ca783ed1dacb291078622912f84483628b9f003397ca9e
7
- data.tar.gz: bd24a58dcdc1fd2f34c50b97d57129e031f17aaa77044af1c6b62a86d67a1ba32506d3234156cb66576f282de82953e95cf52460fc409f86a82ff5d6c64e42de
6
+ metadata.gz: 2f7988dd0108eb50afc3a44dabf5b2d3a04f272af2c6d58135a525250d50c42d5e7902e1f1c07768cabb80fb88cfca0e8a2513fe2a5274a7c1a1d1ac90c02650
7
+ data.tar.gz: b5199ca795795821bc7ba6605ec276995e9a29b73c597c5700424de0a0f01ba5d9a39392d24528c981e9d05956dbbd2691aeb513a22413ff84610ce122d29ee1
@@ -17,7 +17,7 @@ name "update" do
17
17
 
18
18
  short_desc "Update toys if a newer version is available"
19
19
 
20
- helper_module :exec
20
+ use :exec
21
21
 
22
22
  execute do
23
23
  version_info = capture("gem query -q -r -e toys")
data/lib/toys/cli.rb CHANGED
@@ -4,19 +4,21 @@ module Toys
4
4
  DEFAULT_DIR_NAME = ".toys"
5
5
  DEFAULT_FILE_NAME = ".toys.rb"
6
6
  DEFAULT_BINARY_NAME = "toys"
7
- ETC_PATH = "/etc"
7
+ DEFAULT_PRELOAD_NAME = ".preload.rb"
8
8
 
9
9
  def initialize(
10
10
  binary_name: nil,
11
11
  logger: nil,
12
12
  config_dir_name: nil,
13
13
  config_file_name: nil,
14
- index_file_name: nil
14
+ index_file_name: nil,
15
+ preload_file_name: nil
15
16
  )
16
17
  @lookup = Toys::Lookup.new(
17
18
  config_dir_name: config_dir_name,
18
19
  config_file_name: config_file_name,
19
- index_file_name: index_file_name)
20
+ index_file_name: index_file_name,
21
+ preload_file_name: preload_file_name)
20
22
  @context = Context.new(
21
23
  @lookup,
22
24
  logger: logger || self.class.default_logger,
@@ -47,6 +49,15 @@ module Toys
47
49
  self
48
50
  end
49
51
 
52
+ def add_standard_config_paths
53
+ toys_path = ENV["TOYS_PATH"].to_s.split(File::PATH_SEPARATOR)
54
+ if toys_path.size == 0
55
+ toys_path << ENV["HOME"] if ENV["HOME"]
56
+ toys_path << "/etc" if File.directory?("/etc") && File.readable?("/etc")
57
+ end
58
+ @lookup.add_config_paths(toys_path)
59
+ end
60
+
50
61
  def run(*args)
51
62
  @context.run(*args)
52
63
  end
@@ -57,12 +68,11 @@ module Toys
57
68
  binary_name: DEFAULT_BINARY_NAME,
58
69
  config_dir_name: DEFAULT_DIR_NAME,
59
70
  config_file_name: DEFAULT_FILE_NAME,
60
- index_file_name: DEFAULT_FILE_NAME
71
+ index_file_name: DEFAULT_FILE_NAME,
72
+ preload_file_name: DEFAULT_PRELOAD_NAME
61
73
  )
62
74
  cli.add_config_path_hierarchy
63
- if !File.directory?(ETC_PATH) || !File.readable?(ETC_PATH)
64
- cli.add_config_paths(ETC_PATH)
65
- end
75
+ cli.add_standard_config_paths
66
76
  cli.add_paths(BUILTINS_PATH)
67
77
  cli
68
78
  end
data/lib/toys/lookup.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module Toys
2
2
  class Lookup
3
- def initialize(config_dir_name: nil, config_file_name: nil, index_file_name: nil)
3
+ def initialize(config_dir_name: nil, config_file_name: nil,
4
+ index_file_name: nil, preload_file_name: nil)
4
5
  @config_dir_name = config_dir_name
5
6
  if @config_dir_name && File.extname(@config_dir_name) == ".rb"
6
7
  raise LookupError, "Illegal config dir name #{@config_dir_name.inspect}"
@@ -13,6 +14,10 @@ module Toys
13
14
  if @index_file_name && File.extname(@index_file_name) != ".rb"
14
15
  raise LookupError, "Illegal index file name #{@index_file_name.inspect}"
15
16
  end
17
+ @preload_file_name = preload_file_name
18
+ if @preload_file_name && File.extname(@preload_file_name) != ".rb"
19
+ raise LookupError, "Illegal preload file name #{@preload_file_name.inspect}"
20
+ end
16
21
  @load_worklist = []
17
22
  @tools = {[] => [Tool.new(nil, nil), nil]}
18
23
  @max_priority = @min_priority = 0
@@ -33,9 +38,7 @@ module Toys
33
38
  paths = Array(paths)
34
39
  paths = paths.reverse if high_priority
35
40
  paths.each do |path|
36
- if !File.directory?(path) || !File.readable?(path)
37
- raise LookupError, "Cannot read config directory #{path}"
38
- end
41
+ path = check_path(path, type: :dir)
39
42
  priority = high_priority ? (@max_priority += 1) : (@min_priority -= 1)
40
43
  if @config_file_name
41
44
  p = File.join(path, @config_file_name)
@@ -135,13 +138,23 @@ module Toys
135
138
  Parser.parse(path, tool, remaining_words, priority, self, IO.read(path))
136
139
  end
137
140
  else
138
- children = Dir.entries(path) - [".", ".."]
139
- children.each do |child|
140
- child_path = File.join(path, child)
141
- if child == @index_file_name
142
- load_path(check_path(child_path), words, remaining_words, priority)
143
- else
144
- next unless check_path(child_path, strict: false)
141
+ if @preload_file_name
142
+ preload_path = File.join(path, @preload_file_name)
143
+ if File.exist?(preload_path)
144
+ preload_path = check_path(preload_path, type: :file)
145
+ require preload_path
146
+ end
147
+ end
148
+ if @index_file_name
149
+ index_path = File.join(path, @index_file_name)
150
+ if File.exist?(index_path)
151
+ index_path = check_path(index_path, type: :file)
152
+ load_path(index_path, words, remaining_words, priority)
153
+ end
154
+ end
155
+ Dir.entries(path).each do |child|
156
+ if !child.start_with?(".") && child != @preload_file_name && child != @index_file_name
157
+ child_path = check_path(File.join(path, child))
145
158
  child_word = File.basename(child, ".rb")
146
159
  next_words = words + [child_word]
147
160
  next_remaining_words =
@@ -158,19 +171,22 @@ module Toys
158
171
  end
159
172
  end
160
173
 
161
- def check_path(path, strict: true)
162
- if File.extname(path) == ".rb"
174
+ def check_path(path, lenient: false, type: nil)
175
+ path = File.expand_path(path)
176
+ type ||= File.extname(path) == ".rb" ? :file : :dir
177
+ case type
178
+ when :file
163
179
  if File.directory?(path) || !File.readable?(path)
180
+ return nil if lenient
164
181
  raise LookupError, "Cannot read file #{path}"
165
182
  end
166
- else
183
+ when :dir
167
184
  if !File.directory?(path) || !File.readable?(path)
168
- if strict
169
- raise LookupError, "Cannot read directory #{path}"
170
- else
171
- return nil
172
- end
185
+ return nil if lenient
186
+ raise LookupError, "Cannot read directory #{path}"
173
187
  end
188
+ else
189
+ raise ArgumentError, "Illegal type #{type}"
174
190
  end
175
191
  path
176
192
  end
data/lib/toys/parser.rb CHANGED
@@ -114,12 +114,8 @@ module Toys
114
114
  self
115
115
  end
116
116
 
117
- def helper_module(mod, &block)
118
- if block
119
- @tool.define_helper_module(mod, &block)
120
- else
121
- @tool.use_helper_module(mod)
122
- end
117
+ def use(mod)
118
+ @tool.use_helper_module(mod)
123
119
  self
124
120
  end
125
121
 
@@ -32,8 +32,8 @@ module Toys
32
32
  name toy_name do
33
33
  short_desc "#{push_gem ? 'Release' : 'Build'} the gem: #{gem_name}"
34
34
 
35
- helper_module :file_utils
36
- helper_module :exec
35
+ use :file_utils
36
+ use :exec
37
37
 
38
38
  execute do
39
39
  gemspec = Gem::Specification.load "#{gem_name}.gemspec"
@@ -30,7 +30,7 @@ module Toys
30
30
  name toy_name do
31
31
  short_desc "Run minitest"
32
32
 
33
- helper_module :exec
33
+ use :exec
34
34
 
35
35
  execute do
36
36
  sh(cmd, report_subprocess_errors: true)
data/lib/toys/tool.rb CHANGED
@@ -26,8 +26,6 @@ module Toys
26
26
  @helpers = {}
27
27
  @modules = []
28
28
  @executor = nil
29
-
30
- @defined_modules = {}
31
29
  end
32
30
 
33
31
  attr_reader :simple_name
@@ -105,29 +103,6 @@ module Toys
105
103
  @alias_target = target_tool
106
104
  end
107
105
 
108
- def define_helper_module(name, &block)
109
- if @alias_target
110
- raise ToolDefinitionError, "Tool #{display_name.inspect} is an alias"
111
- end
112
- unless name.is_a?(String)
113
- raise ToolDefinitionError,
114
- "Helper module name #{name.inspect} is not a string"
115
- end
116
- if @defined_modules.key?(name)
117
- raise ToolDefinitionError,
118
- "Helper module #{name.inspect} is already defined"
119
- end
120
- mod = Module.new(&block)
121
- mod.instance_methods.each do |meth|
122
- name_str = meth.to_s
123
- unless name_str =~ /^[a-z]\w+$/
124
- raise ToolDefinitionError,
125
- "Illegal helper method name: #{name_str.inspect}"
126
- end
127
- end
128
- @defined_modules[name] = mod
129
- end
130
-
131
106
  def short_desc=(str)
132
107
  check_definition_state
133
108
  @short_desc = str
@@ -158,8 +133,6 @@ module Toys
158
133
  require "toys/helpers/#{file_name}"
159
134
  const_name = mod.gsub(/(^|_)([a-zA-Z0-9])/){ |m| $2.upcase }
160
135
  @modules << Toys::Helpers.const_get(const_name)
161
- when String
162
- @modules << mod
163
136
  else
164
137
  raise ToolDefinitionError, "Illegal helper module name: #{mod.inspect}"
165
138
  end
@@ -220,12 +193,6 @@ module Toys
220
193
 
221
194
  protected
222
195
 
223
- def find_module_named(name)
224
- return @defined_modules[name] if @defined_modules.key?(name)
225
- return @parent.find_module_named(name) if @parent
226
- nil
227
- end
228
-
229
196
  def ensure_collection_only(source_name)
230
197
  if has_definition?
231
198
  raise ToolDefinitionError, "Cannot create tool #{source_name.inspect}" \
@@ -393,11 +360,6 @@ module Toys
393
360
  full_name, args, execution_data[:options])
394
361
  context.logger.level += execution_data[:delta_severity]
395
362
  @modules.each do |mod|
396
- unless Module === mod
397
- found = find_module_named(mod)
398
- raise StandardError, "Unable to find module #{mod}" unless found
399
- mod = found
400
- end
401
363
  context.extend(mod)
402
364
  end
403
365
  @helpers.each do |name, block|
data/lib/toys/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Toys
2
- VERSION = "0.2.0".freeze
2
+ VERSION = "0.2.1".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toys
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Azuma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-01 00:00:00.000000000 Z
11
+ date: 2018-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler