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_filename = "mustaches.yml.tml") ⇒ Builder

Returns a new instance of Builder.



5
6
7
8
9
10
11
12
13
14
# File 'lib/microservice_precompiler/builder.rb', line 5

def initialize(project_root = ".", build_path = "dist", mustaches_filename = "mustaches.yml.tml")
  @project_root = project_root
  # Compare project and build path without trailing slash,
  # they cannot be the same else `cleanup` will wipe the current directory
  if project_root.chomp("/") == build_path.chomp("/")
    raise ArgumentError, "project_root and build_path cannot be the same"
  end
  @build_path = File.join(project_root, build_path)
  @mustaches_filename = mustaches_filename
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/microservice_precompiler/builder.rb', line 87

def method_missing(method_id, *args)
  if match = matches_file_exists_check?(method_id)
    File.exists?(send(method_id.to_s.gsub(/_exists\?/,"")))
  else
    super
  end
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_filenameObject

Returns the value of attribute mustaches_filename.



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

def mustaches_filename
  @mustaches_filename
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

Public function for running cleanup of previous build



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/microservice_precompiler/builder.rb', line 32

def cleanup(sprocket_assets = [:javascripts, :stylesheets])
  # Remove previous dist path
  FileUtils.rm_r build_path if File.exists?(build_path)
  # Clean compass project
  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
  if mustaches_config_file_exists?
    mustaches_yaml.each_key do |dir|
      FileUtils.mkdir_p File.join(build_path, dir.to_s)
    end
  end
end

#compass_buildObject Also known as: compass

Public function for wrapping a compass compiler



49
50
51
# File 'lib/microservice_precompiler/builder.rb', line 49

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

#compileObject

Convenience method runs all the compilation tasks in order



24
25
26
27
28
29
# File 'lib/microservice_precompiler/builder.rb', line 24

def compile
  cleanup
  compass_build
  sprockets_build
  mustache_build
end

#mustache_buildObject Also known as: mustache

Public function for building mustache tree into html



78
79
80
81
82
83
84
# File 'lib/microservice_precompiler/builder.rb', line 78

def mustache_build
  if mustaches_config_file_exists?
    if mustaches_yaml.is_a? Hash
      mustache_build_folder_structure(mustaches_yaml)
    end
  end
end

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

Public function for building sprockets assets and minifying



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/microservice_precompiler/builder.rb', line 55

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.file?(file)
        asset = sprockets_env[filename]
        attributes = sprockets_env.find_asset(asset.pathname)
        # logical_path is the filename
        build_file = File.join(build_path, asset_type.to_s, attributes.logical_path)
        File.open(build_file, 'w') do |f|
          extension = attributes.logical_path.split(".").last
          f.write(minify(asset, extension))
        end
      end
    end
  end
end