Skip to content

Commit

Permalink
Refactoring : tests and extract templating logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jjaffeux committed Sep 26, 2015
1 parent 3ae0ded commit 74d932d
Show file tree
Hide file tree
Showing 18 changed files with 315 additions and 131 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.*.sw?
app
AppRakefile
pkg
tmp
vendor/Gradle
Expand Down
12 changes: 6 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
desc "Build the gem"
desc 'Build the gem'
task :gem do
sh "bundle exec gem build motion-gradle.gemspec"
sh "mkdir -p pkg"
sh "mv *.gem pkg/"
sh 'bundle exec gem build motion-gradle.gemspec'
sh 'mkdir -p pkg'
sh 'mv *.gem pkg/'
end

task :clean do
FileUtils.rm_rf 'pkg'
end

desc "Run all the specs"
desc 'Run all the specs'
task :spec do
sh "bundle exec bacon #{FileList['spec/*_spec.rb'].join(' ')}"
end

task :default => :spec
task default: :spec
10 changes: 7 additions & 3 deletions lib/motion-gradle.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require 'pathname'
require 'shellwords'
require 'motion/project/legacy_dependency'
require 'motion_gradle/legacy_dependency'
require 'motion_gradle/aidl'
require 'motion_gradle/template'
require 'motion_gradle/version'
require 'motion/project/gradle'
require 'motion/project/aidl'
require 'motion/project/version'

module MotionGradle
end
54 changes: 0 additions & 54 deletions lib/motion/project/aidl.rb

This file was deleted.

50 changes: 25 additions & 25 deletions lib/motion/project/gradle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def dependency(name, options = {})
if name.include?(':')
@dependencies << name
else
@dependencies << LegacyDependency.new(name, options)
@dependencies << MotionGradle::LegacyDependency.new(name, options)
end
end

Expand All @@ -59,15 +59,11 @@ def library(library_name, options = {})
unless Pathname.new(path).absolute?
path = File.join('../..', path)
end

@libraries << {
name: library_name,
path: path
}
@libraries << { name: library_name, path: path }
end

def aidl(package, aidl_file_path)
@aidl_files << Aidl.new(package, aidl_file_path)
@aidl_files << MotionGradle::Aidl.new(package, aidl_file_path)
end

def classpath(classpath)
Expand All @@ -84,9 +80,9 @@ def repository(url)

def install!
vendor_aidl_files
generate_gradle_settings_file
generate_gradle_build_file
system("#{gradle_command} --build-file #{gradle_build_file} generateDependencies")
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
Expand Down Expand Up @@ -169,7 +165,7 @@ def android_gui_path
end

def extract_aars
aars = Dir[File.join(GRADLE_ROOT, "dependencies/**/*.aar")]
aars = Dir[File.join(GRADLE_ROOT, 'dependencies/**/*.aar')]
aar_dir = File.join(GRADLE_ROOT, 'aar')
FileUtils.mkdir_p(aar_dir)
aars.each do |aar|
Expand All @@ -178,27 +174,31 @@ def extract_aars
end
end

def generate_gradle_settings_file
template_path = File.expand_path('../settings.erb', __FILE__)
template = ERB.new(File.new(template_path).read, nil, '%')
File.open(gradle_settings_file, 'w') do |io|
io.puts(template.result(binding))
end
def generate_settings_file
template = MotionGradle::Template.new('settings.gradle')
template.destination = settings_file
template.write({libraries: @libraries})
end

def generate_gradle_build_file
template_path = File.expand_path('../gradle.erb', __FILE__)
template = ERB.new(File.new(template_path).read, nil, '%')
File.open(gradle_build_file, 'w') do |io|
io.puts(template.result(binding))
end
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

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

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

Expand Down
21 changes: 0 additions & 21 deletions lib/motion/project/legacy_dependency.rb

This file was deleted.

7 changes: 0 additions & 7 deletions lib/motion/project/version.rb

This file was deleted.

51 changes: 51 additions & 0 deletions lib/motion_gradle/aidl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module MotionGradle
class Aidl
def initialize(package, aidl_file_path)
@package = package
@aidl_file_path = File.expand_path(aidl_file_path)
end

def create_lib
create_structure
create_gradle_build_file
create_manifest
end

def name
@name ||= File.basename(@aidl_file_path, '.aidl').downcase
end

def path
@path ||= File.join(Motion::Project::Gradle::GRADLE_ROOT, name)
end

protected

def create_manifest
template = MotionGradle::Template.new('android_manifest.xml')
template.destination = File.join(path, 'src', 'main', 'AndroidManifest.xml')
template.write({ package: @package })
end

def create_gradle_build_file
template = MotionGradle::Template.new('aidl_build.gradle')
template.destination = File.join(path, 'build.gradle')
template.write({ last_build_tools_version: last_build_tools_version })
end

def create_structure
aidl_file_dir = File.join(path, 'src', 'main', 'aidl', *@package.split('.'))
FileUtils.mkdir_p(aidl_file_dir)
FileUtils.cp(@aidl_file_path, aidl_file_dir)
end

def last_build_tools_version
build_tools = File.join(ENV['RUBYMOTION_ANDROID_SDK'], 'build-tools')
glob_pattern = File.join(build_tools, '*')
builds_tools_directories = Dir.glob(glob_pattern).select do |file|
File.directory?(file)
end
File.basename(builds_tools_directories.last)
end
end
end
23 changes: 23 additions & 0 deletions lib/motion_gradle/legacy_dependency.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module MotionGradle
class LegacyDependency
def initialize(name, params)
@options = normalized_dependency(name, params)
end

def parse
options = @options.delete_if { |_, v| v.nil? }.map { |k, v| "#{k}: '#{v}'" }
"compile #{options.join(', ')}"
end

protected

def normalized_dependency(name, params)
{
group: name,
version: params.fetch(:version, '+'),
name: params.fetch(:artifact, name),
ext: params.fetch(:extension, nil)
}
end
end
end
17 changes: 17 additions & 0 deletions lib/motion_gradle/template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module MotionGradle
class Template
attr_accessor :destination

def initialize(name)
template_path = File.expand_path("../templates/#{name}.erb", __FILE__)
@template = ERB.new(File.new(template_path).read)
end

def write(locals = {})
File.open(self.destination, 'w') do |io|
struct = OpenStruct.new(locals)
io.puts(@template.result(struct.instance_eval { binding }))
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ buildscript {
}

android {
compileSdkVersion <%= App.config.api_version %>
compileSdkVersion <%= App.config.api_version.to_i %>
buildToolsVersion "<%= last_build_tools_version %>"

defaultConfig {
minSdkVersion <%= App.config.api_version %>
targetSdkVersion <%= App.config.api_version %>
minSdkVersion <%= App.config.api_version.to_i %>
targetSdkVersion <%= App.config.api_version.to_i %>
versionCode 1
versionName "1.0"
}
Expand Down
2 changes: 2 additions & 0 deletions lib/motion_gradle/templates/android_manifest.xml.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="<%= package %>">
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
mavenCentral()
}
dependencies {
<% @classpaths.each do |classpath| %>
<% classpaths.each do |classpath| %>
classpath '<%= classpath %>'
<% end %>
}
Expand All @@ -19,7 +19,7 @@ allprojects {

apply plugin: 'java'
apply plugin: 'eclipse'
<% @plugins.each do |plugin| %>
<% plugins.each do |plugin| %>
apply plugin: '<%= plugin %>'
<% end %>

Expand All @@ -39,19 +39,19 @@ repositories {
url "<%= ENV['RUBYMOTION_ANDROID_SDK'].shellescape %>/extras/google/m2repository/"
}
<% end %>
<% @repositories.each do |url| %>
<% repositories.each do |url| %>
maven {
url "<%= url %>"
}
<% end %>
}

dependencies {
<% @libraries.each do |library| %>
<% libraries.each do |library| %>
compile project(':<%= library[:name] %>')
<% end %>
<% @dependencies.each do |dependency| %>
<% if dependency.is_a?(LegacyDependency) %>
<% dependencies.each do |dependency| %>
<% if dependency.is_a?(MotionGradle::LegacyDependency) %>
<%= dependency.parse %>
<% else %>
compile '<%= dependency %>'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% @libraries.each do |library| %>
<% libraries.each do |library| %>
include '<%= library[:name] %>'
project(':<%= library[:name] %>').projectDir = new File('<%= library[:path].shellescape %>')
<% end %>
3 changes: 3 additions & 0 deletions lib/motion_gradle/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module MotionGradle
VERSION = '1.6.0'
end
Loading

0 comments on commit 74d932d

Please sign in to comment.