Class: Jank::Janker

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Janker

Returns a new instance of Janker.



6
7
8
# File 'lib/janker.rb', line 6

def initialize(config)
  @config = config
end

Instance Method Details



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

def link
  return false if !@config.janked
  mark_janked_package
  sync_packages
  true
end

#location_of_package(pkg) ⇒ Object



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
61
62
63
64
65
66
67
# File 'lib/janker.rb', line 31

def location_of_package(pkg)
  local_gp = nil
  path = nil

  # Find location in gopath
  (@config.janked_gopaths + @config.gopaths).each do |gp|
    path = "#{gp}/src/#{pkg}"
    next if !File.exist?(path) || !File.directory?(path)
    local_gp = gp
    break
  end

  return "" if local_gp == nil

  # Translate path if janked
  root = "#{local_gp}/src"
  dir = path
  janked_path = nil
  janked_location = nil

  while dir != root
    jankfile = "#{dir}/JANKED"
    if File.exist?(jankfile)
      janked_path = dir
      janked_location = File.read(jankfile)
      break
    end

    dir = File.realpath("#{dir}/..")
  end

  if janked_path != nil
    return path.gsub(/^#{janked_path}/, janked_location)
  end

  return path
end

#mark_janked_packageObject



85
86
87
88
89
90
91
92
# File 'lib/janker.rb', line 85

def mark_janked_package
  if @config.janked_local_path.start_with? @config.janked_gopath
    raise "Trying to link a package already inside the gopath!"
  end

  FileUtils.mkdir_p(@config.janked_gopath)
  File.open("#{@config.janked_gopath}/JANKED", 'w') {|f| f.write(@config.janked_local_path) }
end

#package_for_location(path) ⇒ Object



24
25
26
27
28
29
# File 'lib/janker.rb', line 24

def package_for_location(path)
  Dir.chdir(File.realpath(path)) do
    conf = Config.new
    return conf.package
  end
end

#rsync(src, dest) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/janker.rb', line 116

def rsync(src, dest)
  return if src == dest

  exclude = %w{JANKED .bzr .bzringore .hg .hgignore .git .gitignore .nfs}
  opts = "--delete"
  exclude.each {|e| opts += " --exclude #{e}"}

  `rsync -az #{opts} "#{src}/" "#{dest}"`
end

#sync_packagesObject



100
101
102
103
104
105
106
107
108
# File 'lib/janker.rb', line 100

def sync_packages
  walk_janked_packages do |package, local_path, go_path|
    if File.exist?(local_path) && File.directory?(local_path) && ! File.symlink?(local_path)
      rsync(local_path, go_path)
    else
      FileUtils.rm_rf(go_path)
    end
  end
end


17
18
19
20
21
22
# File 'lib/janker.rb', line 17

def unlink
  return false if !@config.janked
  unmark_janked_package
  sync_packages
  true
end


110
111
112
113
114
# File 'lib/janker.rb', line 110

def unlink_all
  walk_janked_packages do |package, local_path, go_path|
    FileUtils.rm_rf(go_path)
  end
end

#unmark_janked_packageObject



94
95
96
97
98
# File 'lib/janker.rb', line 94

def unmark_janked_package
  if File.exist?(@config.janked_gopath)
    FileUtils.rm_rf(@config.janked_gopath)
  end
end

#walk_janked_packagesObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/janker.rb', line 69

def walk_janked_packages
  @config.gopaths.each do |gp|
    Find.find("#{gp}/src") do |path|
      next if !FileTest.directory?(path)
      Find.prune if File.basename(path).start_with? '.'

      janked_file = "#{path}/JANKED"
      if File.exist?(janked_file)
        package = path.slice(gp.length+5..path.length)
        yield package, File.read(janked_file), path
        Find.prune
      end
    end
  end
end