Skip to content

Instantly share code, notes, and snippets.

@Integralist

Integralist/Gemfile

Last active Dec 26, 2015
Embed
What would you like to do?
Go Guardfile: `bundle exec guard`
source "https://rubygems.org"
gem "guard"
gem "guard-shell"
gem "terminal-notifier" # brew upgrade terminal-notifier
gem "terminal-notifier-guard"
require "terminal-notifier-guard"
guard :shell do
watch(/(.*).go/) do |m|
puts "m: #{m}" # m: ["file-that-was-modified", "folder-file-sits-inside"]
issues = ""
`golint #{m.first}`.tap { |res| issues = res }
puts "\nissues:\n#{issues}" unless issues.empty?
`go run #{m.first}` if issues.empty?
end
end
@Integralist

This comment has been minimized.

Copy link
Owner Author

@Integralist Integralist commented Aug 25, 2015

Alternative that works with long running go programs (such as a web server)...

require "terminal-notifier-guard"

$thread_finished = false

def notify(t)
  $thread_finished = true
  t.kill
end

def pattern(str)
  "ps aux | grep '#{str}' | awk '{ print $2 }'"
end

def extract(pid)
  pid.split("\n").last.to_i
end

def cleanup(regex)
  pid   = `#{pattern regex}`
  pid   = extract pid
  `kill #{pid}`
end

guard :shell do
  watch(/(.*).go/) do |m|
    issues = ""

    `golint #{m.first}`.tap { |res| issues = res }

    puts "\nissues:\n#{issues}" unless issues.empty?

    Thread.new { `go run #{m.first}`; notify(Thread.current) } if issues.empty?

    seconds = 10 # threshold to wait for program to complete

    while (seconds > 0)
      break if $thread_finished
      puts "sleeping\n"
      sleep 1
      seconds -= 1
    end

    cleanup("[0-9] go run #{m.first}")
    cleanup("go-build")

    puts "Program finished\n"
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment