Module: Conveyor::Workers::Syntax

Included in:
Conveyor::Worker
Defined in:
lib/conveyor/workers/syntax.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, value = nil) ⇒ Object

Will return a string of any method returned that isn’t handled ex: match extension txt => match(extension(“foo”))



7
8
9
# File 'lib/conveyor/workers/syntax.rb', line 7

def method_missing(method, value = nil)
  return method.to_s
end

Instance Method Details

#anyObject

Returns a glob string that will match any file



22
23
24
# File 'lib/conveyor/workers/syntax.rb', line 22

def any
  '*'
end

#chdir(dir, &block) ⇒ Object

Change current working directory, optionally takes a block NOTE: Consider removing this as it can cause problems with threaded workers



55
56
57
# File 'lib/conveyor/workers/syntax.rb', line 55

def chdir(dir, &block)
  Dir.chdir(File.expand_path(dir), &block)
end

#copy(src = [], dest = nil) ⇒ Object

Copy files to destination



101
102
103
104
105
106
107
108
# File 'lib/conveyor/workers/syntax.rb', line 101

def copy(src = [], dest = nil)
  destination = dest unless dest.nil?
  source = src unless src.empty?

  if source && destination
    verified_copy(source, destination)
  end      
end

#delete(files) ⇒ Object

Deletes passed in files



84
85
86
87
88
89
90
91
92
# File 'lib/conveyor/workers/syntax.rb', line 84

def delete(files)
  # sync before we delete
  sync
  Array.wrap(files).each do |f|
    info "removing #{f}"
    FileUtils.rm(f)
    error "#{f} wasn't removed" if File.exists?(f)
  end
end

#extension(glob) ⇒ Object

Returns an extension glob string for passed in string



17
18
19
# File 'lib/conveyor/workers/syntax.rb', line 17

def extension(glob)
  "*.#{glob}"
end

#file(glob) ⇒ Object

Returns a recursive file glob string for the passed in string



12
13
14
# File 'lib/conveyor/workers/syntax.rb', line 12

def file(glob)
  "**/#{glob}"
end

#filenameObject

Returns the filename of the file that triggered the job



34
35
36
# File 'lib/conveyor/workers/syntax.rb', line 34

def filename
  @filename
end

#like(name) ⇒ Object

Returns a list of files that have the same basename but different extension in the same directory



28
29
30
31
# File 'lib/conveyor/workers/syntax.rb', line 28

def like(name)
  dir = File.dirname(name)
  Dir.glob(File.join(dir, File.basename(name, '.*') + '.*'))
end

#match(glob) {|@filename| ... } ⇒ Object

Match string/glob for the files that should trigger file change events

Yields:



44
45
46
# File 'lib/conveyor/workers/syntax.rb', line 44

def match(glob, &block)
  yield @filename
end

#mkdir(dir) ⇒ Object

Create a new directory



95
96
97
98
# File 'lib/conveyor/workers/syntax.rb', line 95

def mkdir(dir)
  FileUtils.mkdir_p(File.expand_path(dir))      
  @status.fail! unless File.exists?(File.expand_path(dir))
end

#move(src = [], dest = nil) ⇒ Object

Move files to destination



111
112
113
114
115
116
117
118
# File 'lib/conveyor/workers/syntax.rb', line 111

def move(src=[], dest = nil)
  destination = dest unless dest.nil?
  source = src unless src.empty?

  if source && destination
    verified_move(source, destination)
  end
end

#run(*cmd) ⇒ Object

Run the system command, and make sure that any errors are caught and returne up the status change



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/conveyor/workers/syntax.rb', line 61

def run(*cmd)
  opts = cmd.extract_options!
  begin
    info cmd.join(' ') unless opts[:quiet]
    output,err,thr = Open3.capture3(Array.wrap(cmd).join(' '))
    info output.chomp unless output.chomp.length == 0
if thr.success?
	if err.chomp.length > 0
		warning "Error output recieved, but no error code recieved"
		warning err.chomp
	end
else
    	error "Error running: `#{cmd.join(' ')}`", err.chomp 
    	@status.fail!
end
  
    return thr.success?
  rescue => e
    error e.class, e.message, e.backtrace.join("\n")
  end
end

#scp(src, dest) ⇒ Object

Scp files to destination See: man scp



122
123
124
# File 'lib/conveyor/workers/syntax.rb', line 122

def scp(src, dest)
  run "scp #{Array.wrap(src).join(' ')} #{dest}"
end

#syncObject

Run the system sync command



49
50
51
# File 'lib/conveyor/workers/syntax.rb', line 49

def sync
  run 'sync', :quiet => true
end

#watch(*args, &block) ⇒ Object

Which directories to watch for file change events.



39
40
41
# File 'lib/conveyor/workers/syntax.rb', line 39

def watch(*args, &block)
  yield
end