class Motion::Project::Gradle

Constants

GRADLE_ROOT

Attributes

aidls[R]
dependencies[R]
libraries[R]

Public Class Methods

new(config) click to toggle source
# File lib/motion/project/gradle.rb, line 28
def initialize(config)
  @gradle_path = '/usr/bin/env gradle'
  @config = config
  @classpaths = []
  @plugins = []
  @dependencies = []
  @repositories = []
  @aidl_files = []
  @libraries = []
  configure_project
end

Public Instance Methods

aidl(package, aidl_file_path) click to toggle source
# File lib/motion/project/gradle.rb, line 61
def aidl(package, aidl_file_path)
  @aidl_files << MotionGradle::Aidl.new(package, aidl_file_path)
end
android_gui_path() click to toggle source
# File lib/motion/project/gradle.rb, line 162
def android_gui_path
  @android_gui_path ||= File.join(ENV['RUBYMOTION_ANDROID_SDK'], 'tools', 'android')
end
android_repository() click to toggle source
# File lib/motion/project/gradle.rb, line 88
def android_repository
  android_repository = File.join(ENV['RUBYMOTION_ANDROID_SDK'], 'extras', 'android', 'm2repository')
  unless exist = File.exist?(android_repository)
    App.info('[warning]', "To avoid issues you should install `Extras/Android Support Repository`. Open the gui to install it : #{android_gui_path}")
  end
  exist
end
build_file() click to toggle source
# File lib/motion/project/gradle.rb, line 196
def build_file
  File.join(GRADLE_ROOT, 'build.gradle')
end
classpath(classpath) click to toggle source
# File lib/motion/project/gradle.rb, line 65
def classpath(classpath)
  @classpaths << classpath
end
configure_project() click to toggle source
# File lib/motion/project/gradle.rb, line 40
def configure_project
  vendor_aars
  vendor_jars
end
dependency(name, &block) click to toggle source
# File lib/motion/project/gradle.rb, line 49
def dependency(name, &block)
  @dependencies << MotionGradle::Dependency.new(name, &block)
end
extract_aars() click to toggle source
# File lib/motion/project/gradle.rb, line 166
def extract_aars
  aars = Dir[File.join(GRADLE_ROOT, 'dependencies/**/*.aar')]
  aar_dir = File.join(GRADLE_ROOT, 'aar')
  FileUtils.mkdir_p(aar_dir)
  aars.each do |aar|
    filename = File.basename(aar, '.aar')
    system("/usr/bin/unzip -o -qq #{aar} -d #{File.join(aar_dir, filename)}")
  end
end
generate_build_file() click to toggle source
# File lib/motion/project/gradle.rb, line 182
def generate_build_file
  template = MotionGradle::Template.new('build.gradle')
  template.destination = build_file
  template.write({
    classpaths: @classpaths,
    plugins: @plugins,
    libraries: @libraries,
    repositories: @repositories,
    dependencies: @dependencies,
    android_repository: android_repository,
    google_repository: google_repository
  })
end
generate_settings_file() click to toggle source
# File lib/motion/project/gradle.rb, line 176
def generate_settings_file
  template = MotionGradle::Template.new('settings.gradle')
  template.destination = settings_file
  template.write({libraries: @libraries})
end
google_repository() click to toggle source
# File lib/motion/project/gradle.rb, line 96
def google_repository
  google_repository = File.join(ENV['RUBYMOTION_ANDROID_SDK'], 'extras', 'google', 'm2repository')
  unless exist = File.exist?(google_repository)
    App.info('[warning]', "To avoid issues you should install `Extras/Google Repository`. Open the gui to install it : #{android_gui_path}")
  end
  exist
end
gradle_command() click to toggle source
# File lib/motion/project/gradle.rb, line 204
def gradle_command
  unless system("command -v #{@gradle_path} >/dev/null")
    $stderr.puts("[!] #{@gradle_path} command doesn’t exist. Verify your gradle installation. Or set a different one with `app.gradle.path=(path)`")
    exit(1)
  end

  if ENV['MOTION_GRADLE_DEBUG']
    "#{@gradle_path} --info"
  else
    "#{@gradle_path}"
  end
end
install!() click to toggle source
# File lib/motion/project/gradle.rb, line 77
def install!
  vendor_aidl_files
  generate_settings_file
  generate_build_file
  system("#{gradle_command} --build-file #{build_file} generateDependencies")

  # this might be uneeded in the future
  # if RM does support .aar out of the box
  extract_aars
end
library(library_name, options = {}) click to toggle source
# File lib/motion/project/gradle.rb, line 53
def library(library_name, options = {})
  path = options.fetch(:path, library_name)
  unless Pathname.new(path).absolute?
    path = File.join('../..', path)
  end
  @libraries << { name: library_name, path: path }
end
path=(path) click to toggle source
# File lib/motion/project/gradle.rb, line 45
def path=(path)
  @gradle_path = path
end
plugin(plugin) click to toggle source
# File lib/motion/project/gradle.rb, line 69
def plugin(plugin)
  @plugins << plugin
end
repository(url) click to toggle source
# File lib/motion/project/gradle.rb, line 73
def repository(url)
  @repositories << url
end
settings_file() click to toggle source
# File lib/motion/project/gradle.rb, line 200
def settings_file
  File.join(GRADLE_ROOT, 'settings.gradle')
end
vendor_aars() click to toggle source
# File lib/motion/project/gradle.rb, line 112
def vendor_aars
  aars_dependendies = Dir[File.join(GRADLE_ROOT, 'aar/*')]
  aars_dependendies.each do |dependency|
    main_jar = File.join(dependency, 'classes.jar')
    jar_path = File.join(dependency, "#{File.basename(dependency)}.jar")

    if File.exist?(main_jar)
      FileUtils.mv(main_jar, jar_path)
      vendor_options = { jar: jar_path }
    elsif File.exist?(jar_path)
      vendor_options = { jar: jar_path }
    else
      next
    end

    # libs/*.jar may contain dependencies, let's vendor them
    Dir[File.join(dependency, 'libs/*.jar')].each do |internal_dependancy|
      @config.vendor_project(jar: internal_dependancy)
    end

    res = File.join(dependency, 'res')
    if File.exist?(res)
      vendor_options[:resources] = res
      vendor_options[:manifest] = File.join(dependency, 'AndroidManifest.xml')
    end

    native = File.join(dependency, 'jni')
    if File.exist?(native)
      archs = @config.archs.uniq.map do |arch|
        @config.armeabi_directory_name(arch)
      end

      libs = Dir[File.join(native, "{#{archs.join(',')}}", '*.so')]
      if libs.count != archs.count
        App.info('[warning]', "Found only #{libs.count} lib(s) -> #{libs.join(',')} for #{archs.count} arch(s) : #{archs.join(',')}")
      end
      vendor_options[:native] = libs
    end

    @config.vendor_project(vendor_options)
  end
end
vendor_aidl_files() click to toggle source

Helpers

# File lib/motion/project/gradle.rb, line 105
def vendor_aidl_files
  @aidl_files.each do |aidl_file|
    aidl_file.create_lib
    library(aidl_file.name, path: aidl_file.path)
  end
end
vendor_jars() click to toggle source
# File lib/motion/project/gradle.rb, line 155
def vendor_jars
  jars = Dir[File.join(GRADLE_ROOT, 'dependencies/*.jar')]
  jars.each do |jar|
    @config.vendor_project(jar: jar)
  end
end