Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nesting of Bundler block DSL #84

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions lib/appraisal/bundler_dsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
require 'appraisal/dependency_list'

module Appraisal
class BundlerDSL
attr_reader :dependencies

PARTS = %w(source ruby_version git_sources path_sources dependencies groups
platforms gemspec)

def initialize
@sources = []
@ruby_version = nil
@dependencies = DependencyList.new
@gemspec = nil
@groups = {}
@platforms = {}
@git_sources = {}
@path_sources = {}
end

def run(&block)
instance_exec(&block)
end

def gem(name, *requirements)
@dependencies.add(name, requirements)
end

def group(*names, &block)
@groups[names] ||= Group.new(names)
@groups[names].run(&block)
end

# :nodoc:
def groups(*names, &block)
$stderr.puts <<-WARNING.gsub(/\n\s+/, " ").strip
Warning: `#groups` is deprecated and will be removed in 2.0.0.
Please use `#group` instead.
WARNING

group(*names, &block)
end

def platforms(*names, &block)
@platforms[names] ||= Platform.new(names)
@platforms[names].run(&block)
end

alias_method :platform, :platforms

def source(source)
@sources << source
end

def ruby(ruby_version)
@ruby_version = ruby_version
end

def git(source, options = {}, &block)
@git_sources[source] ||= GitSource.new(source, options)
@git_sources[source].run(&block)
end

def path(source, options = {}, &block)
@path_sources[source] ||= PathSource.new(source, options)
@path_sources[source].run(&block)
end

def to_s
Utils.join_parts PARTS.map { |part| send("#{part}_entry") }
end

def for_dup
Utils.join_parts PARTS.map { |part| send("#{part}_entry_for_dup") }
end

def gemspec(options = {})
@gemspec = Gemspec.new(options)
end

private

def source_entry
@sources.uniq.map { |source| "source #{source.inspect}" }.join("\n")
end

alias_method :source_entry_for_dup, :source_entry

def ruby_version_entry
if @ruby_version
"ruby #{@ruby_version.inspect}"
end
end

alias_method :ruby_version_entry_for_dup, :ruby_version_entry

[:dependencies, :gemspec].each do |method_name|
class_eval <<-METHODS, __FILE__, __LINE__
private

def #{method_name}_entry
if @#{method_name}
@#{method_name}.to_s
end
end

def #{method_name}_entry_for_dup
if @#{method_name}
@#{method_name}.for_dup
end
end
METHODS
end

[:git_sources, :path_sources, :platforms, :groups].each do |method_name|
class_eval <<-METHODS, __FILE__, __LINE__
private

def #{method_name}_entry
@#{method_name}.values.map(&:to_s).join("\n\n")
end

def #{method_name}_entry_for_dup
@#{method_name}.values.map(&:for_dup).join("\n\n")
end
METHODS
end

def indent(string)
string.strip.gsub(/^(.+)$/, ' \1')
end
end
end
135 changes: 9 additions & 126 deletions lib/appraisal/gemfile.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
require 'appraisal/dependency_list'
require 'appraisal/gemspec'
require 'appraisal/git_source'
require 'appraisal/path_source'
require 'appraisal/group'
require 'appraisal/platform'
require 'appraisal/bundler_dsl'

module Appraisal
# Load bundler Gemfiles and merge dependencies
class Gemfile
attr_reader :dependencies

PARTS = %w(source ruby_version git_sources path_sources dependencies groups
platforms gemspec)

def initialize
@sources = []
@ruby_version = nil
@dependencies = DependencyList.new
@gemspec = nil
@groups = {}
@platforms = {}
@git_sources = {}
@path_sources = {}
end
autoload :Gemspec, 'appraisal/gemspec'
autoload :GitSource, 'appraisal/git_source'
autoload :PathSource, 'appraisal/path_source'
autoload :Group, 'appraisal/group'
autoload :Platform, 'appraisal/platform'

# Load bundler Gemfiles and merge dependencies
class Gemfile < BundlerDSL
def load(path)
run(IO.read(path))
end
Expand All @@ -32,112 +17,10 @@ def run(definitions)
instance_eval(definitions, __FILE__, __LINE__) if definitions
end

def gem(name, *requirements)
@dependencies.add(name, requirements)
end

def group(*names, &block)
@groups[names] ||= Group.new(names)
@groups[names].run(&block)
end

# :nodoc:
def groups(*names, &block)
$stderr.puts <<-WARNING.gsub(/\n\s+/, " ").strip
Warning: `#groups` is deprecated and will be removed in 2.0.0.
Please use `#group` instead.
WARNING

group(*names, &block)
end

def platforms(*names, &block)
@platforms[names] ||= Platform.new(names)
@platforms[names].run(&block)
end

alias_method :platform, :platforms

def source(source)
@sources << source
end

def ruby(ruby_version)
@ruby_version = ruby_version
end

def git(source, options = {}, &block)
@git_sources[source] ||= GitSource.new(source, options)
@git_sources[source].run(&block)
end

def path(source, options = {}, &block)
@path_sources[source] ||= PathSource.new(source, options)
@path_sources[source].run(&block)
end

def to_s
Utils.join_parts PARTS.map { |part| send("#{part}_entry") }
end

def dup
Gemfile.new.tap do |gemfile|
gemfile.run(
Utils.join_parts PARTS.map { |part| send("#{part}_entry_for_dup") }
)
gemfile.run(for_dup)
end
end

def gemspec(options = {})
@gemspec = Gemspec.new(options)
end

private

def source_entry
@sources.uniq.map { |source| "source #{source.inspect}" }.join("\n")
end

alias_method :source_entry_for_dup, :source_entry

def ruby_version_entry
if @ruby_version
"ruby #{@ruby_version.inspect}"
end
end

alias_method :ruby_version_entry_for_dup, :ruby_version_entry

[:dependencies, :gemspec].each do |method_name|
class_eval <<-METHODS, __FILE__, __LINE__
private

def #{method_name}_entry
if @#{method_name}
@#{method_name}.to_s
end
end

def #{method_name}_entry_for_dup
if @#{method_name}
@#{method_name}.for_dup
end
end
METHODS
end

[:git_sources, :path_sources, :platforms, :groups].each do |method_name|
class_eval <<-METHODS, __FILE__, __LINE__
private

def #{method_name}_entry
@#{method_name}.values.map(&:to_s).join("\n\n")
end

def #{method_name}_entry_for_dup
@#{method_name}.values.map(&:for_dup).join("\n\n")
end
METHODS
end
end
end
26 changes: 7 additions & 19 deletions lib/appraisal/git_source.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,30 @@
require 'appraisal/dependency_list'
require 'appraisal/bundler_dsl'
require 'appraisal/utils'

module Appraisal
class GitSource
class GitSource < BundlerDSL
def initialize(source, options = {})
@dependencies = DependencyList.new
super()
@source = source
@options = options
end

def gem(name, *requirements)
@dependencies.add(name, requirements)
end

def run(&block)
instance_exec(&block)
end

def to_s
dependencies = @dependencies.to_s.strip.gsub(/^/, ' ')

if @options.empty?
"git #{Utils.prefix_path(@source).inspect} do\n#{dependencies}\nend"
"git #{Utils.prefix_path(@source).inspect} do\n#{indent(super)}\nend"
else
"git #{Utils.prefix_path(@source).inspect}, #{Utils.format_string(@options)} do\n" +
"#{dependencies}\nend"
"#{indent(super)}\nend"
end
end

# :nodoc:
def for_dup
dependencies = @dependencies.for_dup.strip.gsub(/^/, ' ')

if @options.empty?
"git #{@source.inspect} do\n#{dependencies}\nend"
"git #{@source.inspect} do\n#{indent(super)}\nend"
else
"git #{@source.inspect}, #{Utils.format_string(@options)} do\n" +
"#{dependencies}\nend"
"#{indent(super)}\nend"
end
end
end
Expand Down
Loading