summaryrefslogtreecommitdiffhomepage
path: root/tasks/mruby_build_gem.rake
blob: b40522331571b6f05de41d3fa55acf00da4c2e7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module MRuby
  class GemList < Array
    def <<(gem)
      fail ArgumentError.new("Don't find directory for this GEM") unless gem.respond_to? :dir
      unless include?(gem)
        super(gem)
      else
        # GEM was already added to this list
      end
    end

    # we assume that a gem with the same directory is equal
    def include?(gem)
      detect {|g| g.dir == gem.dir }
    end
  end

  module LoadGems
    def gembox(gemboxfile)
      gembox = File.expand_path("#{gemboxfile}.gembox", "#{MRUBY_ROOT}/mrbgems")
      fail "Can't find gembox '#{gembox}'" unless File.exists?(gembox)
      GemBox.config = self
      instance_eval File.read(gembox)
    end

    def gem(gemdir, &block)
      caller_dir = File.expand_path(File.dirname(/^(.*?):\d/.match(caller.first).to_a[1]))
      if gemdir.is_a?(Hash)
        gemdir = load_special_path_gem(gemdir)
      else
        gemdir = File.expand_path(gemdir, caller_dir)
      end
      gemrake = File.join(gemdir, "mrbgem.rake")

      fail "Can't find #{gemrake}" unless File.exists?(gemrake)
      Gem.current = nil
      load gemrake
      return nil unless Gem.current

      Gem.current.dir = gemdir
      Gem.current.build = MRuby::Build.current
      Gem.current.build_config_initializer = block
      gems << Gem.current
      Gem.current
    end

    def load_special_path_gem(params)
      if params[:github]
        params[:git] = "https://github.com/#{params[:github]}.git"
      elsif params[:bitbucket]
        params[:git] = "https://bitbucket.org/#{params[:bitbucket]}.git"
      end

      if params[:core]
        gemdir = "#{root}/mrbgems/#{params[:core]}"
      elsif params[:git]
        url = params[:git]
        gemdir = "build/mrbgems/#{url.match(/([-_\w]+)(\.[-_\w]+|)$/).to_a[1]}"
        return gemdir if File.exists?(gemdir)

        options = [params[:options]] || []
        options << "--branch \"#{params[:branch]}\"" if params[:branch]

        FileUtils.mkdir_p "build/mrbgems"
        git.run_clone gemdir, url, options
      else
        fail "unknown gem option #{params}"
      end

      gemdir
    end

    def enable_gems?
      [email protected]?
    end
  end # LoadGems
end # MRuby