bake/utopia/site.rb in utopia-2.22.1 vs bake/utopia/site.rb in utopia-2.22.2

- old
+ new

@@ -18,11 +18,11 @@ # Configuration files which should be installed/updated: CONFIGURATION_FILES = ['.gitignore', 'config.ru', 'config/environment.rb', 'falcon.rb', 'gems.rb', 'Guardfile', 'bake.rb', 'test/website.rb', 'fixtures/website.rb'] # Directories that should exist: -DIRECTORIES = ["config", "lib", "pages", "public", "bake", "test"] +DIRECTORIES = ["config", "lib", "pages", "public", "bake", "fixtures", "test"] # Directories that should be removed during upgrade process: OLD_PATHS = ["access_log", "cache", "tmp", "Rakefile", "tasks", ".bowerrc"] # The root directory of the template site: @@ -62,20 +62,20 @@ end end system("bundle", "install", chdir: root) or warn "could not install bundled gems" + context.lookup('utopia:environment:setup').call(root: root) + if !File.exist?('.git') Console.logger.info(self) {"Setting up git repository..."} system("git", "init", chdir: root) or warn "could not create git repository" system("git", "add", ".", chdir: root) or warn "could not add all files" system("git", "commit", "-q", "-m", "Initial Utopia v#{Utopia::VERSION} site.", chdir: root) or warn "could not commit files" end - context.lookup('utopia:environment:defaults').call(root) - name = `git config user.name || whoami`.chomp puts puts " #{name},".ljust(78) puts "Thank you for using Utopia!".center(78) @@ -90,66 +90,68 @@ puts " ~ Samuel. ".rjust(78) end # Upgrade an existing site to use the latest configuration files from the template. def upgrade(root: context.root) - branch_name = "utopia-upgrade-#{Utopia::VERSION}" + message = "Upgrade to utopia v#{Utopia::VERSION}." - $stderr.puts "Upgrading #{destination_root}..." + Console.logger.info(self) {"Upgrading #{root}..."} - system('git', 'checkout', '-b', branch_name, chdir: root) or fail "could not change branch" - - DIRECTORIES.each do |directory| - FileUtils.mkdir_p(File.join(root, directory)) - end - - OLD_PATHS.each do |path| - path = File.join(root, path) - Console.logger.info(self) {"Removing #{path}..."} - FileUtils.rm_rf(path) - end - - CONFIGURATION_FILES.each do |configuration_file| - source_path = File.join(SITE_ROOT, configuration_file) - destination_path = File.join(root, configuration_file) + commit_changes(root, message) do + DIRECTORIES.each do |directory| + FileUtils.mkdir_p(File.join(root, directory)) + end - Console.logger.info(self) {"Updating #{destination_path}..."} + OLD_PATHS.each do |path| + path = File.join(root, path) + + if File.exist?(path) + Console.logger.info(self) {"Removing #{path}..."} + FileUtils.rm_rf(path) + end + end - 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 + CONFIGURATION_FILES.each do |configuration_file| + source_path = File.join(SITE_ROOT, configuration_file) + destination_path = File.join(root, configuration_file) + + Console.logger.info(self) {"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 + + context.lookup('utopia:environment:setup').call(root: root) - context.lookup('environment:defaults').call(root) - - begin # Stage any files that have been changed or removed: system("git", "add", "-u", chdir: root) or fail "could not add files" # Stage any new files that we have explicitly added: system("git", "add", *CONFIGURATION_FILES, chdir: root) or fail "could not add files" move_static!(root) update_gemfile!(root) - - # Commit all changes: - system("git", "commit", "-m", "Upgrade to utopia #{Utopia::VERSION}.", chdir: root) or fail "could not commit changes" - - # Checkout main.. - system("git", "checkout", "main", chdir: root) or fail "could not checkout main" - - # and merge: - system("git", "merge", "--squash", "--no-commit", branch_name, chdir: root) or fail "could not merge changes" - rescue => error - Console.logger.error(self, error) {"Upgrade failed."} - - system("git", "checkout", "master", chdir: root) - ensure - system("git", "branch", "-D", branch_name, chdir: root) end end private + +def commit_changes(root, message) + # Ensure the current branch is clean: + system("git", "diff-index", "--quiet", "HEAD", chdir: root) or fail "current branch is not clean" + + begin + yield + rescue + # Clear out the changes: + system("git", "reset", "--hard", "HEAD", chdir: root) or fail "could not reset changes" + raise + end + + # Commit all changes: + system("git", "commit", "-m", message, chdir: root) or fail "could not commit changes" +end # Move legacy `pages/_static` to `public/_static`. def move_static!(root) old_static_path = File.expand_path('pages/_static', root)