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



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/utopia/command.rb', line 163

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::SYMLINKS.each do |path, target|
		FileUtils.ln_s(target, File.join(destination_root, path), force: true)
	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, *Setup::Site::SYMLINKS.keys)
			
			# 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