|
9 | 9 | require_relative 'patches/json_patches'
|
10 | 10 | require_relative 'patches/nokogiri_patches'
|
11 | 11 | require_relative 'patches/pg_patches'
|
| 12 | +module Truffle |
| 13 | + module CExt |
| 14 | + class Preprocessor |
12 | 15 |
|
13 |
| -class Truffle::CExt::Preprocessor |
| 16 | + PATCHED_FILES = {} |
14 | 17 |
|
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 |
27 | 34 | 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 |
34 | 35 |
|
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) |
38 | 39 |
|
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 |
49 | 50 |
|
50 |
| - make_function = <<-EOF |
| 51 | + make_function = <<-EOF |
51 | 52 | $(if\\
|
52 | 53 | $(or\\
|
53 | 54 | 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 |
57 | 58 | $(and\\
|
58 | 59 | $(findstring #{dir}, $(realpath $(<))),\\
|
59 | 60 | $(or\\
|
60 | 61 | EOF
|
61 |
| - files.each do |file| |
62 |
| - make_function += <<-EOF |
| 62 | + files.each do |file| |
| 63 | + make_function += <<-EOF |
63 | 64 | $(findstring #{file}, $(<)),\\
|
64 | 65 | EOF
|
65 |
| - end |
66 |
| - make_function += <<-EOF |
| 66 | + end |
| 67 | + make_function += <<-EOF |
67 | 68 | )\\
|
68 | 69 | ),\\
|
69 | 70 | EOF
|
70 |
| - end |
71 |
| - end |
72 |
| - make_function += <<-EOF |
| 71 | + end |
| 72 | + end |
| 73 | + make_function += <<-EOF |
73 | 74 | ),\\
|
74 | 75 | #{command1},\\
|
75 | 76 | #{command2}\\
|
76 | 77 | )
|
77 | 78 | EOF
|
78 | 79 |
|
79 |
| - end |
| 80 | + end |
80 | 81 |
|
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 |
92 | 95 | end
|
| 96 | + contents |
93 | 97 | end
|
94 | 98 | end
|
95 |
| - contents |
96 | 99 | end
|
97 | 100 | end
|
0 commit comments