Skip to content
Browse files

First stab at go guard

  • Loading branch information...
0 parents commit 19056f52a75dabbab00888ec7de0d253e7a63848 @victorcoder victorcoder committed Apr 5, 2012
Showing with 209 additions and 0 deletions.
  1. +17 −0 .gitignore
  2. +4 −0 Gemfile
  3. +22 −0 LICENSE
  4. +29 −0 README.md
  5. +2 −0 Rakefile
  6. +19 −0 guard-go.gemspec
  7. +1 −0 lib/guard-go.rb
  8. +36 −0 lib/guard-go/go.rb
  9. +74 −0 lib/guard-go/go/runner.rb
  10. +5 −0 lib/guard-go/go/version.rb
17 .gitignore
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
4 Gemfile
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in guard-go.gemspec
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Victor Castell
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 README.md
@@ -0,0 +1,29 @@
+# Guard::Go
+
+TODO: Write a gem description
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'guard-go'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install guard-go
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
2 Rakefile
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
19 guard-go.gemspec
@@ -0,0 +1,19 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/guard-go/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["Victor Castell"]
+ gem.email = ["victorcoder@gmail.com"]
+ gem.description = %q{TODO: Write a gem description}
+ gem.summary = %q{TODO: Write a gem summary}
+ gem.homepage = ""
+
+ gem.files = `git ls-files`.split($\)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.name = "guard-go"
+ gem.require_paths = ["lib"]
+ gem.version = Guard::Go::VERSION
+
+ gem.add_dependency 'guard', '>= 0.2.2
+end
1 lib/guard-go.rb
@@ -0,0 +1 @@
+require 'guard-go/go'
36 lib/guard-go/go.rb
@@ -0,0 +1,36 @@
+require "guard-go/go/version"
+require 'guard'
+require 'guard/guard'
+require 'guard/watcher'
+
+module Guard
+ class Go < Guard::Guard
+ attr_reader :options, :runner
+
+ # Call once when Guard starts. Please override initialize method to init stuff.
+ # @raise [:task_has_failed] when start has failed
+ def start
+ run_all if options[:all_on_start]
+ end
+
+ def run_all
+ run_on_change(Watcher.match_files(self, Dir.glob('{,**/}*{,.*}').uniq))
+ end
+
+ def run_on_change(paths)
+ UI.info "Restarting Go..."
+ if runner.restart
+ UI.info "Rack restarted, pid #{runner.pid}"
+ Notifier.notify("Rack restarted on port #{options[:port]}.", :title => "Rack restarted!", :image => :success)
+ else
+ UI.info "Rack NOT restarted, check your log files."
+ Notifier.notify("Rack NOT restarted, check your log files.", :title => "Rack NOT restarted!", :image => :failed)
+ end
+ end
+
+ def stop
+ Notifier.notify("Until next time...", :title => "Go shutting down.", :image => :pending)
+ runner.stop
+ end
+ end
+end
74 lib/guard-go/go/runner.rb
@@ -0,0 +1,74 @@
+require 'fileutils'
+
+module Guard
+ class RailsRunner
+ MAX_WAIT_COUNT = 10
+
+ attr_reader :options
+
+ def initialize(options)
+ @options = options
+ end
+
+ def start
+ run_rails_command!
+ wait_for_pid
+ end
+
+ def stop
+ if File.file?(pid_file)
+ system %{kill -KILL #{File.read(pid_file).strip}}
+ wait_for_no_pid if $?.exitstatus == 0
+ FileUtils.rm pid_file
+ end
+ end
+
+ def restart
+ stop
+ start
+ end
+
+ def build_go_command
+ %{sh -c 'cd #{Dir.pwd} && go run #{options[:go_file]} &'}
+ end
+
+ def pid_file
+ File.expand_path("tmp/pids/#{options[:go_file]}.pid")
+ end
+
+ def pid
+ File.file?(pid_file) ? File.read(pid_file).to_i : nil
+ end
+
+ def sleep_time
+ options[:timeout].to_f / MAX_WAIT_COUNT.to_f
+ end
+
+ private
+ def run_go_command!
+ system build_go_command
+ system %{echo $! > #{pid_file}}
+ end
+
+ def has_pid?
+ File.file?(pid_file)
+ end
+
+ def wait_for_pid_action
+ sleep sleep_time
+ end
+
+ def wait_for_pid
+ wait_for_pid_loop
+ end
+
+ def wait_for_pid_loop
+ count = 0
+ while !has_pid? && count < MAX_WAIT_COUNT
+ wait_for_pid_action
+ count += 1
+ end
+ !(count == MAX_WAIT_COUNT)
+ end
+ end
+end
5 lib/guard-go/go/version.rb
@@ -0,0 +1,5 @@
+module Guard
+ module Go
+ VERSION = "0.0.1"
+ end
+end

0 comments on commit 19056f5

Please sign in to comment.
Something went wrong with that request. Please try again.