Class: Filemerger::Merger

Inherits:
Object
  • Object
show all
Defined in:
lib/filemerger/merger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Merger

Returns a new instance of Merger.



10
11
12
13
14
15
# File 'lib/filemerger/merger.rb', line 10

def initialize(config)
  @config = config
  unless config.xcode_project.nil?
    @xcode_helper = XcodeHelper.new(config)
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/filemerger/merger.rb', line 8

def config
  @config
end

#xcode_helperObject

Returns the value of attribute xcode_helper.



8
9
10
# File 'lib/filemerger/merger.rb', line 8

def xcode_helper
  @xcode_helper
end

Instance Method Details

#merge_filesObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/filemerger/merger.rb', line 17

def merge_files
  first_mask_files = Searcher.find_files_for_mask(@config.masks.first, @config.working_folders)

  if first_mask_files.count == 0
    Poster.post_nothing_found
    exit
  end

  unless @xcode_helper.nil?
    project = @xcode_helper.project
  end
  errors = 0

  first_mask_files.each do |first_mask_file|
    file_name = File.basename(first_mask_file).to_s.chomp(@config.masks.first)
    content = ""
    @config.masks.each_with_index do |mask, index|
      file = File.dirname(first_mask_file) + "/" + file_name + mask
      if File.exist?(file)
        if !@config.ommit_lines.nil? && index != 0
          content += File.readlines(file).drop(@config.ommit_lines).join() + "\n"
        else
          content += File.readlines(file).join() + "\n"
        end
        delete_file_if_needed(file)
      else
        Poster.post_file_not_found(file)
      end
    end
    file_name_helper = file_name + @config.result_mask

    new_file_name = File.dirname(first_mask_file) + "/" + file_name_helper
    File.open(new_file_name, "w") { |f| f.puts content }
    Poster.post_file_created(new_file_name)

    if !@xcode_helper.nil? && @xcode_helper.add_file_to_project(first_mask_file, file_name_helper) == false
      errors += 1
    end
  end
  Poster.post_merge_finished(errors)
  unless @xcode_helper.nil?
    project.save
  end
end