forked from ElMassimo/vite_ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Upgrade to dry-cli 0.7 while avoiding dry-files dependency
- Loading branch information
Showing
10 changed files
with
296 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters