Class: MicroservicePrecompiler::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_root = ".", build_path = "dist", mustaches_config = "mustaches.yml.tml") ⇒ Builder

Returns a new instance of Builder.


5
6
7
8
9
# File 'lib/microservice_precompiler/builder.rb', line 5

def initialize project_root = ".", build_path = "dist", mustaches_config = "mustaches.yml.tml"
  @project_root = project_root
  @build_path = File.join(@project_root, build_path)
  @mustaches_config = mustaches_config
end

Instance Attribute Details

#build_pathObject

Returns the value of attribute build_path.


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

def build_path
  @build_path
end

#mustaches_configObject

Returns the value of attribute mustaches_config.


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

def mustaches_config
  @mustaches_config
end

#project_rootObject

Returns the value of attribute project_root.


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

def project_root
  @project_root
end

Instance Method Details

#cleanup(sprocket_assets = [:javascripts, :stylesheets]) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/microservice_precompiler/builder.rb', line 18

def cleanup sprocket_assets = [:javascripts, :stylesheets]
  FileUtils.rm_r @build_path if File.exists?(@build_path)
  Compass::Exec::SubCommandUI.new(["clean", @project_root]).run!
  # Don't initialize Compass assets, the config will take care of it
  sprocket_assets.each do |asset|
    FileUtils.mkdir_p File.join(@build_path, asset.to_s)
  end
  mustaches_config_file = File.join(@project_root, @mustaches_config)
  if File.exists?(mustaches_config_file)
    mustaches_config = YAML.load_file(mustaches_config_file)
    if mustaches_config
      mustaches_config.each_key do |dir|
        FileUtils.mkdir_p File.join(@build_path, dir.to_s)
      end
    end
  end
end

#compass_buildObject Also known as: compass


36
37
38
# File 'lib/microservice_precompiler/builder.rb', line 36

def compass_build
  Compass::Exec::SubCommandUI.new(["compile", @project_root, "-s", "compact"]).run!
end

#compileObject


11
12
13
14
15
16
# File 'lib/microservice_precompiler/builder.rb', line 11

def compile
  self.cleanup
  self.compass_build
  self.sprockets_build
  self.mustache_build
end

#mustache_buildObject Also known as: mustache


63
64
65
66
67
68
69
70
71
72
# File 'lib/microservice_precompiler/builder.rb', line 63

def mustache_build
  mustaches_config_file = "#{@project_root}/#{@mustaches_config}"
  if File.exists?(mustaches_config_file)
    # Load up file as a hash
    mustaches_config = YAML.load_file(mustaches_config_file)
    if mustaches_config.is_a? Hash
      mustache_build_folder_structure(mustaches_config)
    end
  end
end

#sprockets_build(sprocket_assets = [:javascripts, :stylesheets]) ⇒ Object Also known as: sprockets


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/microservice_precompiler/builder.rb', line 41

def sprockets_build sprocket_assets = [:javascripts, :stylesheets]
  sprocket_assets.each do |asset_type|
    load_path = File.join(@project_root, asset_type.to_s)
    next unless File.exists?(load_path)
    sprockets_env.append_path load_path
    Dir.new(load_path).each do |filename|
      file = File.join(load_path, filename)
      if File.exists?(file) and File.file?(file)
        asset = sprockets_env[filename]
        attributes = sprockets_env.attributes_for(asset.pathname)
        name, format = attributes.logical_path, attributes.format_extension
        asset = minify(asset, format)
        build_file = File.join(@build_path, asset_type.to_s, name)
        File.open(build_file, 'w') do |f|
          f.write(asset)
        end
      end
    end
  end
end