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

Ruby 3.x support #140

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
58 changes: 31 additions & 27 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
require 'rubygems'
require 'bundler'
require 'rspec/core/rake_task'
require File.expand_path('../lib/rautomation/adapter/helper', __FILE__)

Bundler::GemHelper.install_tasks

def ext_dependencies(name)
FileList["ext/#{name}/**/*"].reject { |file| file =~ /(Release|Debug)/ }
end

def ms_build(name)
name = File.basename(name, File.extname(name))
cmd = "msbuild /p:Configuration=Release ext\\#{name}\\#{name}.sln"
cmd += " && #{cmd} /p:Platform=x64" unless name == 'WindowsForms'
sh(cmd)
end

namespace :build do
build_tasks = [
namespace :compile do
compile_tasks = [
{:name => :uia_dll, :path => "UiaDll", :ext => "dll"},
{:name => :i_accessible_dll, :path => "IAccessibleDLL", :ext => "dll"},
{:name => :windows_forms, :path => "WindowsForms", :ext => "exe"}
]

build_tasks.each do |build_task|
full_ext_path = "ext/#{build_task[:path]}/Release/#{build_task[:path]}.#{build_task[:ext]}"

%w[x86Release x64Release].each do |output_dir|
full_ext_path = full_ext_path.gsub(/(?<!x86|x64)Release/, output_dir) unless build_task[:name] == :windows_forms

file full_ext_path => ext_dependencies(build_task[:path]) do |t|
ms_build(t.name)
compile_tasks.each do |compile_task|
desc "Compile #{compile_task[:path]}"
task compile_task[:name] do
full_ext_path = "ext/#{compile_task[:path]}/Release/#{compile_task[:path]}.#{compile_task[:ext]}"
%w[x86Release x64Release].each do |output_dir|
full_ext_path = full_ext_path.gsub(/(?<!x86|x64)Release/, output_dir) unless compile_task[:name] == :windows_forms
RAutomation::Adapter::Helper.build_solution(full_ext_path)
end
end
end

desc "Compile all external dependencies"
task :all => compile_tasks.map { |t| "compile:#{t[:name]}"}
end

desc "Build #{build_task[:path]}"
task build_task[:name] => full_ext_path
task :compile => "compile:all"

namespace :build do
platforms = %w[x86-mingw32 x64-mingw32 x86-mingw-ucrt x64-mingw-ucrt]
platforms.each do |platform|
desc "Build gem for platform: #{platform}"
task platform => "compile:all" do
RAutomation::Adapter::Helper.move_adapter_dlls(platform)
sh "gem build --platform #{platform}"
mkdir_p 'pkg' unless Dir.exist?('pkg')
mv Dir.glob("rautomation-*-#{platform}.gem"), 'pkg'
end
end

desc "Build all external dependencies"
task :all => build_tasks.map { |t| "build:#{t[:name]}"}
desc "Build gem for all platforms"
task :all => platforms.map(&:to_sym)
end

task :build => "build:all"
task :build => 'build:all'

namespace :spec do
adapters = %w[win_32]
adapters << "ms_uia" if Platform.is_x86?
adapters << "ms_uia" if %w[x86 i386].any? { |p| RUBY_PLATFORM =~ /#{p}/i }

adapters.each do |adapter|
desc "Run RSpec code examples against #{adapter} adapter"
Expand Down
22 changes: 14 additions & 8 deletions lib/rautomation/adapter/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ module Helper
require File.expand_path('../../platform', __FILE__)
extend self

ADAPTER_DIRS = [
"ext/IAccessibleDLL/Release/IAccessibleDLL.dll",
"ext/UiaDll/Release/UiaDll.dll",
"ext/UiaDll/Release/RAutomation.UIA.dll"
]

# @private
# Retrieves default {Adapter} for the current platform.
def default_adapter
Expand All @@ -23,15 +29,17 @@ def supported_for_current_platform?(adapter)
Platform.is_x86? || adapter == :win_32
end

def find_missing_externals(externals)
externals.select do |ext|
def find_missing_externals
ADAPTER_DIRS.select do |ext|
path = "#{Dir.pwd}/#{File.dirname(ext)}"
file = File.basename(ext)
!Dir.exist?(path) && !File.exist?("#{path}/#{file}")
full_path = "#{path}/#{file}"
!Dir.exist?(path) || !File.exist?(full_path)
end
end

def build_solution(ext)
return if File.exist?(ext)
return if ext =~ /RAutomation.UIA.dll/ # skip this since its built in UiaDll.sln

name = File.basename(ext, File.extname(ext))
Expand All @@ -45,19 +53,17 @@ def msbuild_solution(name)
"Make sure msbuild binary is in your PATH and the project is configured correctly"
end

def move_adapter_dlls(externals, architecture)
def move_adapter_dlls(ruby_platform)
architecture = ruby_platform.split("-").first
raise ArgumentError, "Invalid platform #{architecture}" unless %w[x86 x64].any? { |arch| arch == architecture }
puts "Moving #{architecture} dll's into 'Release' folder.."

externals.each do |dest_path|
next if dest_path =~ /WindowsForms/
ADAPTER_DIRS.each do |dest_path|
dll_path = dest_path.gsub('Release', "#{architecture}Release")
dest_dir = File.dirname(dest_path)
FileUtils.mkdir_p(dest_dir) unless Dir.exist?(dest_dir)
FileUtils.cp(dll_path, dest_path)
end

externals
end
end
end
Expand Down
26 changes: 10 additions & 16 deletions rautomation.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,20 @@ RAutomation provides:
s.license = "MIT"
s.platform = Gem::Platform.local if s.platform == 'ruby'

ext_locations = [
"ext/IAccessibleDLL/Release/IAccessibleDLL.dll",
"ext/UiaDll/Release/UiaDll.dll",
"ext/UiaDll/Release/RAutomation.UIA.dll"
]
missing_externals = RAutomation::Adapter::Helper.find_missing_externals
if missing_externals.any?
missing_externals.each { |ext_location | puts "Missing external: #{ext_location}" }
raise Gem::InstallError,
"One or more required DLL files are missing. See Rake task 'compile' in order to build."
end

ext_locations = RAutomation::Adapter::Helper::ADAPTER_DIRS
winforms_files = Dir[
"ext/WindowsForms/Release/*.dll",
"ext/WindowsForms/Release/*.exe"
"ext/WindowsForms/Release/*.dll",
"ext/WindowsForms/Release/*.exe"
]

RAutomation::Adapter::Helper.find_missing_externals(ext_locations).each do |ext|
RAutomation::Adapter::Helper.build_solution(ext)
end

# move .dll files and get array containing paths
# send first three externals and first three characters from platform eg 'x86' from 'x86-mingw32'
externals = RAutomation::Adapter::Helper.move_adapter_dlls(ext_locations[0, 3], s.platform.to_s[0, 3])

s.files = `git ls-files`.split("\n") + externals + winforms_files
s.files = `git ls-files`.split("\n") + ext_locations + winforms_files
s.test_files = `git ls-files -- spec/*`.split("\n")
s.require_paths = ["lib"]

Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def navigate_to_simple_elements
# Path to some binary, which opens up a window, what can be
# minimized, maximized, activated, closed and etc.
window1: "ext\\WindowsForms\\Release\\WindowsForms.exe",
window2: "notepad",
window2_title: /notepad/i,
window2: "regedit",
window2_title: /registry editor/i,
# Window 1 title, has to be a Regexp.
window1_title: /FormWindow/i,
window1_full_title: 'MainFormWindow',
Expand Down