Class: Motion::Project::Gradle

Inherits:
Object
  • Object
show all
Defined in:
lib/motion/project/gradle.rb

Constant Summary collapse

GRADLE_ROOT =
'vendor/Gradle'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Gradle

Returns a new instance of Gradle.



28
29
30
31
32
33
34
35
36
37
38
# 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

Instance Attribute Details

#aidlsObject (readonly)

Returns the value of attribute aidls.



26
27
28
# File 'lib/motion/project/gradle.rb', line 26

def aidls
  @aidls
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



24
25
26
# File 'lib/motion/project/gradle.rb', line 24

def dependencies
  @dependencies
end

#librariesObject (readonly)

Returns the value of attribute libraries.



25
26
27
# File 'lib/motion/project/gradle.rb', line 25

def libraries
  @libraries
end

Instance Method Details

#aidl(package, aidl_file_path) ⇒ Object



61
62
63
# 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_pathObject



162
163
164
# 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_repositoryObject



88
89
90
91
92
93
94
# 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_fileObject



196
197
198
# File 'lib/motion/project/gradle.rb', line 196

def build_file
  File.join(GRADLE_ROOT, 'build.gradle')
end

#classpath(classpath) ⇒ Object



65
66
67
# File 'lib/motion/project/gradle.rb', line 65

def classpath(classpath)
  @classpaths << classpath
end

#configure_projectObject



40
41
42
43
# File 'lib/motion/project/gradle.rb', line 40

def configure_project
  vendor_aars
  vendor_jars
end

#dependency(name, &block) ⇒ Object



49
50
51
# File 'lib/motion/project/gradle.rb', line 49

def dependency(name, &block)
  @dependencies << MotionGradle::Dependency.new(name, &block)
end

#extract_aarsObject



166
167
168
169
170
171
172
173
174
# 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_fileObject



182
183
184
185
186
187
188
189
190
191
192
193
194
# 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_fileObject



176
177
178
179
180
# 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_repositoryObject



96
97
98
99
100
101
102
# 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_commandObject



204
205
206
207
208
209
210
211
212
213
214
215
# 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!Object



77
78
79
80
81
82
83
84
85
86
# 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 = {}) ⇒ Object



53
54
55
56
57
58
59
# 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) ⇒ Object



45
46
47
# File 'lib/motion/project/gradle.rb', line 45

def path=(path)
  @gradle_path = path
end

#plugin(plugin) ⇒ Object



69
70
71
# File 'lib/motion/project/gradle.rb', line 69

def plugin(plugin)
  @plugins << plugin
end

#repository(url) ⇒ Object



73
74
75
# File 'lib/motion/project/gradle.rb', line 73

def repository(url)
  @repositories << url
end

#settings_fileObject



200
201
202
# File 'lib/motion/project/gradle.rb', line 200

def settings_file
  File.join(GRADLE_ROOT, 'settings.gradle')
end

#vendor_aarsObject



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
141
142
143
144
145
146
147
148
149
150
151
152
153
# 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_filesObject

Helpers



105
106
107
108
109
110
# 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_jarsObject



155
156
157
158
159
160
# 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