gakubuchi 1.3.0 → 1.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0d5aac84118b90bfe6ff298c120189aa4561e4cd
4
- data.tar.gz: f963f5d180b747be66a68bdbac981520ee3c3fad
3
+ metadata.gz: 0e79a372af60dae91f7a2800bf92105775443301
4
+ data.tar.gz: f5f85209a9cbb2d8d051f184e56daa6509ceb1a9
5
5
  SHA512:
6
- metadata.gz: ca7f0bbff7e8d4d0a1d72f7272f01ed37a5a2336249251882c675810091f7b60b63f2a3c72270c6ab841f71e694c936598d01ecaa875618a68220c234545065a
7
- data.tar.gz: 1a1127e8783cff92e2b546a7dcabad8f67d8dc6e77db62c852b768eb0c10a86e633e0e456eba5ec64074a3ebe7f040ab9d17f573d9789ca105a035901cdd3adc
6
+ metadata.gz: e5ad244af5a0a6d1294b953e9869252329c5b12a80c605956330dd91c273dbda10acf15531808d37380eb31f363a0f642b5442e2adf56a6a81850cdc40b9e1bc
7
+ data.tar.gz: a63cba1baa07b640d5ce4d2645cc5646133a30a9b7c5a1d418dac7b73d5424e63804df31a4ffa55b561010f06942194ca9085dea948f2cd67ba2e5bdb8579946
data/README.md CHANGED
@@ -17,13 +17,13 @@ gem 'gakubuchi'
17
17
  Run the installation generator with:
18
18
 
19
19
  ```shell
20
- rails generate gakubuchi:install
20
+ rails g gakubuchi:install
21
21
  ```
22
22
 
23
23
  Or specify the name of directory for static pages:
24
24
 
25
25
  ```shell
26
- rails generate gakubuchi:install -d html
26
+ rails g gakubuchi:install -d html
27
27
  ```
28
28
 
29
29
  They will install the initializer into `config/initializers/gakubuchi.rb` and create the directory in `app/assets`.
@@ -57,21 +57,21 @@ http://localhost:3000/assets/404.html
57
57
  Compile the templeate with:
58
58
 
59
59
  ```shell
60
- rake assets:precompile
60
+ rails assets:precompile
61
61
  ```
62
62
 
63
63
  This will generate `public/404.html`.
64
64
 
65
65
  ## Template engines
66
66
  Gakubuchi supports some template engines: `ERB`, `Haml` and `Slim`.
67
- If you want to use `Haml` or `Slim`, you need to put them in your Gemfile:
67
+ When you want to use `Haml` or `Slim`, you need to put them in your Gemfile:
68
68
 
69
69
  ```ruby
70
70
  # Use Haml
71
- gem 'haml-rails'
71
+ gem 'haml'
72
72
 
73
73
  # Use Slim
74
- gem 'slim-rails'
74
+ gem 'slim'
75
75
  ```
76
76
 
77
77
  ## Using assets
@@ -111,9 +111,9 @@ You should follow the steps below.
111
111
 
112
112
  1. [Fork the repository](https://help.github.com/articles/fork-a-repo/)
113
113
  2. Create a feature branch: `git checkout -b add-new-feature`
114
- 3. Commit your changes: `git commit -am 'add new feature'`
114
+ 3. Commit your changes: `git commit -am 'Add new feature'`
115
115
  4. Push the branch: `git push origin add-new-feature`
116
- 4. [Send us a pull request](https://help.github.com/articles/using-pull-requests/)
116
+ 4. [Send us a pull request](https://help.github.com/articles/about-pull-requests/)
117
117
 
118
118
  We use [Appraisal](https://github.com/thoughtbot/appraisal) to test with different combinations of
119
119
  [sprockets](https://github.com/rails/sprockets) and [sprockets-rails](https://github.com/rails/sprockets-rails).
@@ -123,7 +123,7 @@ bundle install
123
123
  appraisal install
124
124
 
125
125
  # Run RSpec with a specific combination
126
- appraisal 'sprockets-rails 3 with sprockets 3' rspec
126
+ appraisal 'sprockets-rails 3 with sprockets 4' rspec
127
127
 
128
128
  # Run RSpec with all combinations
129
129
  appraisal rspec
@@ -1,35 +1,5 @@
1
- require "fileutils"
2
- require "forwardable"
3
- require "logger"
4
- require "active_support/configurable"
5
-
1
+ # frozen_string_literal: true
6
2
  require "gakubuchi/configuration"
7
- require "gakubuchi/error"
8
- require "gakubuchi/fileutils"
3
+ require "gakubuchi/railtie"
9
4
  require "gakubuchi/task"
10
5
  require "gakubuchi/version"
11
-
12
- if defined?(::Rails::Railtie) && defined?(::Sprockets::Railtie)
13
- require "pathname"
14
- require "gakubuchi/engine_registrar"
15
- require "gakubuchi/template"
16
- require "gakubuchi/railtie"
17
- end
18
-
19
- module Gakubuchi
20
- class << self
21
- attr_writer :configuration
22
-
23
- def configuration
24
- @configuration ||= Configuration.new
25
- end
26
-
27
- def configure
28
- yield(configuration) if block_given?
29
- end
30
-
31
- def reset
32
- @configuration = nil
33
- end
34
- end
35
- end
@@ -1,4 +1,24 @@
1
+ # frozen_string_literal: true
2
+ require "active_support/configurable"
3
+ require "forwardable"
4
+
1
5
  module Gakubuchi
6
+ class << self
7
+ attr_writer :configuration
8
+
9
+ def configuration
10
+ @configuration ||= ::Gakubuchi::Configuration.new
11
+ end
12
+
13
+ def configure
14
+ yield(configuration) if block_given?
15
+ end
16
+
17
+ def reset
18
+ @configuration = nil
19
+ end
20
+ end
21
+
2
22
  class Configuration
3
23
  extend ::Forwardable
4
24
  include ::ActiveSupport::Configurable
@@ -1,30 +1,54 @@
1
+ # frozen_string_literal: true
2
+ require "active_support/inflector"
3
+ require "grease"
4
+ require "sprockets"
5
+
1
6
  module Gakubuchi
2
7
  class EngineRegistrar
8
+ EXTENSION_WITH_SIGLE_DOT = /\A\.[^\.]+\z/
9
+
3
10
  def initialize(env)
4
11
  @env = env
5
12
  end
6
13
 
7
- def register(target, engine)
8
- klass = constantize(engine)
9
- return false if !klass.instance_of?(::Class) || registered?(target)
14
+ def register(mime_type, engine_name_or_class)
15
+ engine = constantize(engine_name_or_class)
16
+ return false if !engine.instance_of?(::Class) || mime_type.extensions.empty?
10
17
 
11
- args = [target, klass]
12
- args << { silence_deprecation: true } if Sprockets::VERSION.start_with?("3")
18
+ if sprockets_major_version >= 4
19
+ register_as_transformer(mime_type, engine)
20
+ else
21
+ register_as_engine(mime_type, engine)
22
+ end
13
23
 
14
- @env.register_engine(*args)
15
24
  true
16
25
  end
17
26
 
18
- def registered?(target)
19
- @env.engines.key?(::Sprockets::Utils.normalize_extension(target))
20
- end
21
-
22
27
  private
23
28
 
24
- def constantize(klass)
25
- klass.to_s.constantize
29
+ def constantize(constant_name)
30
+ constant_name.to_s.constantize
26
31
  rescue ::LoadError, ::NameError
27
32
  nil
28
33
  end
34
+
35
+ def sprockets_major_version
36
+ @sprockets_major_version ||= ::Gem::Version.new(::Sprockets::VERSION).segments.first
37
+ end
38
+
39
+ def register_as_engine(mime_type, engine)
40
+ mime_type.extensions.select { |ext| ext =~ EXTENSION_WITH_SIGLE_DOT }.each do |ext|
41
+ args = [ext, engine]
42
+ args << { silence_deprecation: true } if sprockets_major_version == 3
43
+ @env.register_engine(*args)
44
+ end
45
+ end
46
+
47
+ def register_as_transformer(mime_type, engine)
48
+ content_type = mime_type.content_type
49
+
50
+ @env.register_mime_type(content_type, extensions: mime_type.extensions)
51
+ @env.register_transformer(content_type, engine.default_mime_type, ::Grease.apply(engine))
52
+ end
29
53
  end
30
54
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
1
2
  module Gakubuchi
2
- class Error < StandardError
3
- InvalidTemplate = Class.new(self)
3
+ class Error < ::StandardError
4
+ InvalidTemplate = ::Class.new(self)
5
+ InvalidMimeType = ::Class.new(self)
4
6
  end
5
7
  end
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require "fileutils"
3
+ require "logger"
4
+
1
5
  module Gakubuchi
2
6
  module FileUtils
3
7
  extend ::FileUtils
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+ require "gakubuchi/error"
3
+ require "set"
4
+
5
+ module Gakubuchi
6
+ class MimeType
7
+ CONTENT_TYPE_FORMAT = %r(\A[^/]+/[^/]+\z)
8
+
9
+ attr_reader :content_type, :extensions
10
+
11
+ def initialize(content_type, extensions: [])
12
+ unless content_type =~ CONTENT_TYPE_FORMAT
13
+ message = %(`#{content_type}' is invalid as Content-Type)
14
+ raise ::Gakubuchi::Error::InvalidMimeType, message
15
+ end
16
+
17
+ @content_type = content_type
18
+ @extensions = ::Set.new(extensions).map(&:to_s)
19
+ end
20
+ end
21
+ end
@@ -1,10 +1,26 @@
1
+ # frozen_string_literal: true
2
+ require "gakubuchi/engine_registrar"
3
+ require "gakubuchi/mime_type"
4
+ require "gakubuchi/template"
5
+ require "rails/railtie"
6
+ require "sprockets/railtie"
7
+
1
8
  module Gakubuchi
2
9
  class Railtie < ::Rails::Railtie
3
10
  config.assets.configure do |env|
4
- engine_registrar = EngineRegistrar.new(env)
11
+ engine_registrar = ::Gakubuchi::EngineRegistrar.new(env)
12
+
13
+ haml = ::Gakubuchi::MimeType.new("text/haml", extensions: %w(.haml .html.haml))
14
+ engine_registrar.register(haml, "Tilt::HamlTemplate")
15
+
16
+ slim = ::Gakubuchi::MimeType.new("text/slim", extensions: %w(.slim .html.slim))
17
+ engine_registrar.register(slim, "Slim::Template")
18
+ end
5
19
 
6
- engine_registrar.register(:haml, "::Tilt::HamlTemplate")
7
- engine_registrar.register(:slim, "::Slim::Template")
20
+ config.after_initialize do
21
+ # NOTE: Call #to_s for Sprockets 4 or later
22
+ templates = ::Gakubuchi::Template.all.map { |template| template.logical_path.to_s }
23
+ config.assets.precompile += templates
8
24
  end
9
25
 
10
26
  rake_tasks do
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+ require "gakubuchi/task"
3
+ require "rake"
4
+ require "rake/tasklib"
5
+ require "sprockets/railtie"
6
+
7
+ rake_tasks do
8
+ ::Gakubuchi::RakeTask.new do |task|
9
+ task.templates = %w(foo.slim bar.slim)
10
+ end
11
+ end
12
+
13
+ module Gakubuchi
14
+ class RakeTask < ::Rake::TaskLib
15
+ attr_accessor :templates
16
+
17
+ def initialize(target_task = "assets:precompile")
18
+ self.target_task = target_task
19
+ self.templates = []
20
+ self.verbose = true
21
+
22
+ yield self if block_given?
23
+ enhance_target_task
24
+ end
25
+
26
+ def task
27
+ @task ||= ::Gakubuchi::Task.new(templates, verbose: verbose)
28
+ end
29
+
30
+ private
31
+
32
+ def enhance_target_task
33
+ ::Rake::Task[target_task].enhance { task.execute! }
34
+ end
35
+ end
36
+ end
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require "gakubuchi/configuration"
3
+ require "gakubuchi/fileutils"
4
+
1
5
  module Gakubuchi
2
6
  class Task
3
7
  attr_reader :templates
@@ -12,9 +16,11 @@ module Gakubuchi
12
16
  next if src.nil?
13
17
 
14
18
  dest = template.destination_path
15
- FileUtils.copy_p(src, dest)
19
+ ::Gakubuchi::FileUtils.copy_p(src, dest)
16
20
 
17
- FileUtils.remove([src, *::Dir.glob("#{src}.gz")]) unless leave_digest_named_templates?
21
+ unless leave_digest_named_templates?
22
+ ::Gakubuchi::FileUtils.remove([src, *::Dir.glob("#{src}.gz")])
23
+ end
18
24
  end
19
25
  end
20
26
 
@@ -1,3 +1,11 @@
1
+ # frozen_string_literal: true
2
+ require "forwardable"
3
+ require "gakubuchi/configuration"
4
+ require "gakubuchi/error"
5
+ require "pathname"
6
+ require "rails"
7
+ require "sprockets/railtie"
8
+
1
9
  module Gakubuchi
2
10
  class Template
3
11
  extend ::Forwardable
@@ -28,9 +36,9 @@ module Gakubuchi
28
36
 
29
37
  case
30
38
  when !@extname.include?("html")
31
- fail Error::InvalidTemplate, "source path must refer to a template file"
39
+ raise ::Gakubuchi::Error::InvalidTemplate, "source path must refer to a template file"
32
40
  when !@source_path.fnmatch?(root.join("*").to_s)
33
- fail Error::InvalidTemplate, "template must exist in #{root}"
41
+ raise ::Gakubuchi::Error::InvalidTemplate, "template must exist in #{root}"
34
42
  end
35
43
  end
36
44
 
@@ -39,7 +47,8 @@ module Gakubuchi
39
47
  end
40
48
 
41
49
  def digest_path
42
- asset = assets.find_asset(logical_path)
50
+ # NOTE: Call #to_s for Sprockets 4 or later
51
+ asset = assets.find_asset(logical_path.to_s)
43
52
  return if asset.nil?
44
53
 
45
54
  ::Pathname.new(::File.join(::Rails.public_path, app.config.assets.prefix, asset.digest_path))
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Gakubuchi
2
- VERSION = "1.3.0".freeze
3
+ VERSION = "1.4.0".freeze
3
4
  end
@@ -1,3 +1,8 @@
1
+ # frozen_string_literal: true
2
+ require "gakubuchi/configuration"
3
+ require "pathname"
4
+ require "rails/generators/base"
5
+
1
6
  module Gakubuchi
2
7
  module Generators
3
8
  class InstallGenerator < ::Rails::Generators::Base
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require "gakubuchi/task"
3
+ require "gakubuchi/template"
4
+
1
5
  Rake::Task["assets:precompile"].enhance do
2
6
  task = Gakubuchi::Task.new(Gakubuchi::Template.all)
3
7
  task.execute!
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gakubuchi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yasaichi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-01 00:00:00.000000000 Z
11
+ date: 2017-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grease
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.3.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: railties
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -98,16 +112,16 @@ dependencies:
98
112
  name: reek
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
- - - "<"
115
+ - - ">="
102
116
  - !ruby/object:Gem::Version
103
- version: 4.0.0
117
+ version: '0'
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
- - - "<"
122
+ - - ">="
109
123
  - !ruby/object:Gem::Version
110
- version: 4.0.0
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: rspec-rails
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -154,16 +168,16 @@ dependencies:
154
168
  name: slim-rails
155
169
  requirement: !ruby/object:Gem::Requirement
156
170
  requirements:
157
- - - ">="
171
+ - - "<"
158
172
  - !ruby/object:Gem::Version
159
- version: '0'
173
+ version: 3.1.1
160
174
  type: :development
161
175
  prerelease: false
162
176
  version_requirements: !ruby/object:Gem::Requirement
163
177
  requirements:
164
- - - ">="
178
+ - - "<"
165
179
  - !ruby/object:Gem::Version
166
- version: '0'
180
+ version: 3.1.1
167
181
  - !ruby/object:Gem::Dependency
168
182
  name: sqlite3
169
183
  requirement: !ruby/object:Gem::Requirement
@@ -193,7 +207,9 @@ files:
193
207
  - lib/gakubuchi/engine_registrar.rb
194
208
  - lib/gakubuchi/error.rb
195
209
  - lib/gakubuchi/fileutils.rb
210
+ - lib/gakubuchi/mime_type.rb
196
211
  - lib/gakubuchi/railtie.rb
212
+ - lib/gakubuchi/rake_task.rb
197
213
  - lib/gakubuchi/task.rb
198
214
  - lib/gakubuchi/template.rb
199
215
  - lib/gakubuchi/version.rb