Skip to content

Commit

Permalink
remove need for forked rails
Browse files Browse the repository at this point in the history
* still prevent Rails const from being defined
  • Loading branch information
tongueroo committed May 30, 2019
1 parent 14ec869 commit faa5ce1
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 81 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "vendor/jets-gems"]
path = vendor/jets-gems
url = https://github.com/tongueroo/jets-gems
[submodule "vendor/rails"]
path = vendor/rails
url = https://github.com/tongueroo/rails.git
4 changes: 2 additions & 2 deletions jets.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

# spec.add_dependency "actionpack", "~> 5.2.1" # using vendor/rails version
# spec.add_dependency "actionview", "~> 5.2.1" # using vendor/rails version
spec.add_dependency "actionpack", "~> 5.2.1"
spec.add_dependency "actionview", "~> 5.2.1"
spec.add_dependency "actionmailer", "~> 5.2.1"
spec.add_dependency "activerecord", "~> 5.2.1"
spec.add_dependency "activesupport", "~> 5.2.1"
Expand Down
6 changes: 0 additions & 6 deletions lib/jets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ class Error < StandardError; end
extend Core # root, logger, etc
end

require "jets/core_ext/kernel"

root = File.expand_path("..", __dir__)

$:.unshift("#{root}/vendor/jets-gems/lib")
require "jets-gems"

$:.unshift("#{root}/vendor/rails/actionpack/lib")
$:.unshift("#{root}/vendor/rails/actionview/lib")
# will require action_controller, action_pack, etc later when needed

Jets::Autoloaders.once.preload("#{__dir__}/jets/db.rb") # required for booter.rb: setup_db
2 changes: 2 additions & 0 deletions lib/jets/booter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "jets/core_ext/kernel" # Hack prevents Rails const from being defined

class Jets::Booter
class << self
@booted = false
Expand Down
61 changes: 9 additions & 52 deletions lib/jets/core_ext/kernel.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,13 @@
# Works with io.rb
# IO.flush # flush output and write to disk for node shim
module Kernel
JETS_OUTPUT = "/tmp/jets-output.log"
@@io_buffer = []
module_function

# List from https://ruby-doc.org/core-2.5.1/Kernel.html
# Note, will lose pp format in the @io_buffer but looks like a lot of work to keep the pp format.
# Must override stdout which can be messy quick: https://www.ruby-forum.com/topic/43725
OVERRIDE_METHODS = %w[
p
pp
print
printf
putc
puts
]
# NOTE adding sprintf produces #<%s: %s:%s/%s> with puma? So not including sprintf
OVERRIDE_METHODS.each do |meth|
# Example of generated code:
#
# alias_method :original_puts, :puts
# def puts(*args, &block)
# @@io_buffer << args.first # message
# original_puts(*args, &block)
# end
#
class_eval <<~CODE
alias_method :original_#{meth}, :#{meth}
def #{meth}(*args, &block)
@@io_buffer << args.first # message
original_#{meth}(*args, &block)
end
CODE
end

def io_buffer
@@io_buffer
end

# Note: Writing binary data to the log will crash the process with an error like this:
# jets/lib/jets/core_ext/kernel.rb:20:in `write': "\x89" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
# Rescue and discard it to keep the process alive.
def io_flush
chunk = @@io_buffer.join("\n")
chunk += "\n" unless chunk == ''
begin
# since we always append to the file, the node shim is responsible for truncating the file
IO.write(JETS_OUTPUT, chunk, mode: 'a') if chunk
# Writing to log with binary content will crash the process so rescuing it and writing an info message.
rescue Encoding::UndefinedConversionError
IO.write(JETS_OUTPUT, "[BINARY DATA]\n", mode: 'a')
end
@@io_buffer = []
alias_method :jets_original_require, :require
# @param path [String]
# @return [Boolean]
def require(path)
# Hack to prevent Rails const from being defined
# Actionview requires "rails-html-sanitizer" and that creates a Rails module
path = "jets-html-sanitizer" if path == "rails-html-sanitizer"
jets_original_require(path)
end
end
14 changes: 0 additions & 14 deletions lib/jets/io.rb

This file was deleted.

3 changes: 0 additions & 3 deletions lib/jets/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ def add(severity, message = nil, progname = nil)
progname = @progname
end
end
# Put the message in the Jets::IO.buffer which will get flushed to CloudWatch.
# No need to include timestamp as CloudWatch already has a timestamp.
IO.buffer << message

super # original logical
end
Expand Down
1 change: 0 additions & 1 deletion vendor/rails
Submodule rails deleted from cde5f3

0 comments on commit faa5ce1

Please sign in to comment.