Last active
June 15, 2017 01:51
-
-
Save jamesbeedy/3a8b4de1d3f38be4e551ef028df0ba9c to your computer and use it in GitHub Desktop.
ruby plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from shutil import rmtree | |
from os import makedirs, environ | |
from os.path import exists, join, abspath | |
from snapcraft import BasePlugin | |
from snapcraft.sources import Tar | |
class RubyPlugin(BasePlugin): | |
@classmethod | |
def schema(cls): | |
schema = super().schema() | |
schema['properties']['ruby-version'] = { | |
'type': 'string', | |
'default': "2.3.1" | |
} | |
schema['properties']['gems'] = { | |
'type': 'array', | |
'minitems': 1, | |
'uniqueItems': True, | |
'items': { | |
'type': 'string' | |
}, | |
'default': [] | |
} | |
return schema | |
@classmethod | |
def get_pull_properties(cls): | |
# Inform Snapcraft of the properties associated with pulling. If these | |
# change in the YAML Snapcraft will consider the build step dirty. | |
return ['ruby-version'] | |
def __init__(self, name, options, project): | |
super().__init__(name, options, project) | |
self._ruby_version = self.options.ruby_version | |
self._rubylib = join(self.installdir, 'lib', 'ruby') | |
self._ruby_dir = join(self.partdir, 'ruby') | |
self._gem_path = join(self._rubylib, 'gems', self._ruby_version, 'gems') | |
self._gem_home = self._gem_path | |
self._ruby_download_url = \ | |
('https://cache.ruby-lang.org/pub/ruby/ruby-%s.tar.gz' % \ | |
self.options.ruby_version) | |
self._ruby_tar = Tar(self._ruby_download_url, self._ruby_dir) | |
self._gems = self.options.gems + ['bundler'] | |
pkgs = ['gcc', 'g++', 'make', 'zlib1g-dev', | |
'libssl-dev', 'libreadline-dev'] | |
for pkg in pkgs: | |
self.build_packages.append(pkg) | |
def pull(self): | |
super().pull() | |
makedirs(self._ruby_dir, exist_ok=True) | |
self._ruby_tar.download() | |
def build(self): | |
super().build() | |
self._ruby_install(builddir=self._ruby_dir) | |
self._gem_install() | |
self._bundle_install() | |
def _env(self): | |
"""Ruby env vars.""" | |
env = environ.copy() | |
env['RUBYLIB'] = self._rubylib | |
env['GEM_PATH'] = self._gem_path | |
env['GEM_HOME'] = self._gem_home | |
return env | |
def _ruby_install(self, builddir): | |
self._ruby_tar.provision( | |
builddir, clean_target=False, keep_tarball=True) | |
self.run(['./configure', '--disable-install-rdoc', '--prefix=/'], cwd=builddir) | |
self.run(['make', '-j{}'.format(self.parallel_build_count)], cwd=builddir) | |
self.run(['make', 'install', 'DESTDIR={}'.format(self.installdir)], cwd=builddir) | |
def _gem_install(self): | |
gem_install = ['gem', 'install'] | |
for gem in self._gems: | |
self.run(gem_install + [gem], env=self._env()) | |
def _bundle_install(self): | |
bundle_install = ['bundle', 'install'] | |
if exists(join(rootdir, 'Gemfile')): | |
self.run(bundle_install, env=self._env()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment