forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor crazy fun namespace 1 (SeleniumHQ#7667)
* Namespaced and fixed up common.rb and export.rb * Sort out folder and python files
- Loading branch information
Showing
20 changed files
with
441 additions
and
429 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,240 +1 @@ | ||
module FileCopyHack | ||
def cp_r(src, dest, opts = {}) | ||
super | ||
rescue => ex | ||
raise unless ex.message =~ /operation not permitted|Permission denied/i | ||
Dir["#{dest}/**/.svn"].each { |file| rm_rf file } | ||
|
||
# virtual box shared folders has a problem with some of the .svn files | ||
if ENV['USER'] == "vagrant" && opts.empty? | ||
sh "cp", "-r", src, dest | ||
else | ||
super(src, dest, opts) | ||
end | ||
end | ||
end | ||
|
||
class Tasks | ||
include FileCopyHack | ||
|
||
def task_name(dir, name) | ||
return name if name.to_s.start_with? "//" | ||
|
||
# Strip any leading ".", "./" or ".\\" | ||
# I am ashamed | ||
use = dir.gsub(/\\/, '/').sub(/^\./, '').sub(/^\//, '') | ||
|
||
"//#{use}:#{name}" | ||
end | ||
|
||
def output_name(dir, name, suffix) | ||
t = task_name(dir, name) | ||
result = "build/" + (t.slice(2 ... t.length)) + "." + suffix | ||
result.gsub(":", "/") | ||
end | ||
|
||
def add_dependencies(target, dir, all_deps) | ||
return if all_deps.nil? | ||
|
||
Array(all_deps).each do |dep| | ||
target.enhance(dep_type(dir, dep)) | ||
end | ||
end | ||
|
||
def copy_resources(dir, to_copy, out_dir) | ||
to_copy.each do |res| | ||
if res.is_a? Symbol | ||
out = Rake::Task[task_name(dir, res)].out | ||
elsif Rake::Task.task_defined?(res) | ||
task = Rake::Task[res] | ||
out = task.out | ||
while out.nil? && task.prerequisites.size == 1 do | ||
task = Rake::Task[task.prerequisites[0]] | ||
out = task.out | ||
end | ||
elsif res.is_a? Hash | ||
# Copy the key to "out_dir + value" | ||
res.each do |from, to| | ||
possible_task = task_name(dir, from) | ||
if Rake::Task.task_defined?(possible_task) and Rake::Task[possible_task].out | ||
target = Rake::Task[possible_task].out | ||
|
||
if File.directory? target | ||
dest = File.join(out_dir, to) | ||
mkdir_p dest | ||
cp_r target, dest | ||
else | ||
dest = File.join(out_dir, to) | ||
mkdir_p File.dirname(dest) | ||
cp_r target, dest, remove_destination: true | ||
end | ||
else | ||
tdir = to.gsub(/\/.*?$/, "") | ||
mkdir_p "#{out_dir}/#{tdir}" | ||
src = find_file(File.join(dir, from)) | ||
|
||
if File.directory? src | ||
mkdir_p "#{out_dir}/#{to}" | ||
else | ||
mkdir_p File.join(out_dir, File.dirname(to)) | ||
end | ||
cp_r src, "#{out_dir}/#{to}" | ||
end | ||
end | ||
|
||
next | ||
else | ||
if File.exists? res | ||
out = res | ||
else | ||
out = File.join(dir, res) | ||
return copy_all(dir, to_copy, out_dir) unless File.exists?(out) | ||
end | ||
end | ||
|
||
if out.is_a? Array | ||
out.each { |o| cp_r o, out_dir } | ||
else | ||
cp_r out, out_dir | ||
end | ||
end | ||
end | ||
|
||
def copy_to_prebuilt(out, fun) | ||
prebuilt = fun.find_prebuilt(out) | ||
puts "Copying #{out} to prebuilt #{prebuilt}" | ||
cp out, prebuilt | ||
end | ||
|
||
def copy_prebuilt(fun, out) | ||
src = fun.find_prebuilt(out) || raise("unable to find prebuilt for #{out.inspect}") | ||
|
||
mkdir_p File.dirname(out) | ||
puts "Falling back to #{src}" | ||
cp src, out | ||
end | ||
|
||
def copy_all(dir, srcs, dest) | ||
if srcs.is_a? Array | ||
copy_array(dir, srcs, dest) | ||
elsif srcs.is_a? String | ||
copy_string(dir, srcs, dest) | ||
elsif srcs.is_a? Hash | ||
copy_hash(dir, srcs, dest) | ||
elsif srcs.is_a? Symbol | ||
copy_symbol(dir, srcs, dest) | ||
else | ||
raise StandardError, "Undetermined type: #{srcs.class}" | ||
end | ||
end | ||
|
||
def zip(src, dest) | ||
out = SeleniumRake::Checks.path_for(File.expand_path(dest)) | ||
Dir.chdir(src) { | ||
# TODO(jari): something very weird going on here on windows | ||
# the 2>&1 is needed for some reason | ||
ok = system(%{jar cMf "#{out}" * 2>&1}) | ||
ok or raise "could not zip #{src} => #{dest}" | ||
} | ||
end | ||
|
||
def to_filelist(dir, src) | ||
str = dir + "/" + src | ||
FileList[str].collect do |file| | ||
SeleniumRake::Checks.path_for(file) | ||
end | ||
end | ||
|
||
private | ||
|
||
def copy_string(dir, src, dest) | ||
if Rake::Task.task_defined? src | ||
from = Rake::Task[src].out | ||
else | ||
from = to_filelist(dir, src) | ||
end | ||
|
||
cp_r from, to_dir(dest), :remove_destination => true | ||
end | ||
|
||
def copy_symbol(dir, src, dest) | ||
from = Rake::Task[task_name(dir, src)].out | ||
|
||
if File.directory? from | ||
cp_r from, to_dir(dest) | ||
else | ||
mkdir_p File.dirname(dest) | ||
cp from, dest | ||
end | ||
end | ||
|
||
def copy_array(dir, src, dest) | ||
src.each do |item| | ||
if item.is_a? Hash | ||
copy_hash(dir, item, dest) | ||
elsif item.is_a? Array | ||
# TODO: Is this correct here? Shouldn't we do +copy_array+ (Luke - Sep 2019') | ||
raise StandardError, "Undetermined type: #{item.class}" | ||
elsif item.is_a? String | ||
copy_string(dir, item, dest) | ||
elsif item.is_a? Symbol | ||
copy_symbol(dir, item, dest) | ||
else | ||
raise StandardError, "Undetermined type: #{item.class}" | ||
end | ||
end | ||
end | ||
|
||
def copy_hash(dir, src, dest) | ||
src.each do |key, value| | ||
if key.is_a? Symbol | ||
copy_symbol dir, key, SeleniumRake::Checks.path_for(File.join(dest, value)) | ||
else | ||
from, to = File.join(dir, key), File.join(dest, value) | ||
cp_r from, to | ||
end | ||
end | ||
|
||
end | ||
|
||
def to_dir(name) | ||
unless File.exists? name | ||
mkdir_p name | ||
end | ||
name | ||
end | ||
|
||
def dep_type(dir, dep) | ||
if dep.is_a? String | ||
if dep.start_with?("//") | ||
return [dep] | ||
else | ||
return to_filelist(dir, dep) | ||
end | ||
end | ||
|
||
if dep.is_a? Symbol | ||
return [task_name(dir, dep)] | ||
end | ||
|
||
if dep.is_a? Hash | ||
all_deps = [] | ||
dep.each do |k, v| | ||
# We only care about the keys | ||
all_deps += dep_type(dir, k) | ||
end | ||
return all_deps | ||
end | ||
|
||
raise "Unmatched dependency type: #{dep.class}" | ||
end | ||
|
||
def halt_on_error? | ||
[nil, 'true'].include?(ENV['haltonerror']) && | ||
[nil, 'true'].include?(ENV['haltonfailure']) | ||
end | ||
|
||
def halt_on_failure? | ||
halt_on_error? | ||
end | ||
end | ||
require_relative 'tasks' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,7 @@ | ||
require 'rake_tasks/crazy_fun/mappings/common' | ||
|
||
class ExportMappings | ||
def add_all(fun) | ||
fun.add_mapping("export_file", Export::CheckPreconditions.new) | ||
fun.add_mapping("export_file", Export::CreateTask.new) | ||
fun.add_mapping("export_file", Export::AddDependencies.new) | ||
end | ||
end | ||
|
||
module Export | ||
class CheckPreconditions < Tasks | ||
def handle(_fun, _dir, args) | ||
raise StandardError, ":name must be set" if args[:name].nil? | ||
raise StandardError, ":srcs must be set" if args[:srcs].nil? | ||
end | ||
end | ||
|
||
class ExportTasks < Tasks | ||
def export_name(dir, name, extension) | ||
name = task_name(dir, name) | ||
name = "build/" + (name.slice(2 ... name.length)) | ||
name = name.sub(":", "/") | ||
name += extension | ||
Platform.path_for name | ||
end | ||
end | ||
|
||
class CreateTask < ExportTasks | ||
def handle(_fun, dir, args) | ||
to_export = dir + "/" + args[:srcs][0] | ||
|
||
name = export_name(dir, args[:name], File.extname(to_export)) | ||
file name => to_export do | ||
src = Platform.path_for "#{dir}/#{args[:srcs][0]}" | ||
mkdir_p File.dirname(name) | ||
cp_r src, name | ||
end | ||
|
||
task_name = task_name(dir, args[:name]) | ||
task task_name => name | ||
task "#{task_name}#{File.extname(to_export)}" => task_name | ||
Rake::Task[task_name].out = name | ||
end | ||
end | ||
|
||
class AddDependencies < ExportTasks | ||
def handle(_fun, dir, args) | ||
to_export = dir + "/" + args[:srcs][0] | ||
name = export_name(dir, args[:name], File.extname(to_export)) | ||
task = Rake::Task[name] | ||
add_dependencies(task, dir, args[:deps]) | ||
add_dependencies(task, dir, args[:srcs]) | ||
end | ||
end | ||
end | ||
require_relative 'export_mappings' | ||
require_relative 'export/check_preconditions' | ||
require_relative 'export/export_tasks' | ||
require_relative 'export/create_task' | ||
require_relative 'export/add_dependencies' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Export | ||
class AddDependencies < ExportTasks | ||
def handle(_fun, dir, args) | ||
to_export = dir + "/" + args[:srcs][0] | ||
name = export_name(dir, args[:name], File.extname(to_export)) | ||
task = Rake::Task[name] | ||
add_dependencies(task, dir, args[:deps]) | ||
add_dependencies(task, dir, args[:srcs]) | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module Export | ||
class CheckPreconditions < Tasks | ||
def handle(_fun, _dir, args) | ||
raise StandardError, ":name must be set" if args[:name].nil? | ||
raise StandardError, ":srcs must be set" if args[:srcs].nil? | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Export | ||
class CreateTask < ExportTasks | ||
def handle(_fun, dir, args) | ||
to_export = dir + "/" + args[:srcs][0] | ||
|
||
name = export_name(dir, args[:name], File.extname(to_export)) | ||
file name => to_export do | ||
src = Platform.path_for "#{dir}/#{args[:srcs][0]}" | ||
mkdir_p File.dirname(name) | ||
cp_r src, name | ||
end | ||
|
||
task_name = task_name(dir, args[:name]) | ||
task task_name => name | ||
task "#{task_name}#{File.extname(to_export)}" => task_name | ||
Rake::Task[task_name].out = name | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Export | ||
class ExportTasks < Tasks | ||
def export_name(dir, name, extension) | ||
name = task_name(dir, name) | ||
name = "build/" + (name.slice(2 ... name.length)) | ||
name = name.sub(":", "/") | ||
name += extension | ||
Platform.path_for name | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class ExportMappings | ||
def add_all(fun) | ||
fun.add_mapping("export_file", Export::CheckPreconditions.new) | ||
fun.add_mapping("export_file", Export::CreateTask.new) | ||
fun.add_mapping("export_file", Export::AddDependencies.new) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module FileCopyHack | ||
def cp_r(src, dest, opts = {}) | ||
super | ||
rescue => ex | ||
raise unless ex.message =~ /operation not permitted|Permission denied/i | ||
Dir["#{dest}/**/.svn"].each { |file| rm_rf file } | ||
|
||
# virtual box shared folders has a problem with some of the .svn files | ||
if ENV['USER'] == "vagrant" && opts.empty? | ||
sh "cp", "-r", src, dest | ||
else | ||
super(src, dest, opts) | ||
end | ||
end | ||
end |
Oops, something went wrong.