Skip to content

Commit

Permalink
Merge pull request #574 from andyundso/standard-rb
Browse files Browse the repository at this point in the history
Use `standardrb` for code formatting
  • Loading branch information
andyundso authored Feb 4, 2025
2 parents fbd2797 + 9bcc011 commit cba28e3
Show file tree
Hide file tree
Showing 27 changed files with 815 additions and 1,168 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,18 @@ jobs:
shell: bash
run: |
ruby -e "require 'tiny_tds'; puts TinyTds::Gem.root_path"
standardrb:
name: standardrb
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4

- uses: ruby/setup-ruby@v1
with:
ruby-version: "2.7"
bundler-cache: true

- name: Check standardrb
shell: bash
run: bundle exec standardrb
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
source 'https://rubygems.org'
source "https://rubygems.org"
gemspec
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,10 @@ $ rake TINYTDS_UNIT_DATASERVER=mydbserver TINYTDS_SCHEMA=sqlserver_2017
$ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_SCHEMA=sqlserver_azure
```

### Code formatting

We are using `standardrb` to format our code. Just run `bundle exec standardrb --fix` to format all Ruby files.

### Compiling Gems for Windows and Linux

> [!WARNING]
Expand Down
55 changes: 27 additions & 28 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
# encoding: UTF-8
require 'rbconfig'
require 'rake'
require 'rake/clean'
require 'rake/extensiontask'
require "rbconfig"
require "rake"
require "rake/clean"
require "rake/extensiontask"

SPEC = Gem::Specification.load(File.expand_path('../tiny_tds.gemspec', __FILE__))
SPEC = Gem::Specification.load(File.expand_path("../tiny_tds.gemspec", __FILE__))

CrossLibrary = Struct.new :platform, :openssl_config
CrossLibraries = [
['x64-mingw-ucrt', 'mingw64'],
['x64-mingw32', 'mingw64'],
['x86_64-linux-gnu', 'linux-x86_64'],
['x86_64-linux-musl', 'linux-x86_64'],
['aarch64-linux-gnu', 'linux-aarch64'],
['aarch64-linux-musl', 'linux-aarch64'],
["x64-mingw-ucrt", "mingw64"],
["x64-mingw32", "mingw64"],
["x86_64-linux-gnu", "linux-x86_64"],
["x86_64-linux-musl", "linux-x86_64"],
["aarch64-linux-gnu", "linux-aarch64"],
["aarch64-linux-musl", "linux-aarch64"]
].map do |platform, openssl_config|
CrossLibrary.new platform, openssl_config
end

# Add our project specific files to clean for a rebuild
CLEAN.include FileList["{ext,lib}/**/*.{so,#{RbConfig::CONFIG['DLEXT']},o}"],
FileList["exe/*"]
CLEAN.include FileList["{ext,lib}/**/*.{so,#{RbConfig::CONFIG["DLEXT"]},o}"],
FileList["exe/*"]

# Clobber all our temp files and ports files including .install files
# and archives
CLOBBER.include FileList["tmp/**/*"],
FileList["ports/**/*"].exclude(%r{^ports/archives})
FileList["ports/**/*"].exclude(%r{^ports/archives})

Dir['tasks/*.rake'].sort.each { |f| load f }
Dir["tasks/*.rake"].sort.each { |f| load f }

Rake::ExtensionTask.new('tiny_tds', SPEC) do |ext|
ext.lib_dir = 'lib/tiny_tds'
Rake::ExtensionTask.new("tiny_tds", SPEC) do |ext|
ext.lib_dir = "lib/tiny_tds"
ext.cross_compile = true
ext.cross_platform = CrossLibraries.map(&:platform)

# Add dependent DLLs to the cross gems
ext.cross_compiling do |spec|
# The fat binary gem doesn't depend on the freetds package, since it bundles the library.
spec.metadata.delete('msys2_mingw_dependencies')
if spec.platform.to_s =~ /mingw/
spec.metadata.delete("msys2_mingw_dependencies")

if /mingw/.match?(spec.platform.to_s)
spec.files += [
"ports/#{spec.platform.to_s}/bin/libsybdb-5.dll",
"ports/#{spec.platform.to_s}/bin/defncopy.exe",
"ports/#{spec.platform.to_s}/bin/tsql.exe"
"ports/#{spec.platform}/bin/libsybdb-5.dll",
"ports/#{spec.platform}/bin/defncopy.exe",
"ports/#{spec.platform}/bin/tsql.exe"
]
elsif spec.platform.to_s =~ /linux/
elsif /linux/.match?(spec.platform.to_s)
spec.files += [
"ports/#{spec.platform.to_s}/lib/libsybdb.so.5",
"ports/#{spec.platform.to_s}/bin/defncopy",
"ports/#{spec.platform.to_s}/bin/tsql"
"ports/#{spec.platform}/lib/libsybdb.so.5",
"ports/#{spec.platform}/bin/defncopy",
"ports/#{spec.platform}/bin/tsql"
]
end
end
Expand Down
92 changes: 46 additions & 46 deletions ext/tiny_tds/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
require 'mkmf'
require_relative 'extconsts'

if ENV['MAINTAINER_MODE']
$stderr.puts "Maintainer mode enabled."
$CFLAGS <<
' -Wall' <<
' -ggdb' <<
' -DDEBUG' <<
' -pedantic'
$LDFLAGS <<
' -ggdb'
require "mkmf"
require_relative "extconsts"

if ENV["MAINTAINER_MODE"]
warn "Maintainer mode enabled."
$CFLAGS << # standard:disable Style/GlobalVars
" -Wall" \
" -ggdb" \
" -DDEBUG" \
" -pedantic"
$LDFLAGS << # standard:disable Style/GlobalVars
" -ggdb"
end

if gem_platform=with_config("cross-build")
require 'mini_portile2'
if (gem_platform = with_config("cross-build"))
require "mini_portile2"

openssl_platform = with_config("openssl-platform")

Expand All @@ -23,7 +23,7 @@ class BuildRecipe < MiniPortile
def initialize(name, version, files)
super(name, version)
self.files = files
rootdir = File.expand_path('../../..', __FILE__)
rootdir = File.expand_path("../../..", __FILE__)
self.target = File.join(rootdir, "ports")
self.patch_files = Dir[File.join("patches", self.name, self.version, "*.patch")].sort
end
Expand All @@ -34,35 +34,35 @@ def port_path
end

def cook_and_activate
checkpoint = File.join(self.target, "#{self.name}-#{self.version}-#{gem_platform}.installed")
checkpoint = File.join(target, "#{name}-#{version}-#{gem_platform}.installed")

unless File.exist?(checkpoint)
self.cook
FileUtils.touch checkpoint
end
self.activate
cook
FileUtils.touch checkpoint
end

activate
self
end
end

openssl_recipe = BuildRecipe.new("openssl", OPENSSL_VERSION, [OPENSSL_SOURCE_URI]).tap do |recipe|
class << recipe
class << recipe
attr_accessor :openssl_platform

def configure
envs = []
envs << "CFLAGS=-DDSO_WIN32 -DOPENSSL_THREADS" if MiniPortile.windows?
envs << "CFLAGS=-fPIC -DOPENSSL_THREADS" if MiniPortile.linux?
execute('configure', ['env', *envs, "./Configure", openssl_platform, "threads", "-static", "CROSS_COMPILE=#{host}-", configure_prefix, "--libdir=lib"], altlog: "config.log")
execute("configure", ["env", *envs, "./Configure", openssl_platform, "threads", "-static", "CROSS_COMPILE=#{host}-", configure_prefix, "--libdir=lib"], altlog: "config.log")
end

def compile
execute('compile', "#{make_cmd} build_libs")
execute("compile", "#{make_cmd} build_libs")
end

def install
execute('install', "#{make_cmd} install_dev")
execute("install", "#{make_cmd} install_dev")
end
end

Expand Down Expand Up @@ -122,30 +122,30 @@ def configure_defaults
end

# enable relative path to later load the FreeTDS shared library
$LDFLAGS << " '-Wl,-rpath=$$ORIGIN/../../../ports/#{gem_platform}/lib'"
$LDFLAGS << " '-Wl,-rpath=$$ORIGIN/../../../ports/#{gem_platform}/lib'" # standard:disable Style/GlobalVars

dir_config('freetds', "#{freetds_recipe.path}/include", "#{freetds_recipe.path}/lib")
dir_config("freetds", "#{freetds_recipe.path}/include", "#{freetds_recipe.path}/lib")
else
# Make sure to check the ports path for the configured host
architecture = RbConfig::CONFIG['arch']
architecture = RbConfig::CONFIG["arch"]

project_dir = File.expand_path("../../..", __FILE__)
freetds_ports_dir = File.join(project_dir, 'ports', architecture, 'freetds', FREETDS_VERSION)
freetds_ports_dir = File.join(project_dir, "ports", architecture, "freetds", FREETDS_VERSION)
freetds_ports_dir = File.expand_path(freetds_ports_dir)

# Add all the special path searching from the original tiny_tds build
# order is important here! First in, first searched.
DIRS = %w(
DIRS = %w[
/opt/local
/usr/local
)
]

if RbConfig::CONFIG['host_os'] =~ /darwin/i
if /darwin/i.match?(RbConfig::CONFIG["host_os"])
# Ruby below 2.7 seems to label the host CPU on Apple Silicon as aarch64
# 2.7 and above print is as ARM64
target_host_cpu = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7') ? 'aarch64' : 'arm64'
target_host_cpu = (Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.7")) ? "aarch64" : "arm64"

if RbConfig::CONFIG['host_cpu'] == target_host_cpu
if RbConfig::CONFIG["host_cpu"] == target_host_cpu
# Homebrew on Apple Silicon installs into /opt/hombrew
# https://docs.brew.sh/Installation
# On Intel Macs, it is /usr/local, so no changes necessary to DIRS
Expand All @@ -162,7 +162,7 @@ def configure_defaults

# Grab freetds environment variable for use by people on services like
# Heroku who they can't easily use bundler config to set directories
DIRS.unshift(ENV['FREETDS_DIR']) if ENV.has_key?('FREETDS_DIR')
DIRS.unshift(ENV["FREETDS_DIR"]) if ENV.has_key?("FREETDS_DIR")

# Add the search paths for freetds configured above
ldirs = DIRS.flat_map do |path|
Expand All @@ -175,15 +175,15 @@ def configure_defaults
[idir, "#{idir}/freetds"]
end

puts "looking for freetds headers in the following directories:\n#{idirs.map{|a| " - #{a}\n"}.join}"
puts "looking for freetds library in the following directories:\n#{ldirs.map{|a| " - #{a}\n"}.join}"
dir_config('freetds', idirs, ldirs)
puts "looking for freetds headers in the following directories:\n#{idirs.map { |a| " - #{a}\n" }.join}"
puts "looking for freetds library in the following directories:\n#{ldirs.map { |a| " - #{a}\n" }.join}"
dir_config("freetds", idirs, ldirs)
end

find_header('sybfront.h') or abort "Can't find the 'sybfront.h' header"
find_header('sybdb.h') or abort "Can't find the 'sybdb.h' header"
find_header("sybfront.h") or abort "Can't find the 'sybfront.h' header"
find_header("sybdb.h") or abort "Can't find the 'sybdb.h' header"

unless have_library('sybdb', 'dbanydatecrack')
unless have_library("sybdb", "dbanydatecrack")
abort "Failed! Do you have FreeTDS 1.0.0 or higher installed?"
end

Expand Down
7 changes: 3 additions & 4 deletions ext/tiny_tds/extconsts.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

ICONV_VERSION = ENV['TINYTDS_ICONV_VERSION'] || "1.17"
ICONV_VERSION = ENV["TINYTDS_ICONV_VERSION"] || "1.17"
ICONV_SOURCE_URI = "http://ftp.gnu.org/pub/gnu/libiconv/libiconv-#{ICONV_VERSION}.tar.gz"

OPENSSL_VERSION = ENV['TINYTDS_OPENSSL_VERSION'] || '3.4.0'
OPENSSL_VERSION = ENV["TINYTDS_OPENSSL_VERSION"] || "3.4.0"
OPENSSL_SOURCE_URI = "https://www.openssl.org/source/openssl-#{OPENSSL_VERSION}.tar.gz"

FREETDS_VERSION = ENV['TINYTDS_FREETDS_VERSION'] || '1.4.23'
FREETDS_VERSION = ENV["TINYTDS_FREETDS_VERSION"] || "1.4.23"
FREETDS_SOURCE_URI = "http://www.freetds.org/files/stable/freetds-#{FREETDS_VERSION}.tar.bz2"
41 changes: 18 additions & 23 deletions lib/tiny_tds.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
# encoding: UTF-8
require 'date'
require 'bigdecimal'
require 'rational'
require "date"
require "bigdecimal"

require 'tiny_tds/version'
require 'tiny_tds/error'
require 'tiny_tds/client'
require 'tiny_tds/result'
require 'tiny_tds/gem'
require "tiny_tds/version"
require "tiny_tds/error"
require "tiny_tds/client"
require "tiny_tds/result"
require "tiny_tds/gem"

module TinyTds

# Is this file part of a fat binary gem with bundled freetds?
# This path must be enabled by add_dll_directory on Windows.
gplat = ::Gem::Platform.local
FREETDS_LIB_PATH = Dir[File.expand_path("../ports/#{gplat.cpu}-#{gplat.os}*/lib", __dir__)].first

add_dll_path = proc do |path, &block|
if RUBY_PLATFORM =~/(mswin|mingw)/i && path
if RUBY_PLATFORM =~ /(mswin|mingw)/i && path
begin
require 'ruby_installer/runtime'
require "ruby_installer/runtime"
RubyInstaller::Runtime.add_dll_directory(path, &block)
rescue LoadError
old_path = ENV['PATH']
ENV['PATH'] = "#{path};#{old_path}"
old_path = ENV["PATH"]
ENV["PATH"] = "#{path};#{old_path}"
block.call
ENV['PATH'] = old_path
ENV["PATH"] = old_path
end
else
# libsybdb is found by a relative rpath in the cross compiled extension dll
Expand All @@ -35,13 +32,11 @@ module TinyTds
end

add_dll_path.call(FREETDS_LIB_PATH) do
begin
# Try the <major>.<minor> subdirectory for fat binary gems
major_minor = RUBY_VERSION[ /^(\d+\.\d+)/ ] or
raise "Oops, can't extract the major/minor version from #{RUBY_VERSION.dump}"
require "tiny_tds/#{major_minor}/tiny_tds"
rescue LoadError
require 'tiny_tds/tiny_tds'
end
# Try the <major>.<minor> subdirectory for fat binary gems
major_minor = RUBY_VERSION[/^(\d+\.\d+)/] or
raise "Oops, can't extract the major/minor version from #{RUBY_VERSION.dump}"
require "tiny_tds/#{major_minor}/tiny_tds"
rescue LoadError
require "tiny_tds/tiny_tds"
end
end
Loading

0 comments on commit cba28e3

Please sign in to comment.