Class: Jets::Builders::RubyPackager
- Inherits:
-
Object
- Object
- Jets::Builders::RubyPackager
show all
- Includes:
- Util
- Defined in:
- lib/jets/builders/ruby_packager.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Util
#full, #headline, #poly_only?, #sh
Constructor Details
#initialize(relative_app_root) ⇒ RubyPackager
Returns a new instance of RubyPackager.
8
9
10
|
# File 'lib/jets/builders/ruby_packager.rb', line 8
def initialize(relative_app_root)
@full_app_root = full(relative_app_root)
end
|
Instance Attribute Details
#full_app_root ⇒ Object
Returns the value of attribute full_app_root.
7
8
9
|
# File 'lib/jets/builders/ruby_packager.rb', line 7
def full_app_root
@full_app_root
end
|
Instance Method Details
#bundle_install ⇒ Object
Installs gems on the current target system: both compiled and non-compiled. If user is on a macosx machine, macosx gems will be installed. If user is on a linux machine, linux gems will be installed.
Copies Gemfile* to /tmp/jetss/demo/bundled folder and installs gems with bundle install from there.
We take the time to copy Gemfile and bundle into a separate directory because it gets left around to act as a ‘cache’. So, when the builds the project gets built again not all the gems from get installed from the beginning.
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/jets/builders/ruby_packager.rb', line 46
def bundle_install
return if poly_only?
full_project_path = @full_app_root
headline "Bundling: running bundle install in cache area: #{cache_area}."
copy_gemfiles(full_project_path)
require "bundler"
Bundler.with_clean_env do
sh(
"cd #{cache_area} && " \
"env BUNDLE_IGNORE_CONFIG=1 bundle install --path bundled/gems --without development test"
)
end
copy_gemfile_lock
puts 'Bundle install success.'
end
|
#clean_old_submodules ⇒ Object
When using submodules, bundler leaves old submodules behind. Over time this inflates the size of the the bundled gems. So we’ll clean it up.
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/jets/builders/ruby_packager.rb', line 99
def clean_old_submodules
lockfile = "#{cache_area}/Gemfile.lock"
return unless File.exist?(lockfile)
parser = Bundler::LockfileParser.new(Bundler.read_file(lockfile))
specs = parser.specs
submoduled_specs = specs.select do |spec|
spec.source.to_s =~ /@\w+\)/
end
git_shas = submoduled_specs.map do |spec|
md = spec.source.to_s.match(/@(\w+)\)/)
md[1]
end
Dir.glob("#{cache_area}/bundled/gems/ruby/2.5.0/bundler/gems/*").each do |path|
sha = path.split('-').last[0..6]
unless git_shas.include?(sha)
FileUtils.rm_rf(path)
end
end
end
|
#copy_bundled_cache ⇒ Object
182
183
184
185
186
187
188
189
190
|
# File 'lib/jets/builders/ruby_packager.rb', line 182
def copy_bundled_cache
app_root_bundled = "#{@full_app_root}/bundled"
if File.exist?(app_root_bundled)
puts "Removing current bundled from project"
FileUtils.rm_rf(app_root_bundled)
end
FileUtils.cp_r("#{cache_area}/bundled", app_root_bundled)
end
|
#copy_gemfile_lock ⇒ Object
70
71
72
73
74
|
# File 'lib/jets/builders/ruby_packager.rb', line 70
def copy_gemfile_lock
src = "#{cache_area}/Gemfile.lock"
dest = "#{@full_app_root}/Gemfile.lock"
FileUtils.cp_r(src, dest)
end
|
#copy_gemfiles(full_project_path) ⇒ Object
130
131
132
133
134
|
# File 'lib/jets/builders/ruby_packager.rb', line 130
def copy_gemfiles(full_project_path)
FileUtils.mkdir_p(cache_area)
FileUtils.cp("#{full_project_path}/Gemfile", "#{cache_area}/Gemfile")
FileUtils.cp("#{full_project_path}/Gemfile.lock", "#{cache_area}/Gemfile.lock")
end
|
#ensure_build_cache_bundle_config_exists! ⇒ Object
On circleci the “#Jets.build_root/.bundle/config” doesnt exist this only happens with ssh debugging, not when the ci.sh script gets ran. But on macosx it exists. Dont know why this is the case.
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/jets/builders/ruby_packager.rb', line 153
def ensure_build_cache_bundle_config_exists!
text ="---\nBUNDLE_PATH: \"bundled/gems\"\nBUNDLE_WITHOUT: \"development:test\"\n"
bundle_config = "#{cache_area}/.bundle/config"
FileUtils.mkdir_p(File.dirname(bundle_config))
IO.write(bundle_config, text)
end
|
177
178
179
180
|
# File 'lib/jets/builders/ruby_packager.rb', line 177
def
headline "Replacing compiled gems with AWS Lambda Linux compiled versions."
GemReplacer.new(Jets::RUBY_VERSION, gems_options).run
end
|
172
173
174
175
|
# File 'lib/jets/builders/ruby_packager.rb', line 172
def
headline "Setting up a vendored copy of ruby."
Jets::Gems::::Ruby.new(Jets::RUBY_VERSION, gems_options).run
end
|
#finish ⇒ Object
21
22
23
24
25
26
27
28
|
# File 'lib/jets/builders/ruby_packager.rb', line 21
def finish
return unless gemfile_exist?
copy_bundled_cache
tidy
end
|
#gemfile_exist? ⇒ Boolean
30
31
32
33
|
# File 'lib/jets/builders/ruby_packager.rb', line 30
def gemfile_exist?
gemfile_path = "#{@full_app_root}/Gemfile"
File.exist?(gemfile_path)
end
|
#gems_options ⇒ Object
164
165
166
167
168
169
170
|
# File 'lib/jets/builders/ruby_packager.rb', line 164
def gems_options
{
s3: "lambdagems",
build_root: cache_area,
project_root: @full_app_root,
}
end
|
#install ⇒ Object
12
13
14
15
16
17
18
19
|
# File 'lib/jets/builders/ruby_packager.rb', line 12
def install
return unless gemfile_exist?
reconfigure_ruby_version
clean_old_submodules
bundle_install
setup_bundle_config
end
|
This is in case the user has a 2.5.x variant. Force usage of ruby version that jets supports The lambda server only has ruby 2.5.0 installed.
92
93
94
95
|
# File 'lib/jets/builders/ruby_packager.rb', line 92
def reconfigure_ruby_version
ruby_version = "#{@full_app_root}/.ruby-version"
IO.write(ruby_version, Jets::RUBY_VERSION)
end
|
#setup_bundle_config ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/jets/builders/ruby_packager.rb', line 136
def setup_bundle_config
ensure_build_cache_bundle_config_exists!
cache_bundle_config = "#{cache_area}/.bundle/config"
app_bundle_config = "#{@full_app_root}/.bundle/config"
FileUtils.mkdir_p(File.dirname(app_bundle_config))
FileUtils.cp(cache_bundle_config, app_bundle_config)
end
|
#tidy ⇒ Object
Clean up extra unneeded files to reduce package size Because we’re removing files (something dangerous) use full paths.
78
79
80
81
82
83
|
# File 'lib/jets/builders/ruby_packager.rb', line 78
def tidy
puts "Tidying project: removing ignored files to reduce package size."
tidy_project(@full_app_root)
tidy_project(@full_app_root+"/rack")
end
|
#tidy_project(path) ⇒ Object
85
86
87
|
# File 'lib/jets/builders/ruby_packager.rb', line 85
def tidy_project(path)
Tidy.new(path).cleanup!
end
|