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