Skip to content

Commit

Permalink
💚 Load files only after entire gem has been written to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
pboling committed Sep 17, 2024
1 parent 47ce677 commit c807a95
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions lib/gem_bench/jersey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def doff_and_don(&block)

puts "Doffing #{gem_path}" if verbose
Dir.mktmpdir do |directory|
files = []
Dir[File.join(gem_path, "lib", "**", "*.rb")].map do |file|
if verbose
puts file
Expand All @@ -85,12 +86,13 @@ def doff_and_don(&block)
dir_path = File.join(directory, relative_path)
Dir.mkdir(dir_path) unless Dir.exist?(dir_path)
puts "creating #{filename} in #{dir_path}" if verbose
create_tempfile_copy(file, filename, dir_path, :dd1, &block)
files << create_tempfile_copy(file, filename, dir_path, :dd1, &block)
else
puts "directory not relative (#{directory}) for file #{filename}" if verbose
create_tempfile_copy(file, filename, directory, :dd2, &block)
files << create_tempfile_copy(file, filename, directory, :dd2, &block)
end
end
load_gem_copy(files)
end

nil
Expand All @@ -107,33 +109,42 @@ def as_klass

private

def load_gem_copy(files)
files.each do |filepath|
# begin
require filepath
# rescue LoadError => e
# puts file.to_s
# puts tempfile.path
# puts e.class
# puts e.message
# end
end
end

# @return [String] the file path of the new copy of the original file
def create_tempfile_copy(file, filename, directory, from, &block)
File.open(File.join(directory, "#{filename}.rb"), "w") do |tempfile|
new_jersey(file, tempfile, from, &block)
# Value of block is returned from File.open
File.open(File.join(directory, "#{filename}.rb"), "w") do |file_copy|
new_jersey(file, file_copy, from, &block)
end
end

def new_jersey(file, tempfile, from)
# @return [String] the file path of the new copy of the original file
def new_jersey(file, file_copy, from)
nj = File.read(file)
trades.each do |old_namespace, new_namespace|
nj.gsub!(old_namespace, new_namespace)
end
if verbose
puts "new_jersey has from: #{from}"
puts "new_jersey has file: #{file}"
puts "new_jersey path: #{tempfile.path}"
puts "new_jersey file_copy path: #{file_copy.path}"
end
nj = yield nj if block_given?
tempfile.write(nj)
tempfile.rewind
# begin
require tempfile.path
# rescue LoadError => e
# puts file.to_s
# puts tempfile.path
# puts e.class
# puts e.message
# end
file_copy.write(nj)
file_copy.close
file_copy.path
end
end
end

0 comments on commit c807a95

Please sign in to comment.