Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move logger instance in the configuration object #9

Merged
merged 1 commit into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format

## [Unreleased]

### Changed

- Logger is now part of the configuration object instead of a class attribute. ([#9](https://github.com/tlatsas/webpacker_uploader/pull/9), [@tlatsas](https://github.com/tlatsas))

## [[0.3.0]](https://github.com/tlatsas/webpacker_uploader/releases/tag/v0.3.0) - 2021-03-06

### Added
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ WebpackerUploader currently supports the following configuration options:
| option | description | default value |
|----------------------|--------------------------------------------------------------|---------------------------------------|
| ignored_extensions | Which files uploader should skip based on the file extension | [] |
| logger | The logger to use for logging | ActiveSupport::Logger.new(STDOUT) |
| log_output | Log output as we upload files | true |
| public_manifest_path | The webpack manifest path | Webpacker.config.public_manifest_path |
| public_path | The webpack public output path | Webpacker.config.public_path |
Expand Down
7 changes: 1 addition & 6 deletions lib/webpacker_uploader.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

require "active_support/core_ext/object/blank"
require "active_support/logger"
require "active_support/tagged_logging"

module WebpackerUploader
extend self
Expand All @@ -20,17 +18,14 @@ def instance
# @!attribute [rw] config
# @see Instance#config
# @!scope class
# @!attribute [rw] logger
# @see Instance.logger
# @!scope class
# @!method configure
# @see Instance#configure
# @!scope class
# @!method upload!
# @return [void]
# @see Instance#upload!
# @!scope class
delegate :logger, :logger=, :configure, :config, :upload!, to: :instance
delegate :configure, :config, :upload!, to: :instance
end

require "webpacker_uploader/configuration"
Expand Down
7 changes: 7 additions & 0 deletions lib/webpacker_uploader/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

require "active_support/logger"
require "active_support/tagged_logging"

# This is the class which holds the configuration options.
#
# Options are set and retrieved using `WebpackerUploader.config`
Expand All @@ -8,6 +11,9 @@ class WebpackerUploader::Configuration
# @return [Array] the file extentions ignored by the uploader.
attr_accessor :ignored_extensions

# @return [ActiveSupport::Logger] the logger to use.
attr_accessor :logger

# @return [Boolean] whether or not to log operations.
attr_accessor :log_output

Expand All @@ -21,6 +27,7 @@ class WebpackerUploader::Configuration

def initialize
@ignored_extensions = []
@logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
@log_output = true
@public_manifest_path = ::Webpacker.config.public_manifest_path
@public_path = ::Webpacker.config.public_path
Expand Down
8 changes: 2 additions & 6 deletions lib/webpacker_uploader/instance.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# frozen_string_literal: true

class WebpackerUploader::Instance
# @!attribute [rw] logger
# @!scope class
cattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }

attr_writer :config

# @private
Expand Down Expand Up @@ -63,11 +59,11 @@ def upload!(provider, prefix: nil)
file_path = config.public_path.join(path)

if name.end_with?(*config.ignored_extensions)
logger.info("Skipping #{file_path}") if config.log_output?
config.logger.info("Skipping #{file_path}") if config.log_output?
else
content_type = WebpackerUploader::Mime.mime_type(path)

logger.info("Processing #{file_path} as #{content_type}") if config.log_output?
config.logger.info("Processing #{file_path} as #{content_type}") if config.log_output?

provider.upload!(remote_path, file_path, content_type)
end
Expand Down
8 changes: 8 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def teardown

def test_default_config_options
assert_empty @config.ignored_extensions

assert_instance_of ActiveSupport::Logger, @config.logger

assert @config.log_output
assert @config.log_output?

Expand All @@ -27,6 +30,9 @@ def test_changing_config_options
@config.ignored_extensions = [".css", ".js"]
assert_equal [".css", ".js"], @config.ignored_extensions

@config.logger = Logger.new(STDOUT)
assert_instance_of Logger, @config.logger

@config.log_output = false
refute @config.log_output
refute @config.log_output?
Expand All @@ -41,12 +47,14 @@ def test_changing_config_options
def test_configure_block
WebpackerUploader.configure do |c|
c.ignored_extensions = [".js"]
c.logger = Logger.new(STDOUT)
c.log_output = false
c.public_manifest_path = "path/to/manifest.json"
c.public_path = "path/to/public/dir"
end

assert_equal [".js"], WebpackerUploader.config.ignored_extensions
assert_instance_of Logger, WebpackerUploader.config.logger
refute WebpackerUploader.config.log_output
refute WebpackerUploader.config.log_output?
assert_equal "path/to/manifest.json", WebpackerUploader.config.public_manifest_path.to_s
Expand Down