toys-core 0.15.6 → 0.17.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.
@@ -304,7 +304,7 @@ module Toys
304
304
  if cmd.is_a?(::Array)
305
305
  if cmd.size > 1
306
306
  binary = canonical_binary_spec(cmd.first, exec_opts)
307
- [binary] + cmd[1..-1].map(&:to_s)
307
+ [binary] + cmd[1..].map(&:to_s)
308
308
  else
309
309
  [canonical_binary_spec(Array(cmd.first), exec_opts)]
310
310
  end
@@ -521,14 +521,12 @@ module Toys
521
521
  def capture(which)
522
522
  stream = stream_for(which)
523
523
  @join_threads << ::Thread.new do
524
- begin
525
- data = stream.read
526
- @mutex.synchronize do
527
- @captures[which] = data
528
- end
529
- ensure
530
- stream.close
524
+ data = stream.read
525
+ @mutex.synchronize do
526
+ @captures[which] = data
531
527
  end
528
+ ensure
529
+ stream.close
532
530
  end
533
531
  self
534
532
  end
@@ -577,16 +575,14 @@ module Toys
577
575
  end
578
576
  stream = stream_for(which, allow_in: true)
579
577
  @join_threads << ::Thread.new do
580
- begin
581
- if which == :in
582
- ::IO.copy_stream(io, stream)
583
- else
584
- ::IO.copy_stream(stream, io)
585
- end
586
- ensure
587
- stream.close
588
- io.close
578
+ if which == :in
579
+ ::IO.copy_stream(io, stream)
580
+ else
581
+ ::IO.copy_stream(stream, io)
589
582
  end
583
+ ensure
584
+ stream.close
585
+ io.close
590
586
  end
591
587
  self
592
588
  end
@@ -1065,7 +1061,7 @@ module Toys
1065
1061
  else
1066
1062
  cmd_binary = @spawn_cmd.first
1067
1063
  cmd_binary = cmd_binary.first if cmd_binary.is_a?(::Array)
1068
- "exec: #{([cmd_binary] + @spawn_cmd[1..-1]).inspect}"
1064
+ "exec: #{([cmd_binary] + @spawn_cmd[1..]).inspect}"
1069
1065
  end
1070
1066
  end
1071
1067
  end
@@ -1218,7 +1214,7 @@ module Toys
1218
1214
 
1219
1215
  def interpret_in_array(setting)
1220
1216
  if setting.first.is_a?(::Symbol)
1221
- setup_in_stream_of_type(setting.first, setting[1..-1])
1217
+ setup_in_stream_of_type(setting.first, setting[1..])
1222
1218
  elsif setting.first.is_a?(::String)
1223
1219
  setup_in_stream_of_type(:file, setting)
1224
1220
  elsif setting.size == 2 && setting.first.is_a?(::IO) && setting.last.is_a?(::IO)
@@ -1292,7 +1288,7 @@ module Toys
1292
1288
 
1293
1289
  def interpret_out_array(key, setting)
1294
1290
  if setting.first.is_a?(::Symbol)
1295
- setup_out_stream_of_type(key, setting.first, setting[1..-1])
1291
+ setup_out_stream_of_type(key, setting.first, setting[1..])
1296
1292
  elsif setting.first.is_a?(::String)
1297
1293
  setup_out_stream_of_type(key, :file, setting)
1298
1294
  elsif setting.size == 2 && setting.first.is_a?(::IO) && setting.last.is_a?(::IO)
@@ -1381,7 +1377,7 @@ module Toys
1381
1377
  (arg.first == :autoclose || arg.first.is_a?(::IO))
1382
1378
  [arg.last, :close]
1383
1379
  else
1384
- arg = arg[1..-1] if arg.first == :file
1380
+ arg = arg[1..] if arg.first == :file
1385
1381
  if arg.empty? || !arg.first.is_a?(::String)
1386
1382
  raise "Expected file name for #{key} tee argument"
1387
1383
  end
@@ -1498,49 +1494,41 @@ module Toys
1498
1494
  def write_string_thread(string)
1499
1495
  stream = make_in_pipe
1500
1496
  @join_threads << ::Thread.new do
1501
- begin
1502
- stream.write string
1503
- ensure
1504
- stream.close
1505
- end
1497
+ stream.write string
1498
+ ensure
1499
+ stream.close
1506
1500
  end
1507
1501
  end
1508
1502
 
1509
1503
  def copy_to_in_thread(io)
1510
1504
  stream = make_in_pipe
1511
1505
  @join_threads << ::Thread.new do
1512
- begin
1513
- ::IO.copy_stream(io, stream)
1514
- ensure
1515
- stream.close
1516
- io.close
1517
- end
1506
+ ::IO.copy_stream(io, stream)
1507
+ ensure
1508
+ stream.close
1509
+ io.close
1518
1510
  end
1519
1511
  end
1520
1512
 
1521
1513
  def copy_from_out_thread(key, io)
1522
1514
  stream = make_out_pipe(key)
1523
1515
  @join_threads << ::Thread.new do
1524
- begin
1525
- ::IO.copy_stream(stream, io)
1526
- ensure
1527
- stream.close
1528
- io.close
1529
- end
1516
+ ::IO.copy_stream(stream, io)
1517
+ ensure
1518
+ stream.close
1519
+ io.close
1530
1520
  end
1531
1521
  end
1532
1522
 
1533
1523
  def capture_stream_thread(key)
1534
1524
  stream = make_out_pipe(key)
1535
1525
  @join_threads << ::Thread.new do
1536
- begin
1537
- data = stream.read
1538
- @mutex.synchronize do
1539
- @captures[key] = data
1540
- end
1541
- ensure
1542
- stream.close
1526
+ data = stream.read
1527
+ @mutex.synchronize do
1528
+ @captures[key] = data
1543
1529
  end
1530
+ ensure
1531
+ stream.close
1544
1532
  end
1545
1533
  end
1546
1534
  end
@@ -146,11 +146,9 @@ module Toys
146
146
  #
147
147
  def activate(name, *requirements)
148
148
  Gems.synchronize do
149
- begin
150
- gem(name, *requirements)
151
- rescue ::Gem::LoadError => e
152
- handle_activation_error(e, name, requirements)
153
- end
149
+ gem(name, *requirements)
150
+ rescue ::Gem::LoadError => e
151
+ handle_activation_error(e, name, requirements)
154
152
  end
155
153
  end
156
154
 
@@ -395,7 +393,7 @@ module Toys
395
393
  modified_lockfile_path = find_lockfile_path(modified_gemfile_path)
396
394
  if ::File.readable?(lockfile_path)
397
395
  lockfile_content = ::File.read(lockfile_path)
398
- ::File.open(modified_lockfile_path, "w") { |file| file.write(lockfile_content) }
396
+ ::File.write(modified_lockfile_path, lockfile_content)
399
397
  end
400
398
  modified_gemfile_path
401
399
  end
@@ -423,9 +421,11 @@ module Toys
423
421
  end
424
422
 
425
423
  def delete_modified_gemfile(modified_gemfile_path)
424
+ # rubocop:disable Lint/NonAtomicFileOperation
426
425
  ::File.delete(modified_gemfile_path) if ::File.exist?(modified_gemfile_path)
427
426
  modified_lockfile_path = find_lockfile_path(modified_gemfile_path)
428
427
  ::File.delete(modified_lockfile_path) if ::File.exist?(modified_lockfile_path)
428
+ # rubocop:enable Lint/NonAtomicFileOperation
429
429
  end
430
430
 
431
431
  def restore_toys_libs
@@ -465,7 +465,7 @@ module Toys
465
465
  return if result.success?
466
466
  terminal.puts("Failed to update. Trying update with clean lockfile...")
467
467
  lockfile_path = find_lockfile_path(gemfile_path)
468
- ::File.delete(lockfile_path) if ::File.exist?(lockfile_path)
468
+ ::File.delete(lockfile_path) if ::File.exist?(lockfile_path) # rubocop:disable Lint/NonAtomicFileOperation
469
469
  result = exec_util.exec_ruby([bundler_bin, "update"] + args)
470
470
  return if result.success?
471
471
  raise ::Bundler::InstallError, "Failed to install or update bundle: #{gemfile_path}"
@@ -274,6 +274,21 @@ module Toys
274
274
  end
275
275
  end
276
276
 
277
+ ##
278
+ # Returns whether shared source files are writable by default.
279
+ # Normally, shared sources are made read-only to protect them from being
280
+ # modified accidentally since multiple clients may be accessing them.
281
+ # However, you can disable this feature by setting the environment
282
+ # variable `TOYS_GIT_CACHE_WRITABLE` to any non-empty value. This can be
283
+ # useful in environments that want to clean up temporary directories and
284
+ # are being hindered by read-only files.
285
+ #
286
+ # @return [boolean]
287
+ #
288
+ def self.sources_writable?
289
+ !::ENV["TOYS_GIT_CACHE_WRITABLE"].to_s.empty?
290
+ end
291
+
277
292
  ##
278
293
  # Access a git cache.
279
294
  #
@@ -318,7 +333,7 @@ module Toys
318
333
  # @param into [String] If provided, copies the specified files into the
319
334
  # given directory path. If omitted or `nil`, populates and returns a
320
335
  # shared source file or directory.
321
- # @param update [Boolean,Integer] Whether to update of non-SHA commit
336
+ # @param update [Boolean,Integer] Whether to update non-SHA commit
322
337
  # references if they were previously loaded. This is useful, for
323
338
  # example, if the commit is `HEAD` or a branch name. Pass `true` or
324
339
  # `false` to specify whether to update, or an integer to update if
@@ -545,7 +560,7 @@ module Toys
545
560
  repo_dir = ::File.join(dir, REPO_DIR_NAME)
546
561
  is_sha = commit =~ /^[0-9a-f]{40}$/
547
562
  update = repo_lock.ref_stale?(commit, update) unless is_sha
548
- if update && !is_sha || !commit_exists?(repo_dir, local_commit)
563
+ if (update && !is_sha) || !commit_exists?(repo_dir, local_commit)
549
564
  git(repo_dir, ["fetch", "--depth=1", "--force", "origin", "#{commit}:#{local_commit}"],
550
565
  error_message: "Unable to fetch commit: #{commit}")
551
566
  repo_lock.update_ref!(commit)
@@ -569,7 +584,7 @@ module Toys
569
584
  ::FileUtils.mkdir_p(source_path)
570
585
  ::FileUtils.chmod_R("u+w", source_path, force: true)
571
586
  copy_from_repo(repo_path, source_path, sha, path)
572
- ::FileUtils.chmod_R("a-w", source_path, force: true)
587
+ ::FileUtils.chmod_R("a-w", source_path, force: true) unless GitCache.sources_writable?
573
588
  end
574
589
  repo_lock.access_source!(sha, path)
575
590
  path == "." ? source_path : ::File.join(source_path, path)
@@ -579,7 +594,7 @@ module Toys
579
594
  repo_path = ::File.join(dir, REPO_DIR_NAME)
580
595
  ::FileUtils.mkdir_p(into)
581
596
  ::FileUtils.chmod_R("u+w", into, force: true)
582
- Compat.dir_children(into).each { |child| ::FileUtils.rm_rf(::File.join(into, child)) }
597
+ ::Dir.children(into).each { |child| ::FileUtils.rm_rf(::File.join(into, child)) }
583
598
  result = copy_from_repo(repo_path, into, sha, path)
584
599
  repo_lock.access_repo!
585
600
  result
@@ -588,7 +603,7 @@ module Toys
588
603
  def copy_from_repo(repo_dir, into, sha, path)
589
604
  git(repo_dir, ["checkout", sha])
590
605
  if path == "."
591
- Compat.dir_children(repo_dir).each do |entry|
606
+ ::Dir.children(repo_dir).each do |entry|
592
607
  next if entry == ".git"
593
608
  to_path = ::File.join(into, entry)
594
609
  unless ::File.exist?(to_path)
@@ -746,6 +761,9 @@ module Toys
746
761
  @data["sources"][sha]&.fetch(path, nil)
747
762
  end
748
763
 
764
+ ##
765
+ # @private
766
+ #
749
767
  def find_sources(paths: nil, shas: nil)
750
768
  results = []
751
769
  @data["sources"].each do |sha, sha_data|
@@ -312,7 +312,7 @@ module Toys
312
312
  remaining_doc = desc
313
313
  if initial.size <= @indent + @left_column_width
314
314
  @lines << "#{initial} #{desc.first}"
315
- remaining_doc = desc[1..-1] || []
315
+ remaining_doc = desc[1..] || []
316
316
  else
317
317
  @lines << initial
318
318
  end
@@ -398,7 +398,7 @@ module Toys
398
398
  else
399
399
  desc = wrap_indent_indent2(WrappableString.new(["#{prefix} -"] + desc.fragments))
400
400
  @lines << indent_str(desc[0])
401
- desc[1..-1].each do |line|
401
+ desc[1..].each do |line|
402
402
  @lines << indent2_str(line)
403
403
  end
404
404
  end
@@ -523,7 +523,7 @@ module Toys
523
523
  desc += delegate.long_desc
524
524
  end
525
525
  end
526
- desc = desc[1..-1] if desc.first == ""
526
+ desc = desc[1..] if desc.first == ""
527
527
  desc = wrap_indent(desc)
528
528
  return if desc.empty?
529
529
  @lines << ""
@@ -728,7 +728,7 @@ module Toys
728
728
  else
729
729
  desc = wrap_indent(WrappableString.new(["#{prefix} -"] + desc.fragments))
730
730
  @lines << desc[0]
731
- desc[1..-1].each do |line|
731
+ desc[1..].each do |line|
732
732
  @lines << indent_str(line)
733
733
  end
734
734
  end
@@ -64,11 +64,9 @@ module Toys
64
64
  def start
65
65
  if @command
66
66
  result = @exec_service.exec(@command, in: :controller) do |controller|
67
- begin
68
- yield controller.in if controller.pid
69
- rescue ::Errno::EPIPE => e
70
- raise e unless @rescue_broken_pipes
71
- end
67
+ yield controller.in if controller.pid
68
+ rescue ::Errno::EPIPE => e
69
+ raise e unless @rescue_broken_pipes
72
70
  end
73
71
  return result.exit_code unless result.failed?
74
72
  end
@@ -155,12 +155,10 @@ module Toys
155
155
  #
156
156
  def write(str = "", *styles)
157
157
  @output_mutex.synchronize do
158
- begin
159
- output&.write(apply_styles(str.to_s, *styles))
160
- output&.flush
161
- rescue ::IOError
162
- nil
163
- end
158
+ output&.write(apply_styles(str.to_s, *styles))
159
+ output&.flush
160
+ rescue ::IOError
161
+ nil
164
162
  end
165
163
  self
166
164
  end
@@ -173,11 +171,9 @@ module Toys
173
171
  #
174
172
  def readline
175
173
  @input_mutex.synchronize do
176
- begin
177
- input&.gets
178
- rescue ::IOError
179
- nil
180
- end
174
+ input&.gets
175
+ rescue ::IOError
176
+ nil
181
177
  end
182
178
  end
183
179
 
@@ -470,7 +466,7 @@ module Toys
470
466
  @terminal.write(@frames[@cur_frame][0])
471
467
  @cond.wait(@frame_length)
472
468
  size = @frames[@cur_frame][1]
473
- @terminal.write("\b" * size + " " * size + "\b" * size)
469
+ @terminal.write(("\b" * size) + (" " * size) + ("\b" * size))
474
470
  @cur_frame += 1
475
471
  @cur_frame = 0 if @cur_frame >= @frames.size
476
472
  end
@@ -260,7 +260,7 @@ module Toys
260
260
 
261
261
  def validate_dir_env(name)
262
262
  path = @env[name].to_s
263
- !path.empty? && Compat.absolute_path?(path) ? path : nil
263
+ !path.empty? && ::File.absolute_path?(path) ? path : nil
264
264
  end
265
265
 
266
266
  def validate_dirs_env(name)
@@ -268,7 +268,7 @@ module Toys
268
268
  end
269
269
 
270
270
  def validate_dirs(paths)
271
- paths = paths.find_all { |path| Compat.absolute_path?(path) }
271
+ paths = paths.find_all { |path| ::File.absolute_path?(path) }
272
272
  paths.empty? ? nil : paths
273
273
  end
274
274
 
data/lib/toys-core.rb CHANGED
@@ -120,10 +120,11 @@ module Toys
120
120
  CORE_LIB_PATH = __dir__
121
121
  end
122
122
 
123
+ require "toys/compat"
124
+
123
125
  require "toys/acceptor"
124
126
  require "toys/arg_parser"
125
127
  require "toys/cli"
126
- require "toys/compat"
127
128
  require "toys/completion"
128
129
  require "toys/context"
129
130
  require "toys/core"
metadata CHANGED
@@ -1,15 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toys-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.6
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Azuma
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-05-15 00:00:00.000000000 Z
12
- dependencies: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: logger
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.4'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.4'
13
26
  description: Toys-Core is the command line tool framework underlying Toys. It can
14
27
  be used to create command line executables using the Toys DSL and classes.
15
28
  email:
@@ -79,11 +92,10 @@ homepage: https://github.com/dazuma/toys
79
92
  licenses:
80
93
  - MIT
81
94
  metadata:
82
- changelog_uri: https://dazuma.github.io/toys/gems/toys-core/v0.15.6/file.CHANGELOG.html
95
+ changelog_uri: https://dazuma.github.io/toys/gems/toys-core/v0.17.0/file.CHANGELOG.html
83
96
  source_code_uri: https://github.com/dazuma/toys/tree/main/toys-core
84
97
  bug_tracker_uri: https://github.com/dazuma/toys/issues
85
- documentation_uri: https://dazuma.github.io/toys/gems/toys-core/v0.15.6
86
- post_install_message:
98
+ documentation_uri: https://dazuma.github.io/toys/gems/toys-core/v0.17.0
87
99
  rdoc_options: []
88
100
  require_paths:
89
101
  - lib
@@ -91,15 +103,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
103
  requirements:
92
104
  - - ">="
93
105
  - !ruby/object:Gem::Version
94
- version: 2.4.0
106
+ version: 2.7.0
95
107
  required_rubygems_version: !ruby/object:Gem::Requirement
96
108
  requirements:
97
109
  - - ">="
98
110
  - !ruby/object:Gem::Version
99
111
  version: '0'
100
112
  requirements: []
101
- rubygems_version: 3.5.9
102
- signing_key:
113
+ rubygems_version: 3.6.9
103
114
  specification_version: 4
104
115
  summary: Framework for creating command line executables
105
116
  test_files: []