Class: Wad::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/wad/target.rb

Overview

A target directory that will gem files through #copy. Also keeps track of what files have already been written.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, postfix, reporter) ⇒ Target

Returns a new instance of Target.



16
17
18
19
20
21
22
23
24
# File 'lib/wad/target.rb', line 16

def initialize path, postfix, reporter
  @postfix = postfix
  @path = path.join(postfix)
  @existing = {}
  @old_source = {}
  @reporter = reporter

  FileUtils.mkdir_p(path)
end

Instance Attribute Details

#existingObject (readonly)

Returns the value of attribute existing.



11
12
13
# File 'lib/wad/target.rb', line 11

def existing
  @existing
end

#old_sourceObject (readonly)

Returns the value of attribute old_source.



12
13
14
# File 'lib/wad/target.rb', line 12

def old_source
  @old_source
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/wad/target.rb', line 8

def path
  @path
end

#postfixObject (readonly)

Returns the value of attribute postfix.



9
10
11
# File 'lib/wad/target.rb', line 9

def postfix
  @postfix
end

#reporterObject (readonly)

Returns the value of attribute reporter.



14
15
16
# File 'lib/wad/target.rb', line 14

def reporter
  @reporter
end

Instance Method Details

#copy(gem_file) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wad/target.rb', line 30

def copy gem_file
  key = gem_file.to.to_s
  gem_name = gem_file.gem_name

  # Try to detect multiple copies of one and the same gem file.
  if existing.has_key?(key) && existing[key] != gem_name
    name = existing[key]
    old_path = old_source[key]

    raise "Cannot copy #{gem_file.path} from #{gem_name}, already exists from #{name} (@#{old_path.to_s})."
  else
    existing[key] = gem_name
    old_source[key] = gem_file.path

    reporter.report_copy_file(gem_file, path)

    dest = path.join(gem_file.to)
    FileUtils.mkdir_p(dest.dirname)
    FileUtils.cp(gem_file.path, dest) unless dest.file?
  end
end

#to_sObject



26
27
28
# File 'lib/wad/target.rb', line 26

def to_s
  postfix
end