manifester 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f2245e87b983a5959438ada67d8918ee9676594a83f696f7334e390560cc06d1
4
+ data.tar.gz: b16152e7230c6724889e55e25f55227f1ab870fadbf5ba76721afed39f4a52f4
5
+ SHA512:
6
+ metadata.gz: 79b6e43a6bd6d4b8cfa4e737d85500dc17a638c01354d8c81e4ee1b1b6589895be5ee57970810568440a6915064b86e11580910f018055c957e0731f4da8fa71
7
+ data.tar.gz: 61f7553cc4ba9cbfc6514283b4ca3ee46281e7806a7698c5fbb78e4192905cb681af16d1c3ab53c69b7be78e99184b6ca259c2e6cd0cea9083d5254513e02796
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Adrian Marin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Manifester
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'manifester'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install manifester
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
9
+
10
+ require "rake/testtask"
11
+
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'test'
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = false
16
+ end
17
+
18
+ task default: :test
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/manifester .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module Manifester
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,24 @@
1
+ module Manifester
2
+ module ApplicationHelper
3
+ def current_manifester_instance
4
+ Manifester.instance
5
+ end
6
+
7
+ def javascript_manifest_tag(*names, **options)
8
+ javascript_include_tag(*sources_from_manifest_entrypoints(names, type: :javascript), **options)
9
+ end
10
+
11
+ def stylesheet_manifest_tag(*names, **options)
12
+ stylesheet_link_tag(*sources_from_manifest_entrypoints(names, type: :stylesheet), **options)
13
+ end
14
+
15
+ def stylesheet_pack_tag(*names, **options)
16
+ end
17
+
18
+ private
19
+
20
+ def sources_from_manifest_entrypoints(names, type:)
21
+ names.map { |name| current_manifester_instance.manifest.lookup_pack_with_chunks!(name.to_s, type: type) }.flatten.uniq
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ module Manifester
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module Manifester
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Manifester
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Manifester</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "manifester/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ Manifester::Engine.routes.draw do
2
+ end
@@ -0,0 +1,19 @@
1
+ default: &default
2
+ public_root_path: public
3
+ public_output_path: packs
4
+
5
+ # Reload manifest.json on all requests so we reload latest compiled packs
6
+ cache_manifest: false
7
+
8
+ development:
9
+ <<: *default
10
+
11
+ test:
12
+ <<: *default
13
+ compile: true
14
+
15
+ production:
16
+ <<: *default
17
+
18
+ # Cache manifest.json for performance
19
+ cache_manifest: true
data/lib/manifester.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "manifester/version"
2
+ require "manifester/engine"
3
+
4
+ module Manifester
5
+ extend self
6
+
7
+ def instance=(instance)
8
+ @instance = instance
9
+ end
10
+
11
+ def instance
12
+ @instance ||= Manifester::Instance.new
13
+ end
14
+ end
@@ -0,0 +1,57 @@
1
+ require "yaml"
2
+ require "active_support/core_ext/hash/keys"
3
+ require "active_support/core_ext/hash/indifferent_access"
4
+
5
+ class Manifester::Configuration
6
+ attr_reader :root_path, :config_path, :env
7
+
8
+ def initialize(root_path:, config_path:, env:)
9
+ @root_path = root_path
10
+ @config_path = config_path
11
+ @env = env
12
+ end
13
+
14
+ def public_path
15
+ root_path.join(fetch(:public_root_path))
16
+ end
17
+
18
+ def public_output_path
19
+ public_path.join(fetch(:public_output_path))
20
+ end
21
+
22
+ def public_manifest_path
23
+ public_output_path.join("manifest.json")
24
+ end
25
+
26
+ def cache_manifest?
27
+ fetch(:cache_manifest)
28
+ end
29
+
30
+ private
31
+ def fetch(key)
32
+ data.fetch(key, defaults[key])
33
+ end
34
+
35
+ def data
36
+ @data ||= load
37
+ end
38
+
39
+ def load
40
+ YAML.load(config_path.read)[env].deep_symbolize_keys
41
+
42
+ rescue Errno::ENOENT => e
43
+ raise "Manifester configuration file not found #{config_path}. " \
44
+ "Please run rails manifester:install " \
45
+ "Error: #{e.message}"
46
+
47
+ rescue Psych::SyntaxError => e
48
+ raise "YAML syntax error occurred while parsing #{config_path}. " \
49
+ "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
50
+ "Error: #{e.message}"
51
+ end
52
+
53
+ def defaults
54
+ @defaults ||= \
55
+ HashWithIndifferentAccess.new(YAML.load_file(File.expand_path("../../install/config/manifester.yml", __FILE__))[env])
56
+ end
57
+ end
@@ -0,0 +1,15 @@
1
+ module Manifester
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Manifester
4
+
5
+ initializer "manifester.helper" do
6
+ ActiveSupport.on_load :action_controller do
7
+ ActionController::Base.helper Manifester::ApplicationHelper
8
+ end
9
+
10
+ ActiveSupport.on_load :action_view do
11
+ include Manifester::ApplicationHelper
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,39 @@
1
+ class Manifester::Env
2
+ DEFAULT = "production".freeze
3
+
4
+ delegate :config_path, :logger, to: :@manifester
5
+
6
+ def self.inquire(manifester)
7
+ new(manifester).inquire
8
+ end
9
+
10
+ def initialize(manifester)
11
+ @manifester = manifester
12
+ end
13
+
14
+ def inquire
15
+ fallback_env_warning if config_path.exist? && !current
16
+ current || DEFAULT.inquiry
17
+ end
18
+
19
+ private
20
+ def current
21
+ Rails.env.presence_in(available_environments)
22
+ end
23
+
24
+ def fallback_env_warning
25
+ logger.info "RAILS_ENV=#{Rails.env} environment is not defined in config/manifester.yml, falling back to #{DEFAULT} environment"
26
+ end
27
+
28
+ def available_environments
29
+ if config_path.exist?
30
+ YAML.load(config_path.read).keys
31
+ else
32
+ [].freeze
33
+ end
34
+ rescue Psych::SyntaxError => e
35
+ raise "YAML syntax error occurred while parsing #{config_path}. " \
36
+ "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
37
+ "Error: #{e.message}"
38
+ end
39
+ end
@@ -0,0 +1,25 @@
1
+ class Manifester::Instance
2
+ cattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }
3
+
4
+ attr_reader :root_path, :config_path
5
+
6
+ def initialize(root_path: Rails.root, config_path: Rails.root.join("config/manifester.yml"))
7
+ @root_path, @config_path = root_path, config_path
8
+ end
9
+
10
+ def env
11
+ @env ||= Manifester::Env.inquire self
12
+ end
13
+
14
+ def config
15
+ @config ||= Manifester::Configuration.new(
16
+ root_path: root_path,
17
+ config_path: config_path,
18
+ env: env
19
+ )
20
+ end
21
+
22
+ def manifest
23
+ @manifest ||= Manifester::Manifest.new self
24
+ end
25
+ end
@@ -0,0 +1,95 @@
1
+
2
+ class Manifester::Manifest
3
+ class MissingEntryError < StandardError; end
4
+
5
+ delegate :config, to: :@manifester
6
+
7
+ def initialize(manifester)
8
+ @manifester = manifester
9
+ end
10
+
11
+ def refresh
12
+ @data = load
13
+ end
14
+
15
+ def lookup_pack_with_chunks(name, pack_type = {})
16
+ manifest_pack_type = manifest_type(pack_type[:type])
17
+ manifest_pack_name = manifest_name(name, manifest_pack_type)
18
+ find("entrypoints")[manifest_pack_name]["assets"][manifest_pack_type]
19
+ rescue NoMethodError
20
+ nil
21
+ end
22
+
23
+ def lookup_pack_with_chunks!(name, pack_type = {})
24
+ lookup_pack_with_chunks(name, pack_type) || handle_missing_entry(name, pack_type)
25
+ end
26
+
27
+ # Computes the relative path for a given Manifester asset using manifest.json.
28
+ # If no asset is found, returns nil.
29
+ #
30
+ # Example:
31
+ #
32
+ # Manifester.manifest.lookup('calendar.js') # => "/packs/calendar-1016838bab065ae1e122.js"
33
+ def lookup(name, pack_type = {})
34
+ find(full_pack_name(name, pack_type[:type]))
35
+ end
36
+
37
+ # Like lookup, except that if no asset is found, raises a Manifester::Manifest::MissingEntryError.
38
+ def lookup!(name, pack_type = {})
39
+ lookup(name, pack_type) || handle_missing_entry(name, pack_type)
40
+ end
41
+
42
+ private
43
+ def data
44
+ if config.cache_manifest?
45
+ @data ||= load
46
+ else
47
+ refresh
48
+ end
49
+ end
50
+
51
+ def find(name)
52
+ data[name.to_s].presence
53
+ end
54
+
55
+ def full_pack_name(name, pack_type)
56
+ return name unless File.extname(name.to_s).empty?
57
+ "#{name}.#{manifest_type(pack_type)}"
58
+ end
59
+
60
+ def handle_missing_entry(name, pack_type)
61
+ raise Manifester::Manifest::MissingEntryError, missing_file_from_manifest_error(full_pack_name(name, pack_type[:type]))
62
+ end
63
+
64
+ def load
65
+ if config.public_manifest_path.exist?
66
+ JSON.parse config.public_manifest_path.read
67
+ else
68
+ {}
69
+ end
70
+ end
71
+
72
+ # The `manifest_name` method strips of the file extension of the name, because in the
73
+ # manifest hash the entrypoints are defined by their pack name without the extension.
74
+ # When the user provides a name with a file extension, we want to try to strip it off.
75
+ def manifest_name(name, pack_type)
76
+ return name if File.extname(name.to_s).empty?
77
+ File.basename(name, ".#{pack_type}")
78
+ end
79
+
80
+ def manifest_type(pack_type)
81
+ case pack_type
82
+ when :javascript then "js"
83
+ when :stylesheet then "css"
84
+ else pack_type.to_s
85
+ end
86
+ end
87
+
88
+ def missing_file_from_manifest_error(bundle_name)
89
+ <<-MSG
90
+ Manifester can't find #{bundle_name} in #{config.public_manifest_path}.
91
+ Your manifest contains:
92
+ #{JSON.pretty_generate(@data)}
93
+ MSG
94
+ end
95
+ end
@@ -0,0 +1,3 @@
1
+ module Manifester
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :manifester do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: manifester
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adrian Marin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-04-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ description: Manifester loads stylesheets and javascript assets from your manifest.json
28
+ file.
29
+ email:
30
+ - adrian@adrianthedev.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - MIT-LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - app/assets/config/manifester_manifest.js
39
+ - app/assets/stylesheets/manifester/application.css
40
+ - app/controllers/manifester/application_controller.rb
41
+ - app/helpers/manifester/application_helper.rb
42
+ - app/jobs/manifester/application_job.rb
43
+ - app/mailers/manifester/application_mailer.rb
44
+ - app/models/manifester/application_record.rb
45
+ - app/views/layouts/manifester/application.html.erb
46
+ - config/routes.rb
47
+ - lib/install/config/manifester.yml
48
+ - lib/manifester.rb
49
+ - lib/manifester/configuration.rb
50
+ - lib/manifester/engine.rb
51
+ - lib/manifester/env.rb
52
+ - lib/manifester/instance.rb
53
+ - lib/manifester/manifest.rb
54
+ - lib/manifester/version.rb
55
+ - lib/tasks/manifester_tasks.rake
56
+ homepage: https://avohq.io
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ homepage_uri: https://avohq.io
61
+ source_code_uri: https://github.com/avo-hq/manifester
62
+ changelog_uri: https://github.com/avo-hq/manifester
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.0.3
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Manifester loads stylesheets and javascript assets from your manifest.json
82
+ file.
83
+ test_files: []