From 5a3b0b87542b9356694f6aafb1f8cb21d4df3731 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Tue, 4 Jun 2013 22:11:41 +0400 Subject: [PATCH] Working prototype --- .gitignore | 14 ----------- Rakefile | 17 +++++++++++++- lib/rake/task.rb | 11 +++++++++ lib/rake_control.rb | 10 ++++++-- lib/rake_control/version.rb | 2 +- lib/rake_control/wrapper.rb | 47 +++++++++++++++++++++++++++++++++++++ rake_control.gemspec | 22 ++++++++--------- 7 files changed, 94 insertions(+), 29 deletions(-) create mode 100644 lib/rake/task.rb create mode 100644 lib/rake_control/wrapper.rb diff --git a/.gitignore b/.gitignore index d87d4be..0b5d400 100644 --- a/.gitignore +++ b/.gitignore @@ -1,17 +1,3 @@ -*.gem -*.rbc .bundle -.config -.yardoc Gemfile.lock -InstalledFiles -_yardoc -coverage -doc/ -lib/bundler/man -pkg -rdoc -spec/reports -test/tmp -test/version_tmp tmp diff --git a/Rakefile b/Rakefile index 2995527..3b211c2 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1,16 @@ -require "bundler/gem_tasks" +require 'bundler/gem_tasks' +require 'rake_control' + +task :regular_task do + puts 'I am being invoked' +end + +task :failing_task do + raise 'wtf' +end + +task :sleepy_task do + puts 'Sleeping...' + sleep 2 + puts 'Woke up!' +end diff --git a/lib/rake/task.rb b/lib/rake/task.rb new file mode 100644 index 0000000..6792e25 --- /dev/null +++ b/lib/rake/task.rb @@ -0,0 +1,11 @@ +module Rake + class Task + alias :original_execute :execute + + def execute(args) + RakeControl.wrap(name, comment) do + original_execute(args) + end + end + end +end diff --git a/lib/rake_control.rb b/lib/rake_control.rb index 2252868..70dcb44 100644 --- a/lib/rake_control.rb +++ b/lib/rake_control.rb @@ -1,5 +1,11 @@ -require "rake_control/version" +require 'rake_control/version' +require 'rake_control/wrapper' +require File.expand_path('../rake/task', __FILE__) module RakeControl - # Your code goes here... + def wrap(name, description, &block) + Wrapper.new(name, description, block).execute + end + + extend self end diff --git a/lib/rake_control/version.rb b/lib/rake_control/version.rb index a4f918c..c3a9c0c 100644 --- a/lib/rake_control/version.rb +++ b/lib/rake_control/version.rb @@ -1,3 +1,3 @@ module RakeControl - VERSION = "0.0.1" + VERSION = '0.1.0' end diff --git a/lib/rake_control/wrapper.rb b/lib/rake_control/wrapper.rb new file mode 100644 index 0000000..a316868 --- /dev/null +++ b/lib/rake_control/wrapper.rb @@ -0,0 +1,47 @@ +module RakeControl + class Wrapper + attr_reader :name, :description, :block, :success, :exception, :execution_time + + def initialize(name, description, block) + @name = name + @description = description + @block = block + @success = true + end + + def execute + result = measure_time do + intercept_exceptions do + block.call + end + end + save_statistics + + success ? result : raise(exception) + end + + private + + def measure_time + t = Time.now + result = yield + @execution_time = Time.now.to_f - t.to_f + result + end + + def intercept_exceptions + begin + yield + rescue => e + @success = false + @exception = e + nil + end + end + + def save_statistics + puts "Task #{name} #{success ? 'finished successfully' : 'failed'}" + puts "Execution time: #{execution_time.round(4)}" + end + end +end diff --git a/rake_control.gemspec b/rake_control.gemspec index fb295bd..ffbd15a 100644 --- a/rake_control.gemspec +++ b/rake_control.gemspec @@ -1,23 +1,23 @@ -# coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'rake_control/version' Gem::Specification.new do |spec| - spec.name = "rake_control" + spec.name = 'rake_control' spec.version = RakeControl::VERSION - spec.authors = ["Gregory Eremin"] - spec.email = ["magnolia_fan@me.com"] - spec.description = %q{TODO: Write a gem description} - spec.summary = %q{TODO: Write a gem summary} - spec.homepage = "" - spec.license = "MIT" + spec.authors = ['Gregory Eremin'] + spec.email = ['magnolia_fan@me.com'] + spec.description = %q{} + spec.summary = %q{Rake tasks manager and statistics collector} + spec.homepage = 'https://github.com/magnolia-fan/rake_control' + spec.license = 'MIT' spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) - spec.require_paths = ["lib"] + spec.require_paths = ['lib'] - spec.add_development_dependency "bundler", "~> 1.3" - spec.add_development_dependency "rake" + spec.add_runtime_dependency 'rake' + spec.add_development_dependency 'bundler', '~> 1.3' + # spec.add_development_dependency 'sqlite' end