Class: Pliny::Commands::Updater

Inherits:
Object
  • Object
show all
Defined in:
lib/pliny/commands/updater.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream = $stdout) ⇒ Updater

Returns a new instance of Updater.



14
15
16
# File 'lib/pliny/commands/updater.rb', line 14

def initialize(stream = $stdout)
  @stream = stream
end

Instance Attribute Details

#streamObject

Returns the value of attribute stream.



8
9
10
# File 'lib/pliny/commands/updater.rb', line 8

def stream
  @stream
end

Class Method Details

.run(stream = $stdout) ⇒ Object



10
11
12
# File 'lib/pliny/commands/updater.rb', line 10

def self.run(stream = $stdout)
  new(stream).run!
end

Instance Method Details

#display(msg) ⇒ Object



78
79
80
# File 'lib/pliny/commands/updater.rb', line 78

def display(msg)
  stream.puts msg
end

#ensure_repo_availableObject

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



39
40
41
42
43
44
45
46
47
48
49
# 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_patchObject



69
70
71
72
73
74
75
76
# 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_versionObject



51
52
53
54
55
56
# 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_fileObject



86
87
88
# File 'lib/pliny/commands/updater.rb', line 86

def patch_file
  File.join(repo_dir, "pliny-update.patch")
end

#repo_dirObject



82
83
84
# File 'lib/pliny/commands/updater.rb', line 82

def repo_dir
  File.join(Dir.home, ".tmp/pliny-repo")
end

#run!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# 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