Skip to content

Commit

Permalink
refactor: Upgrade to dry-cli 0.7 while avoiding dry-files dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo committed May 10, 2021
1 parent 9592f5c commit f5b87e6
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 25 deletions.
5 changes: 2 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ PATH
remote: vite_ruby
specs:
vite_ruby (1.2.10)
dry-cli (~> 0.6.0)
dry-cli (~> 0.7.0)
rack-proxy (~> 0.6, >= 0.6.1)
zeitwerk (~> 2.2)

Expand Down Expand Up @@ -83,8 +83,7 @@ GEM
concurrent-ruby (1.1.8)
crass (1.0.6)
docile (1.3.5)
dry-cli (0.6.0)
concurrent-ruby (~> 1.0)
dry-cli (0.7.0)
erubi (1.10.0)
globalid (0.4.2)
activesupport (>= 4.2.0)
Expand Down
142 changes: 142 additions & 0 deletions test/file_utils_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# frozen_string_literal: true

require 'test_helper'
require 'securerandom'

class FilesTest < ViteRuby::Test
delegate(
:append,
:write,
:replace_first_line,
:inject_line_before,
:inject_line_after,
:inject_line_after_last,
to: 'ViteRuby::CLI::FileUtils',
)

def root
@root ||= Pathname.new(Dir.pwd).join('tmp', SecureRandom.uuid).tap(&:mkpath)
end

def teardown
FileUtils.remove_entry_secure(root)
end

def test_write
path = root.join('write')
write(path, "Hello\nWorld")
assert path.exist?
assert_content path, "Hello\nWorld"
end

def test_append
path = root.join('append.rb')
content = <<~CONTENT
class Append
end
CONTENT

write(path, content)
append(path, "\nFoo.register Append")

assert_content path, <<~CONTENT
class Append
end
Foo.register Append
CONTENT
end

def test_replace_first_line
path = root.join('replace_string.rb')
content = <<~CONTENT
class Replace
def self.perform
end
end
CONTENT

write(path, content)
replace_first_line(path, 'perform', ' def self.call(input)')

assert_content path, <<~CONTENT
class Replace
def self.call(input)
end
end
CONTENT
end

def test_injects_line_before
path = root.join('inject_before_string.rb')
content = <<~CONTENT
class InjectBefore
def self.call
end
end
CONTENT

write(path, content)
inject_line_before(path, 'call', ' # It performs the operation')

assert_content path, <<~CONTENT
class InjectBefore
# It performs the operation
def self.call
end
end
CONTENT
end

def test_injects_line_after
path = root.join('inject_after.rb')
content = <<~CONTENT
class InjectAfter
def self.call
end
end
CONTENT

write(path, content)
inject_line_after(path, 'call', ' :result')

assert_content path, <<~CONTENT
class InjectAfter
def self.call
:result
end
end
CONTENT
end

def test_injects_line_after_last
path = root.join('inject_after_last.rb')
content = <<~CONTENT
class InjectAfter
def self.call
end
def self.call
end
end
CONTENT

write(path, content)
inject_line_after_last(path, 'call', ' :result')

assert_content path, <<~CONTENT
class InjectAfter
def self.call
end
def self.call
:result
end
end
CONTENT
end

private

def assert_content(path, content)
assert_includes content, path.read
end
end
2 changes: 1 addition & 1 deletion vite_rails/lib/vite_rails/installation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module ViteRails::Installation
def setup_app_files
cp RAILS_TEMPLATES.join('config/rails-vite.json'), config.config_path
if dir = %w[app/javascript app/packs].find { |path| root.join(path).exist? }
Dry::CLI::Utils::Files.replace_first_line config.config_path, 'app/frontend', %( "sourceCodeDir": "#{ dir }",)
replace_first_line config.config_path, 'app/frontend', %( "sourceCodeDir": "#{ dir }",)
end
setup_content_security_policy root.join('config/initializers/content_security_policy.rb')
end
Expand Down
2 changes: 1 addition & 1 deletion vite_rails_legacy/lib/vite_rails_legacy/installation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module ViteRailsLegacy::Installation
def setup_app_files
cp RAILS_TEMPLATES.join('config/rails-vite.json'), config.config_path
if root.join('app/javascript').exist?
Dry::CLI::Utils::Files.replace_first_line config.config_path, 'app/frontend', %( "sourceCodeDir": "app/javascript",)
replace_first_line config.config_path, 'app/frontend', %( "sourceCodeDir": "app/javascript",)
end
end

Expand Down
18 changes: 3 additions & 15 deletions vite_ruby/lib/vite_ruby/cli/build.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
# frozen_string_literal: true

class ViteRuby::CLI::Build < Dry::CLI::Command
CURRENT_ENV = ENV['RACK_ENV'] || ENV['RAILS_ENV']
class ViteRuby::CLI::Build < ViteRuby::CLI::Vite
DEFAULT_ENV = CURRENT_ENV || 'production'

def self.shared_options
option(:mode, default: self::DEFAULT_ENV, values: %w[development production test], aliases: ['m'], desc: 'The build mode for Vite')
option(:clobber, desc: 'Clear cache and previous builds', type: :boolean, aliases: %w[clean clear])
option(:debug, desc: 'Run Vite in verbose mode, printing all debugging output', aliases: ['verbose'], type: :boolean)
option(:inspect, desc: 'Run Vite in a debugging session with node --inspect-brk', aliases: ['inspect-brk'], type: :boolean)
option(:trace_deprecation, desc: 'Run Vite in debugging mode with node --trace-deprecation', aliases: ['trace-deprecation'], type: :boolean)
end

desc 'Bundle all entrypoints using Vite.'
shared_options
option(:force, desc: 'Force the build even if assets have not changed', type: :boolean)

def call(mode:, args: [], clobber: false, **boolean_opts)
ViteRuby.env['VITE_RUBY_MODE'] = mode
ViteRuby.commands.clobber if clobber
boolean_opts.map { |name, value| args << "--#{ name }" if value }
block_given? ? yield(args) : ViteRuby.commands.build_from_task(*args)
def call(**options)
super { |args| ViteRuby.commands.build_from_task(*args) }
end
end
2 changes: 1 addition & 1 deletion vite_ruby/lib/vite_ruby/cli/dev.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class ViteRuby::CLI::Dev < ViteRuby::CLI::Build
class ViteRuby::CLI::Dev < ViteRuby::CLI::Vite
DEFAULT_ENV = CURRENT_ENV || 'development'

desc 'Start the Vite development server.'
Expand Down
123 changes: 123 additions & 0 deletions vite_ruby/lib/vite_ruby/cli/file_utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# frozen_string_literal: true

require 'pathname'
require 'fileutils'

# NOTE: Extracted from dry-cli version 0.6.0, which later removed this file as
# it was refactored and extracted into the more complete (and complex) dry-files.
module ViteRuby::CLI::FileUtils
class << self
# Creates a new file or rewrites the contents of an existing file.
#
# @since 1.2.11
# @api private
def write(path, *content)
mkdir_p(path)
File.open(path, File::CREAT | File::WRONLY | File::TRUNC) do |file|
file.write(Array(content).flatten.join)
end
end

# Copies source into destination.
#
# @since 1.2.11
# @api private
def cp(source, destination)
mkdir_p(destination)
FileUtils.cp(source, destination)
end

# Adds a new line at the bottom of the file.
#
# @since 1.2.11
# @api private
def append(path, contents)
mkdir_p(path)

content = File.readlines(path)
content << "\n" unless content.last.end_with?("\n")
content << "#{ contents }\n"

write(path, content)
end

# Replace first line in `path` that contains `target` with `replacement`.
#
# @since 1.2.11
# @api private
def replace_first_line(path, target, replacement)
content = File.readlines(path)
content[index(content, path, target)] = "#{ replacement }\n"

write(path, content)
end

# Inject `contents` in `path` before `target`.
#
# @since 1.2.11
# @api private
def inject_line_before(path, target, contents)
_inject_line_before(path, target, contents, method(:index))
end

# Inject `contents` in `path` after `target`.
#
# @since 1.2.11
# @api private
def inject_line_after(path, target, contents)
_inject_line_after(path, target, contents, method(:index))
end

# Inject `contents` in `path` after last `target`.
#
# @since 1.2.11
# @api private
def inject_line_after_last(path, target, contents)
_inject_line_after(path, target, contents, method(:rindex))
end

private

# Creates all parent directories for the given file path.
#
# @since 1.2.11
# @api private
def mkdir_p(path)
Pathname.new(path).dirname.mkpath
end

# @since 1.2.11
# @api private
def index(content, path, target)
content.index { |line| line.include?(target) } ||
raise(ArgumentError, "Cannot find `#{ target }' inside `#{ path }'.")
end

# @since 1.2.11
# @api private
def rindex(content, path, target)
content.rindex { |line| line.include?(target) } ||
raise(ArgumentError, "Cannot find `#{ target }' inside `#{ path }'.")
end

# @since 1.2.11
# @api private
def _inject_line_before(path, target, contents, finder)
content = File.readlines(path)
i = finder.call(content, path, target)

content.insert(i, "#{ contents }\n")
write(path, content)
end

# @since 1.2.11
# @api private
def _inject_line_after(path, target, contents, finder)
content = File.readlines(path)
i = finder.call(content, path, target)

content.insert(i + 1, "#{ contents }\n")
write(path, content)
end
end
end
5 changes: 2 additions & 3 deletions vite_ruby/lib/vite_ruby/cli/install.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require 'dry/cli/utils/files'
require 'stringio'

class ViteRuby::CLI::Install < Dry::CLI::Command
Expand Down Expand Up @@ -49,9 +48,9 @@ def install_sample_files

def_delegators 'ViteRuby', :config

%i[append cp inject_line_after inject_line_after_last inject_line_before write].each do |util|
%i[append cp inject_line_after inject_line_after_last inject_line_before replace_first_line write].each do |util|
define_method(util) { |*args|
Dry::CLI::Utils::Files.send(util, *args) rescue nil
ViteRuby::CLI::FileUtils.send(util, *args) rescue nil
}
end

Expand Down
20 changes: 20 additions & 0 deletions vite_ruby/lib/vite_ruby/cli/vite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class ViteRuby::CLI::Vite < Dry::CLI::Command
CURRENT_ENV = ENV['RACK_ENV'] || ENV['RAILS_ENV']

def self.shared_options
option(:mode, default: self::DEFAULT_ENV, values: %w[development production test], aliases: ['m'], desc: 'The build mode for Vite')
option(:clobber, desc: 'Clear cache and previous builds', type: :boolean, aliases: %w[clean clear])
option(:debug, desc: 'Run Vite in verbose mode, printing all debugging output', aliases: ['verbose'], type: :boolean)
option(:inspect, desc: 'Run Vite in a debugging session with node --inspect-brk', aliases: ['inspect-brk'], type: :boolean)
option(:trace_deprecation, desc: 'Run Vite in debugging mode with node --trace-deprecation', aliases: ['trace-deprecation'], type: :boolean)
end

def call(mode:, args: [], clobber: false, **boolean_opts)
ViteRuby.env['VITE_RUBY_MODE'] = mode
ViteRuby.commands.clobber if clobber
boolean_opts.map { |name, value| args << "--#{ name }" if value }
yield(args)
end
end
2 changes: 1 addition & 1 deletion vite_ruby/vite_ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |s|

s.required_ruby_version = Gem::Requirement.new('>= 2.5')

s.add_dependency 'dry-cli', '~> 0.6.0'
s.add_dependency 'dry-cli', '~> 0.7.0'
s.add_dependency 'rack-proxy', '~> 0.6', '>= 0.6.1'
s.add_dependency 'zeitwerk', '~> 2.2'

Expand Down

0 comments on commit f5b87e6

Please sign in to comment.