class RakeVersion::Copier

Public Class Methods

new(*args) click to toggle source
# File lib/rake-version/copier.rb, line 7
def initialize *args
  options = args.last.kind_of?(Hash) ? args.pop : {}

  @file_patterns = args.collect{ |arg| check_file_pattern arg }
  @version_pattern = Version::REGEXP
  @replace_all = !!options[:all]
end

Public Instance Methods

copy(version, context) click to toggle source
# File lib/rake-version/copier.rb, line 15
def copy version, context
  find_all_files(context).each{ |f| copy_version f, version }
end

Private Instance Methods

check_file_pattern(pattern) click to toggle source
# File lib/rake-version/copier.rb, line 21
def check_file_pattern pattern
  unless [ String, Regexp ].any?{ |klass| pattern.kind_of? klass }
    raise BadFilePattern, "Expected file pattern to be a glob string or regexp, got #{pattern.class.name}."
  end
  pattern
end
copy_version(file, version) click to toggle source
# File lib/rake-version/copier.rb, line 28
def copy_version file, version
  File.open(file, 'r+') do |f|
    contents = f.read
    return unless match? contents
    f.rewind
    f.truncate 0
    f.write process(contents, version)
  end
end
find_all_files(context) click to toggle source
# File lib/rake-version/copier.rb, line 50
def find_all_files context
  @file_patterns.collect{ |p| find_files p, context }.flatten
end
find_files(pattern, context) click to toggle source
# File lib/rake-version/copier.rb, line 54
def find_files pattern, context
  if pattern.kind_of? String
    Dir.chdir context.root
    Dir.glob(pattern).select{ |f| File.file? f }
  elsif pattern.kind_of? Regexp
    files = []
    Find.find(context.root) do |path|
      files << path if File.file?(path) and path.match(pattern)
    end
    files
  end
end
match?(contents) click to toggle source
# File lib/rake-version/copier.rb, line 38
def match? contents
  contents.match @version_pattern
end
process(contents, version) click to toggle source
# File lib/rake-version/copier.rb, line 42
def process contents, version
  if @replace_all
    contents.gsub(@version_pattern, version.to_s)
  else
    contents.sub(@version_pattern, version.to_s)
  end
end