module DistribBuilder module Versioner class FileProcessor attr_accessor :config_path, :cmakelist_path, :config, :cmakelist, :logger # nlevchuk: need to use logger here without passing it through argument to .new() def initialize(logger=nil) @logger = logger @config_path = WORK_DIR + "/" + CONFIG_FILE @cmakelist_path = WORK_DIR + "/" + CMAKELIST_FILE @config = nil @cmakelist = nil end def check_existing_files unless File.exists?(config_path) && File.exists?(cmakelist_path) logger.missing_files end end def check_mandatory_sections_in_config unless (config.has_key?('files') && !config['files'].empty?) && (config.has_key?('patterns') && !config['patterns'].empty?) logger.missing_mandatory_sections_in_config end end def read_config_files check_existing_files @config = YAML.load_file(config_path) File.open(cmakelist_path, 'r') { |file| @cmakelist = file.read } end def read_version_and_build version = [] File.readlines(cmakelist_path).each do |line| if line.match(VERSION_PATTERN) line.slice!(/.*_VERSION/) line.gsub!(/[\"\) ]/, '') version = line.split('.').map { |s| s.to_i } end if line.match(BUILD_PATTERN) line.slice!(/.*_BUILD/) line.gsub!(/[\"\) ]/, '') version << line.to_i end end version end def replace_version_and_build_in_files(versions_hash) check_mandatory_sections_in_config config['files'].each do |path, patterns_array| full_path = WORK_DIR + "/" + path logger.file_not_found(full_path) unless File.exists?(full_path) text = File.read(full_path) patterns_array.each do |pattern| if text.match(pattern) prepared_pattern = config['patterns'][pattern] template = Mustache.render(prepared_pattern, versions_hash) text.gsub!(Regexp.new(pattern), template) logger.found_and_replace_pattern_in_text(pattern, path) else logger.pattern_not_match(pattern, path) end end File.open(full_path, 'w') { |file| file.puts text } logger.write_changes_into_file(path) end end end end end