Class: Dockerun::Context::Rubygems

Inherits:
Object
  • Object
show all
Includes:
TR::CondUtils
Defined in:
lib/dockerun/context/rubygems.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ Rubygems

Returns a new instance of Rubygems.



18
19
20
# File 'lib/dockerun/context/rubygems.rb', line 18

def initialize(conf)
  @_config = conf
end

Class Method Details

.is_rubygems?(path) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
# File 'lib/dockerun/context/rubygems.rb', line 12

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

Instance Method Details

#call(ops, *val) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dockerun/context/rubygems.rb', line 22

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

#check_integrityObject



44
45
46
# File 'lib/dockerun/context/rubygems.rb', line 44

def check_integrity
  puts `cd #{Dir.getwd} && bundle`
end

#dev_gems_mountsObject



100
101
102
103
104
105
# File 'lib/dockerun/context/rubygems.rb', line 100

def dev_gems_mounts
  if @_dev_gems_mounts.nil?
    @_dev_gems_mounts = {}
  end
  @_dev_gems_mounts
end

#docker_cliObject

find_local_dev_gem



132
133
134
135
136
137
# File 'lib/dockerun/context/rubygems.rb', line 132

def docker_cli
  if @_docker_cli.nil?
    @_docker_cli = Dockerun::Cli::CommandFactory.new
  end
  @_docker_cli
end

#duplicate_dev_env(bool = true) ⇒ Object



92
93
94
# File 'lib/dockerun/context/rubygems.rb', line 92

def duplicate_dev_env(bool = true)
  @dup_dev_env = bool
end

#find_local_dev_gemsObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/dockerun/context/rubygems.rb', line 112

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

#gen_file_nameObject



55
56
57
# File 'lib/dockerun/context/rubygems.rb', line 55

def gen_file_name
  "script_for_gem.sh"
end

#has_dev_gems?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/dockerun/context/rubygems.rb', line 108

def has_dev_gems?
  not find_local_dev_gems.empty?        
end

#init_script_in_dockerfileObject

shell script to set the env



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
# File 'lib/dockerun/context/rubygems.rb', line 60

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

#is_duplicate_dev_env?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/dockerun/context/rubygems.rb', line 95

def is_duplicate_dev_env?
  @dup_dev_env.nil? ? true : @dup_dev_env
end

#is_keep_gen_file?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/dockerun/context/rubygems.rb', line 51

def is_keep_gen_file?
  @_keep_gen_file.nil? ? true : @_keep_gen_file
end

#keep_gen_file(bool = true) ⇒ Object



48
49
50
# File 'lib/dockerun/context/rubygems.rb', line 48

def keep_gen_file(bool = true)
  @_keep_gen_file = bool
end