Add nanoc/ruledsl/require_rules.rb

This commit is contained in:
0x1eef 2024-05-02 10:14:04 -03:00
parent c4593eac90
commit 4824d3cada
3 changed files with 29 additions and 22 deletions

17
Rules
View file

@ -6,11 +6,14 @@ require "ryo/json"
require "nanoc-gzip"
require "nanoc-webpack"
require "nanoc-tidy"
require_relative "nanoc/lib/require_rules"
Nanoc::RuleDSL::CompilationRuleContext.prepend(Nanoc::RuleDSL::Filter)
##
# Common vars
# DSL extensions
Nanoc::RuleDSL::CompilationRuleContext.prepend(Nanoc::RuleDSL::Filter)
Nanoc::RuleDSL::CompilerDSL.prepend(Nanoc::RuleDSL::RequireRules)
##
# variables
locales = %w[ar en]
json_dir = File.join(Dir.getwd, "src", "json")
name_by_id = Ryo.from_json_file("#{json_dir}/nameById.json")
@ -18,7 +21,7 @@ tdata = Ryo.from_json_file("#{json_dir}/t.json")
buildenv = ENV["buildenv"] || "development"
##
# Plugins
# plugins
Nanoc::Webpack
.default_argv
.concat ["--config", "webpack.#{buildenv}.js"]
@ -27,7 +30,7 @@ Nanoc::Tidy
.concat ["-upper"]
##
# See packages/typescript/postman
# see packages/typescript/postman
compile "/css/vendor/postman.scss" do
filter :sass, syntax: :scss, style: :compact
filter :rainpress
@ -49,7 +52,7 @@ compile "/robots.txt" do
end
##
# Require rules
# require rules
require_rules "nanoc/rules/assets"
require_rules "nanoc/rules/redirect"
require_rules "nanoc/rules/random", {locales:}
@ -57,6 +60,6 @@ require_rules "nanoc/rules/stream", {locales:, tdata:, name_by_id:}
require_rules "nanoc/rules/index", {locales:, tdata:}
##
# Defaults
# defaults
compile("/**/*") { write(nil) }
layout("**/*", :erb)

View file

@ -1,15 +0,0 @@
def require_rules(rules, locals = {}, target = binding)
locals.each { target.local_variable_set(_1, _2) }
path = File.join(Dir.getwd, rules)
target.eval(
if File.readable?(path)
File.read(path)
elsif File.readable?("#{path}.rb")
File.read("#{path}.rb")
elsif File.readable?("#{path}.rules")
File.read("#{path}.rules")
else
raise LoadError, "#{path} is not readable"
end
)
end

View file

@ -0,0 +1,19 @@
module Nanoc::RuleDSL
module RequireRules
def require_rules(rules, locals = {}, target = binding)
locals.each { target.local_variable_set(_1, _2) }
path = File.join(Dir.getwd, rules)
target.eval(
if File.readable?(path)
File.read(path)
elsif File.readable?("#{path}.rb")
File.read("#{path}.rb")
elsif File.readable?("#{path}.rules")
File.read("#{path}.rules")
else
raise LoadError, "#{path} is not readable"
end
)
end
end
end