Statistics
| Branch: | Tag: | Revision:

one / share / rubygems / generate @ 05241124

History | View | Annotate | Download (6.06 KB)

1
#!/usr/bin/env ruby
2

    
3
# -------------------------------------------------------------------------- #
4
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                #
5
#                                                                            #
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
7
# not use this file except in compliance with the License. You may obtain    #
8
# a copy of the License at                                                   #
9
#                                                                            #
10
# http://www.apache.org/licenses/LICENSE-2.0                                 #
11
#                                                                            #
12
# Unless required by applicable law or agreed to in writing, software        #
13
# distributed under the License is distributed on an "AS IS" BASIS,          #
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15
# See the License for the specific language governing permissions and        #
16
# limitations under the License.                                             #
17
#--------------------------------------------------------------------------- #
18

    
19
require 'fileutils'
20
require 'tmpdir'
21

    
22

    
23
DEFAULTS={
24
    :version => "5.3.90",
25
    :date => Time.now.strftime("%Y-%m-%d"),
26
    :dependencies => []
27
}
28

    
29
TEMPLATE=<<EOT
30
# This file is automatically generated
31

    
32
Gem::Specification.new do |s|
33
  s.name        = '$(NAME)'
34
  s.version     = '$(VERSION)'
35
  s.date        = '$(DATE)'
36
  s.summary     = "$(SUMMARY)"
37
  s.description = "$(DESCRIPTION)"
38
  s.authors     = ["OpenNebula"]
39
  s.email       = 'contact@opennebula.org'
40
  s.license     = 'Apache-2.0'
41
  s.files       = [
42
$(FILES)
43
  ]
44
  s.homepage    = 'http://opennebula.org'
45
$(EXECUTABLES)
46
$(DEPENDENCIES)
47
end
48

    
49
EOT
50

    
51
GEM_DESCRIPTION={
52
    :oca => {
53
        :name => 'opennebula',
54
        :files => [
55
            ['src/oca/ruby/opennebula.rb', '/lib'],
56

    
57
            # Needed for onevcenter
58
            ['src/vmm_mad/remotes/vcenter/vcenter_driver.rb', '/lib'],
59

    
60
            # vcenter_driver.rb dependencies
61
            ['src/mad/ruby/VirtualMachineDriver.rb', '/lib'],
62
            ['src/mad/ruby/OpenNebulaDriver.rb', '/lib'],
63
            ['src/mad/ruby/CommandManager.rb', '/lib'],
64
            ['src/mad/ruby/ActionManager.rb', '/lib'],
65
            ['src/mad/ruby/DriverExecHelper.rb', '/lib'],
66

    
67
            ['src/oca/ruby/opennebula/*.rb', '/lib/opennebula'],
68
            ['src/authm_mad/remotes/**/*.rb', '/lib/opennebula'],
69
            ['src/cloud/common/CloudClient.rb', '/lib/cloud'],
70
            ['NOTICE', ''],
71
            ['LICENSE', '']
72
        ],
73
        :summary => 'OpenNebula Client API',
74
        :description => 'Libraries needed to talk to OpenNebula',
75
        :dependencies => [
76
            'nokogiri',
77
            'json',
78
            'rbvmomi'
79
        ]
80
    },
81

    
82
    :cli => {
83
        :name => 'opennebula-cli',
84
        :files => [
85
            ['src/cli/one[a-z]*', '/bin'],
86
            ['src/cli/*.rb', '/lib'],
87
            ['src/cli/one_helper/*.rb', '/lib/one_helper'],
88
            ['NOTICE', ''],
89
            ['LICENSE', '']
90
        ],
91
        :summary => 'OpenNebula Command Line Interface',
92
        :description => 'Commands used to talk to OpenNebula',
93
        :dependencies => [
94
            ['opennebula', "= #{DEFAULTS[:version]}"]
95
        ]
96
    }
97
}
98

    
99
def sane_prefix(prefix)
100
    return '' if !prefix or prefix.empty?
101

    
102
    p=prefix
103

    
104
    p.slice!(0) if p[0,1]=='/'
105
    p<<'/' if p[-1,1]!='/'
106

    
107
    p
108
end
109

    
110
def file_list(description)
111
    files=Array.new
112

    
113
    description[:files].each do |f, prefix|
114
        source=Dir.glob(f)
115

    
116
        files+=source.map do |source|
117
            [source, sane_prefix(prefix)+File.basename(source)]
118
        end
119
    end
120

    
121
    files
122
end
123

    
124
def copy_files(files, source_prefix='', destination_prefix='')
125
    files.each do |file|
126
        source=source_prefix+file[0]
127
        destination=destination_prefix+file[1]
128

    
129
        dir=File.dirname destination
130

    
131
        FileUtils.mkdir_p(dir) if !File.exist?(dir)
132

    
133
        FileUtils.cp(source, destination)
134
    end
135
end
136

    
137
def generate_gem_file_list(files)
138
    files.map do |f|
139
        "    '#{f.last}'"
140
    end.join(",\n")
141
end
142

    
143
def generate_gem_executable_list(files)
144
    executables=files.
145
        select {|f| f.last.match(/^bin\//) }.
146
        map {|f| "'#{File.basename(f.last)}'" }.
147
        join(", ")
148

    
149
    if !executables.empty?
150
        "  s.executables=[#{executables}]"
151
    else
152
        nil
153
    end
154
end
155

    
156
def generate_dependencies(dependencies)
157
    dependencies.map do |d|
158
        line="  s.add_runtime_dependency "
159
        if Array===d
160
            line<<d.map {|part| "'#{part}'" }.join(", ")
161
        else
162
            line<<"'#{d}'"
163
        end
164
        line
165
    end.join("\n")
166
end
167

    
168
def populate_gem_template(description, files)
169
    reg=/\$\((\w+)\)/
170
    TEMPLATE.gsub(reg) do |chunk|
171
        m=chunk.match(reg)
172
        var=m[1].downcase.to_sym
173

    
174
        if var==:files
175
            generate_gem_file_list(files)
176
        elsif var==:dependencies
177
            generate_dependencies(
178
                description[:dependencies]||DEFAULTS[:dependencies])
179
        elsif var==:executables
180
            generate_gem_executable_list(files)
181
        elsif description[var]
182
            description[var]
183
        else
184
            DEFAULTS[var]
185
        end
186
    end
187
end
188

    
189
def generate_gem_file(gem_file, description, files)
190
    File.open(gem_file, "w") do |f|
191
        f.write(populate_gem_template(description, files))
192
    end
193
end
194

    
195
def generate_gem(description)
196
    Dir.mktmpdir do |tmp|
197
        files=file_list(description)
198
        copy_files(files, '', tmp+"/")
199

    
200
        gem_file=tmp+"/gem.gemspec"
201
        generate_gem_file(gem_file, description, files)
202

    
203
        pwd=Dir.pwd
204

    
205
        Dir.chdir(tmp) do
206
            system("gem build gem.gemspec")
207

    
208
            if $?.exitstatus!=0
209
                puts "Error generating gem"
210
                exit(-1)
211
            end
212

    
213
            Dir.glob("*.gem").each do |f|
214
                FileUtils.cp(f, pwd) 
215
            end
216
        end
217
    end
218
end
219

    
220
# Go to source code root dir
221
root=Dir.pwd
222
root.gsub!(/\/share\/rubygems$/, '')
223
Dir.chdir(root)
224

    
225

    
226
GEM_DESCRIPTION.each {|name, gem| generate_gem(gem) }
227