Sha256: a5e286dde4879b0f724eef0e50339157df07c39ed2349488174c94b1c60061f9

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

require 'listen'

require 'mwc/utils/command'
require 'mwc/tasks'
require 'mwc'

module Mwc
  module Commands
    # :nodoc:
    class Watch < Thor::Group
      include Thor::Actions
      include Utils::Command

      name 'watch'
      description 'watch src changes and auto re-compile'
      add_option :format, default: 'html', enum: %w[html js wasm]

      DESIRE_FILES = /\.(h|c|cpp|js)$/.freeze

      def prepare
        @stopped = false
        @dirs = [
          Mwc.root.join('src'),
          Mwc.root.join('include')
        ].map(&:to_s)
      end

      def setup_tasks
        Tasks.new
      end

      def setup_listener
        @listener = Listen.to(*@dirs, only: DESIRE_FILES) do |*_|
          Mwc.use(parent_options['env']) do
            # TODO: Allow change output directory
            empty_directory('dist')

            compile
          end
        end
      end

      def start
        puts 'Starting watch file changes...'
        @listener.start

        Signal.trap(:INT) { exit }
        sleep
      end

      private

      def task
        Rake::Task[parent_options['format']]
      end

      def compile
        task.invoke
        puts 'Compiled!'
      rescue RuntimeError
        puts 'Compile Failed'
      ensure
        reset
      end

      def reset
        task.all_prerequisite_tasks.each(&:reenable)
        task.reenable
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mwc-0.4.0 lib/mwc/commands/watch.rb