File: target.rb

package info (click to toggle)
jruby 1.5.1-1%2Bdeb6u1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze-lts
  • size: 47,024 kB
  • ctags: 74,144
  • sloc: ruby: 398,155; java: 169,506; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (120 lines) | stat: -rw-r--r-- 2,884 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require 'java'
require 'ant/ant'

class Ant
  java_import org.apache.tools.ant.Target

  class RakeTarget < Target
    ALREADY_DEFINED_PREFIX = "rake_"

    def initialize(ant, rake_task)
      super()
      set_project ant.project
      set_name generate_unique_target_name rake_task.name

      rake_task.prerequisites.each { |prereq| add_dependency prereq }

      @rake_task = rake_task
    end

    def execute
      @rake_task.execute
    end

    private
    def generate_unique_target_name(name)
      # FIXME: This is not guaranteed to be unique and may be a wonky naming convention?
      if project.targets.get(name)
        project.log "ant already defines #{name}.  Redefining as #{ALREADY_DEFINED_PREFIX}#{name}"
        name = ALREADY_DEFINED_PREFIX + name
      end
      name
    end
  end

  class BlockTarget < Target
    def initialize(ant, *options, &block)
      super()
      set_project ant.project
      hash = extract_options(options)
      hash.each_pair {|k,v| send("set_#{k}", v) }
      @ant, @block = ant, block
    end

    def execute
      # Have to dupe this logic b/c Ant doesn't provide a way to
      # override inner part of execute
      if_cond, unless_cond = if_condition, unless_condition
      if if_cond && unless_cond
        execute_target
      elsif !if_cond
        project.log(self, "Skipped because property '#{if_cond}' not set.", Project::MSG_VERBOSE)
      else
        project.log(self, "Skipped because property '#{unless_cond}' set.", Project::MSG_VERBOSE)
      end
    end

    def defined_tasks
      define_target.tasks
    end

    private
    def extract_options(options)
      hash = Hash === options.last ? options.pop : {}
      hash[:name] = options[0].to_s if options[0]
      hash[:description] = options[1].to_s if options[1]
      hash
    end

    def if_condition
      cond = get_if
      return true unless cond
      val = project.replace_properties(cond)
      project.get_property(val) && val
    end

    def unless_condition
      cond = get_unless
      return true unless cond
      val = project.replace_properties(cond)
      project.get_property(val).nil? && val
    end

    def execute_target
      @ant.instance_eval(&@block) if @block
    end

    def define_target
      Target.new.tap do |t|
        t.name = ""
        begin
          @ant.current_target = t
          execute_target
        ensure
          @ant.current_target = nil
        end
      end
    end
  end

  class TargetWrapper
    def initialize(project, name)
      @project, @name = project, name
    end

    def execute
      @project.execute_target(@name)
    end
  end

  class MissingWrapper
    def initialize(project, name)
      @project_name = project.name || "<anonymous>"
      @name = name
    end

    def execute
      raise "Target `#{@name}' does not exist in project `#{@project_name}'"
    end
  end
end