Sha256: 7a35d712b8a335767722086a8e9530bc09e795feac7e5c869ed495d131ce1b42

Contents?: true

Size: 1.82 KB

Versions: 4

Compression:

Stored size: 1.82 KB

Contents

require 'thor/shell'
require 'execution'
require 'preconditions'

module Docker_Sync
  module WatchStrategy
    class Fswatch
      include Thor::Shell
      include Execution
      include Preconditions

      @options
      @sync_name
      @watch_thread

      def initialize(sync_name, options)
        @sync_name = sync_name
        @options = options

        begin
          fswatch_available
        rescue Exception => e
          say_status 'error', e.message, :red
          exit 1
        end
      end

      def run
        watch
      end

      def stop
      end

      def clean
      end

      def watch
        args = watch_options
        say_status 'success', "Starting to watch #{@options['src']} - Press CTRL-C to stop", :green
        cmd = 'fswatch ' + args.join(' ')
        say_status 'command', cmd, :white if @options['verbose']

        # run a thread here, since it is blocking
        @watch_thread = threadexec(cmd, "Sync #{@sync_name}", :blue)
      end

      def watch_options
        args = []
        unless @options['watch_excludes'].nil?
          args = @options['watch_excludes'].map { |pattern| "--exclude='#{pattern}'" } + args
        end
        args.push('-orIE')
        args.push(@options['watch_args']) if @options.key?('watch_args')
        args.push(@options['src'])

        sync_command = 'thor sync:sync'
        begin
          docker_sync_available
          sync_command = 'docker-sync sync'
        rescue Exception => e
          say_status 'warning', 'docker-sync not available, assuming dev mode, using thor', :yellow
          puts e.message
          sync_command = 'thor sync:sync'
        end
        args.push(" | xargs -I -n1 #{sync_command} -n #{@sync_name} --config='#{@options['config_path']}'")
      end

      def watch_thread
        return @watch_thread
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
docker-sync-0.0.11 lib/docker_sync/watch_strategy/fswatch.rb
docker-sync-0.0.9 lib/docker_sync/watch_strategy/fswatch.rb
docker-sync-0.0.8 lib/docker_sync/watch_strategy/fswatch.rb
docker-sync-0.0.7 lib/docker_sync/watch_strategy/fswatch.rb