Class: Lam::Build::TravelingRuby

Inherits:
Object
  • Object
show all
Defined in:
lib/lam/build/traveling_ruby.rb

Instance Method Summary collapse

Instance Method Details

#buildObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lam/build/traveling_ruby.rb', line 10

def build
  if File.exist?("#{Lam.root}bundled")
    puts "Ruby bundled already exists."
    puts "To force rebundling: rm -rf bundled"
    return
  end

  check_ruby_version

  FileUtils.mkdir_p(TEMP_BUILD_DIR)
  copy_gemfiles

  Dir.chdir(TEMP_BUILD_DIR) do
    download_traveling_ruby
    unpack_traveling_ruby
    bundle_install
    vendor_gemfiles
  end

  move_bundled_to_project
end

#bundle_installObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lam/build/traveling_ruby.rb', line 73

def bundle_install
  puts 'Installing bundle.'
  require "bundler" # dynamicaly require bundler so user can use any bundler
  Bundler.with_clean_env do
    success = system(
      "cd #{TEMP_BUILD_DIR} && " \
      'env BUNDLE_IGNORE_CONFIG=1 bundle install --path bundled/gems --without development'
    )

    abort('Bundle install failed, exiting.') unless success
  end

  puts 'Bundle install success.'
end

#bundled_gems_destObject



121
122
123
# File 'lib/lam/build/traveling_ruby.rb', line 121

def bundled_gems_dest
  "bundled/gems"
end

#bundled_ruby_destObject



117
118
119
# File 'lib/lam/build/traveling_ruby.rb', line 117

def bundled_ruby_dest
  "bundled/ruby"
end

#check_ruby_versionObject



32
33
34
35
36
37
38
39
# File 'lib/lam/build/traveling_ruby.rb', line 32

def check_ruby_version
  return if ENV['LAM_SKIP_RUBY_CHECK'] # only use if you absolutely need to
  traveling_version = TRAVELING_RUBY_VERSION.match(/-((\d+)\.(\d+)\.(\d+))-/)[1]
  if RUBY_VERSION != traveling_version
    puts "You are using ruby version #{RUBY_VERSION}."
    abort("You must use ruby #{traveling_version} to build the project because it's what Traveling Ruby uses.".colorize(:red))
  end
end

#copy_gemfilesObject



41
42
43
44
# File 'lib/lam/build/traveling_ruby.rb', line 41

def copy_gemfiles
  FileUtils.cp("#{Lam.root}Gemfile", "#{TEMP_BUILD_DIR}/")
  FileUtils.cp("#{Lam.root}Gemfile.lock", "#{TEMP_BUILD_DIR}/")
end

#download_traveling_rubyObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lam/build/traveling_ruby.rb', line 46

def download_traveling_ruby
  puts "Downloading traveling ruby from #{traveling_ruby_url}."

  FileUtils.rm_rf("#{TEMP_BUILD_DIR}/#{bundled_ruby_dest}")
  File.open(traveling_ruby_tar_file, 'wb') do |saved_file|
    # the following "open" is provided by open-uri
    open(traveling_ruby_url, 'rb') do |read_file|
      saved_file.write(read_file.read)
    end
  end

  puts 'Download complete.'
end

#move_bundled_to_projectObject



108
109
110
111
112
113
114
115
# File 'lib/lam/build/traveling_ruby.rb', line 108

def move_bundled_to_project
  if File.exist?("#{Lam.root}bundled")
    puts "Removing current bundled folder"
    FileUtils.rm_rf("#{Lam.root}bundled")
  end
  puts "Moving bundled ruby to your project."
  FileUtils.mv("#{TEMP_BUILD_DIR}/bundled", Lam.root)
end

#traveling_ruby_tar_fileObject



129
130
131
# File 'lib/lam/build/traveling_ruby.rb', line 129

def traveling_ruby_tar_file
  File.basename(traveling_ruby_url)
end

#traveling_ruby_urlObject



125
126
127
# File 'lib/lam/build/traveling_ruby.rb', line 125

def traveling_ruby_url
  TRAVELING_RUBY_VERSION
end

#unpack_traveling_rubyObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lam/build/traveling_ruby.rb', line 60

def unpack_traveling_ruby
  puts 'Unpacking traveling ruby.'

  FileUtils.mkdir_p(bundled_ruby_dest)

  success = system("tar -xzf #{traveling_ruby_tar_file} -C #{bundled_ruby_dest}")
  abort('Unpacking traveling ruby failed') unless success
  puts 'Unpacking traveling ruby successful.'

  puts 'Removing tar.'
  FileUtils.rm_rf(traveling_ruby_tar_file)
end

#vendor_gemfilesObject

The wrapper script doesnt work unless you move the gem files in the bundled/gems folder and export it to BUNDLE_GEMFILE in the wrapper script.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/lam/build/traveling_ruby.rb', line 91

def vendor_gemfiles
  puts "Moving gemfiles into #{bundled_gems_dest}/"
  FileUtils.mv("Gemfile", "#{bundled_gems_dest}/")
  FileUtils.mv("Gemfile.lock", "#{bundled_gems_dest}/")

  bundle_config_path = "#{bundled_gems_dest}/.bundle/config"
  puts "Generating #{bundle_config_path}"
  FileUtils.mkdir_p(File.dirname(bundle_config_path))
  bundle_config ="BUNDLE_PATH: .\nBUNDLE_WITHOUT: development\nBUNDLE_DISABLE_SHARED_GEMS: '1'\n"
  IO.write(bundle_config_path, bundle_config)

end