2014-07-11 23:24:22 +02:00
|
|
|
class Middleman::Extensions::ExternalPipeline < ::Middleman::Extension
|
|
|
|
self.supports_multiple_instances = true
|
|
|
|
|
2014-07-24 06:08:20 +02:00
|
|
|
option :name, nil, 'The name of the pipeline', required: true
|
|
|
|
option :command, nil, 'The command to initialize', required: true
|
|
|
|
option :source, nil, 'Path to merge into sitemap', required: true
|
2014-07-11 23:24:22 +02:00
|
|
|
option :latency, 0.25, 'Latency between refreshes of source'
|
2015-12-14 07:12:11 +01:00
|
|
|
option :disable_background_execution, false, "Don't run the command in a separate background thread"
|
2014-07-11 23:24:22 +02:00
|
|
|
|
|
|
|
def initialize(app, config={}, &block)
|
|
|
|
super
|
|
|
|
|
2016-03-21 00:31:15 +01:00
|
|
|
return if app.mode?(:config)
|
|
|
|
|
2014-07-11 23:24:22 +02:00
|
|
|
require 'thread'
|
|
|
|
|
2015-12-08 23:46:03 +01:00
|
|
|
@watcher = app.files.watch :source,
|
|
|
|
path: File.expand_path(options[:source], app.root),
|
2016-01-20 22:34:12 +01:00
|
|
|
latency: options[:latency],
|
|
|
|
frontmatter: false
|
2014-07-11 23:24:22 +02:00
|
|
|
|
2015-12-08 23:46:03 +01:00
|
|
|
logger.info "== Executing: `#{options[:command]}`"
|
|
|
|
|
2015-12-14 07:12:11 +01:00
|
|
|
if app.build? || options[:disable_background_execution]
|
2015-12-10 02:03:55 +01:00
|
|
|
watch_command!
|
|
|
|
else
|
2014-07-11 23:24:22 +02:00
|
|
|
::Thread.new { watch_command! }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def watch_command!
|
|
|
|
::IO.popen(options[:command], 'r') do |pipe|
|
|
|
|
while buf = pipe.gets
|
2014-07-24 06:08:20 +02:00
|
|
|
without_newline = buf.sub(/\n$/, '')
|
2016-02-24 23:20:42 +01:00
|
|
|
logger.info "== External: #{without_newline}" unless without_newline.empty?
|
2014-07-11 23:24:22 +02:00
|
|
|
end
|
|
|
|
end
|
2015-12-10 02:08:58 +01:00
|
|
|
|
2016-02-18 05:41:48 +01:00
|
|
|
unless $?.success?
|
|
|
|
logger.error '== External: Command failed with non-zero exit status'
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
|
2015-12-10 02:08:58 +01:00
|
|
|
@watcher.poll_once!
|
2015-08-10 19:00:23 +02:00
|
|
|
rescue ::Errno::ENOENT => e
|
|
|
|
logger.error "== External: Command failed with message: #{e.message}"
|
|
|
|
exit(1)
|
2014-07-11 23:24:22 +02:00
|
|
|
end
|
|
|
|
end
|