Sha256: 1a1aef37ac6233401471f943dca14fc7103e99a75ad7f87f92b57bd7f6842696

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

require 'commandline' # for debug

module Watchful
	
	class Action
		
		# todo: support blocks for @in, @out, and @command
		
		PROPERTIES = [:name, :dependencies, :command, :in, :out]
		
		PROPERTIES.each { |k| attr_accessor k }
		
		@enabled = true
		def enabled?; @enabled ;end
		
		
		def initialize(options = {})
			
			options[:dependencies] ||= []
			
			PROPERTIES.each do |key|
				self.instance_variable_set('@' + key.to_s, options[key])
			end
			
			@dependencies = [@dependencies] if @dependencies.kind_of? String
			
			raise "Dependencies option must be a string or array" if not @dependencies.kind_of? Array
			
			[@in, @out].each do |i|
				@enabled = false unless i and i.instance_of? String and i.starts_with? '.'
			end
			
			@enabled = @enabled && self.has_dependencies?
			
		end
		
		def has_dependencies?
			return true if @dependencies.empty?
			have_all = @dependencies.any? do |d|
				(Action.have_command?(d)) || (File.exists?(File.expand_path(d)))
			end
			# todo: more detailed messages about missing dependencies
			debug "Missing dependencies for action \"#{@name}\"" unless have_all
			return have_all
		end
		
		def command_string(input_path)
			Kernel.sprintf(@command, input_path, self.output_path_for(input_path))
		end
		
		# todo: better have_command?
		def Action.have_command?(cmd)
			raise 'Argument must be a string' if not cmd.kind_of? String
			raise 'Argument cannot be an empty string' if cmd.empty?
			
			ENV['PATH'].split(':').each do |dir|
				return true if File.exists?("#{dir}/#{cmd}")
			end
			false
		end
		
		def output_path_for(source_path)
			return File.dirname(source_path) + '/' + File.basename(source_path, @in) + @out
		end
		
		# does the given path look like a path to which this action might write output?
		def output_file?(path)
			Watchful::compound_extension_of(path) == @in
		end
		
		# does the given path look like a path to which this action could be applied?
		def input_file?(path)
			Watchful::compound_extension_of(path) == @in
		end

	end
	
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
watchful-0.0.0.pre1 lib/watchful/action.rb