Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update apps by reapplying the templates files #272

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 35 additions & 40 deletions lib/pliny/commands/updater.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'fileutils'
require 'pathname'
require 'pliny/version'
require 'pliny/commands/creator'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this line is not needed anymore?

require 'uri'

module Pliny::Commands
Expand Down Expand Up @@ -29,62 +30,56 @@ def run!
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

display template_dir
display app_dir
FileUtils.copy_entry template_dir, app_dir
parse_erb_files
end
end

# we need a local copy of the pliny repo to produce a diff
def ensure_repo_available
if File.exists?(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
private

def display(msg)
stream.puts msg
end

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
def template_dir
File.expand_path('../../template', File.dirname(__FILE__))
end

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/`
def app_dir
Pathname.new(name).expand_path
end

# 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')
def parse_erb_files
Dir.glob("#{app_dir}/{*,.*}.erb").each do |file|
static_file = file.gsub(/\.erb$/, '')

File.open(patch_file, "w") { |f| f.puts diff }
end
template = ERB.new(File.read(file), 0)
context = OpenStruct.new(app_name: name)
content = template.result(context.instance_eval { binding })

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}'"
File.open(static_file, "w") do |f|
f.write content
end
FileUtils.rm(file)
end
end

def display(msg)
stream.puts msg
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

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

def patch_file
File.join(repo_dir, "pliny-update.patch")
def app_dir
Dir.pwd
end
end
end
23 changes: 17 additions & 6 deletions spec/commands/updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,36 @@
before do
@io = StringIO.new
@cmd = Pliny::Commands::Updater.new(@io)

allow(@cmd).to receive(:exec_patch)
Config.optional :app_name, nil
end

describe "#run!" do
let(:version) { "0.6.3" }

before do
FileUtils.rm_rf("/tmp/plinytest")
FileUtils.mkdir_p("/tmp/plinytest")
Dir.chdir("/tmp/plinytest")
File.open("/tmp/plinytest/Gemfile.lock", "w") do |f|
f.puts " pliny (0.6.3)"
f.puts " pliny (#{version})"
end
end

it "creates a patch with Pliny diffs between the two versions" do
it "updates the templates to the latest version" do
assert !File.exists?("./Gemfile")
@cmd.run!
patch = File.read(@cmd.patch_file)
assert patch.include?('--- a/Gemfile')
assert patch.include?('-gem "pliny", "~> 0.6"')
assert File.exists?("./Gemfile")
end

describe "when the version is the current one" do
let(:version) { Pliny::VERSION }

it "doesn't do anything" do
assert !File.exists?("./Gemfile")
@cmd.run!
assert !File.exists?("./Gemfile")
end
end
end
end