class Autoproj::Jenkins::Relativize

Process files that might contain full paths and transform them by replacing the original path with a new one

Constants

FILE_PATTERN

Attributes

file_pattern[R]

A pattern used to filter which files should be filtered

@return [#===]

original_text[R]

The text to be replaced

@return [String]

replacement_text[R]

The text replacing {#original_text}

@return [String]

root_path[R]

The path of the root to be filtered

@return [Pathname]

Public Class Methods

new(root_path, original_text, replacement_text, file_pattern: FILE_PATTERN) click to toggle source
# 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

Public Instance Methods

process() click to toggle source

Process all files matching {#file_pattern} within {#root_path}

@return [Array<Pathname>] the files that have been processed

# 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) click to toggle source

@api private

Replaces text in a given file

@return [Boolean] true if the pattern was found

# 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