Class: Autoproj::Jenkins::Relativize
- Inherits:
-
Object
- Object
- Autoproj::Jenkins::Relativize
- Defined in:
- lib/autoproj/jenkins/relativize.rb
Overview
Process files that might contain full paths and transform them by replacing the original path with a new one
Constant Summary collapse
- FILE_PATTERN =
Regexp.union( /\/pkgconfig\/.*.pc$/, /\.cmake$/, /\.txt$/ )
Instance Attribute Summary collapse
-
#file_pattern ⇒ #===
readonly
A pattern used to filter which files should be filtered.
-
#original_text ⇒ String
readonly
The text to be replaced.
-
#replacement_text ⇒ String
readonly
The text replacing #original_text.
-
#root_path ⇒ Pathname
readonly
The path of the root to be filtered.
Instance Method Summary collapse
-
#initialize(root_path, original_text, replacement_text, file_pattern: FILE_PATTERN) ⇒ Relativize
constructor
A new instance of Relativize.
-
#process ⇒ Array<Pathname>
Process all files matching #file_pattern within #root_path.
-
#process_file(path) ⇒ Boolean
private
Replaces text in a given file.
Constructor Details
#initialize(root_path, original_text, replacement_text, file_pattern: FILE_PATTERN) ⇒ Relativize
Returns a new instance of Relativize.
32 33 34 35 36 37 |
# File 'lib/autoproj/jenkins/relativize.rb', line 32 def initialize(root_path, original_text, replacement_text, file_pattern: FILE_PATTERN) @root_path = root_path @original_text = original_text @replacement_text = replacement_text @file_pattern = file_pattern end |
Instance Attribute Details
#file_pattern ⇒ #=== (readonly)
A pattern used to filter which files should be filtered
30 31 32 |
# File 'lib/autoproj/jenkins/relativize.rb', line 30 def file_pattern @file_pattern end |
#original_text ⇒ String (readonly)
The text to be replaced
20 21 22 |
# File 'lib/autoproj/jenkins/relativize.rb', line 20 def original_text @original_text end |
#replacement_text ⇒ String (readonly)
The text replacing #original_text
25 26 27 |
# File 'lib/autoproj/jenkins/relativize.rb', line 25 def replacement_text @replacement_text end |
#root_path ⇒ Pathname (readonly)
The path of the root to be filtered
15 16 17 |
# File 'lib/autoproj/jenkins/relativize.rb', line 15 def root_path @root_path end |
Instance Method Details
#process ⇒ Array<Pathname>
Process all files matching #file_pattern within #root_path
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/autoproj/jenkins/relativize.rb', line 42 def process processed_files = Array.new root_path.find do |candidate| if candidate.file? && (file_pattern === candidate.to_s) if process_file(candidate) processed_files << candidate end end end processed_files end |
#process_file(path) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Replaces text in a given file
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/autoproj/jenkins/relativize.rb', line 59 def process_file(path) replaced = false filtered = path.each_line.map do |line| line.gsub(original_text) { replaced = true; replacement_text } end if replaced path.open('w') do |io| io.puts filtered.join end true end end |