forked from AlfredoRamos/phpbb-ext-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
224 lines (185 loc) · 5.32 KB
/
Rakefile
File metadata and controls
224 lines (185 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# frozen_string_literal: true
require 'sassc'
require 'autoprefixer-rails'
require 'uglifier'
require 'oj'
require 'rubocop/rake_task'
require 'scss_lint/rake_task'
require 'logger'
$stdout.sync = $stderr.sync = true
# Logger
logger = Logger.new($stdout)
logger.datetime_format = '%F %T %:z'
logger.formatter = proc do |severity, datetime, _progname, msg|
"#{datetime} | #{severity} | #{msg}\n"
end
# Tests
RuboCop::RakeTask.new
SCSSLint::RakeTask.new
# Helper
class Helper
attr_reader :ext
def initialize(logger)
@logger = logger
json = Oj.load_file('composer.json')
@ext = json['name'].split('/')
end
def compile_scss(args = {})
unless args.key?(:output)
args[:output] = args[:input]
.sub(%r{^scss/}, '')
.sub(/\.scss$/, '.css')
end
@logger.info(format('Processing file: %<filename>s', filename: args[:input]))
@logger.info(format('Style: %<style>s', style: args[:style].to_s))
File.open(args[:output], 'w') do |f|
css = SassC::Engine.new(
File.read(args[:input]),
style: args[:style],
cache: false,
syntax: :scss,
filename: args[:input],
sourcemap: :none
).render
f.puts AutoprefixerRails.process(
css,
map: false,
cascade: false,
from: args[:input],
to: args[:output],
browsers: [
'>= 1%',
'last 1 major version',
'not dead',
'Chrome >= 45',
'Firefox >= 38',
'Edge >= 12',
'Explorer >= 10',
'iOS >= 9',
'Safari >= 9',
'Android >= 4.4',
'Opera >= 30'
]
).css
end
@logger.info(format('Generated file: %<filename>s', filename: args[:output]))
end
def minify_file(args = {})
args[:output] = minified_ext(args[:input]) unless args.key?(:output)
args[:path] = asset_path(args[:input]) unless args.key?(:path)
# Minify file
@logger.info(format('Processing file: %<filename>s', filename: args[:output]))
case args[:path]
when 'css'
File.open(args[:output], 'w') do |f|
css = File.read(args[:input])
f.puts SassC::Engine.new(
css,
style: :compressed,
cache: false,
syntax: :css,
filename: args[:output],
sourcemap: :none
).render
end
when 'js'
File.open(args[:output], 'w') do |f|
js = File.read(args[:input])
f.puts Uglifier.compile(
js,
comments: :none,
harmony: true,
output: {
quote_style: :single
}
)
end
else
@logger.error(format('Invalid path: %<directory>s', directory: args[:path]))
abort
end
end
def replace_asset_file(files = [], html = '')
files.each do |f|
namespace = twig_namespace(f)
next unless html.include?(namespace)
# Generate minified file
minify_file(input: f)
# Replace filename in template
html = html.gsub(namespace, minified_ext(namespace))
end
html
end
def base_dir
File.join('build', 'package', @ext.first, @ext.last)
end
def twig_namespace(file_path)
format(
'@%<vendor>s_%<extname>s/%<path>s/%<filename>s',
vendor: @ext.first,
extname: @ext.last,
path: asset_path(file_path),
filename: File.basename(file_path)
)
end
def asset_path(file_path)
File.extname(file_path).gsub('.', '')
end
def minified_ext(file_path)
ext = File.extname(file_path)
return file_path if file_path.end_with?(format('.min%<ext>s', ext: ext))
file_path.gsub(ext, format('.min%<ext>s', ext: ext))
end
end
namespace :build do
files = {
scss: Dir.glob('scss/styles/**/theme/css/*.scss'),
css: Dir.glob('styles/**/theme/css/*.css') + Dir.glob('adm/style/css/*.css'),
js: Dir.glob('styles/**/theme/js/*.js') + Dir.glob('adm/style/js/*.js')
}
# Exclude minified files
files[:css].delete_if { |file| file.end_with?('.min.css') }
files[:js].delete_if { |file| file.end_with?('.min.js') }
helper = Helper.new(logger)
desc 'Build CSS file'
task :css do
logger.info('Compiling CSS files')
files[:scss].each do |file|
helper.compile_scss(
input: file,
style: :expanded
)
end
end
desc 'Minify assets'
task :minify do
logger.info('Minifying assets')
base_dir = helper.base_dir
unless Dir.exist?(base_dir)
logger.fatal(format('Directory not found: %<directory>s', directory: base_dir))
abort
end
Dir.chdir(base_dir) do
template = Dir.glob('styles/**/template/**/*.html') + Dir.glob('adm/style/**/*.html')
template.each do |file|
html = old_html = File.read(file)
# Replace assets (CSS and JS)
html = helper.replace_asset_file(files[:css], html)
html = helper.replace_asset_file(files[:js], html)
next if html.eql?(old_html)
# Update template file
logger.warn(format('Overwritting file: %<filename>s', filename: file))
File.open(file, 'w') { |f| f.puts html }
unless File.size(file).positive?
logger.fatal(format('Generated empty file: %<filename>s', filename: file))
abort
end
end
end
end
desc 'Build assets'
task :all do
Rake::Task['build:css'].invoke
Rake::Task['build:minify'].invoke
end
end