require 'bundler' require_relative '../cli/command_factory' module Dockerun module Context class Rubygems include TR::CondUtils def self.is_rubygems?(path) fs = Dir.entries(path) gs = fs.select { |s| s =~ /.gemspec/ } fs.include?("Gemfile") and fs.include?("Gemfile.lock") and not_empty?(gs) end def initialize(conf) @_config = conf end def call(ops, *val) case ops when :dsl_before_go check_integrity when :dsl_image_built FileUtils.rm(gen_file_name) if not is_keep_gen_file? when :dockerfile_before_workdir init_script_in_dockerfile when :docker_cli_construct_command para = val.first if not_empty?(dev_gems_mounts) para[:mounts] = {} if para[:mounts].nil? para[:mounts].merge!(dev_gems_mounts) end para end end def check_integrity puts `cd #{Dir.getwd} && bundle` end def keep_gen_file(bool = true) @_keep_gen_file = bool end def is_keep_gen_file? @_keep_gen_file.nil? ? true : @_keep_gen_file end def gen_file_name "script_for_gem.sh" end # shell script to set the env def init_script_in_dockerfile cmd = ["#!/usr/bin/env ruby"] if is_duplicate_dev_env? if has_dev_gems? find_local_dev_gems.each do |name, path| if @_config.is_workdir_given? root = File.dirname(@_config.workdir) else root = "/opt" end dockerPath = File.join(root, name) cmd << "`bundle config --global local.#{name} #{dockerPath}`" dev_gems_mounts[path] = dockerPath end end end cmd << "`echo 'alias be=\"bundle exec\"' >> ~/.bashrc`" initScript = cmd.join("\n") File.open(gen_file_name,"w") do |f| f.write initScript end `chmod +x script_for_gem.sh` cont = [] cont << "COPY script_for_gem.sh /tmp" cont << "RUN /tmp/script_for_gem.sh" cont.join("\n") end def duplicate_dev_env(bool = true) @dup_dev_env = bool end def is_duplicate_dev_env? @dup_dev_env.nil? ? true : @dup_dev_env end def dev_gems_mounts if @_dev_gems_mounts.nil? @_dev_gems_mounts = {} end @_dev_gems_mounts end def has_dev_gems? not find_local_dev_gems.empty? end def find_local_dev_gems if @_dev_gems.nil? @_dev_gems = {} Bundler.load.dependencies.each do |d| if not d.source.nil? src = d.source if src.path.to_s != "." @_dev_gems[d.name] = src.path.expand_path.to_s end end end end @_dev_gems end # find_local_dev_gem def docker_cli if @_docker_cli.nil? @_docker_cli = Dockerun::Cli::CommandFactory.new end @_docker_cli end end # class RubygemsEngine end end