Class: Middleman::Cli::Build

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
middleman-cli/lib/middleman-cli/build.rb

Overview

The CLI Build class

Instance Method Summary collapse

Instance Method Details

#build

This method returns an undefined value.

Core build Thor command

Raises:

  • (Thor::Error)


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
# File 'middleman-cli/lib/middleman-cli/build.rb', line 72

def build
  root = ENV['MM_ROOT'] || Dir.pwd

  raise Thor::Error, 'Error: Could not find a Middleman project config, perhaps you are in the wrong folder?' unless File.exist?(File.join(root, 'config.rb'))

  require 'middleman-core'
  require 'middleman-core/logger'
  require 'middleman-core/builder'
  require 'fileutils'

  verbose = options['verbose'] ? 0 : 1
  instrument = options['instrument']

  builder = nil
  cli_options = options

  ::Middleman::Logger.singleton(verbose, instrument)

  ::Middleman::Util.instrument 'builder.setup' do
    missing_and_changed = !options['only_changed'] && options['missing_and_changed']
    should_track_dependencies = options['only_changed'] || missing_and_changed || options['track_dependencies']
    data_collection_depth = options['data_collection_depth']

    @app = ::Middleman::Application.new do
      config[:mode] = :build
      config[:show_exceptions] = false
      config[:cli_options] = cli_options.each_with_object({}) do |(k, v), sum|
        sum[k] = v
      end
      config[:track_data_access] = should_track_dependencies
      config[:data_collection_depth] = data_collection_depth
    end

    builder = Middleman::Builder.new(@app,
                                     glob: options['glob'],
                                     dry_run: options['dry_run'],
                                     clean: options['clean'],
                                     parallel: options['parallel'],
                                     only_changed: options['only_changed'],
                                     missing_and_changed: missing_and_changed,
                                     track_dependencies: should_track_dependencies,
                                     visualize_graph: options['visualize_graph'])
    builder.thor = self
    builder.on_build_event(&method(:on_event))
  end

  ::Middleman::Util.instrument 'builder.run' do
    if builder.run!
      clean_directories! if options['clean']
      puts 'Project built successfully.'
    else
      msg = 'There were errors during this build'
      msg << ', re-run with `middleman build --verbose` to see the full exception.' unless options['verbose']
      shell.say msg, :red

      exit(1)
    end
  end
end