Class: PurgecssRails::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/purgecss_rails/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(purge_css_path:) ⇒ Builder

Returns a new instance of Builder.



3
4
5
6
# File 'lib/purgecss_rails/builder.rb', line 3

def initialize(purge_css_path:)
  @purge_css_executable = purge_css_path
  @css_files = []
end

Instance Method Details

#ignore_filesObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/purgecss_rails/builder.rb', line 19

def ignore_files
  return @ignore_files if @ignore_files.present?
  @ignore_files = @css_files.select do |f|
    included = true
    @ignored_names.each do |ignored_name|
      if f.include?(ignored_name)
        break included = false
      end
    end
    included
  end
end

#match_html_files(*paths) ⇒ Object



14
15
16
17
# File 'lib/purgecss_rails/builder.rb', line 14

def match_html_files(*paths)
  @html_files_match = paths
  self
end

#optimize!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/purgecss_rails/builder.rb', line 32

def optimize!
  print "\n"
  ignore_files.each do |f|
    print "purging #{f}\n"
  end

  @result = `#{purge_css_executable} --css #{ignore_files.join(" ")} --content #{html_files_match.join(" ")}`
  @result = JSON.parse(result)

  result.each do |result_item|
    file_name = result_item["file"]
    File.write(file_name, result_item["css"])

    zipped = "#{file_name}.gz"
    print "zipping #{zipped}\n"

    Zlib::GzipWriter.open(zipped) do |gz|
      gz.mtime = File.mtime(file_name)
      gz.orig_name = file_name
      gz.write IO.binread(file_name)
    end
  end
  self
end

#refresh!Object



57
58
59
60
61
# File 'lib/purgecss_rails/builder.rb', line 57

def refresh!
  @css_files = []
  @html_files_match = []
  @ignore_files = []
end

#search_css_files(path, ignore: []) ⇒ Object



8
9
10
11
12
# File 'lib/purgecss_rails/builder.rb', line 8

def search_css_files(path, ignore: [])
  @css_files += Dir[path]
  @ignored_names = ignore
  self
end