Class: Utopia::Command::Site::Update

Inherits:
Samovar::Command
  • Object
show all
Defined in:
lib/utopia/command.rb

Instance Method Summary collapse

Instance Method Details

#invoke(parent) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/utopia/command.rb', line 240

def invoke(parent)
	destination_root = parent.root
	branch_name = "utopia-upgrade-#{Utopia::VERSION}"
	
	$stderr.puts "Upgrading #{destination_root}..."
	
	Dir.chdir(destination_root) do
		system('git', 'checkout', '-b', branch_name)
	end
	
	Setup::Site::DIRECTORIES.each do |directory|
		FileUtils.mkdir_p(File.join(destination_root, directory))
	end
	
	Setup::Site::OLD_DIRECTORIES.each do |directory|
		path = File.join(destination_root, directory)
		$stderr.puts "\tRemoving #{path}..."
		FileUtils.rm_rf(path)
	end
	
	Setup::Site::CONFIGURATION_FILES.each do |configuration_file|
		source_path = File.join(Setup::Site::ROOT, configuration_file)
		destination_path = File.join(destination_root, configuration_file)
		
		$stderr.puts "Updating #{destination_path}..."
		
		FileUtils.copy_entry(source_path, destination_path)
		buffer = File.read(destination_path).gsub('$UTOPIA_VERSION', Utopia::VERSION)
		File.open(destination_path, "w") { |file| file.write(buffer) }
	end
	
	begin
		Dir.chdir(destination_root) do
			# Stage any files that have been changed or removed:
			system("git", "add", "-u")
			
			# Stage any new files that we have explicitly added:
			system("git", "add", *Setup::Site::CONFIGURATION_FILES)
			
			move_static!
			
			# Commit all changes:
			system("git", "commit", "-m", "Upgrade to utopia #{Utopia::VERSION}.")
			
			# Checkout master..
			system("git", "checkout", "master")
			
			# and merge:
			system("git", "merge", "--no-commit", "--no-ff", branch_name)
		end
	rescue RuntimeError
		$stderr.puts "** Detected error with upgrade, reverting changes. Some new files may still exist in tree. **"
		
		system("git", "checkout", "master")
		system("git", "branch", "-d", branch_name)
	end
end

#move_static!Object



230
231
232
233
234
235
236
237
238
# File 'lib/utopia/command.rb', line 230

def move_static!
	if File.lstat("public/_static").symlink?
		FileUtils.rm_f "public/_static"
	end
	
	if File.directory?("pages/_static") and !File.exist?("public/_static")
		system("git", "mv", "pages/_static", "public/_static")
	end
end