diff options
Diffstat (limited to 'import/omnibus.to_chunk')
-rw-r--r-- | import/omnibus.to_chunk | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/import/omnibus.to_chunk b/import/omnibus.to_chunk new file mode 100644 index 00000000..67ff1175 --- /dev/null +++ b/import/omnibus.to_chunk @@ -0,0 +1,106 @@ +#!/usr/bin/env ruby +# +# Create a chunk morphology to integrate Omnibus software in Baserock +# +# Copyright (C) 2014 Codethink Limited +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +require 'bundler' +require 'omnibus' + +require 'optparse' +require 'rubygems/commands/install_command' +require 'shellwords' +require 'yaml' + +TARGET_PROJECT = 'chefdk' # should come from args + +TARGET_SOFTWARE = 'chefdk' # should come from args + +class Omnibus::Builder + # It's possible to use `gem install` in build commands, which is a great + # way of subverting the dependency tracking Omnibus provides. It's done + # in `omnibus-chef/config/software/chefdk.rb`, for example. + # + # To handle this, here we extend the class that executes the build commands + # to detect when `gem install` is run. It uses the Gem library to turn the + # commandline back into a Bundler::Dependency object that we can use. + + class GemInstallCommandParser < Gem::Commands::InstallCommand + def dependency_list_from_commandline(args) + handle_options args + options[:args].collect do |gem_name| + Bundler::Dependency.new(gem_name, options[:version]) + end + end + end + + def gem(command, options = {}) + # This function implements the 'gem' function in the build-commands DSL. + if command.match /^install/ + parser = GemInstallCommandParser.new + args = Shellwords.split(command).drop(1) + gems = parser.dependency_list_from_commandline(args) + manually_installed_rubygems.concat gems + end + end + + def manually_installed_rubygems + @manually_installed_rubygems ||= [] + end +end + +class OmnibusChunkMorphologyGenerator + def deps_for_software(software) + deps = Hash.new + software.dependencies.each do |dep| + deps[dep] = 0 + end + software.builder.manually_installed_rubygems.each do |dep| + deps[dep.name] = dep.requirement.to_s + end + deps + end + + def generate_chunk_morph_for_software(software) + { + "name" => software.name, + "kind" => "chunk", + # Possibly this tool should look at software.build and + # generate suitable configure, build and install-commands. + # For now: don't bother! + + # FIXME: are these build or runtime dependencies? We'll assume both. + "x-build-dependencies-omnibus" => deps_for_software(software), + "x-runtime-dependencies-omnibus" => deps_for_software(software), + } + end + + def write_morph(file, morph) + file.write(YAML.dump(morph)) + end + + def run + project = Omnibus::Project.load(TARGET_PROJECT) + + software = Omnibus::Software.load(project, TARGET_SOFTWARE) + + morph = generate_chunk_morph_for_software(software) + + write_morph(STDOUT, morph) + end +end + +OmnibusChunkMorphologyGenerator.new.run |