Class: Autoproj::Jenkins::Relativize

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Returns:

  • (#===)


30
31
32
# File 'lib/autoproj/jenkins/relativize.rb', line 30

def file_pattern
  @file_pattern
end

#original_textString (readonly)

The text to be replaced

Returns:

  • (String)


20
21
22
# File 'lib/autoproj/jenkins/relativize.rb', line 20

def original_text
  @original_text
end

#replacement_textString (readonly)

The text replacing #original_text

Returns:

  • (String)


25
26
27
# File 'lib/autoproj/jenkins/relativize.rb', line 25

def replacement_text
  @replacement_text
end

#root_pathPathname (readonly)

The path of the root to be filtered

Returns:

  • (Pathname)


15
16
17
# File 'lib/autoproj/jenkins/relativize.rb', line 15

def root_path
  @root_path
end

Instance Method Details

#processArray<Pathname>

Process all files matching #file_pattern within #root_path

Returns:

  • (Array<Pathname>)

    the files that have been processed



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

Returns:

  • (Boolean)

    true if the pattern was found



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