Class: Spock

Inherits:
Object
  • Object
show all
Defined in:
lib/spock.rb,
lib/spock/cli.rb,
lib/spock/version.rb

Defined Under Namespace

Modules: CLI Classes: Config

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil, opts = {}) ⇒ Spock

Returns a new instance of Spock.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/spock.rb', line 6

def initialize(config_file = nil, opts = {})
  if config_file.nil?
    config_file = "#{ENV['HOME']}/.spockrc"
    config_file = "#{ENV['HOME']}/Dropbox/.spockrc" \
      if not File.exists? config_file
    config_file = "#{ENV['HOME']}/Dropbox/Apps/Spock/spockrc" \
      if not File.exists? config_file
    throw "No spockrc in default locations" if not File.exists? config_file
  end
  throw "#{config_file}: No such file or directory" if not File.exists? config_file
  @config_file = config_file
  @opts = opts
end

Instance Method Details

#binread(file) ⇒ Object



72
73
74
# File 'lib/spock.rb', line 72

def binread(file)
  File.open(file, 'rb:ASCII-8BIT') { |f| f.read }
end

#mergeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/spock.rb', line 20

def merge
  config = Config.new
  config.instance_eval(binread(@config_file), @config_file)
  
  config.sources.reverse.each do |source|
    if not File.directory? source
      puts "Not found: source directory #{source}"
      next
    end
    Dir.chdir(source) do
      Dir['*'].each do |path|
        source_file = File.join(source, path)
        target_file = File.join(config.target, config.target_prefix + path)
        
        if needs_replacement? source_file, target_file
          puts "Merging #{target_file} from #{source}"
          merge_file source_file, target_file
        else
          puts "Skipping #{target_file}"
        end
      end
    end
  end
end

#merge_file(source_file, target_file) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/spock.rb', line 45

def merge_file(source_file, target_file)
  if not @opts[:dry_run]
    FileUtils.rm_rf target_file
    if symlink_supported?
      File.symlink source_file, target_file
    else
      FileUtils.cp_r :preserve => true
    end
  end
end

#needs_replacement?(source_file, target_file) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
# File 'lib/spock.rb', line 56

def needs_replacement?(source_file, target_file)
  if symlink_supported?
    not File.symlink? target_file or
    not File.readlink(target_file) == source_file
  else
    not File.exists? target_file or
    not File.size(target_file) == File.size(source_file) or
    not binread(target_file) == binread(source_file)
  end
end

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/spock.rb', line 67

def symlink_supported?
  not RbConfig::CONFIG["host_os"] =~
      %r!(msdos|mswin|djgpp|mingw|[Ww]indows)!
end