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.



16
17
18
# File 'lib/pliny/commands/updater.rb', line 16

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

Instance Attribute Details

#streamObject

Returns the value of attribute stream.



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

def stream
  @stream
end

Class Method Details

.run(stream = $stdout) ⇒ Object



12
13
14
# File 'lib/pliny/commands/updater.rb', line 12

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

Instance Method Details

#display(msg) ⇒ Object



80
81
82
# File 'lib/pliny/commands/updater.rb', line 80

def display(msg)
  stream.puts msg
end

#ensure_repo_availableObject

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



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pliny/commands/updater.rb', line 41

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



71
72
73
74
75
76
77
78
# File 'lib/pliny/commands/updater.rb', line 71

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



53
54
55
56
57
58
# File 'lib/pliny/commands/updater.rb', line 53

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



88
89
90
# File 'lib/pliny/commands/updater.rb', line 88

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

#repo_dirObject



84
85
86
# File 'lib/pliny/commands/updater.rb', line 84

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

#run!Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pliny/commands/updater.rb', line 20

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



60
61
62
63
64
65
66
67
68
69
# File 'lib/pliny/commands/updater.rb', line 60

def save_patch(curr, target)
  # take a diff of changes that happened to the template app in Pliny
  diff = `cd #{repo_dir} && GIT_CONFIG_GLOBAL=/dev/null 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