Skip to content

Commit

Permalink
Remove static methods from Pack Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
pulkitkkr committed Aug 17, 2022
1 parent d6fb855 commit fd324b8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
################################################################################
# FILE SYSTEM BASED COMPONENT REGISTRY
################################################################################
# components_subdirectory is the directory that is used to automatically detect and register components for the usage on
# the rails view. default is nil, you can enable the feature by updating it in the next line.
# components_subdirectory is the directory that is used to automatically detect and register components for the usage
# on the rails view. default is nil, you can enable the feature by updating it in the next line.
# config.components_subdirectory = "ror_components"
#
# For automated component registry, `render_component` view helper method tries to load bundle for component from
Expand Down
101 changes: 57 additions & 44 deletions lib/react_on_rails/packs_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ class PacksGenerator
MINIMUM_SHAKAPACKER_PATCH_VERSION = 1

def self.generate
packs_generator = PacksGenerator.new
packs_generator.verify_setup_and_generate_packs
end

def self.raise_nested_enteries_disabled
packs_generator = PacksGenerator.new
packs_generator.raise_nested_enteries_disabled
end

def verify_setup_and_generate_packs
return unless components_subdirectory.present?

raise_webpacker_not_installed unless ReactOnRails::WebpackerUtils.using_webpacker?
Expand All @@ -25,14 +35,26 @@ def self.generate
generate_packs
end

def self.generate_packs
def raise_nested_enteries_disabled
msg = <<~MSG
**ERROR** ReactOnRails: `nested_entries` is configured to be disabled in shakapacker. Please update \
webpacker.yml to enable nested enteries. for more information read
https://www.shakacode.com/react-on-rails/docs/guides/file-system-based-automated-bundle-generation.md#enable-nested_entries-for-shakapacker
MSG

raise ReactOnRails::Error, msg
end

private

def generate_packs
common_component_to_path.each_value { |component_path| create_pack component_path }
client_component_to_path.each_value { |component_path| create_pack component_path }

create_server_pack if ReactOnRails.configuration.server_bundle_js_file.present?
end

def self.create_pack(file_path)
def create_pack(file_path)
output_path = generated_pack_path file_path
content = pack_file_contents file_path

Expand All @@ -41,7 +63,7 @@ def self.create_pack(file_path)
puts(Rainbow("Generated Packs: #{output_path}").yellow)
end

def self.pack_file_contents(file_path)
def pack_file_contents(file_path)
registered_component_name = component_name file_path
<<~FILE_CONTENT
import ReactOnRails from 'react-on-rails';
Expand All @@ -51,14 +73,14 @@ def self.pack_file_contents(file_path)
FILE_CONTENT
end

def self.create_server_pack
def create_server_pack
File.write(generated_server_bundle_file_path, generated_server_pack_file_content)

add_generated_pack_to_server_bundle
puts(Rainbow("Generated Server Bundle: #{generated_server_bundle_file_path}").orange)
end

def self.generated_server_pack_file_content
def generated_server_pack_file_content
common_components_for_server_bundle = common_component_to_path.delete_if { |k| server_component_to_path.key? k }
component_for_server_registration_to_path = common_components_for_server_bundle.merge server_component_to_path

Expand All @@ -77,7 +99,7 @@ def self.generated_server_pack_file_content
FILE_CONTENT
end

def self.add_generated_pack_to_server_bundle
def add_generated_pack_to_server_bundle
relative_path_to_generated_server_bundle = relative_path(defined_server_bundle_file_path,
generated_server_bundle_file_path)
content = <<~FILE_CONTENT
Expand All @@ -87,66 +109,67 @@ def self.add_generated_pack_to_server_bundle
prepend_to_file_if_not_present(defined_server_bundle_file_path, content)
end

def self.generated_server_bundle_file_path
def generated_server_bundle_file_path
generated_server_bundle_file_name = component_name defined_server_bundle_file_path.sub(".js", "-generated.js")

"#{source_entry_path}/#{generated_server_bundle_file_name}.js"
end

def self.clean_generated_packs_directory
def clean_generated_packs_directory
FileUtils.rm_rf generated_packs_directory_path
FileUtils.mkdir_p generated_packs_directory_path
end

def self.defined_server_bundle_file_path
def defined_server_bundle_file_path
ReactOnRails::Utils.server_bundle_js_file_path
end

def self.generated_packs_directory_path
def generated_packs_directory_path
"#{source_entry_path}/generated"
end

def self.source_entry_path
def source_entry_path
ReactOnRails::WebpackerUtils.webpacker_source_entry_path
end

def self.relative_component_path_from_generated_pack(ror_component_path)
component_file_path = Pathname.new ror_component_path
generated_pack_path = Pathname.new generated_pack_path ror_component_path
def relative_component_path_from_generated_pack(ror_component_path)
component_file_pathname = Pathname.new ror_component_path
component_generated_pack_path = generated_pack_path(ror_component_path)
generated_pack_pathname = Pathname.new component_generated_pack_path

relative_path(generated_pack_path, component_file_path)
relative_path(generated_pack_pathname, component_file_pathname)
end

def self.relative_path(from, to)
def relative_path(from, to)
from_path = Pathname.new from
to_path = Pathname.new to

relative_path = to_path.relative_path_from from_path
relative_path.sub("../", "")
end

def self.generated_pack_path(file_path)
def generated_pack_path(file_path)
"#{generated_packs_directory_path}/#{component_name file_path}.jsx"
end

def self.component_name(file_path)
def component_name(file_path)
basename = File.basename(file_path, File.extname(file_path))

basename.sub(CONTAINS_CLIENT_OR_SERVER_REGEX, "")
end

def self.component_name_to_path(paths)
def component_name_to_path(paths)
paths.to_h { |path| [component_name(path), path] }
end

def self.common_component_to_path
def common_component_to_path
common_components_paths = Dir.glob("#{components_search_path}/*").reject do |f|
CONTAINS_CLIENT_OR_SERVER_REGEX.match?(f)
end
component_name_to_path(common_components_paths)
end

def self.client_component_to_path
def client_component_to_path
client_render_components_paths = Dir.glob("#{components_search_path}/*.client.*")
client_specific_components = component_name_to_path(client_render_components_paths)

Expand All @@ -156,7 +179,7 @@ def self.client_component_to_path
client_specific_components
end

def self.server_component_to_path
def server_component_to_path
server_render_components_paths = Dir.glob("#{components_search_path}/*.server.*")
server_specific_components = component_name_to_path(server_render_components_paths)

Expand All @@ -170,25 +193,25 @@ def self.server_component_to_path
server_specific_components
end

def self.components_search_path
def components_search_path
source_path = ReactOnRails::WebpackerUtils.webpacker_source_path

"#{source_path}/**/#{components_subdirectory}"
end

def self.components_subdirectory
def components_subdirectory
ReactOnRails.configuration.components_subdirectory
end

def self.webpack_assets_status_checker
def webpack_assets_status_checker
@webpack_assets_status_checker ||= ReactOnRails::TestHelper::WebpackAssetsStatusChecker.new(
source_path: ReactOnRails::Utils.source_path,
generated_assets_full_path: ReactOnRails::Utils.generated_assets_full_path,
webpack_generated_files: ReactOnRails.configuration.webpack_generated_files
)
end

def self.raise_client_component_overrides_common(component_name)
def raise_client_component_overrides_common(component_name)
msg = <<~MSG
**ERROR** ReactOnRails: client specific definition for Component '#{component_name}' overrides the \
common definition. Please delete the common definition and have separate server and client files. For more \
Expand All @@ -198,7 +221,7 @@ def self.raise_client_component_overrides_common(component_name)
raise ReactOnRails::Error, msg
end

def self.raise_server_component_overrides_common(component_name)
def raise_server_component_overrides_common(component_name)
msg = <<~MSG
**ERROR** ReactOnRails: server specific definition for Component '#{component_name}' overrides the \
common definition. Please delete the common definition and have separate server and client files. For more \
Expand All @@ -208,7 +231,7 @@ def self.raise_server_component_overrides_common(component_name)
raise ReactOnRails::Error, msg
end

def self.raise_missing_client_component(component_name)
def raise_missing_client_component(component_name)
msg = <<~MSG
**ERROR** ReactOnRails: Component '#{component_name}' is missing a client specific file. For more \
information, please see https://www.shakacode.com/react-on-rails/docs/guides/file-system-based-automated-bundle-generation.md
Expand All @@ -217,7 +240,7 @@ def self.raise_missing_client_component(component_name)
raise ReactOnRails::Error, msg
end

def self.raise_shakapacker_version_incompatible
def raise_shakapacker_version_incompatible
msg = <<~MSG
**ERROR** ReactOnRails: Please upgrade Shakapacker to version #{minimum_required_shakapacker_version} or \
above to use the automated bundle generation feature. The currently installed version is \
Expand All @@ -227,7 +250,7 @@ def self.raise_shakapacker_version_incompatible
raise ReactOnRails::Error, msg
end

def self.raise_webpacker_not_installed
def raise_webpacker_not_installed
msg = <<~MSG
**ERROR** ReactOnRails: Missing Shakapacker gem. Please upgrade to use Shakapacker \
#{minimum_required_shakapacker_version} or above to use the \
Expand All @@ -237,24 +260,14 @@ def self.raise_webpacker_not_installed
raise ReactOnRails::Error, msg
end

def self.shakapacker_major_minor_version
def shakapacker_major_minor_version
shakapacker_version = ReactOnRails::WebpackerUtils.shakapacker_version
match = shakapacker_version.match(ReactOnRails::VersionChecker::MAJOR_MINOR_PATCH_VERSION_REGEX)

[match[1].to_i, match[2].to_i, match[3].to_i]
end

def self.raise_nested_enteries_disabled
msg = <<~MSG
**ERROR** ReactOnRails: `nested_entries` is configured to be disabled in shakapacker. Please update \
webpacker.yml to enable nested enteries. for more information read
https://www.shakacode.com/react-on-rails/docs/guides/file-system-based-automated-bundle-generation.md#enable-nested_entries-for-shakapacker
MSG

raise ReactOnRails::Error, msg
end

def self.shackapacker_version_requirement_met?
def shackapacker_version_requirement_met?
major = shakapacker_major_minor_version[0]
minor = shakapacker_major_minor_version[1]
patch = shakapacker_major_minor_version[2]
Expand All @@ -263,11 +276,11 @@ def self.shackapacker_version_requirement_met?
patch >= MINIMUM_SHAKAPACKER_PATCH_VERSION
end

def self.minimum_required_shakapacker_version
def minimum_required_shakapacker_version
"#{MINIMUM_SHAKAPACKER_MAJOR_VERSION}.#{MINIMUM_SHAKAPACKER_MINOR_VERSION}.#{MINIMUM_SHAKAPACKER_PATCH_VERSION}"
end

def self.prepend_to_file_if_not_present(file, text_to_prepend)
def prepend_to_file_if_not_present(file, text_to_prepend)
file_content = File.read(file)

return if file_content.include? text_to_prepend
Expand Down
4 changes: 4 additions & 0 deletions spec/react_on_rails/packs_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module ReactOnRails
let(:webpacker_source_entry_path) { File.expand_path("fixtures/automated_packs_generation/packs", __dir__) }
let(:generated_directory) { File.expand_path("fixtures/automated_packs_generation/packs/generated", __dir__) }
let(:server_bundle_js_file) { "server-bundle.js" }
let(:server_bundle_js_file_path) do
File.expand_path("fixtures/automated_packs_generation/packs/#{server_bundle_js_file}", __dir__)
end
let(:generated_assets_full_path) do
File.expand_path("fixtures/automated_packs_generation/packs", __dir__)
end
Expand All @@ -27,6 +30,7 @@ module ReactOnRails
.and_return(webpacker_source_entry_path)
allow(ReactOnRails::WebpackerUtils).to receive(:shakapacker_version).and_return("6.5.1")
allow(ReactOnRails::Utils).to receive(:generated_assets_full_path).and_return(generated_assets_full_path)
allow(ReactOnRails::Utils).to receive(:server_bundle_js_file_path).and_return(server_bundle_js_file_path)
end

after do
Expand Down

0 comments on commit fd324b8

Please sign in to comment.