def version
  @version ||= begin
    plist = File.expand_path('../../Terminal Notifier/Terminal Notifier-Info.plist', __FILE__)
    `/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' '#{plist}'`.strip
  end
end

def filename
  "terminal-notifier-#{version}"
end

def zipfile
  "#{version}.zip"
end

task :clean do
  rm zipfile
  rm_rf "vendor"
end

desc "Fetch latest build from the GitHub releases"
task :update_build do
  unless File.exist?(zipfile)
    sh "curl -L -O 'https://github.com/alloy/terminal-notifier/archive/#{zipfile}'"
  end

  rm_rf "vendor"
  mkdir "vendor"

  sh "unzip -o -d vendor #{zipfile}"
  mv "vendor/#{filename}", 'vendor/terminal-notifier'

  %x(xcodebuild -project '../Terminal\ Notifier.xcodeproj' -target terminal-notifier SYMROOT=build -verbose)
  mv '../build/Release/terminal-notifier.app', 'vendor/terminal-notifier/'
  chmod 0755, 'vendor/terminal-notifier/terminal-notifier.app/Contents/MacOS/terminal-notifier'
end

desc "Build gem"
task :gem => :update_build do
  sh "gem build terminal-notifier.gemspec"
end

desc "Run specs"
task :spec do
  sh "bundle exec ruby spec/terminal-notifier_spec.rb"
end

task :default => :spec
