Skip to content

Commit ef1e833

Browse files
committed
Ensure the preprocessor can be run without Truffle module defined.
1 parent 466adc3 commit ef1e833

File tree

1 file changed

+59
-56
lines changed

1 file changed

+59
-56
lines changed

lib/truffle/truffle/cext_preprocessor.rb

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,89 +9,92 @@
99
require_relative 'patches/json_patches'
1010
require_relative 'patches/nokogiri_patches'
1111
require_relative 'patches/pg_patches'
12+
module Truffle
13+
module CExt
14+
class Preprocessor
1215

13-
class Truffle::CExt::Preprocessor
16+
PATCHED_FILES = {}
1417

15-
PATCHED_FILES = {}
16-
17-
def self.add_gem_patches(patch_hash, gem_patches)
18-
gem = gem_patches[:gem]
19-
patch_list = gem_patches[:patches]
20-
patch_list.each do |path_parts, patch|
21-
processed_patch = {}
22-
if path_parts.kind_of?(String)
23-
key = path_parts
24-
else
25-
key = path_parts.last
26-
processed_patch[:ext_dir] = path_parts.first if path_parts.size > 1
18+
def self.add_gem_patches(patch_hash, gem_patches)
19+
gem = gem_patches[:gem]
20+
patch_list = gem_patches[:patches]
21+
patch_list.each do |path_parts, patch|
22+
processed_patch = {}
23+
if path_parts.kind_of?(String)
24+
key = path_parts
25+
else
26+
key = path_parts.last
27+
processed_patch[:ext_dir] = path_parts.first if path_parts.size > 1
28+
end
29+
processed_patch[:patches] = patch
30+
processed_patch[:gem] = gem
31+
raise "Duplicate patch file #{key}." if patch_hash.include?(key)
32+
patch_hash[key] = processed_patch
33+
end
2734
end
28-
processed_patch[:patches] = patch
29-
processed_patch[:gem] = gem
30-
raise "Duplicate patch file #{key}." if patch_hash.include?(key)
31-
patch_hash[key] = processed_patch
32-
end
33-
end
3435

35-
add_gem_patches(PATCHED_FILES, ::JsonPatches::PATCHES)
36-
add_gem_patches(PATCHED_FILES, ::NokogiriPatches::PATCHES)
37-
add_gem_patches(PATCHED_FILES, ::PgPatches::PATCHES)
36+
add_gem_patches(PATCHED_FILES, ::JsonPatches::PATCHES)
37+
add_gem_patches(PATCHED_FILES, ::NokogiriPatches::PATCHES)
38+
add_gem_patches(PATCHED_FILES, ::PgPatches::PATCHES)
3839

39-
def self.makefile_matcher(command1, command2)
40-
file_list = {}
41-
PATCHED_FILES.each_pair do |file, patch|
42-
dir = if patch[:ext_dir]
43-
File.join(patch[:gem], 'ext', patch[:ext_dir])
44-
else
45-
"/#{patch[:gem]}"
46-
end
47-
(file_list[dir] ||= []) << file
48-
end
40+
def self.makefile_matcher(command1, command2)
41+
file_list = {}
42+
PATCHED_FILES.each_pair do |file, patch|
43+
dir = if patch[:ext_dir]
44+
File.join(patch[:gem], 'ext', patch[:ext_dir])
45+
else
46+
"/#{patch[:gem]}"
47+
end
48+
(file_list[dir] ||= []) << file
49+
end
4950

50-
make_function = <<-EOF
51+
make_function = <<-EOF
5152
$(if\\
5253
$(or\\
5354
EOF
54-
file_list.each_pair do |dir, files|
55-
if !files.empty?
56-
make_function += <<-EOF
55+
file_list.each_pair do |dir, files|
56+
if !files.empty?
57+
make_function += <<-EOF
5758
$(and\\
5859
$(findstring #{dir}, $(realpath $(<))),\\
5960
$(or\\
6061
EOF
61-
files.each do |file|
62-
make_function += <<-EOF
62+
files.each do |file|
63+
make_function += <<-EOF
6364
$(findstring #{file}, $(<)),\\
6465
EOF
65-
end
66-
make_function += <<-EOF
66+
end
67+
make_function += <<-EOF
6768
)\\
6869
),\\
6970
EOF
70-
end
71-
end
72-
make_function += <<-EOF
71+
end
72+
end
73+
make_function += <<-EOF
7374
),\\
7475
#{command1},\\
7576
#{command2}\\
7677
)
7778
EOF
7879

79-
end
80+
end
8081

81-
def self.patch(file, contents, directory)
82-
if patched_file = PATCHED_FILES[File.basename(file)]
83-
matched = if patched_file[:ext_dir]
84-
directory.end_with?(File.join(patched_file[:gem], 'ext', patched_file[:ext_dir]))
85-
else
86-
regexp = /^#{Regexp.escape(patched_file[:gem])}\b/
87-
directory.split('/').last(3).any? { |part| part =~ regexp } || file.split('/').last(2).any? { |part| part =~ regexp }
88-
end
89-
if matched
90-
patched_file[:patches].each do |patch|
91-
contents = contents.gsub(patch[:match], patch[:replacement].rstrip)
82+
def self.patch(file, contents, directory)
83+
if patched_file = PATCHED_FILES[File.basename(file)]
84+
matched = if patched_file[:ext_dir]
85+
directory.end_with?(File.join(patched_file[:gem], 'ext', patched_file[:ext_dir]))
86+
else
87+
regexp = /^#{Regexp.escape(patched_file[:gem])}\b/
88+
directory.split('/').last(3).any? { |part| part =~ regexp } || file.split('/').last(2).any? { |part| part =~ regexp }
89+
end
90+
if matched
91+
patched_file[:patches].each do |patch|
92+
contents = contents.gsub(patch[:match], patch[:replacement].rstrip)
93+
end
94+
end
9295
end
96+
contents
9397
end
9498
end
95-
contents
9699
end
97100
end

0 commit comments

Comments
 (0)