Class: JunitMerge::App

Inherits:
Object
  • Object
show all
Defined in:
lib/junit_merge/app.rb

Defined Under Namespace

Classes: SummaryDiff

Constant Summary collapse

Error =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ App

Returns a new instance of App.



10
11
12
13
14
15
# File 'lib/junit_merge/app.rb', line 10

def initialize(options={})
  @stdin  = options[:stdin ] || STDIN
  @stdout = options[:stdout] || STDOUT
  @stderr = options[:stderr] || STDERR
  @update_only = false
end

Instance Attribute Details

#stderrObject (readonly)

Returns the value of attribute stderr.



17
18
19
# File 'lib/junit_merge/app.rb', line 17

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



17
18
19
# File 'lib/junit_merge/app.rb', line 17

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



17
18
19
# File 'lib/junit_merge/app.rb', line 17

def stdout
  @stdout
end

Instance Method Details

#run(*args) ⇒ Object



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
# File 'lib/junit_merge/app.rb', line 19

def run(*args)
  *source_paths, target_path = parse_args(args)
  all_paths = [*source_paths, target_path]

  not_found = all_paths.select { |path| !File.exist?(path) }
  not_found.empty? or
    raise Error, "no such file(s): #{not_found.join(', ')}"

  if source_paths.empty?
    stderr.puts "warning: no source files given"
  else
    source_paths.each do |source_path|
      if File.directory?(source_path)
        Find.find(source_path) do |source_file_path|
          next if !File.file?(source_file_path)
          target_file_path = source_file_path.sub(source_path, target_path)
          if File.exist?(target_file_path)
            merge_file(source_file_path, target_file_path)
          else
            FileUtils.mkdir_p(File.dirname(target_file_path))
            FileUtils.cp(source_file_path, target_file_path)
          end
        end
      else File.exist?(source_path)
        merge_file(source_path, target_path)
      end
    end
  end
  0
rescue Error, OptionParser::ParseError => error
  stderr.puts error.message
  1
end