Skip to content

Commit

Permalink
feat: Enable usage in engines by using run from the current instance
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo committed Jul 7, 2021
1 parent 73734cf commit 023a61d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion test/builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_external_env_variables
end

def test_missing_executable
refresh_config('VITE_RUBY_VITE_BIN_PATH' => 'none/vite')
refresh_config(vite_bin_path: 'none/vite')

# It fails because we stub the File.exist? check, so the binary is missing.
error = assert_raises(ViteRuby::MissingExecutableError) {
Expand Down
4 changes: 2 additions & 2 deletions test/dev_server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class DevServerTest < ViteRuby::Test
def test_not_running
refute ViteRuby.instance.dev_server_running?

refresh_config('VITE_RUBY_MODE' => 'development')
refresh_config(mode: 'development')
refute ViteRuby.instance.dev_server_running?
end

def test_running
refresh_config('VITE_RUBY_MODE' => 'development')
refresh_config(mode: 'development')
ViteRuby.instance.instance_variable_set('@running_at', Time.now)
assert ViteRuby.instance.dev_server_running?
ensure
Expand Down
10 changes: 8 additions & 2 deletions test/engine_rake_tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ def test_cli
def test_cli_commands
within_mounted_app_root {
ViteRuby.commands.verify_install
refresh_config('VITE_RUBY_ROOT' => Dir.pwd)

ENV['VITE_RUBY_ROOT'] = Dir.pwd
refresh_config

ViteRuby::CLI::Install.new.call
ViteRuby.commands.verify_install
ViteRuby::CLI::Version.new.call
Expand All @@ -70,6 +73,9 @@ def test_cli_commands
}
ViteRuby::CLI::Clobber.new.call(mode: ViteRuby.mode)
}
ensure
ENV.delete('VITE_RUBY_ROOT')
refresh_config
end

private
Expand All @@ -88,7 +94,7 @@ def stub_runner(*args, **opts, &block)
mock.expect(:call, [:stdout, :stderr, status]) do |*argv, **options|
assert_equal [args, opts].flatten.reject(&:blank?), (argv + [options]).flatten.reject(&:blank?)
end
ViteRuby.stub(:run, mock, &block)
ViteRuby.stub_any_instance(:run, ->(*stub_args, **stub_opts) { mock.call(*stub_args, **stub_opts) }, &block)
mock.verify
end

Expand Down
5 changes: 2 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ def teardown
refresh_config
end

def refresh_config(env_variables = ViteRuby.load_env_variables)
ViteRuby.env.tap(&:clear)
ViteRuby.reload_with(env_variables)
def refresh_config(**options)
ViteRuby.reload_with(**options).config
end

def with_rails_env(env)
Expand Down
49 changes: 23 additions & 26 deletions vite_ruby/lib/vite_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,11 @@ class ViteRuby
class << self
extend Forwardable

def_delegators :instance, :config, :commands, :run_proxy?
def_delegators :instance, :config, :commands, :env, :run, :run_proxy?
def_delegators :config, :mode

def instance
@instance ||= ViteRuby.new
end

# Public: Additional environment variables to pass to Vite.
#
# Example:
# ViteRuby.env['VITE_RUBY_CONFIG_PATH'] = 'config/alternate_vite.json'
def env
@env ||= load_env_variables
@instance ||= new
end

# Internal: Refreshes the manifest.
Expand All @@ -60,21 +52,9 @@ def install_tasks
load File.expand_path('tasks/vite.rake', __dir__)
end

# Internal: Executes the vite binary.
def run(argv, **options)
ViteRuby::Runner.new(instance).run(argv, **options)
end

# Internal: Refreshes the config after setting the env vars.
def reload_with(env_vars)
env.update(env_vars)
@instance = nil
config
end

# Internal: Allows to obtain any env variables for configuration options.
def load_env_variables
ENV.select { |key, _| key.start_with?(ENV_PREFIX) }
# Internal: Creates a new instance with the specified options.
def reload_with(**config_options)
@instance = new(**config_options)
end

# Internal: Detects if the application has installed a framework-specific
Expand All @@ -90,6 +70,10 @@ def framework_libraries

attr_writer :logger

def initialize(**config_options)
@config_options = config_options
end

def logger
@logger ||= Logger.new($stdout)
end
Expand All @@ -107,6 +91,14 @@ def dev_server_running?
@running_at = false
end

# Public: Additional environment variables to pass to Vite.
#
# Example:
# ViteRuby.env['VITE_RUBY_CONFIG_PATH'] = 'config/alternate_vite.json'
def env
@env ||= ENV.select { |key, _| key.start_with?(ENV_PREFIX) }
end

# Public: The proxy for assets should only run in development mode.
def run_proxy?
config.mode == 'development'
Expand All @@ -115,6 +107,11 @@ def run_proxy?
false
end

# Internal: Executes the vite binary.
def run(argv, **options)
(@runner ||= ViteRuby::Runner.new(self)).run(argv, **options)
end

# Public: Keeps track of watched files and triggers builds as needed.
def builder
@builder ||= ViteRuby::Builder.new(self)
Expand All @@ -127,7 +124,7 @@ def commands

# Public: Current instance configuration for Vite.
def config
@config ||= ViteRuby::Config.resolve_config
@config ||= ViteRuby::Config.resolve_config(**@config_options)
end

# Public: Enables looking up assets managed by Vite using name and type.
Expand Down
4 changes: 2 additions & 2 deletions vite_ruby/lib/vite_ruby/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def last_build_metadata

extend Forwardable

def_delegators :@vite_ruby, :config, :logger
def_delegators :@vite_ruby, :config, :logger, :run

# Internal: Reads metadata recorded on the last build, if it exists.
def last_build_attrs
Expand Down Expand Up @@ -69,7 +69,7 @@ def watched_files_digest
def build_with_vite(*args)
logger.info 'Building with Vite ⚡️'

stdout, stderr, status = ViteRuby.run(['build', *args])
stdout, stderr, status = run(['build', *args])
log_build_result(stdout, stderr.to_s, status)

status.success?
Expand Down
2 changes: 1 addition & 1 deletion vite_ruby/lib/vite_ruby/cli/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def copy_template(path, to:)
def create_configuration_files
copy_template 'config/vite.config.ts', to: root.join('vite.config.ts')
setup_app_files
ViteRuby.reload_with('VITE_RUBY_CONFIG_PATH' => config.config_path)
ViteRuby.reload_with(config_path: config.config_path)
end

# Internal: Installs vite and vite-plugin-ruby at the project level.
Expand Down

0 comments on commit 023a61d

Please sign in to comment.