Skip to content

Commit

Permalink
Move all code inside ruby.rb crazy fun into its own namespace to make…
Browse files Browse the repository at this point in the history
… it easier to triage
  • Loading branch information
luke-hill authored and barancev committed Oct 30, 2019
1 parent dd80d5e commit 95cd0f7
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 210 deletions.
221 changes: 11 additions & 210 deletions rake_tasks/crazy_fun/mappings/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
class RubyMappings
require_relative 'ruby_mappings/add_test_defaults'
require_relative 'ruby_mappings/add_test_dependencies'
require_relative 'ruby_mappings/check_test_args'
require_relative 'ruby_mappings/expand_source_files'
require_relative 'ruby_mappings/ruby_docs'
require_relative 'ruby_mappings/ruby_gem'
require_relative 'ruby_mappings/ruby_library'
require_relative 'ruby_mappings/ruby_linter'
require_relative 'ruby_mappings/ruby_test'

class RubyMappings
def add_all(fun)
fun.add_mapping "ruby_library", RubyLibrary.new

Expand All @@ -15,215 +24,7 @@ def add_all(fun)
fun.add_mapping "rubydocs", RubyDocs.new
fun.add_mapping "rubygem", RubyGem.new
end

class RubyLibrary < Tasks
def handle(_fun, dir, args)
desc "Build #{args[:name]} in build/#{dir}"
task_name = task_name(dir, args[:name])

t = task task_name do
puts "Preparing: #{task_name} in #{build_dir}/#{dir}"
copy_sources dir, args[:srcs]
copy_resources dir, args[:resources], build_dir if args[:resources]
remove_svn_dirs
end

add_dependencies t, dir, args[:deps]
add_dependencies t, dir, args[:resources]
end

def copy_sources(dir, globs)
globs.each do |glob|
Dir[File.join(dir, glob)].each do |file|
destination = destination_for(file)
mkdir_p File.dirname(destination)
cp_r file, destination
end
end
end

def remove_svn_dirs
Dir["#{build_dir}/rb/**/.svn"].each { |file| rm_rf file }
end

def destination_for(file)
File.join build_dir, file
end

def build_dir
"build"
end
end

class CheckTestArgs
def handle(_fun, dir, args)
raise "no :srcs specified for #{dir}" unless args.has_key? :srcs
raise "no :name specified for #{dir}" unless args.has_key? :name
end
end

class AddTestDefaults
def handle(_fun, dir, args)
args[:include] = Array(args[:include])
args[:include] << "#{dir}/spec"

args[:command] = args[:command] || "rspec"
end
end

class ExpandSourceFiles
def handle(_fun, dir, args)
args[:srcs] = args[:srcs].map { |str| Dir[File.join(dir, str)] }.flatten
end
end

class AddTestDependencies < Tasks
def handle(_fun, dir, args)
task = Rake::Task[task_name(dir, "#{args[:name]}-test")]

if args.has_key?(:deps)
add_dependencies task, dir, args[:deps]
end
end
end

class RubyTest < Tasks
def handle(_fun, dir, args)
desc "Run ruby tests for #{args[:name]}"
task task_name(dir, "#{args[:name]}-test") => %W[//#{dir}:bundle] do
STDOUT.sync = true
puts "Running: #{args[:name]} ruby tests"

if args[:name].match /^remote-(.*)/
puts $1
ENV['WD_REMOTE_BROWSER'] = $1.tr('-', '_')
puts ENV['WD_REMOTE_BROWSER']
ENV['WD_SPEC_DRIVER'] = 'remote'
else
ENV['WD_SPEC_DRIVER'] = args[:name].tr('-', '_')
end

ruby :include => args[:include],
:command => args[:command],
:args => %w[--format doc --color] + (!!ENV['example'] ? ['--example', ENV['example']] : []),
:debug => !!ENV['log'],
:files => args[:srcs]
end
end
end

class RubyLinter < Tasks
def handle(_fun, dir, args)
desc 'Run RuboCop'
task task_name(dir, args[:name]) => args[:deps] do
ruby :command => 'rubocop',
:files => args[:srcs]
end
end
end # RubyLinter

class RubyDocs < Tasks
def handle(_fun, dir, args)
files = args[:files] || raise("no :files specified for rubydocs")
output_dir = args[:output_dir] || raise("no :output_dir specified for rubydocs")
readme = args[:readme] || raise("no :readme specified for rubydocs")

files = files.map { |pattern| "build/rb/#{pattern}" }
output_dir = "build/#{output_dir}"
readme = "build/rb/#{readme}"

desc 'Generate Ruby API docs'
task "//#{dir}:docs" => args[:deps] do
yard_args = %w[doc --verbose]
yard_args += ["--output-dir", output_dir]
yard_args += ["--readme", readme]

ruby :command => "yard",
:args => yard_args,
:files => files
end
end
end # RubyDocs

class RubyGem
def handle(_fun, dir, args)
raise "no :gemspec for rubygem" unless args[:gemspec]

define_clean_task dir, args
define_build_task dir, args
define_release_task dir, args

define_gem_install_task dir, args
end

def define_build_task(dir, args)
gemspec = File.expand_path(args[:gemspec])
deps = Array(args[:deps])
spec_dir = File.dirname(gemspec)

desc "Build #{args[:gemspec]}"
task "//#{dir}:gem:build" => deps do
require 'rubygems/package'

file = Dir.chdir(spec_dir) do
spec = eval(File.read(gemspec))
Gem::Package.build(spec)
end

mv File.join(spec_dir, file), "build/"
end
end

def define_clean_task(dir, _args)
desc 'Clean rubygem artifacts'
task "//#{dir}:gem:clean" do
Dir['build/*.gem'].each { |gem| rm(gem) }
end
end

def define_release_task(dir, _args)
desc 'Build and release the ruby gem to Gemcutter'
task "//#{dir}:gem:release" => %W[//#{dir}:gem:clean //#{dir}:gem:build] do
gem = Dir['build/*.gem'].first # safe as long as :clean does its job
sh "gem", "push", gem
end
end

def define_gem_install_task(dir, _args)
desc 'Install gem dependencies for the current Ruby'
task "//#{dir}:bundle" do
bundler_path = "#{Dir.pwd}/build/third_party/rb/bundler"
mkdir_p bundler_path

bin_path = [bundler_path, "bin"].join(File::SEPARATOR)
bin_path.tr!(File::SEPARATOR, File::ALT_SEPARATOR) if File::ALT_SEPARATOR # Windows
mkdir_p bin_path

path = ENV["PATH"].split(File::PATH_SEPARATOR)
path = [bin_path, path].flatten.uniq.join(File::PATH_SEPARATOR)

ENV["BUNDLE_GEMFILE"] = "rb/Gemfile"
ENV["GEM_PATH"] = bundler_path
ENV["PATH"] = path

gems = `gem list`.split("\n")
if gems.grep(/^bundler\s/).empty?
bundler_gem = Dir["third_party/rb/bundler-*.gem"].first

sh "gem", "install", "--local", "--no-ri", "--no-rdoc",
"--install-dir", ENV["GEM_PATH"],
"--bindir", ENV["PATH"].split(File::PATH_SEPARATOR).first,
bundler_gem
end

sh "bundle", "config", "--local", "cache_path", "../third_party/rb/vendor/cache"
sh "bundle", "config", "--local", "path", "#{Dir.pwd}/build/third_party/rb/vendor/bundle"

sh "bundle", "install", "--local"
end
end
end # RubyGem
end # RubyMappings
end

def ruby(opts)
cmd = ["bundle", "exec", "ruby", "-w"]
Expand Down
10 changes: 10 additions & 0 deletions rake_tasks/crazy_fun/mappings/ruby_mappings/add_test_defaults.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class RubyMappings
class AddTestDefaults
def handle(_fun, dir, args)
args[:include] = Array(args[:include])
args[:include] << "#{dir}/spec"

args[:command] = args[:command] || "rspec"
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class RubyMappings
class AddTestDependencies < Tasks
def handle(_fun, dir, args)
task = Rake::Task[task_name(dir, "#{args[:name]}-test")]

if args.has_key?(:deps)
add_dependencies task, dir, args[:deps]
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class RubyMappings
class CheckTestArgs
def handle(_fun, dir, args)
raise "no :srcs specified for #{dir}" unless args.has_key? :srcs
raise "no :name specified for #{dir}" unless args.has_key? :name
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class RubyMappings
class ExpandSourceFiles
def handle(_fun, dir, args)
args[:srcs] = args[:srcs].map { |str| Dir[File.join(dir, str)] }.flatten
end
end
end
24 changes: 24 additions & 0 deletions rake_tasks/crazy_fun/mappings/ruby_mappings/ruby_docs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class RubyMappings
class RubyDocs < Tasks
def handle(_fun, dir, args)
files = args[:files] || raise("no :files specified for rubydocs")
output_dir = args[:output_dir] || raise("no :output_dir specified for rubydocs")
readme = args[:readme] || raise("no :readme specified for rubydocs")

files = files.map { |pattern| "build/rb/#{pattern}" }
output_dir = "build/#{output_dir}"
readme = "build/rb/#{readme}"

desc 'Generate Ruby API docs'
task "//#{dir}:docs" => args[:deps] do
yard_args = %w[doc --verbose]
yard_args += ["--output-dir", output_dir]
yard_args += ["--readme", readme]

ruby :command => "yard",
:args => yard_args,
:files => files
end
end
end
end
80 changes: 80 additions & 0 deletions rake_tasks/crazy_fun/mappings/ruby_mappings/ruby_gem.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
class RubyMappings
class RubyGem
def handle(_fun, dir, args)
raise "no :gemspec for rubygem" unless args[:gemspec]

define_clean_task dir, args
define_build_task dir, args
define_release_task dir, args

define_gem_install_task dir, args
end

def define_build_task(dir, args)
gemspec = File.expand_path(args[:gemspec])
deps = Array(args[:deps])
spec_dir = File.dirname(gemspec)

desc "Build #{args[:gemspec]}"
task "//#{dir}:gem:build" => deps do
require 'rubygems/package'

file = Dir.chdir(spec_dir) do
spec = eval(File.read(gemspec))
Gem::Package.build(spec)
end

mv File.join(spec_dir, file), "build/"
end
end

def define_clean_task(dir, _args)
desc 'Clean rubygem artifacts'
task "//#{dir}:gem:clean" do
Dir['build/*.gem'].each { |gem| rm(gem) }
end
end

def define_release_task(dir, _args)
desc 'Build and release the ruby gem to Gemcutter'
task "//#{dir}:gem:release" => %W[//#{dir}:gem:clean //#{dir}:gem:build] do
gem = Dir['build/*.gem'].first # safe as long as :clean does its job
sh "gem", "push", gem
end
end

def define_gem_install_task(dir, _args)
desc 'Install gem dependencies for the current Ruby'
task "//#{dir}:bundle" do
bundler_path = "#{Dir.pwd}/build/third_party/rb/bundler"
mkdir_p bundler_path

bin_path = [bundler_path, "bin"].join(File::SEPARATOR)
bin_path.tr!(File::SEPARATOR, File::ALT_SEPARATOR) if File::ALT_SEPARATOR # Windows
mkdir_p bin_path

path = ENV["PATH"].split(File::PATH_SEPARATOR)
path = [bin_path, path].flatten.uniq.join(File::PATH_SEPARATOR)

ENV["BUNDLE_GEMFILE"] = "rb/Gemfile"
ENV["GEM_PATH"] = bundler_path
ENV["PATH"] = path

gems = `gem list`.split("\n")
if gems.grep(/^bundler\s/).empty?
bundler_gem = Dir["third_party/rb/bundler-*.gem"].first

sh "gem", "install", "--local", "--no-ri", "--no-rdoc",
"--install-dir", ENV["GEM_PATH"],
"--bindir", ENV["PATH"].split(File::PATH_SEPARATOR).first,
bundler_gem
end

sh "bundle", "config", "--local", "cache_path", "../third_party/rb/vendor/cache"
sh "bundle", "config", "--local", "path", "#{Dir.pwd}/build/third_party/rb/vendor/bundle"

sh "bundle", "install", "--local"
end
end
end
end
Loading

0 comments on commit 95cd0f7

Please sign in to comment.