Class: DockerSync::WatchStrategy::Fswatch

Inherits:
Object
  • Object
show all
Includes:
Execution, Thor::Shell
Defined in:
lib/docker-sync/watch_strategy/fswatch.rb

Instance Method Summary collapse

Methods included from Execution

#fork_exec, #thread_exec, #with_time

Constructor Details

#initialize(sync_name, options) ⇒ Fswatch

Returns a new instance of Fswatch.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 15

def initialize(sync_name, options)
  @sync_name = sync_name
  @options = options
  @events_to_watch = %w(AttributeModified Created Link MovedFrom MovedTo Renamed Removed Updated)

    unless Dependencies::Fswatch.available?
      begin
        Dependencies::Fswatch.ensure!
      rescue StandardError => e
        say_status 'error', e.message, :red
        exit 1
      end
      puts "please restart docker sync so the installation of fswatch takes effect"
      raise(UNSUPPORTED_OPERATING_SYSTEM)
    end

end

Instance Method Details

#cleanObject


40
41
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 40

def clean
end

#get_sync_cli_callObject


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 66

def get_sync_cli_call
  sync_command = 'thor sync:'
  case @options['cli_mode']
    when 'docker-sync'
      say_status 'ok','Forcing cli mode docker-sync',:yellow if @options['verbose']
      sync_command = 'docker-sync '
    when 'thor'
      say_status 'ok','Forcing cli mode thor',:yellow if @options['verbose']
      sync_command = 'thor sync:'
    else # 'auto' or any other, try to guss
      say_status 'ok','Cli mode is auto, selecting .. ',:white if @options['verbose']
      exec_name = File.basename($PROGRAM_NAME)
      if exec_name != 'thor'
        sync_command = 'docker-sync '
      else
        say_status 'warning', 'Called user thor, not docker-sync* wise, assuming dev mode, using thor', :yellow
      end
      say_status 'ok',".. #{sync_command}",:white if @options['verbose']
  end

  # append the actual operation
  return "#{sync_command}sync"
end

#runObject


33
34
35
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 33

def run
  watch
end

#stopObject


37
38
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 37

def stop
end

#watchObject


43
44
45
46
47
48
49
50
51
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 43

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 = thread_exec(cmd, "Sync #{@sync_name}", :blue)
end

#watch_forkObject


90
91
92
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 90

def watch_fork
  return nil
end

#watch_optionsObject


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 53

def watch_options
  args = []
  unless @options['watch_excludes'].nil?
    args = @options['watch_excludes'].map { |pattern| "--exclude='#{pattern}'" } + args
  end
  args.push('-orIE')
  args.push(@events_to_watch.map { |pattern| "--event #{pattern}" })
  args.push(@options['watch_args']) if @options.key?('watch_args')
  args.push("'#{@options['src']}'")
  sync_command = get_sync_cli_call
  args.push(" | xargs -I -n1 #{sync_command} -n #{@sync_name} --config='#{@options['config_path']}'")
end

#watch_threadObject


94
95
96
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 94

def watch_thread
  return @watch_thread
end