class Pliny::Commands::Updater

Attributes

stream[RW]

Public Class Methods

new(stream = $stdout) click to toggle source
# File lib/pliny/commands/updater.rb, line 14
def initialize(stream = $stdout)
  @stream = stream
end
run(stream = $stdout) click to toggle source
# File lib/pliny/commands/updater.rb, line 10
def self.run(stream = $stdout)
  new(stream).run!
end

Public Instance Methods

display(msg) click to toggle source
# File lib/pliny/commands/updater.rb, line 78
def display(msg)
  stream.puts msg
end
ensure_repo_available() click to toggle source

we need a local copy of the pliny repo to produce a diff

# File lib/pliny/commands/updater.rb, line 39
def ensure_repo_available
  if File.exist?(repo_dir)
    unless system("cd #{repo_dir} && git fetch --tags")
      abort("Could not update Pliny repo at #{repo_dir}")
    end
  else
    unless system("git clone https://github.com/interagent/pliny.git #{repo_dir}")
      abort("Could not git clone the Pliny repo")
    end
  end
end
exec_patch() click to toggle source
# File lib/pliny/commands/updater.rb, line 69
def exec_patch
  msg = [
    "Pliny update applied. Please review the changes staged for",
    "commit, and consider applying the diff in .rej files manually.",
    "You can then remove these files with `git clean -f`.",
  ].join("\n")
  exec "git apply -v --reject #{patch_file}; echo '\n\n#{msg}'"
end
get_current_version() click to toggle source
# File lib/pliny/commands/updater.rb, line 51
def get_current_version
  File.read("./Gemfile.lock").split("\n").each do |line|
    next unless pliny_version = line.match(/pliny \(([\d+\.]+)\)/)
    return Gem::Version.new(pliny_version[1])
  end
end
patch_file() click to toggle source
# File lib/pliny/commands/updater.rb, line 86
def patch_file
  File.join(repo_dir, "pliny-update.patch")
end
repo_dir() click to toggle source
# File lib/pliny/commands/updater.rb, line 82
def repo_dir
  File.join(Dir.home, ".tmp/pliny-repo")
end
run!() click to toggle source
# File lib/pliny/commands/updater.rb, line 18
def run!
  unless File.exist?("Gemfile.lock")
    abort("Pliny app not found - looking for Gemfile.lock")
  end

  version_current = get_current_version
  version_target  = Gem::Version.new(Pliny::VERSION)

  if version_current == version_target
    display "Version #{version_current} is current, nothing to update."
  elsif version_current > version_target
    display "pliny-update is outdated. Please update it with `gem install pliny` or similar."
  else
    display "Updating from #{version_current} to #{version_target}..."
    ensure_repo_available
    save_patch(version_current, version_target)
    exec_patch
  end
end
save_patch(curr, target) click to toggle source
# File lib/pliny/commands/updater.rb, line 58
def save_patch(curr, target)
  # take a diff of changes that happened to the template app in Pliny
  diff = `cd #{repo_dir} && git diff v#{curr}..v#{target} lib/template/`

  # remove /lib/template from the path of files in the patch so that we can
  # apply these to the current folder
  diff.gsub!(/(\w)\/lib\/template/, '\1')

  File.open(patch_file, "w") { |f| f.puts diff }
end