Skip to content

Commit e712610

Browse files
committed
#36 Added example and test for bundler.require all. Include Gemfile and Gemfile.lock in package if lock file is present
1 parent d99b545 commit e712610

File tree

6 files changed

+65
-7
lines changed

6 files changed

+65
-7
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
gem 'httparty'
3+
gem 'nokogiri'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
httparty (0.18.1)
5+
mime-types (~> 3.0)
6+
multi_xml (>= 0.5.2)
7+
mime-types (3.3.1)
8+
mime-types-data (~> 3.2015)
9+
mime-types-data (3.2020.1104)
10+
mini_portile2 (2.5.1)
11+
multi_xml (0.6.0)
12+
nokogiri (1.11.3)
13+
mini_portile2 (~> 2.5.0)
14+
racc (~> 1.4)
15+
racc (1.5.2)
16+
17+
PLATFORMS
18+
ruby
19+
20+
DEPENDENCIES
21+
httparty
22+
nokogiri
23+
24+
BUNDLED WITH
25+
2.1.4
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'rubygems'
2+
require 'bundler/setup'
3+
Bundler.require(:default)
4+
5+
def hello(event:, context:)
6+
body = HTTParty.get("https://github.com").body
7+
doc = Nokogiri::HTML(body)
8+
9+
{ statusCode: 200, body: doc.title }
10+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
service: bundler-require-all
2+
3+
plugins:
4+
- serverless-ruby-layer
5+
provider:
6+
name: aws
7+
runtime: ruby2.5
8+
9+
custom:
10+
rubyLayer:
11+
use_docker: true
12+
13+
functions:
14+
hello:
15+
handler: handler.hello

lib/bundle.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ function bundleInstall(){
4747

4848
fs.copySync(path.join(this.servicePath,'Gemfile'), path.join(this.ruby_layer,'Gemfile') )
4949
const gem_file_lock_path = path.join(this.servicePath,'Gemfile.lock')
50-
const gem_file_lock_exists = fs.pathExistsSync(gem_file_lock_path)
51-
if (!this.options.ignore_gemfile_lock && gem_file_lock_exists){
50+
if (this.use_gemfile_lock){
5251
fs.copySync(gem_file_lock_path, path.join(this.ruby_layer,'Gemfile.lock') )
5352
}
5453

@@ -102,7 +101,7 @@ function bundleInstall(){
102101
this.docker_gem_path = '/var/gem_build'
103102
docker(['exec', docker_id, 'mkdir', this.docker_gem_path], options, this.cli)
104103
docker(['cp', '-L', 'Gemfile', container_id+':'+this.docker_gem_path], options,this.cli)
105-
if (!this.options.ignore_gemfile_lock && gem_file_lock_exists) {
104+
if (this.use_gemfile_lock){
106105
docker(['cp', '-L', 'Gemfile.lock', container_id+':'+this.docker_gem_path], options,this.cli)
107106
const data = fs.readFileSync(gem_file_lock_path).toString().match(/BUNDLED WITH[\r\n]+([^\r\n]+)/)
108107
docker(['exec','-w',this.docker_gem_path, docker_id].concat(['gem','install','bundler:'+ data[data.length - 1].trim()]),options,this.cli)
@@ -207,7 +206,12 @@ function excludePackage(){
207206
this.serverless.service.package["exclude"] = Object.assign([])
208207
}
209208
this.serverless.service.package["exclude"].push("node_modules/**", "package-lock.json", "package.json",
210-
"vendor/**", "Gemfile", "Gemfile.lock")
209+
"vendor/**")
210+
this.use_gemfile_lock = !this.options.ignore_gemfile_lock && fs.pathExistsSync(path.join(this.servicePath,'Gemfile.lock'))
211+
212+
if (!this.use_gemfile_lock) {
213+
this.serverless.service.package["exclude"].push("Gemfile", "Gemfile.lock")
214+
}
211215
if (this.options.docker_file) {
212216
this.serverless.service.package["exclude"].push(this.options.docker_file)
213217
}

test/test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,19 @@ let test_data = [
3838
function_files: ['handler.rb'], include_functions: ['Hello'], exclude_functions:[] },
3939

4040
{ folder: 'basic-with-gemfile-lock', gem_zip_dirs: ['/','/bin/','/build_info/','/doc/','/extensions/', '/gems/', '/specifications/','/gems/httparty-0.18.1/', '/gems/mime-types-data-3.2020.1104/', '/gems/multi_xml-0.6.0/', '/gems/mime-types-3.3.1/'],
41-
function_files: ['handler.rb'], include_functions: ['Hello'], exclude_functions:[], check_version: true },
41+
function_files: ['Gemfile', 'Gemfile.lock', 'handler.rb'], include_functions: ['Hello'], exclude_functions:[], check_version: true },
4242

4343
{ folder: 'use-docker-with-gemfile-lock', gem_zip_dirs: ['/','/bin/','/build_info/','/doc/','/extensions/', '/gems/', '/specifications/','/gems/httparty-0.18.1/', '/gems/mime-types-data-3.2020.1104/', '/gems/multi_xml-0.6.0/', '/gems/mime-types-3.3.1/'],
44-
function_files: ['handler.rb'], include_functions: ['Hello'], exclude_functions:[], check_version: true },
44+
function_files: ['Gemfile', 'Gemfile.lock', 'handler.rb'], include_functions: ['Hello'], exclude_functions:[], check_version: true },
4545

4646
{ folder: 'basic-ignore-gemfile-lock', gem_zip_dirs: ['/','/bin/','/build_info/','/doc/','/extensions/', '/gems/', '/specifications/','/gems/httparty-0.18.1/', '/gems/mime-types-data-3.2021.0225/', '/gems/multi_xml-0.6.0/', '/gems/mime-types-3.3.1/'],
4747
function_files: ['handler.rb'], include_functions: ['Hello'], exclude_functions:[], check_version: true },
4848

4949
{ folder: 'use-docker-ignore-gemfile-lock', gem_zip_dirs: ['/','/bin/','/build_info/','/doc/','/extensions/', '/gems/', '/specifications/','/gems/httparty-0.18.1/', '/gems/mime-types-data-3.2021.0225/', '/gems/multi_xml-0.6.0/', '/gems/mime-types-3.3.1/'],
5050
function_files: ['handler.rb'], include_functions: ['Hello'], exclude_functions:[], check_version: true },
5151

52-
52+
{ folder: 'bundler-require-all', gem_zip_dirs: ['/','/bin/','/build_info/','/doc/','/extensions/', '/gems/', '/specifications/','/gems/httparty-0.18.1/', '/gems/mime-types-data-3.2020.1104/', '/gems/multi_xml-0.6.0/', '/gems/mime-types-3.3.1/','/gems/nokogiri-1.11.3-x86_64-linux/', '/gems/racc-1.5.2/', '/extensions/x86_64-linux/', '/gems/mini_portile2-2.5.1/' ],
53+
function_files: ['Gemfile', 'Gemfile.lock', 'handler.rb'], include_functions: ['Hello'], exclude_functions:[], check_version: true },
5354

5455
]
5556

0 commit comments

Comments
 (0)