diff --git a/README.md b/README.md index 1cb8b3e..3de3756 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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._ @@ -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. diff --git a/Rakefile b/Rakefile index 3942da5..0bc77e5 100644 --- a/Rakefile +++ b/Rakefile @@ -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 @@ -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}" @@ -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 - diff --git a/lib/tasks/loomlib.rake b/lib/tasks/loomlib.rake index 3fca034..143f76c 100644 --- a/lib/tasks/loomlib.rake +++ b/lib/tasks/loomlib.rake @@ -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 @@ -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']}" @@ -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 @@ -50,7 +104,7 @@ 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 @@ -58,7 +112,7 @@ file LIBRARY do |t, args| 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 @@ -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 @@ -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' @@ -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 @@ -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 diff --git a/lib/tasks/support.rb b/lib/tasks/support.rb new file mode 100644 index 0000000..d873a06 --- /dev/null +++ b/lib/tasks/support.rb @@ -0,0 +1,56 @@ + +module LoomTasks + + EXIT_OK = 0 + + def exec_with_echo(cmd) + puts(cmd) + stdout = %x[#{cmd}] + puts(stdout) unless stdout.empty? + $?.exitstatus + end + + def fail(message) + abort("✘ #{message}") + end + + def sdk_root() + File.join(Dir.home, '.loom', 'sdks') + end + + def try(cmd, failure_message) + fail(failure_message) if (exec_with_echo(cmd) != EXIT_OK) + end + + def parse_loom_config(file) + JSON.parse(File.read(file)) + end + + def write_loom_config(file, config) + File.open(file, 'w') { |f| f.write(JSON.pretty_generate(config)) } + 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 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 + +end