Skip to content

Commit

Permalink
refactor to extract common methods into support module
Browse files Browse the repository at this point in the history
  • Loading branch information
ellemenno committed Jan 3, 2015
1 parent 7fc3d33 commit 58c6306
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 141 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ If you use [Rake][rake] and follow a consistent file layout across projects, the
The tasks install into your `.loom` directory, and can be loaded from there into the Rakefiles of your projects.
They are no substitute for something like Gem or Bundler for Ruby, but they're a first step in that direction.

loom tasks do not interfere with the [loomcli][loomcli]; the two can be used safely together.
loom tasks do not replace or interfere with the [loomcli][loomcli]; the two can be used safely together.


## conventions
Expand Down Expand Up @@ -83,7 +83,7 @@ Clone this repo.

0. Run `rake install` to:
* create a `tasks` folder in your Loom SDK home directory (`~/.loom`)
* put the `.rake` files from this project into it.
* install the Rake tasks from this project into it.
0. Run `rake uninstall` to:
* delete the `tasks` folder.
* _**Note:** this deletes the whole folder! So be careful if you decide to put your own tasks in there._
Expand Down Expand Up @@ -116,6 +116,7 @@ Now run `rake` to execute the default task, which will print the list of availab
rake test:build # builds FooTest.loom with the SDK specified in test/loom.config
rake test:ci # runs FooTest.loom for CI
rake test:run # runs FooTest.loom
(using loomlib.rake v1.0.0)

The Rake tasks are defined with dependencies and modification triggers, so you can just run `rake test:run` every time you edit a source file, and the library and test app will be rebuilt as needed automatically.

Expand Down
53 changes: 11 additions & 42 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# encoding: utf-8

require 'etc'
require 'fileutils'
require 'json'
require File.join(File.dirname(__FILE__), 'lib', 'tasks', 'support')
include LoomTasks

@loom_config = nil

def available_tasks_dir()
File.join(File.dirname(__FILE__), 'lib', 'tasks')
end

def installed_tasks_dir()
File.join(Dir.home, '.loom', 'tasks')
end


task :default => :list_targets
Expand All @@ -21,7 +27,7 @@ desc "installs rake tasks for Loom"
task :install do |t, args|
Dir.mkdir(installed_tasks_dir) unless Dir.exists?(installed_tasks_dir)

cmd = "cp lib/tasks/*.rake #{installed_tasks_dir}"
cmd = "cp lib/tasks/*.rake lib/tasks/*.rb #{installed_tasks_dir}"
try(cmd, "failed to install tasks")

puts "[#{t.name}] task completed, tasks installed to #{installed_tasks_dir}"
Expand Down Expand Up @@ -63,40 +69,3 @@ task :uninstall do |t, args|
puts "[#{t.name}] task completed, #{installed_tasks_dir} was removed"
puts ''
end


def config()
@loom_config || (@loom_config = JSON.parse(File.read(loom_config_file)))
end

def exec_with_echo(cmd)
puts(cmd)
stdout = %x[#{cmd}]
puts(stdout) unless stdout.empty?
$?.exitstatus
end

def fail(message)
abort("◈ #{message}")
end

def loom_config_file()
'loom.config'
end

def available_tasks_dir()
File.join(File.dirname(__FILE__), 'lib', 'tasks')
end

def installed_tasks_dir()
File.join(Dir.home, '.loom', 'tasks')
end

def try(cmd, failure_message)
fail(failure_message) if (exec_with_echo(cmd) != 0)
end

def write_loom_config(config)
File.open(loom_config_file, 'w') { |f| f.write(JSON.pretty_generate(config)) }
end

173 changes: 76 additions & 97 deletions lib/tasks/loomlib.rake
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,55 @@ require 'fileutils'
require 'json'
require 'rake/clean'

require File.join(File.dirname(__FILE__), 'support')
include LoomTasks


def const_find(name)
Module.const_defined?(name) ? Module.const_get(name) : nil
end

def const_lib_name
const_find('LIB_NAME')
end

def const_lib_version_file
const_find('LIB_VERSION_FILE')
end

def lib_config_file()
File.join('lib', 'loom.config')
end

def lib_version_file()
const_lib_version_file
end

def readme_file()
File.join('README.md')
end

def test_config_file()
File.join('test', 'loom.config')
end

def lib_config()
@lib_loom_config || (@lib_loom_config = parse_loom_config(lib_config_file))
end

def test_config()
@test_loom_config || (@test_loom_config = parse_loom_config(test_config_file))
end

def write_lib_config(config)
write_loom_config(lib_config_file, config)
end

def write_test_config(config)
write_loom_config(test_config_file, config)
end


@lib_loom_config = nil
@test_loom_config = nil

Expand All @@ -25,8 +74,8 @@ Rake::Task[:clobber].enhance ['lib:uninstall']

task :default => :list_targets

task :list_targets do |t, args|
a = "#{LIB_NAME} v#{lib_version} Rakefile"
task :list_targets => :check_consts do |t, args|
a = "#{const_lib_name} v#{lib_version} Rakefile"
b = "running on Ruby #{RUBY_VERSION}"
c = "lib=#{lib_config['sdk_version']}"
d = "test=#{test_config['sdk_version']}"
Expand All @@ -36,8 +85,13 @@ task :list_targets do |t, args|
puts ''
end

task :check_consts do |t, args|
fail("please define the LIB_NAME constant before loading #{File.basename(__FILE__)}") unless const_lib_name
fail("please define the LIB_VERSION_FILE constant before loading #{File.basename(__FILE__)}") unless const_lib_version_file
end


LIBRARY = "lib/build/#{LIB_NAME}.loomlib"
LIBRARY = "lib/build/#{const_lib_name}.loomlib"

FileList['lib/src/**/*.ls'].each do |src|
file LIBRARY => src
Expand All @@ -50,15 +104,15 @@ file LIBRARY do |t, args|

Dir.chdir('lib') do
Dir.mkdir('build') unless Dir.exists?('build')
cmd = "#{sdk_root}/#{sdk_version}/tools/lsc #{LIB_NAME}.build"
cmd = "#{sdk_root}/#{sdk_version}/tools/lsc #{const_lib_name}.build"
try(cmd, "failed to compile .loomlib")
end

puts ''
end


APP = "test/bin/#{LIB_NAME}Test.loom"
APP = "test/bin/#{const_lib_name}Test.loom"

FileList['test/src/**/*.ls'].each do |src|
file APP => src
Expand All @@ -68,13 +122,13 @@ file APP => LIBRARY do |t, args|
puts "[file] creating #{t.name}..."

sdk_version = test_config['sdk_version']
file_installed = "#{sdk_root}/#{sdk_version}/libs/#{LIB_NAME}.loomlib"
file_installed = "#{sdk_root}/#{sdk_version}/libs/#{const_lib_name}.loomlib"

Rake::Task['lib:install'].invoke unless FileUtils.uptodate?(file_installed, [LIBRARY])

Dir.chdir('test') do
Dir.mkdir('bin') unless Dir.exists?('bin')
cmd = "#{sdk_root}/#{sdk_version}/tools/lsc #{LIB_NAME}Test.build"
cmd = "#{sdk_root}/#{sdk_version}/tools/lsc #{const_lib_name}Test.build"
try(cmd, "failed to compile .loom")
end

Expand All @@ -99,15 +153,15 @@ end

namespace :lib do

desc "builds #{LIB_NAME}.loomlib for the SDK specified in lib/loom.config"
desc "builds #{const_lib_name}.loomlib for the SDK specified in lib/loom.config"
task :build => LIBRARY do |t, args|
puts "[#{t.name}] task completed, find .loomlib in lib/build/"
puts ''
end

desc "prepares sdk-specific #{LIB_NAME}.loomlib for release"
desc "prepares sdk-specific #{const_lib_name}.loomlib for release"
task :release => LIBRARY do |t, args|
lib = "lib/build/#{LIB_NAME}.loomlib"
lib = "lib/build/#{const_lib_name}.loomlib"
sdk = lib_config['sdk_version']
ext = '.loomlib'
release_dir = 'releases'
Expand All @@ -123,30 +177,30 @@ namespace :lib do
puts ''
end

desc "installs #{LIB_NAME}.loomlib into the SDK specified in lib/loom.config"
desc "installs #{const_lib_name}.loomlib into the SDK specified in lib/loom.config"
task :install => LIBRARY do |t, args|
lib = "lib/build/#{LIB_NAME}.loomlib"
lib = "lib/build/#{const_lib_name}.loomlib"
sdk_version = lib_config['sdk_version']
libs_path = "#{sdk_root}/#{sdk_version}/libs"

cmd = "cp #{lib} #{libs_path}"
try(cmd, "failed to install lib")

puts "[#{t.name}] task completed, #{LIB_NAME}.loomlib installed for #{sdk_version}"
puts "[#{t.name}] task completed, #{const_lib_name}.loomlib installed for #{sdk_version}"
puts ''
end

desc "removes #{LIB_NAME}.loomlib from the SDK specified in lib/loom.config"
desc "removes #{const_lib_name}.loomlib from the SDK specified in lib/loom.config"
task :uninstall do |t, args|
sdk_version = lib_config['sdk_version']
lib = "#{sdk_root}/#{sdk_version}/libs/#{LIB_NAME}.loomlib"
lib = "#{sdk_root}/#{sdk_version}/libs/#{const_lib_name}.loomlib"

if (File.exists?(lib))
cmd = "rm -f #{lib}"
try(cmd, "failed to remove lib")
puts "[#{t.name}] task completed, #{LIB_NAME}.loomlib removed from #{sdk_version}"
puts "[#{t.name}] task completed, #{const_lib_name}.loomlib removed from #{sdk_version}"
else
puts "[#{t.name}] nothing to do; no #{LIB_NAME}.loomlib found in #{sdk_version} sdk"
puts "[#{t.name}] nothing to do; no #{const_lib_name}.loomlib found in #{sdk_version} sdk"
end
puts ''
end
Expand All @@ -165,107 +219,32 @@ end

namespace :test do

desc "builds #{LIB_NAME}Test.loom with the SDK specified in test/loom.config"
desc "builds #{const_lib_name}Test.loom with the SDK specified in test/loom.config"
task :build => APP do |t, args|
puts "[#{t.name}] task completed, find .loom in test/bin/"
puts ''
end

desc "runs #{LIB_NAME}Test.loom"
desc "runs #{const_lib_name}Test.loom"
task :run => APP do |t, args|
puts "[#{t.name}] running #{t.prerequisites[0]}..."

sdk_version = test_config['sdk_version']
cmd = "#{sdk_root}/#{sdk_version}/tools/loomexec test/bin/#{LIB_NAME}Test.loom --format ansi"
cmd = "#{sdk_root}/#{sdk_version}/tools/loomexec test/bin/#{const_lib_name}Test.loom --format ansi"
try(cmd, "failed to run .loom")

puts ''
end

desc "runs #{LIB_NAME}Test.loom for CI"
desc "runs #{const_lib_name}Test.loom for CI"
task :ci => APP do |t, args|
puts "[#{t.name}] running #{t.prerequisites[0]}..."

sdk_version = test_config['sdk_version']
cmd = "#{sdk_root}/#{sdk_version}/tools/loomexec test/bin/#{LIB_NAME}Test.loom --format junit --format console"
cmd = "#{sdk_root}/#{sdk_version}/tools/loomexec test/bin/#{const_lib_name}Test.loom --format junit --format console"
try(cmd, "failed to run .loom")

puts ''
end

end


def readme_file()
File.join('README.md')
end

def lib_config_file()
File.join('lib', 'loom.config')
end

def lib_version_file()
LIB_VERSION_FILE
end

def test_config_file()
File.join('test', 'loom.config')
end

def lib_config()
@lib_loom_config || (@lib_loom_config = JSON.parse(File.read(lib_config_file)))
end

def test_config()
@test_loom_config || (@test_loom_config = JSON.parse(File.read(test_config_file)))
end

def readme_version_regex()
Regexp.new(%q/download\/v(\d\.\d\.\d)/)
end

def readme_version_literal()
"download/v#{lib_version}"
end

def update_readme_version()
IO.write(
readme_file,
File.open(readme_file, 'r') { |f| f.read.gsub!(readme_version_regex, readme_version_literal) }
)
end

def lib_version_regex()
Regexp.new(%q/^\s*public static const version:String = '(\d\.\d\.\d)';/)
end

def lib_version()
File.open(lib_version_file, 'r') { |f| f.read.scan(lib_version_regex).first[0] }
end

def write_lib_config(config)
File.open(lib_config_file, 'w') { |f| f.write(JSON.pretty_generate(config)) }
end

def write_test_config(config)
File.open(test_config_file, 'w') { |f| f.write(JSON.pretty_generate(config)) }
end

def sdk_root()
File.join(Dir.home, '.loom', 'sdks')
end

def fail(message)
abort("◈ #{message}")
end

def try(cmd, failure_message)
fail(failure_message) if (exec_with_echo(cmd) != 0)
end

def exec_with_echo(cmd)
puts(cmd)
stdout = %x[#{cmd}]
puts(stdout) unless stdout.empty?
$?.exitstatus
end
Loading

0 comments on commit 58c6306

Please sign in to comment.