#!/usr/bin/env ruby # Copyright, 2015, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'rubygems' require 'rake' require_relative '../lib/utopia/version' require 'fileutils' require 'find' require 'rake' $app = Rake.application = Rake::Application.new $app.init(File.basename($0)) Rake::TaskManager.record_task_metadata = true verbose(false) module Setup BASE = File.expand_path("../setup", __dir__) module Site CONFIGURATION_FILES = ['config.ru', 'Gemfile', 'Rakefile'] DIRECTORIES = ["cache", "cache/meta", "cache/body", "lib", "pages", "public", "tmp"] SYMLINKS = {"public/_static" => "../pages/_static"} # Removed during upgrade process OLD_DIRECTORIES = ["access_log"] ROOT = File.join(BASE, 'site') end module Server ROOT = File.join(BASE, 'server') end end desc "Create a local utopia instance which includes a basic website template.\n" + "Uses current working directory or path argument if provided." task :create do destination_root = File.expand_path(ARGV.last || '.', Dir.getwd) $stderr.puts "Setting up initial site in #{destination_root}..." Setup::Site::DIRECTORIES.each do |directory| FileUtils.mkdir_p(File.join(destination_root, directory)) end Find.find(Setup::Site::ROOT) do |source_path| # What is this doing? destination_path = File.join(destination_root, source_path[Setup::Site::ROOT.size..-1]) if File.directory?(source_path) FileUtils.mkdir_p(destination_path) else unless File.exist? destination_path FileUtils.copy_entry(source_path, destination_path) end end 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| destination_path = File.join(destination_root, configuration_file) buffer = File.read(destination_path).gsub('$UTOPIA_VERSION', Utopia::VERSION) File.open(destination_path, "w") { |file| file.write(buffer) } end Dir.chdir(destination_root) do if `which bundle`.strip != '' puts "Generating initial package list with bundle..." sh("bundle", "install", "--quiet") end if `which git`.strip == "" $stderr.puts "Now is a good time to learn about git: http://git-scm.com/" elsif !File.exist?('.git') puts "Setting up git repository..." sh("git", "init") sh("git", "add", ".") sh("git", "commit", "-q", "-m", "Initial Utopia v#{Utopia::VERSION} site.") end end name = `git config user.name || whoami`.chomp puts puts " #{name},".ljust(78) puts "Thank you for using Utopia!".center(78) puts "We sincerely hope that Utopia helps to".center(78) puts "make your life easier and more enjoyable.".center(78) puts "" puts "To start the development server, run:".center(78) puts "rake server".center(78) puts "" puts "For extreme productivity, please consult the online documentation".center(78) puts "https://github.com/ioquatix/utopia".center(78) puts " ~ Samuel. ".rjust(78) end desc "Upgrade an existing site to use the latest configuration files from the template.\n" + "Uses current working directory or path argument if provided." task :upgrade do destination_root = File.expand_path(ARGV.last || '.', Dir.getwd) branch_name = "utopia-upgrade-#{Utopia::VERSION}" $stderr.puts "Upgrading #{destination_root}..." Dir.chdir(destination_root) do sh('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: sh("git", "add", "-u") # Stage any new files that we have explicitly added: sh("git", "add", *Setup::Site::CONFIGURATION_FILES, *Setup::Site::SYMLINKS.keys) # Commit all changes: sh("git", "commit", "-m", "Upgrade to utopia #{Utopia::VERSION}.") # Checkout master.. sh("git", "checkout", "master") # and merge: sh("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. **" sh("git", "checkout", "master") sh("git", "branch", "-d", branch_name) end end namespace :server do desc "Create a remote utopia instance suitable for deployment using git.\n" + "Uses current working directory or path argument if provided." task :create do destination_root = File.expand_path(ARGV.last || '.', Dir.getwd) FileUtils.mkdir_p File.join(destination_root, "public") FileUtils.mkdir_p File.join(destination_root, "tmp") Dir.chdir(destination_root) do # Shared allows multiple users to access the site with the same group: sh("git", "init", "--shared") sh("git", "config", "receive.denyCurrentBranch", "ignore") sh("git", "config", "core.worktree", destination_root) sh("cp", "-r", File.join(Setup::Server::ROOT, 'git', 'hooks'), File.join(destination_root, '.git')) end hostname = `hostname`.chomp puts "Now add the git remote to your local repository:\n\tgit remote add production ssh://#{hostname}#{destination_root}" puts "Then push to it:\n\tgit push --set-upstream production master" end end task :help do $app.options.show_tasks = :describe $app.options.show_task_pattern = Regexp.new('') $app.display_tasks_and_comments end task :default => :help $app.top_level