Upgrade to 1.46.0
[platform/upstream/nghttp2.git] / third-party / mruby / lib / mruby / build / load_gems.rb
1 module MRuby
2   module LoadGems
3     def gembox(gemboxfile)
4       gembox = File.expand_path("#{gemboxfile}.gembox", "#{MRUBY_ROOT}/mrbgems")
5       fail "Can't find gembox '#{gembox}'" unless File.exist?(gembox)
6
7       GemBox.config = self
8       GemBox.path = gembox
9
10       instance_eval File.read(gembox)
11
12       GemBox.path = nil
13     end
14
15     def gem(gemdir, &block)
16       if gemdir.is_a?(Hash)
17         gemdir = load_special_path_gem(gemdir)
18       elsif GemBox.path && gemdir.is_a?(String)
19         gemdir = File.expand_path(gemdir, File.dirname(GemBox.path))
20       else
21         caller_dir = File.expand_path(File.dirname(caller(1,1)[0][/^(.*?):\d/,1]))
22         gemdir = File.expand_path(gemdir, caller_dir)
23       end
24
25       gemrake = File.join(gemdir, "mrbgem.rake")
26
27       fail "Can't find #{gemrake}" unless File.exist?(gemrake)
28       Gem.current = nil
29       load gemrake
30       return nil unless Gem.current
31
32       Gem.current.dir = gemdir
33       Gem.current.build = self.is_a?(MRuby::Build) ? self : MRuby::Build.current
34       Gem.current.build_config_initializer = block
35       gems << Gem.current
36
37       cxx_srcs = ['src', 'test', 'tools'].map do |subdir|
38         Dir.glob("#{Gem.current.dir}/#{subdir}/*.{cpp,cxx,cc}")
39       end.flatten
40       enable_cxx_exception unless cxx_srcs.empty?
41
42       Gem.current
43     end
44
45     def load_special_path_gem(params)
46       if params[:github]
47         params[:git] = "https://github.com/#{params[:github]}.git"
48       elsif params[:bitbucket]
49         if params[:method] == "ssh"
50           params[:git] = "git@bitbucket.org:#{params[:bitbucket]}.git"
51         else
52           params[:git] = "https://bitbucket.org/#{params[:bitbucket]}.git"
53         end
54       elsif params[:mgem]
55         mgem_list_dir = "#{gem_clone_dir}/mgem-list"
56         mgem_list_url = 'https://github.com/mruby/mgem-list.git'
57         if File.exist? mgem_list_dir
58           git.run_pull mgem_list_dir, mgem_list_url if $pull_gems
59         else
60           mkdir_p mgem_list_dir
61           git.run_clone mgem_list_dir, mgem_list_url, "--depth 1"
62         end
63
64         require 'yaml'
65
66         conf_path = "#{mgem_list_dir}/#{params[:mgem]}.gem"
67         conf_path = "#{mgem_list_dir}/mruby-#{params[:mgem]}.gem" unless File.exist? conf_path
68         fail "mgem not found: #{params[:mgem]}" unless File.exist? conf_path
69         conf = YAML.load File.read conf_path
70
71         fail "unknown mgem protocol: #{conf['protocol']}" if conf['protocol'] != 'git'
72         params[:git] = conf['repository']
73         params[:branch] = conf['branch'] if conf['branch']
74       end
75
76       if params[:core]
77         gemdir = "#{root}/mrbgems/#{params[:core]}"
78       elsif params[:git]
79         url = params[:git]
80         gemdir = "#{gem_clone_dir}/#{url.match(/([-\w]+)(\.[-\w]+|)$/).to_a[1]}"
81
82         # by default the 'master' branch is used
83         branch = params[:branch] ? params[:branch] : 'master'
84
85         lock = locks[url] if lock_enabled?
86
87         if File.exist?(gemdir)
88           if $pull_gems
89             # Jump to the top of the branch
90             git.run_checkout gemdir, branch
91             git.run_pull gemdir, url
92           elsif params[:checksum_hash]
93             git.run_checkout_detach gemdir, params[:checksum_hash]
94           elsif lock
95             git.run_checkout_detach gemdir, lock['commit']
96           end
97         else
98           options = [params[:options]] || []
99           options << "--recursive"
100           options << "--branch \"#{branch}\""
101           options << "--depth 1" unless params[:checksum_hash]
102           mkdir_p "#{gem_clone_dir}"
103           git.run_clone gemdir, url, options
104
105           # Jump to the specified commit
106           if params[:checksum_hash]
107             git.run_checkout_detach gemdir, params[:checksum_hash]
108           elsif lock
109             git.run_checkout_detach gemdir, lock['commit']
110           end
111         end
112
113         if lock_enabled?
114           @gem_dir_to_repo_url[gemdir] = url unless params[:path]
115           locks[url] = {
116             'url' => url,
117             'branch' => git.current_branch(gemdir),
118             'commit' => git.commit_hash(gemdir),
119           }
120         end
121
122         gemdir << "/#{params[:path]}" if params[:path]
123       elsif params[:path]
124         require 'pathname'
125         gemdir = Pathname.new(params[:path]).absolute? ? params[:path] : "#{root}/#{params[:path]}"
126       else
127         fail "unknown gem option #{params}"
128       end
129
130       gemdir
131     end
132
133     def enable_gems?
134       !@gems.empty?
135     end
136   end # LoadGems
137 end # MRuby