Class: Middleman::Extensions::ExternalPipeline

Inherits:
Middleman::Extension show all
Defined in:
middleman-core/lib/middleman-core/extensions/external_pipeline.rb

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER

Instance Attribute Summary

Attributes inherited from Middleman::Extension

#app, #options

Instance Method Summary collapse

Methods inherited from Middleman::Extension

activated_extension, #add_exposed_to_context, #after_build, #after_configuration, after_extension_activated, #after_extension_activated, #before_build, #before_configuration, clear_after_extension_callbacks, config, define_setting, expose_to_application, expose_to_config, expose_to_template, global_config, helpers, #manipulate_resource_list, option, #ready, resources

Methods included from Contracts

#Contract

Constructor Details

#initialize(app, options_hash = ::Middleman::EMPTY_HASH, &block) ⇒ ExternalPipeline

Returns a new instance of ExternalPipeline.


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
# File 'middleman-core/lib/middleman-core/extensions/external_pipeline.rb', line 53

def initialize(app, options_hash = ::Middleman::EMPTY_HASH, &block)
  super

  return if app.mode?(:config)

  require 'servolux'
  require 'fileutils'

  source_path = File.expand_path(options[:source], app.root)

  # Make sure it exists, or `listen` will explode.
  ::FileUtils.mkdir_p(source_path)

  @watcher = app.files.watch :source,
                             path: source_path,
                             latency: options[:latency],
                             frontmatter: false

  @current_thread = nil
  app.reload(&method(:reload!))

  logger.info "== Executing: `#{options[:command]}`"

  if app.build? || options[:disable_background_execution]
    watch_command!(false)

    @watcher.poll_once!
  else
    watch_command!(true)
  end
end

Instance Method Details

#reload!Object


85
86
87
88
89
90
91
92
# File 'middleman-core/lib/middleman-core/extensions/external_pipeline.rb', line 85

def reload!
  return unless @current_thread

  logger.info "== Stopping: `#{options[:command]}`"

  @current_thread.stop
  @current_thread = nil
end

#watch_command!(async) ⇒ Object


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
# File 'middleman-core/lib/middleman-core/extensions/external_pipeline.rb', line 94

def watch_command!(async)
  @current_thread = ::Servolux::Child.new(
    command: options[:command],
    suspend: 2
  )

  @current_thread.start

  watch_thread = Thread.new do
    buf = @current_thread.io.gets

    while buf
      without_newline = buf.sub(/\n$/, '')
      logger.info "== External: #{without_newline}" unless without_newline.empty?
      buf = @current_thread.io.gets
    end

    @current_thread.wait

    if !options[:ignore_exit_code] && !@current_thread.exitstatus.nil? && @current_thread.exitstatus != 0
      logger.error '== External: Command failed with non-zero exit status'
      exit(1)
    end
  end

  watch_thread.join unless async
rescue ::Errno::ENOENT => e
  logger.error "== External: Command failed with message: #{e.message}"
  exit(1)
end