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

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

Overview

Update a local site.

Instance Method Summary collapse

Instance Method Details

#callObject



154
155
156
157
158
159
160
161
162
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
# File 'lib/utopia/command/site.rb', line 154

def call
	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) or fail "could not change branch"
	end
	
	DIRECTORIES.each do |directory|
		FileUtils.mkdir_p(File.join(destination_root, directory))
	end
	
	OLD_PATHS.each do |path|
		path = File.join(destination_root, path)
		$stderr.puts "\tRemoving #{path}..."
		FileUtils.rm_rf(path)
	end
	
	CONFIGURATION_FILES.each do |configuration_file|
		source_path = File.join(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
	
	Environment.defaults(destination_root)
	
	begin
		Dir.chdir(destination_root) do
			# Stage any files that have been changed or removed:
			system("git", "add", "-u") or fail "could not add files"
			
			# Stage any new files that we have explicitly added:
			system("git", "add", *Site::CONFIGURATION_FILES) or fail "could not add files"
			
			move_static!
			update_gemfile!
			
			# Commit all changes:
			system("git", "commit", "-m", "Upgrade to utopia #{Utopia::VERSION}.") or fail "could not commit changes"
			
			# Checkout master..
			system("git", "checkout", "master") or fail "could not checkout master"
			
			# and merge:
			system("git", "merge", "--squash", "--no-commit", branch_name) or fail "could not merge changes"
		end
	rescue RuntimeError
		$stderr.puts "** Detected error with upgrade, reverting changes. Some new files may still exist in tree. **"
		
		system("git", "checkout", "master")
	ensure
		system("git", "branch", "-D", branch_name)
	end
end

#move_static!Object

Move legacy ‘pages/_static` to `public/_static`.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/utopia/command/site.rb', line 126

def move_static!
	# If public/_static doens't exist, we are done.
	return unless File.exist? 'pages/_static'
	
	if File.exist? 'public/_static'
		if File.lstat("public/_static").symlink?
			FileUtils.rm_f "public/_static"
		else
			warn "Can't move pages/_static to public/_static, destination already exists."
			return
		end
	end
	
	# One more sanity check:
	if File.directory? 'pages/_static'
		system("git", "mv", "pages/_static", "public/")
	end
end

#update_gemfile!Object

Move ‘Gemfile` to `gems.rb`.



146
147
148
149
150
151
152
# File 'lib/utopia/command/site.rb', line 146

def update_gemfile!
	# If `Gemfile` doens't exist, we are done:
	return unless File.exist?('Gemfile')
	
	system("git", "mv", "Gemfile", "gems.rb")
	system("git", "mv", "Gemfile.lock", "gems.locked")
end