catalyst-rails 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5f4062e6e325a8a191e435eb75e272cb7746c863ad9d75f1b6d274f5d41a5433
4
+ data.tar.gz: 34c8cd4232f14da1d9c06af6c70e6cc363f7c5d20b3a1b151766156a9b18f15e
5
+ SHA512:
6
+ metadata.gz: a5f6edf6124e90a195fcb36e08548b3c5e04d368ea4f9550a0671903eec576e0011cac87275c392b7a85049fd9f722bdcbb370e56ae7306030dc98c378fa2cca
7
+ data.tar.gz: 9a75fb0d529bdeb052f7eabf7ac9d17fbceb2d83ba5c1a41d30a3d4b98eb6724e8bc6fb354e00b344b7a17e16cac4710bf26201728ca33a0a93c9f000c79660c
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-configurable'
4
+ require 'open3'
5
+
6
+ module Catalyst
7
+ extend Dry::Configurable
8
+
9
+ default_environment = if ENV['NODE_ENV']
10
+ ENV['NODE_ENV'].to_sym
11
+ elsif defined? Rails
12
+ Rails.env.to_sym
13
+ end
14
+
15
+ default_manifest_path = if defined? Rails
16
+ File.expand_path('./public/assets/manifest.json', Dir.pwd)
17
+ end
18
+
19
+ setting :environment, default_environment
20
+ setting :manifest_path, default_manifest_path
21
+ setting :assets_base_path, '/assets'
22
+ setting :dev_server_host, ENV.fetch('DEV_SERVER_HOST') { 'localhost' }
23
+ setting :dev_server_port, ENV.fetch('DEV_SERVER_PORT') { 8080 }.to_i
24
+ setting :running_feature_tests, -> {
25
+ !defined?(RSpec) || RSpec.world.all_example_groups.any? do |group|
26
+ group.metadata[:type] == :system
27
+ end
28
+ }
29
+
30
+ def self.log(message, level = :info)
31
+ message = message.split("\n").reduce('') do |reduction, line|
32
+ reduction + "\e[31m[Catalyst]\e[0m #{line}\n"
33
+ end
34
+
35
+ puts message
36
+ end
37
+
38
+ def self.development?
39
+ config.environment == :development
40
+ end
41
+
42
+ def self.test?
43
+ config.environment == :test
44
+ end
45
+
46
+ def self.production?
47
+ config.environment == :production
48
+ end
49
+
50
+ def self.build!(environment = nil)
51
+ ::Catalyst::Builder.build!(environment)
52
+ end
53
+
54
+ def self.serve!
55
+ unless $catalyst_server_pid.nil?
56
+ log(
57
+ "A Catalyst server is already running (#{$catalyst_server_pid}).",
58
+ :warn
59
+ )
60
+
61
+ return
62
+ end
63
+
64
+ check_for_catalyst!
65
+
66
+ stdin, stdout, stderr, wait_thr = Open3.popen3('yarn start')
67
+
68
+ $catalyst_server_pid = wait_thr.pid
69
+
70
+ Thread.new do
71
+ while line = stdout.gets
72
+ puts line
73
+ end
74
+ rescue IOError
75
+ end
76
+
77
+ at_exit do
78
+ stdin.close
79
+ stdout.close
80
+ stderr.close
81
+ end
82
+ end
83
+
84
+ def self.check_for_yarn!
85
+ unless system 'which yarn > /dev/null 2>&1'
86
+ raise NotInstalled, <<~MESSAGE
87
+ The yarn binary is not available in this directory.
88
+ Please follow the instructions here to install it:
89
+ https://yarnpkg.com/lang/en/docs/install
90
+ MESSAGE
91
+ end
92
+ end
93
+
94
+ def self.check_for_catalyst!
95
+ check_for_yarn!
96
+
97
+ unless system 'yarn run which catalyst > /dev/null 2>&1'
98
+ raise NotInstalled, <<~MESSAGE
99
+ The catalyst binary is not available in this directory.
100
+ Please follow the instructions here to intall it:
101
+ https://github.com/friendsoftheweb/catalyst
102
+ MESSAGE
103
+ end
104
+ end
105
+ end
106
+
107
+ require_relative './catalyst/errors'
108
+ require_relative './catalyst/builder'
109
+ require_relative './catalyst/helpers'
110
+ require_relative './catalyst/manifest'
111
+ require_relative './catalyst/railtie' if defined? Rails
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+ require 'forwardable'
5
+ require_relative './config'
6
+
7
+ module Catalyst
8
+ class Builder
9
+ BUILD_COMMAND = 'yarn run catalyst build'
10
+
11
+ include Singleton
12
+
13
+ class << self
14
+ extend Forwardable
15
+ def_delegator :instance, :build!
16
+ end
17
+
18
+ def build!(environment = nil)
19
+ Catalyst.check_for_catalyst!
20
+
21
+ environment ||= Catalyst.config.environment
22
+
23
+ case environment
24
+ when :test
25
+ test_build!
26
+ when :production
27
+ production_build!
28
+ else
29
+ raise ArgumentError,
30
+ 'Invalid environment. Must be one of: :test, :production.'
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def test_build!
37
+ unless Catalyst.config.running_feature_tests.call
38
+ Catalyst.log('Not running feature tests -- skipping build')
39
+ return
40
+ end
41
+
42
+ # Only rebuild the assets if they've been modified since the last time
43
+ # they were built successfully.
44
+ if assets_last_built >= assets_last_modified
45
+ Catalyst.log('Assets have not been modified -- skipping build')
46
+ return
47
+ end
48
+
49
+ if system("NODE_ENV=test #{BUILD_COMMAND}")
50
+ unless assets_last_built_file_path.nil?
51
+ FileUtils.touch(assets_last_built_file_path)
52
+ end
53
+ else
54
+ puts <<~MESSAGE
55
+ \e[31m
56
+ ***************************************************************
57
+ * *
58
+ * Failed to compile assets with Catalyst! *
59
+ * Make sure 'yarn run catalyst build' runs without failing. *
60
+ * *
61
+ ***************************************************************\e[0m
62
+ MESSAGE
63
+
64
+ exit 1
65
+ end
66
+ end
67
+
68
+ def production_build!
69
+ unless system("NODE_ENV=production #{BUILD_COMMAND}")
70
+ Catalyst.log('Failed to compile assets!')
71
+ end
72
+ end
73
+
74
+ def assets_last_modified
75
+ asset_paths.lazy.map { |path| File.ctime(path) }.max || Time.now
76
+ end
77
+
78
+ def asset_paths
79
+ if ::Catalyst::Config.root_path
80
+ Dir.glob(File.join(::Catalyst::Config.root_path, '**/*.{js,scss}')) + [
81
+ File.join(Dir.pwd, 'yarn.lock')
82
+ ]
83
+ else
84
+ []
85
+ end
86
+ end
87
+
88
+ def assets_last_built_file_path
89
+ if defined?(Rails)
90
+ Rails.root.join('tmp/assets-last-built')
91
+ end
92
+ end
93
+
94
+ def assets_last_built
95
+ if assets_last_built_file_path.nil?
96
+ Time.at(0)
97
+ else
98
+ File.mtime(assets_last_built_file_path) rescue Time.at(0)
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+ require 'forwardable'
5
+ require 'json'
6
+ require_relative './errors'
7
+
8
+ module Catalyst
9
+ class Config
10
+ PACKAGE_PATH = File.expand_path('./package.json', Dir.pwd)
11
+
12
+ include Singleton
13
+
14
+ class << self
15
+ extend Forwardable
16
+ def_delegators :instance, :root_path, :build_path
17
+ end
18
+
19
+ def initialize
20
+ unless File.exists?(PACKAGE_PATH)
21
+ raise ::Catalyst::MissingConfig,
22
+ "Missing package.json file in: #{Dir.pwd}"
23
+ end
24
+
25
+ @values = JSON.parse(File.read(PACKAGE_PATH))['catalyst']
26
+
27
+ if @values.nil?
28
+ raise ::Catalyst::MissingConfig, <<~MESSAGE
29
+ Missing "catalyst" config in package.json file.
30
+ Please follow the instructions here to set up Catalyst:
31
+ https://github.com/friendsoftheweb/catalyst
32
+ MESSAGE
33
+ end
34
+ end
35
+
36
+ def root_path
37
+ File.join(Dir.pwd, @values['rootPath'])
38
+ end
39
+
40
+ def build_path
41
+ File.join(Dir.pwd, @values['buildPath'])
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Catalyst
4
+ CatalystError = Class.new(StandardError)
5
+ NotInstalled = Class.new(CatalystError)
6
+ MissingConfig = Class.new(CatalystError)
7
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+ require_relative './manifest'
5
+
6
+ module Catalyst
7
+ module Helpers
8
+ include ActionView::Helpers::TagHelper
9
+
10
+ def catalyst_javascript_vendor_include_tag
11
+ if Catalyst.development?
12
+ content_tag(:script, nil, src: ::Catalyst::Manifest['vendor-dll.js'])
13
+ end
14
+ end
15
+
16
+ def catalyst_javascript_include_tag(path)
17
+ path = path.to_s.gsub(/\.js\z/, '')
18
+
19
+ content_tag(
20
+ :script,
21
+ nil,
22
+ src: ::Catalyst::Manifest["#{path}.js"],
23
+ type: 'text/javascript'
24
+ )
25
+ end
26
+
27
+ def catalyst_stylesheet_link_tag(path)
28
+ path = path.to_s.gsub(/\.css\z/, '')
29
+
30
+ unless Catalyst.development?
31
+ content_tag(
32
+ :link,
33
+ nil,
34
+ href: ::Catalyst::Manifest["#{path}.css"],
35
+ media: 'screen',
36
+ rel: 'stylesheet'
37
+ )
38
+ end
39
+ end
40
+
41
+ def catalyst_asset_path(path)
42
+ ::Catalyst::Manifest[path]
43
+ end
44
+
45
+ def catalyst_asset_url(path)
46
+ if Catalyst.development? || ENV['HOST'].blank?
47
+ webpack_asset_path(path)
48
+ else
49
+ "https://#{ENV['HOST']}#{webpack_asset_path(path)}"
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+ require 'forwardable'
5
+
6
+ module Catalyst
7
+ class Manifest
8
+ AssetMissing = Class.new(StandardError)
9
+
10
+ include Singleton
11
+
12
+ class << self
13
+ extend Forwardable
14
+ def_delegator :instance, :[]
15
+ end
16
+
17
+ def initialize
18
+ if Catalyst.development?
19
+ @manifest = {}
20
+ else
21
+ if Catalyst.config.manifest_path.nil?
22
+ raise 'Missing "manifest_path" configuration.'
23
+ end
24
+
25
+ @manifest = JSON.parse(File.read(Catalyst.config.manifest_path))
26
+ end
27
+ end
28
+
29
+ def [](path)
30
+ path = path.to_s.gsub(/\A\/+/, '')
31
+
32
+ if Catalyst.development?
33
+ dev_server_host = Catalyst.config.dev_server_host
34
+ dev_server_port = Catalyst.config.dev_server_port
35
+
36
+ if dev_server_host.nil?
37
+ raise 'Missing "dev_server_host" configuration.'
38
+ end
39
+
40
+ if dev_server_port.nil?
41
+ raise 'Missing "dev_server_port" configuration.'
42
+ end
43
+
44
+ return "http://#{dev_server_host}:#{dev_server_port}/#{path}"
45
+ else
46
+ if @manifest.key?(path)
47
+ assets_base_path = Catalyst.config.assets_base_path
48
+
49
+ if assets_base_path.nil?
50
+ raise 'Missing "assets_base_path" configuration.'
51
+ end
52
+
53
+ return "#{assets_base_path}/#{@manifest[path]}"
54
+ else
55
+ raise AssetMissing, "Couldn't find an asset for path: #{path}"
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './helpers'
4
+
5
+ module Catalyst
6
+ class Railtie < Rails::Railtie
7
+ initializer 'catalyst_rails.view_helpers' do
8
+ ActionView::Base.include(::Catalyst::Helpers)
9
+ end
10
+
11
+ rake_tasks do
12
+ load File.expand_path('./tasks/build.rake', __dir__)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ namespace :catalyst do
2
+ desc 'Build assets with Catalyst'
3
+ task :build do
4
+ if File.exists?('./public/assets/manifest.json')
5
+ puts "[Catalyst] Removing previous assets..."
6
+
7
+ manifest = JSON.parse(File.read('./public/assets/manifest.json'))
8
+
9
+ manifest.values.each do |asset_path|
10
+ system "rm -f ./public/assets/#{asset_path}*"
11
+ end
12
+ end
13
+
14
+ puts "[Catalyst] Compiling assets..."
15
+ Catalyst.build!
16
+
17
+ if Catalyst.production?
18
+ puts "[Catalyst] Removing 'node_modules' directory..."
19
+ system 'rm -rf ./node_modules'
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: catalyst-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dan Martens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-configurable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionview
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ - - "<="
35
+ - !ruby/object:Gem::Version
36
+ version: '6.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '3.0'
44
+ - - "<="
45
+ - !ruby/object:Gem::Version
46
+ version: '6.0'
47
+ description:
48
+ email: dan@friendsoftheweb.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/catalyst-rails.rb
54
+ - lib/catalyst/builder.rb
55
+ - lib/catalyst/config.rb
56
+ - lib/catalyst/errors.rb
57
+ - lib/catalyst/helpers.rb
58
+ - lib/catalyst/manifest.rb
59
+ - lib/catalyst/railtie.rb
60
+ - lib/catalyst/tasks/build.rake
61
+ homepage: http://rubygems.org/gems/catalyst-rails
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.7.3
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Ruby helpers for the "catalyst" node package
85
+ test_files: []