Permalink
Browse files

Autogessing code type and support for setting it in workerfiles.

  • Loading branch information...
1 parent 2c8e476 commit 8847731a9a1dafba58dcbeab93fe021edc3b0098 Andrew Kirilenko committed May 2, 2012
View
@@ -57,9 +57,13 @@ if command == 'codes.create'
exit 1
end
- runtime = runtimes[0][:name] if runtime.nil?
+ code = nil
- code = runtimes.find { |r| r[:name] == runtime }[:klass].new(:name => name)
+ if code.nil?
+ code = IronWorkerNG::Code.create(:name => name)
+ else
+ code = runtimes.find { |r| r[:name] == runtime }[:klass].new(:name => name)
+ end
execute_features.each do |f|
code.send(f[:name], *f[:args])
@@ -7,8 +7,59 @@
module IronWorkerNG
module Code
+ def self.create(*args, &block)
+ IronWorkerNG::Code::Base.create(*args, &block)
+ end
+
+ class RuntimeFinder
+ def initialize(*args, &block)
+ @runtime = nil
+
+ name = nil
+ exec_path = nil
+
+ if args.length == 1 && args[0].class == String
+ exec_path = args[0]
+ elsif args.length == 1 && args[0].class == Hash
+ name = args[0][:name] || args[0]['name']
+
+ exec_path = args[0][:exec] || args[0]['exec'] || args[0][:worker] || args[0]['worker']
+ end
+
+ if args.length == 1 && args[0].class == Hash && ((not args[0][:workerfile].nil?) || (not args[0]['workerfile'].nil?))
+ eval(File.read(File.expand_path(args[0][:workerfile] || args[0]['workerfile'])))
+ else
+ unless exec_path.nil?
+ name ||= IronWorkerNG::Code::Base.guess_name(exec_path)
+ end
+
+ if (not name.nil?) && File.exists?(name + '.worker')
+ eval(File.read(name + '.worker'))
+ elsif File.exists?('Workerfile')
+ eval(File.read('Workerfile'))
+ end
+ end
+
+ unless block.nil?
+ instance_eval(&block)
+ end
+ end
+
+ def runtime(runtime = nil)
+ @runtime = runtime if @runtime.nil?
+
+ @runtime
+ end
+
+ # prevent Kernel.exec
+ def exec(exec = nil)
+ end
+
+ def method_missing(name, *args, &block)
+ end
+ end
+
class Base
- attr_accessor :name
attr_reader :features
@@registered_types = []
@@ -34,6 +85,16 @@ def self.register_feature(feature)
include IronWorkerNG::Feature::Common::MergeFile::InstanceMethods
include IronWorkerNG::Feature::Common::MergeDir::InstanceMethods
+ def self.guess_name(path)
+ File.basename(path).gsub(/\..*$/, '').capitalize.gsub(/_./) { |x| x[1].upcase }
+ end
+
+ def self.create(*args, &block)
+ runtime = IronWorkerNG::Code::RuntimeFinder.new(*args, &block).runtime || 'ruby'
+
+ IronWorkerNG::Code::Base.registered_types.find { |r| r[:name] == runtime }[:klass].new(*args, &block)
+ end
+
def initialize(*args, &block)
@name = nil
@features = []
@@ -50,18 +111,24 @@ def initialize(*args, &block)
if args.length == 1 && args[0].class == Hash && ((not args[0][:workerfile].nil?) || (not args[0]['workerfile'].nil?))
eval(File.read(File.expand_path(args[0][:workerfile] || args[0]['workerfile'])))
else
- if (not @name.nil?) && File.exists?(@name + '.worker')
- eval(File.read(@name + '.worker'))
+ unless @exec.nil?
+ @name ||= IronWorkerNG::Code::Base.guess_name(@exec.path)
end
- if File.exists?('Workerfile')
+ if (not @name.nil?) && File.exists?(@name + '.worker')
+ eval(File.read(@name + '.worker'))
+ elsif File.exists?('Workerfile')
eval(File.read('Workerfile'))
end
end
unless block.nil?
instance_eval(&block)
end
+
+ unless @exec.nil?
+ @name ||= IronWorkerNG::Code::Base.guess_name(@exec.path)
+ end
end
def fixate
@@ -107,10 +174,19 @@ def create_zip
def create_runner(zip)
end
- def runtime
+ def runtime(runtime = nil)
nil
end
+ # support setting name throgh func call for workerfile
+ def name(name = nil)
+ @name ||= name
+ end
+
+ def name=(name)
+ @name = name
+ end
+
def runner
nil
end
@@ -29,7 +29,7 @@ def create_runner(zip)
end
end
- def runtime
+ def runtime(runtime = nil)
'sh'
end
@@ -41,7 +41,7 @@ def create_runner(zip)
end
end
- def runtime
+ def runtime(runtime = nil)
'sh'
end
@@ -27,7 +27,7 @@ def create_runner(zip)
end
end
- def runtime
+ def runtime(runtime = nil)
'sh'
end
@@ -212,14 +212,8 @@ def iron_task_id
require '#{File.basename(@exec.path)}'
-exec_class = nil
-
-begin
+unless #{@exec.klass == nil}
exec_class = Kernel.const_get('#{@exec.klass}')
-rescue
-end
-
-unless exec_class.nil?
exec_inst = exec_class.new
params.keys.each do |param|
@@ -236,7 +230,7 @@ def iron_task_id
end
end
- def runtime
+ def runtime(runtime = nil)
'ruby'
end
@@ -34,8 +34,6 @@ def merge_exec(path)
return
end
- @name ||= File.basename(path).gsub(/\..*$/, '').capitalize.gsub(/_./) { |x| x[1].upcase }
-
@exec = IronWorkerNG::Feature::Binary::MergeExec::Feature.new(path)
IronCore::Logger.info 'IronWorkerNG', "Merging binary exec with path='#{path}'"
@@ -40,12 +40,6 @@ def merge_exec(path, klass = nil)
return
end
- if klass.nil?
- @name ||= File.basename(path).gsub(/\.jar$/, '').capitalize.gsub(/_./) { |x| x[1].upcase }
- else
- @name ||= klass.split('.')[-1]
- end
-
@exec = IronWorkerNG::Feature::Java::MergeExec::Feature.new(path, klass)
IronCore::Logger.info 'IronWorkerNG', "Merging java exec with path='#{path}' and class='#{klass}'"
@@ -34,8 +34,6 @@ def merge_exec(path)
return
end
- @name ||= File.basename(path).gsub(/\.js$/, '').capitalize.gsub(/_./) { |x| x[1].upcase }
-
@exec = IronWorkerNG::Feature::Node::MergeExec::Feature.new(path)
IronCore::Logger.info 'IronWorkerNG', "Merging node exec with path='#{path}'"
@@ -31,17 +31,11 @@ module InstanceMethods
def merge_exec(path, klass = nil)
@exec ||= nil
- if klass == nil
- klass = File.basename(path).gsub(/\.rb$/, '').capitalize.gsub(/_./) { |x| x[1].upcase }
- end
-
unless @exec.nil?
IronCore::Logger.warn 'IronWorkerNG', "Ignoring attempt to merge ruby exec with path='#{path}' and class='#{klass}'"
return
end
- @name ||= klass
-
@exec = IronWorkerNG::Feature::Ruby::MergeExec::Feature.new(path, klass)
IronCore::Logger.info 'IronWorkerNG', "Merging ruby exec with path='#{path}' and class='#{klass}'"

0 comments on commit 8847731

Please sign in to comment.