-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
gem.rb
114 lines (96 loc) · 3.57 KB
/
gem.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true
# The MIT License (MIT)
#
# Copyright (c) 2015 Samuel E. Giddins
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
require "fileutils"
module Automatiek
class Gem
def initialize(gem_name, &block)
@gem_name = gem_name
@dependencies = []
block.call(self) if block
end
def vendor!(version = nil)
update(version || self.version)
@dependencies.each do |dependency|
dependency.vendor!
dependency.namespace_files(vendor_lib)
end
namespace_files(vendor_lib)
clean
end
def download=(opts = {}, &block)
if block
@download = block
elsif github = opts.delete(:github)
@download = lambda do |version|
Dir.chdir File.dirname(vendor_lib) do
`curl -L #{github}/archive/#{version}.tar.gz | tar -xz`
unless $?.success?
raise "Downloading & untarring #{gem_name} (#{version}) failed"
end
FileUtils.mv "#{github.split("/").last}-#{version.sub(/^v/, "")}", gem_name
end
end
end
end
def dependency(name, &block)
dep = self.class.new(name, &block)
@dependencies << dep
end
attr_accessor :gem_name
attr_accessor :namespace
attr_accessor :prefix
attr_accessor :vendor_lib
attr_accessor :version
def update(version)
FileUtils.rm_rf vendor_lib
@download.call(version)
end
def require_target
@require_target ||= vendor_lib.sub(%r{^(.+?/)?lib/}, "") << "/lib"
end
def require_entrypoint
@require_entrypoint ||= gem_name.tr("-", "/")
end
attr_writer :require_entrypoint
def namespace_files(folder)
files = Dir.glob("#{folder}/**/*.rb")
process(files, /module Kernel/, "module #{prefix}")
process(files, /::#{namespace}/, "::#{prefix}::#{namespace}")
process(files, /(?<!\w|def |:)#{namespace}\b/, "#{prefix}::#{namespace}")
process(files, /require (["'])#{Regexp.escape require_entrypoint}/, "require \\1#{require_target}/#{require_entrypoint}")
process(files, %r{(autoload\s+[:\w]+,\s+["'])(#{Regexp.escape require_entrypoint}[\w\/]+["'])}, "\\1#{require_target}/\\2")
end
def clean
files = Dir.glob("#{vendor_lib}/*", File::FNM_DOTMATCH).reject {|f| %(. .. lib).include? f.split("/").last }
FileUtils.rm_r files
end
private
def process(files, regex, replacement = "")
files.each do |file|
contents = File.read(file)
contents.gsub!(regex, replacement)
File.open(file, "w") {|f| f << contents }
end
end
end
end