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

Use standardrb for code formatting #574

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
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,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 @@ -446,6 +446,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.

## Docker Builds

If you use a [multi stage](https://docs.docker.com/develop/develop-images/multistage-build/) Docker build to assemble your gems in one phase and then copy your app and gems
Expand Down
43 changes: 21 additions & 22 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
# encoding: UTF-8
require 'rbconfig'
require 'rake'
require 'rake/clean'
require 'rake/extensiontask'
require_relative './ext/tiny_tds/extconsts'
require "rbconfig"
require "rake"
require "rake/clean"
require "rake/extensiontask"
require_relative "ext/tiny_tds/extconsts"

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

ruby_cc_ucrt_versions = "3.4.0:3.3.5:3.2.0:3.1.0".freeze
ruby_cc_mingw32_versions = "3.0.0:2.7.0".freeze

GEM_PLATFORM_HOSTS = {
'x64-mingw32' => {
host: 'x86_64-w64-mingw32',
"x64-mingw32" => {
host: "x86_64-w64-mingw32",
ruby_versions: ruby_cc_mingw32_versions
},
'x64-mingw-ucrt' => {
host: 'x86_64-w64-mingw32',
"x64-mingw-ucrt" => {
host: "x86_64-w64-mingw32",
ruby_versions: ruby_cc_ucrt_versions
},
}
}

# 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 = GEM_PLATFORM_HOSTS.keys

# 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')
spec.metadata.delete("msys2_mingw_dependencies")

# We don't need the sources in a fat binary gem
spec.files = spec.files.reject { |f| f =~ %r{^ports\/archives/} }
spec.files = spec.files.reject { |f| f =~ %r{^ports/archives/} }

# Make sure to include the ports binaries and libraries
spec.files += FileList["ports/#{spec.platform.to_s}/**/**/{bin,lib}/*"].exclude do |f|
spec.files += FileList["ports/#{spec.platform}/**/**/{bin,lib}/*"].exclude do |f|
File.directory? f
end

spec.files += Dir.glob('exe/*')
spec.files += Dir.glob("exe/*")
end
end

Expand Down
54 changes: 27 additions & 27 deletions ext/tiny_tds/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
ENV['RC_ARCHS'] = '' if RUBY_PLATFORM =~ /darwin/
ENV["RC_ARCHS"] = "" if RUBY_PLATFORM.match?(/darwin/)

# :stopdoc:

require 'mkmf'
require 'rbconfig'
require_relative './extconsts'
require "mkmf"
require "rbconfig"
require_relative "extconsts"

# Shamelessly copied from nokogiri
#

def do_help
print <<HELP
usage: ruby #{$0} [options]
--with-freetds-dir=DIR
Use the freetds library placed under DIR.
HELP
print <<~HELP
usage: ruby #{$0} [options]
--with-freetds-dir=DIR
Use the freetds library placed under DIR.
HELP
exit! 0
end

do_help if arg_config('--help')
do_help if arg_config("--help")

# 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 @@ -56,7 +56,7 @@ def do_help

# 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 @@ -69,23 +69,23 @@ def do_help
[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)

have_dependencies = [
find_header('sybfront.h'),
find_header('sybdb.h'),
find_library('sybdb', 'tdsdbopen'),
find_library('sybdb', 'dbanydatecrack')
find_header("sybfront.h"),
find_header("sybdb.h"),
find_library("sybdb", "tdsdbopen"),
find_library("sybdb", "dbanydatecrack")
].inject(true) do |memo, current|
memo && current
end

unless have_dependencies
abort 'Failed! Do you have FreeTDS 1.0.0 or higher installed?'
abort "Failed! Do you have FreeTDS 1.0.0 or higher installed?"
end

create_makefile('tiny_tds/tiny_tds')
create_makefile("tiny_tds/tiny_tds")

# :startdoc:
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"
52 changes: 23 additions & 29 deletions lib/tiny_tds.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
# 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"

# Support multiple ruby versions, fat binaries under Windows.
if RUBY_PLATFORM =~ /mingw|mswin/ && RUBY_VERSION =~ /(\d+.\d+)/
ver = Regexp.last_match(1)

add_dll_path = proc do |path, &block|
require "ruby_installer/runtime"
RubyInstaller::Runtime.add_dll_directory(path, &block)
rescue LoadError
old_path = ENV["PATH"]
ENV["PATH"] = "#{path};#{old_path}"
begin
require 'ruby_installer/runtime'
RubyInstaller::Runtime.add_dll_directory(path, &block)
rescue LoadError
old_path = ENV['PATH']
ENV['PATH'] = "#{path};#{old_path}"
begin
block.call
ensure
ENV['PATH'] = old_path
end
block.call
ensure
ENV["PATH"] = old_path
end
end

add_dll_paths = proc do |paths, &block|
if path=paths.shift
if (path = paths.shift)
add_dll_path.call(path) do
add_dll_paths.call(paths, &block)
end
Expand All @@ -39,23 +35,21 @@
end

# Temporary add bin directories for DLL search, so that freetds DLLs can be found.
add_dll_paths.call( TinyTds::Gem.ports_bin_paths ) do
begin
require "tiny_tds/#{ver}/tiny_tds"
rescue LoadError
require 'tiny_tds/tiny_tds'
end
add_dll_paths.call(TinyTds::Gem.ports_bin_paths) do
require "tiny_tds/#{ver}/tiny_tds"
rescue LoadError
require "tiny_tds/tiny_tds"
end
else
# Load dependent shared libraries into the process, so that they are already present,
# when tiny_tds.so is loaded. This ensures, that shared libraries are loaded even when
# the path is different between build and run time (e.g. Heroku).
ports_libs = File.join(TinyTds::Gem.ports_root_path,
"#{RbConfig::CONFIG['host']}/lib/*.so")
"#{RbConfig::CONFIG["host"]}/lib/*.so")
Dir[ports_libs].each do |lib|
require 'fiddle'
require "fiddle"
Fiddle.dlopen(lib)
end

require 'tiny_tds/tiny_tds'
require "tiny_tds/tiny_tds"
end
29 changes: 14 additions & 15 deletions lib/tiny_tds/bin.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
require_relative './version'
require_relative './gem'
require 'shellwords'
require_relative "version"
require_relative "gem"
require "shellwords"

module TinyTds
class Bin

attr_reader :name

class << self
def exe(name, *args)
bin = new(name)
puts bin.info unless args.any? { |x| x == '-q' }
puts bin.info unless args.any? { |x| x == "-q" }
bin.run(*args)
end
end

def initialize(name)
@root = Gem.root_path
@exts = (ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']) | ['.exe']
@exts = (ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""]) | [".exe"]

@name = name
@binstub = find_bin
Expand All @@ -33,7 +32,7 @@ def run(*args)
end

def path
@path ||= @exefile && File.exist?(@exefile) ? @exefile : which
@path ||= (@exefile && File.exist?(@exefile)) ? @exefile : which
end

def info
Expand All @@ -43,26 +42,26 @@ def info
private

def search_paths
ENV['PATH'].split File::PATH_SEPARATOR
ENV["PATH"].split File::PATH_SEPARATOR
end

def with_ports_paths
old_path = ENV['PATH']
old_path = ENV["PATH"]

begin
ENV['PATH'] = [
ENV["PATH"] = [
Gem.ports_bin_paths,
old_path
].flatten.join File::PATH_SEPARATOR

yield if block_given?
ensure
ENV['PATH'] = old_path
ENV["PATH"] = old_path
end
end

def find_bin
File.join @root, 'bin', name
File.join @root, "bin", name
end

def find_exe
Expand Down Expand Up @@ -96,9 +95,9 @@ def binary?(file)
bytes = File.stat(file).blksize
return false unless bytes
bytes = 4096 if bytes > 4096
s = (File.read(file, bytes) || '')
s = s.encode('US-ASCII', undef: :replace).split(//)
((s.size - s.grep(' '..'~').size) / s.size.to_f) > 0.30
s = (File.read(file, bytes) || "")
s = s.encode("US-ASCII", undef: :replace).chars
((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
end
end
end
Loading