Module: Pindah

Extended by:
Rake::DSL
Defined in:
lib/pindah.rb

Constant Summary collapse

VERSION =
'0.1.3'
DEFAULTS =
{
  :output => File.expand_path("bin"),
  :src => File.expand_path("src"),
  :classpath => Dir["jars/*jar", "libs/*jar"],
  :sdk => Pindah.infer_sdk_location(ENV["PATH"])
}
TARGETS =
{
  "1.5"   => 3,
  "1.6"   => 4,
  "2.1"   => 7,
  "2.2"   => 8,
  "2.3"   => 9, "2.3.1" => 9, "2.3.2" => 9,
  "2.3.3" => 10, "2.3.4" => 10,
  "3.0"   => 11,
  "3.1"   => 12,
  "3.2"   => 13,
  "4.0"   => 14, "4.0.1" => 14, "4.0.2" => 14,
  "4.0.3" => 15, "4.0.4" => 15,
  "4.1"   => 16, "4.1.1" => 16, "4.1.2" => 16,
  "4.2"   => 17, "4.2.2" => 17,
  "4.3"   => 18,
  "4.4"   => 19
}
ANT_TASKS =
["javac", "compile", "clean", "debug", "release",
"release_unsigned", "instrument", "test", "install", "installd",
"installr", "installi", "installt", "uninstall"]
SIGNED_TASKS =
["release"]
ANT_TASK_MAP =
(Hash.new { |h, k| h[k] = k }).merge({ "release_unsigned" => "release" })

Class Method Summary collapse

Class Method Details

.ant_setupObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/pindah.rb', line 102

def self.ant_setup
  @ant = Ant.new

  # TODO: this is lame, but ant interpolation doesn't work for project name
  build_template = ERB.new(File.read(File.join(File.dirname(__FILE__), '..',
                                               'templates', 'build.xml')))
  @build = Tempfile.new(["pindah-build", ".xml"])
  @build.write(build_template.result(binding))
  @build.close

  if @spec.has_key?(:libraries)
    # Push the libs in through project.properties
    props_template = ERB.new(File.read(File.join(File.dirname(__FILE__), '..',
                                                 'templates', 'project.properties')))
    @props = File.new("project.properties", 'w')
    @props.write(props_template.result(binding))
    @props.close
  end

  user_properties = {
    "target" => "android-#{@spec[:target]}",
    "target-version" => "android-#{@spec[:target_version]}",
    "src" => @spec[:src],
    "sdk.dir" => @spec[:sdk],
    "classes" => @spec[:classes],
    "classpath" => @spec[:classpath].join(Java::JavaLang::System::
                                          getProperty("path.separator"))
  }

  user_properties.each do |key, value|
    @ant.project.set_user_property(key, value)
  end

  Ant::ProjectHelper.configure_project(@ant.project, java.io.File.new(@build.path))

  # Turn ant tasks into rake tasks
  add_tasks(ANT_TASKS)
  add_tasks(@spec[:extra_tasks])
end

.infer_sdk_location(path) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/pindah.rb', line 23

def self.infer_sdk_location(path)
  return ENV["ANDROID_HOME"] unless ENV["ANDROID_HOME"].nil?

  tools = path.split(File::PATH_SEPARATOR).detect { |p| File.exists?("#{p}/android") || File.exists?("#{p}/android.bat") }
  abort "\"android\" executable not found on $PATH" if tools.nil?
  real_location = Pathname.new("#{tools}/android").realpath.dirname
  File.expand_path("#{real_location}/..")
end

.spec=(spec) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pindah.rb', line 77

def self.spec=(spec)
  abort "Must provide :target_version in Pindah.spec!" if !spec[:target_version]
  abort "Must provide :name in Pindah.spec!" if !spec[:name]

  @spec = DEFAULTS.merge(spec)

  @spec[:root] = File.expand_path "."
  @spec[:target] ||= TARGETS[@spec[:target_version].to_s.sub(/android-/, '')]
  @spec[:classes] ||= "#{@spec[:output]}/classes/"
  @spec[:classpath] << @spec[:classes]

  if @spec.has_key?(:libraries)
    @spec[:libraries].each do |path|
      # TODO: do libraries always build to bin/classes?
      @spec[:classpath] << "#{File.expand_path path}/bin/classes/"
    end
  end

  @spec[:classpath] << "#{@spec[:sdk]}/platforms/android-#{@spec[:target]}/android.jar"
  @spec[:log_spec] ||= "ActivityManager:I #{@spec[:name]}:D " +
    "AndroidRuntime:E *:S"

  ant_setup
end