Skip to content

Commit

Permalink
Move core extensions to new Foundation gem…more to follow!
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcwhite committed Apr 16, 2024
1 parent 3ab6549 commit bef4af8
Show file tree
Hide file tree
Showing 19 changed files with 257 additions and 77 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ end
gem "bridgetown", path: "bridgetown"
gem "bridgetown-builder", path: "bridgetown-builder"
gem "bridgetown-core", path: "bridgetown-core"
gem "bridgetown-foundation", path: "bridgetown-foundation"
gem "bridgetown-paginate", path: "bridgetown-paginate"
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ PATH
activesupport (>= 6.0, < 8.0)
addressable (~> 2.4)
amazing_print (~> 1.2)
bridgetown-foundation (= 1.3.4)
colorator (~> 1.0)
csv (~> 3.2)
erubi (~> 1.9)
Expand All @@ -34,6 +35,12 @@ PATH
tilt (~> 2.0)
zeitwerk (~> 2.5)

PATH
remote: bridgetown-foundation
specs:
bridgetown-foundation (1.3.4)
zeitwerk (~> 2.5)

PATH
remote: bridgetown-paginate
specs:
Expand Down Expand Up @@ -234,6 +241,7 @@ DEPENDENCIES
bridgetown!
bridgetown-builder!
bridgetown-core!
bridgetown-foundation!
bridgetown-paginate!
memory_profiler
minitest
Expand Down
1 change: 1 addition & 0 deletions bridgetown-core/bridgetown-core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency("activesupport", [">= 6.0", "< 8.0"])
s.add_runtime_dependency("addressable", "~> 2.4")
s.add_runtime_dependency("amazing_print", "~> 1.2")
s.add_runtime_dependency("bridgetown-foundation", Bridgetown::VERSION)
s.add_runtime_dependency("colorator", "~> 1.0")
s.add_runtime_dependency("csv", "~> 3.2")
s.add_runtime_dependency("erubi", "~> 1.9")
Expand Down
49 changes: 6 additions & 43 deletions bridgetown-core/lib/bridgetown-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,8 @@ def require_all(path)
require "json"
require "yaml"

# Initial Zeitwerk setup
require "zeitwerk"

module Bridgetown
module CoreExt; end
end

class Module
# Due to Active Support incompatibility, we can't extend Gem::Deprecate in Object
# So we're pulling this in from:
# https://github.com/rubygems/rubygems/blob/v3.5.9/lib/rubygems/deprecate.rb
def gem_deprecate(name, repl, year, month)
# rubocop:disable Style/FormatStringToken
class_eval do
old = "_deprecated_#{name}"
alias_method old, name
define_method name do |*args, &block|
klass = is_a? Module
target = klass ? "#{self}." : "#{self.class}#"
msg = [
"NOTE: #{target}#{name} is deprecated",
repl == :none ? " with no replacement" : "; use #{repl} instead",
format(". It will be removed on or after %4d-%02d.", year, month),
"\n#{target}#{name} called from #{Gem.location_of_caller.join(":")}",
]
warn "#{msg.join}." unless Gem::Deprecate.skip
send old, *args, &block
end
ruby2_keywords name if respond_to?(:ruby2_keywords, true)
end
# rubocop:enable Style/FormatStringToken
end
end

Zeitwerk::Loader.new.tap do |loader|
loader.push_dir File.join(__dir__, "bridgetown-core/core_ext"), namespace: Bridgetown::CoreExt
loader.setup
loader.eager_load
end
# Pull in Foundation gem
require "bridgetown-foundation"

# 3rd party
require "active_support"
Expand Down Expand Up @@ -433,9 +396,9 @@ def self.register_extension(mod)
end
end

Zeitwerk::Loader.new.tap do |loader|
loader.push_dir File.join(__dir__, "bridgetown-core/model"), namespace: Bridgetown::Model
loader.push_dir File.join(__dir__, "bridgetown-core/resource"), namespace: Bridgetown::Resource
loader.setup # ready!
Zeitwerk.with_loader do |l|
l.push_dir File.join(__dir__, "bridgetown-core/model"), namespace: Bridgetown::Model
l.push_dir File.join(__dir__, "bridgetown-core/resource"), namespace: Bridgetown::Resource
l.setup # ready!
end
Bridgetown::Model::Origin # this needs to load first

This file was deleted.

6 changes: 6 additions & 0 deletions bridgetown-foundation/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
inherit_from: ../.rubocop.yml

AllCops:
Exclude:
- "*.gemspec"
11 changes: 11 additions & 0 deletions bridgetown-foundation/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require "bundler/gem_tasks"

task spec: :test
require "rake/testtask"
Rake::TestTask.new(:test) do |test|
test.libs << "lib" << "test"
test.pattern = "test/**/test_*.rb"
test.verbose = true
end
27 changes: 27 additions & 0 deletions bridgetown-foundation/bridgetown-foundation.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require_relative "lib/bridgetown/foundation/version"

Gem::Specification.new do |spec|
spec.name = "bridgetown-foundation"
spec.version = Bridgetown::Foundation::VERSION
spec.author = "Bridgetown Team"
spec.email = "[email protected]"
spec.summary = "Ruby language extensions and other utilities useful for the Bridgetown ecosystem"
spec.homepage = "https://github.com/bridgetownrb/bridgetown/tree/main/bridgetown-foundation"
spec.license = "MIT"
spec.required_ruby_version = ">= 3.1"

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r!^(test|script)/!) }
spec.require_paths = ["lib"]

spec.metadata = {
"source_code_uri" => "https://github.com/bridgetownrb/bridgetown",
"bug_tracker_uri" => "https://github.com/bridgetownrb/bridgetown/issues",
"changelog_uri" => "https://github.com/bridgetownrb/bridgetown/releases",
"homepage_uri" => spec.homepage,
"rubygems_mfa_required" => "true",
}

spec.add_dependency("zeitwerk", "~> 2.5")
end
45 changes: 45 additions & 0 deletions bridgetown-foundation/lib/bridgetown-foundation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require "bridgetown/foundation/version"
require "zeitwerk"

class Module
# Due to Active Support incompatibility, we can't extend Gem::Deprecate directly in `Object`
# So we're pulling this in as `gem_deprecate` from `deprecate`:
# https://github.com/rubygems/rubygems/blob/v3.5.9/lib/rubygems/deprecate.rb
#
# Pass in the deprecated method name, the new method name, and the year & month it'll be removed
#
# @param name [Symbol] e.g. `:howdy`
# @param repl [Symbol] e.g. `:hello`
# @param year [Integer] e.g. `2025`
# @param month [Integer] e.g. `1` for January
def gem_deprecate(name, repl, year, month)
# rubocop:disable Style/FormatStringToken
class_eval do
old = "_deprecated_#{name}"
alias_method old, name
define_method name do |*args, &block|
klass = is_a? Module
target = klass ? "#{self}." : "#{self.class}#"
msg = [
"NOTE: #{target}#{name} is deprecated",
repl == :none ? " with no replacement" : "; use #{repl} instead",
format(". It will be removed on or after %4d-%02d.", year, month),
"\n#{target}#{name} called from #{Gem.location_of_caller.join(":")}",
]
warn "#{msg.join}." unless Gem::Deprecate.skip
send old, *args, &block
end
end
# rubocop:enable Style/FormatStringToken
end
end

Zeitwerk.with_loader do |l|
l.push_dir "#{__dir__}/bridgetown/foundation", namespace: Bridgetown::Foundation
l.ignore "#{__dir__}/bridgetown/foundation/version.rb"
#l.ignore "#{__dir__}/bridgetown/foundation/core_ext/string.rb"
l.setup
l.eager_load
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module Bridgetown
module Bridgetown::Foundation
module CoreExt
module Class
module Descendants
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
# frozen_string_literal: true

module Bridgetown
module Bridgetown::Foundation
module CoreExt
module Object
module WithinOther
# This method lets you check if the receiver is "within" the other object. In most cases,
# this check is accomplished via the `include?` method…aka, `10.within? [5, 10]` would
# return `true` as `[5, 10].include? 10` is true.
# return `true` as `[5, 10].include? 10` is true. And String/String comparison are
# case-insensivitve.
#
# However, for certain comparison types: Module/Class, Hash, and Set, the lesser-than (`<`)
# operator is used instead. This is so you can check `BigDecimal.within? Numeric`,
# `{easy_as: 123}.within?({indeed: "it's true", easy_as: 123})`, and if a Set is a
# `proper_subset?` of another Set.
#
# For Array/Array comparisons, a difference is checked, so `[1,2].within? [3,2,1]` is true,
# but `[1,2].within? [2,3]` is false.
#
# Also for Range, the `cover?` method is used instead of `include?`.
#
# @param other [Object] for determining if receiver lies within this value
# @return [Boolean]
def within?(other) # rubocop:disable Metrics
# rubocop:disable Style/CaseEquality
if self.class < other.class && (Module === other || Hash === other || Set === other)
# rubocop:disable Style/IfUnlessModifier
if is_a?(Module) && other.is_a?(Module)
return self < other
end

if Range === other
other&.cover?(self) == true
else
other&.include?(self) == true
if (is_a?(Hash) && other.is_a?(Hash)) || (is_a?(Set) && other.is_a?(Set))
return self < other
end
# rubocop:enable Style/CaseEquality

if is_a?(Array) && other.is_a?(Array)
return difference(other).empty?
end

if other.is_a?(Range)
return other.cover?(self) == true
end

if is_a?(::String) && other.is_a?(::String)
return other.downcase.include?(downcase)
end

other&.include?(self) == true
# rubocop:enable Style/IfUnlessModifier
rescue NoMethodError
false
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module Bridgetown
module Bridgetown::Foundation
module CoreExt
module String
module Indentation
Expand All @@ -9,7 +9,7 @@ def indent!(indent_by, *args)
Kernel.warn "multiple arguments aren't supported by `indent!' in Bridgetown", uplevel: 1
end

gsub %r!^(?\!$)!, " " * indent_by
gsub! %r!^(?\!$)!, " " * indent_by
end

def indent(indent_by, *args)
Expand All @@ -22,7 +22,7 @@ def indent(indent_by, *args)
end

module Questionable
def questionable = CoreExt::QuestionableString.new(self)
def questionable = Bridgetown::Foundation::QuestionableString.new(self)
alias_method :inquiry, :questionable
gem_deprecate :inquiry, :questionable, 2024, 12
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Bridgetown::Foundation
class QuestionableString < ::String
def method_missing(method_name, *args)
value = method_name.to_s
if value.end_with?("?")
value.chop!
return self == value
end

super
end

def respond_to_missing?(method_name, include_private = false)
method_name.end_with?("?") || super
end
end
end
8 changes: 8 additions & 0 deletions bridgetown-foundation/lib/bridgetown/foundation/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Bridgetown
module Foundation
# TODO: should we define versions here now and pull it within Core?
VERSION = "1.3.4"
end
end
6 changes: 6 additions & 0 deletions bridgetown-foundation/script/cibuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

set -ex

script/fmt
script/test
10 changes: 10 additions & 0 deletions bridgetown-foundation/script/fmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e

echo "Rubocop $(bundle exec rubocop --version)"
bundle exec rubocop -D $@
success=$?
if ((success != 0)); then
echo -e "\nTry running \`script/fmt -a\` to automatically fix errors"
fi
exit $success
23 changes: 23 additions & 0 deletions bridgetown-foundation/script/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -e

# Usage:
# script/test <test_file>
# script/test

if [ -d test/dest ]
then rm -r test/dest
fi

testopts="--profile"

if [[ $# -lt 1 ]]
then
set -x
time ruby -S bundle exec \
rake TESTOPTS=$testopts test
else
set -x
time ruby -S bundle exec ruby -I test \
"$@" $testops
fi
Loading

0 comments on commit bef4af8

Please sign in to comment.