Skip to content

Commit 316fcf6

Browse files
committed
chore(ruby): fix rubocop errors and add new workflow for linting checks
1 parent a8eb073 commit 316fcf6

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

spannerlib/wrappers/spannerlib-ruby/Rakefile

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ LIB_DIR = File.expand_path("lib/spannerlib", __dir__)
3636
# The 'ext' is the file extension for the shared library.
3737
PLATFORMS = {
3838
"aarch64-darwin" => { goos: "darwin", goarch: "arm64", ext: "dylib" },
39-
"x86_64-darwin" => { goos: "darwin", goarch: "amd64", ext: "dylib" },
40-
"aarch64-linux" => { goos: "linux", goarch: "arm64", ext: "so" },
41-
"x86_64-linux" => { goos: "linux", goarch: "amd64", ext: "so" },
42-
"x64-mingw32" => { goos: "windows", goarch: "amd64", ext: "dll" } # For Windows
39+
"x86_64-darwin" => { goos: "darwin", goarch: "amd64", ext: "dylib" },
40+
"aarch64-linux" => { goos: "linux", goarch: "arm64", ext: "so" },
41+
"x86_64-linux" => { goos: "linux", goarch: "amd64", ext: "so" },
42+
"x64-mingw32" => { goos: "windows", goarch: "amd64", ext: "dll" } # For Windows
4343
}.freeze
4444

4545
# --- Rake Tasks for Compilation ---
4646

47+
# rubocop:disable Metrics/BlockLength
4748
# Create a 'compile' namespace for all build tasks
4849
namespace :compile do
4950
desc "Remove all compiled native libraries"
@@ -54,8 +55,10 @@ namespace :compile do
5455
rm_rf target_dir
5556
end
5657
end
58+
# rubocop:enable Metrics/BlockLength
5759

5860
# Dynamically create a build task for each platform
61+
# rubocop:disable Metrics/BlockLength
5962
PLATFORMS.each do |arch, config|
6063
desc "Compile native library for #{arch}"
6164
task arch do
@@ -93,7 +96,7 @@ namespace :compile do
9396
env["RANLIB"] = ENV.fetch("RANLIB", "x86_64-w64-mingw32-ranlib")
9497
end
9598
when "darwin"
96-
# Note: cross-compiling darwin/arm64 on an x86 mac runner is NOT reliably supported
99+
# NOTE: cross-compiling darwin/arm64 on an x86 mac runner is NOT reliably supported
97100
# unless you use osxcross or an ARM mac runner. We try nothing here; rely on runner's clang.
98101
env["CC"] ||= ENV.fetch("CC", "clang")
99102
end
@@ -114,6 +117,7 @@ namespace :compile do
114117
puts "Successfully built #{output_file}"
115118
end
116119
end
120+
# rubocop:enable Metrics/BlockLength
117121

118122
desc "Compile native libraries for all platforms"
119123
task all: PLATFORMS.keys

spannerlib/wrappers/spannerlib-ruby/lib/spannerlib/ffi.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
module SpannerLib
2929
extend FFI::Library
3030

31-
ENV_OVERRIDE = ENV["SPANNERLIB_PATH"]
31+
ENV_OVERRIDE = ENV.fetch("SPANNERLIB_PATH", nil)
3232

3333
def self.platform_dir_from_host
3434
host_os = RbConfig::CONFIG["host_os"]
@@ -41,15 +41,15 @@ def self.platform_dir_from_host
4141
host_cpu =~ /arm|aarch64/ ? "aarch64-linux" : "x86_64-linux"
4242
when /mswin|mingw|cygwin/
4343
"x64-mingw32"
44-
else
45-
nil
4644
end
4745
end
4846

4947
# Build list of candidate paths (ordered): env override, platform-specific, any packaged lib, system library
48+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
5049
def self.library_path
5150
if ENV_OVERRIDE && !ENV_OVERRIDE.empty?
5251
return ENV_OVERRIDE if File.file?(ENV_OVERRIDE)
52+
5353
warn "SPANNERLIB_PATH set to #{ENV_OVERRIDE} but file not found"
5454
end
5555

@@ -71,7 +71,9 @@ def self.library_path
7171
# Attempt to open system lib name; if succeeds, return bare name so ffi_lib can resolve it
7272
FFI::DynamicLibrary.open("spannerlib", FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_GLOBAL)
7373
return "spannerlib"
74-
rescue StandardError
74+
rescue LoadError
75+
# This is intentional. If the system library fails to load,
76+
# we'll proceed to the final LoadError with all search paths.
7577
end
7678

7779
searched = []
@@ -86,6 +88,7 @@ def self.library_path
8688
You can set SPANNERLIB_PATH to the absolute path of the library file, or install a platform-specific native gem.
8789
ERR
8890
end
91+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
8992

9093
ffi_lib library_path
9194

spannerlib/wrappers/spannerlib-ruby/spannerlib-ruby.gemspec

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ Gem::Specification.new do |spec|
2929
spec.files = Dir.chdir(File.expand_path(__dir__)) do
3030
files = []
3131
# prefer git-tracked files when available (local dev), but also pick up built files present on disk (CI)
32-
if system('git rev-parse --is-inside-work-tree > /dev/null 2>&1')
33-
files += `git ls-files -z`.split("\x0")
34-
end
32+
files += `git ls-files -z`.split("\x0") if system("git rev-parse --is-inside-work-tree > /dev/null 2>&1")
3533

3634
# include any built native libs (CI places them under lib/spannerlib/)
37-
files += Dir.glob('lib/spannerlib/**/*').select { |f| File.file?(f) }
35+
files += Dir.glob("lib/spannerlib/**/*").select { |f| File.file?(f) }
3836

3937
# dedupe and reject unwanted entries
40-
files.map! { |f| f.sub(%r{\A\./}, '') }.uniq!
38+
files.map! { |f| f.sub(%r{\A\./}, "") }.uniq!
4139
files.reject do |f|
4240
f.match(%r{^(pkg|Gemfile\.lock|.*\.gem|Rakefile|spec/|.*\.o|.*\.h)$})
4341
end

0 commit comments

Comments
 (0)