add a spinner when deploying, pulling and syncing a site + display a message when a command has been executed

did committed Nov 11, 2015
commit 6905875de71c80e8d75e9d5e0e29cf6c8644159b
Showing 6 changed files with 88 additions and 21 deletions
locomotive/wagon.rb b/lib/locomotive/wagon.rb +2 -2
@@ @@ -92,9 +92,9 @@ module Locomotive
# @param [ Hash ] connection_info The information to get connected to the remote site
# @param [ Hash ] options The options passed to the pull process
#
- def self.sync(env, path, options = {})
+ def self.sync(env, path, options = {}, shell)
require_relative 'wagon/commands/sync_command'
- Locomotive::Wagon::SyncCommand.sync(env, path, options)
+ Locomotive::Wagon::SyncCommand.sync(env, path, options, shell)
end
# Clone a site from a remote LocomotiveCMS engine.
locomotive/wagon/cli.rb b/lib/locomotive/wagon/cli.rb +3 -3
@@ @@ -225,7 +225,7 @@ module Locomotive
def backup(name, host, path = '.')
begin
if Locomotive::Wagon.clone(name, path, { host: host }.merge(options), shell)
- self.print_next_instructions_when_site_created(name, path, true)
+ self.print_next_instructions_when_site_created(name, path, false)
end
rescue Exception => e
self.print_exception(e, options[:verbose])
@@ @@ -324,7 +324,7 @@ module Locomotive
def sync(env, path = '.')
if check_path!(path)
begin
- Locomotive::Wagon.sync(env, path, options)
+ Locomotive::Wagon.sync(env, path, options, shell)
rescue Exception => e
self.print_exception(e, options[:verbose])
exit(1)
@@ @@ -338,7 +338,7 @@ module Locomotive
def pull(env, path = '.')
if check_path!(path)
begin
- Locomotive::Wagon.pull(env, path, options, options[:shell] ? shell : nil)
+ Locomotive::Wagon.pull(env, path, options, shell)
rescue Exception => e
self.print_exception(e, options[:verbose])
exit(1)
locomotive/wagon/commands/concerns/spinner_concern.rb b/lib/locomotive/wagon/commands/concerns/spinner_concern.rb +29 -0
@@ @@ -0,0 +1,29 @@
+ module Locomotive::Wagon
+
+ module SpinnerConcern
+
+ # http://stackoverflow.com/questions/10262235/printing-an-ascii-spinning-cursor-in-the-console
+ def show_wait_spinner(sentence = nil, fps = 10)
+ chars = %w[| / - \\]
+ delay = 1.0/fps
+ iter = 0
+
+ spinner = Thread.new do
+ print sentence if sentence
+
+ while iter do # Keep spinning until told otherwise
+ print chars[(iter += 1) % chars.length]
+ sleep delay
+ print "\b"
+ end
+ end
+
+ yield.tap { # After yielding to the block, save the return value
+ iter = false # Tell the thread to exit, cleaning up after itself…
+ spinner.join # …and wait for it to do so.
+ } # Use the block's return value as the method's
+ end
+
+ end
+
+ end
locomotive/wagon/commands/pull_command.rb b/lib/locomotive/wagon/commands/pull_command.rb +18 -3
@@ @@ -18,23 +18,33 @@ module Locomotive::Wagon
include ApiConcern
include DeployFileConcern
include InstrumentationConcern
+ include SpinnerConcern
def self.pull(env, path, options, shell)
self.new(env, path, options, shell).pull
end
def pull
- PullLogger.new if options[:verbose]
+ if options[:verbose]
+ PullLogger.new
+ _pull
+ else
+ show_wait_spinner('Pulling...') { _pull }
+ end
+ end
+ private
+
+ def _pull
api_client = api_site_client(connection_information)
site = api_client.current_site.get
each_resource do |klass|
klass.pull(api_client, site, path)
end
- end
- private
+ print_result_message
+ end
def each_resource
RESOURCES.each do |name|
@@ @@ -50,6 +60,11 @@ module Locomotive::Wagon
read_deploy_settings(self.env, self.path)
end
+ def print_result_message
+ shell.say "\n\nThe templates, theme assets and content have been pulled from the remote version.", :green
+ true
+ end
+
end
end
locomotive/wagon/commands/push_command.rb b/lib/locomotive/wagon/commands/push_command.rb +15 -7
@@ @@ -22,6 +22,7 @@ module Locomotive::Wagon
include DeployFileConcern
include SteamConcern
include InstrumentationConcern
+ include SpinnerConcern
attr_accessor :platform_url, :credentials
@@ @@ -30,8 +31,17 @@ module Locomotive::Wagon
end
def push
- PushLogger.new if options[:verbose]
+ if options[:verbose]
+ PushLogger.new
+ _push
+ else
+ show_wait_spinner('Deploying...') { _push }
+ end
+ end
+ private
+
+ def _push
require_misc_gems
api_client = build_api_site_client(connection_information)
@@ @@ -44,11 +54,9 @@ module Locomotive::Wagon
klass.push(api_client, steam_services, content_assets_pusher, remote_site)
end
- display_result_message
+ print_result_message
end
- private
-
# To push all the other resources, the big requirement is to
# have the same locales between the local site and the remote one.
def validate!
@@ @@ -98,7 +106,7 @@ module Locomotive::Wagon
# get an instance of the Steam services in order to load the information about the site (SiteRepository)
steam_services.current_site.tap do |site|
# ask for a handle if not found (blank: random one)
- site[:handle] ||= shell.try(:ask, "What is the handle of your site?")
+ site[:handle] ||= shell.try(:ask, "What is the handle of your site? (default: a random one)")
# create the site
attributes = SiteDecorator.new(site).to_hash
@@ @@ -152,8 +160,8 @@ module Locomotive::Wagon
Bundler.require 'misc'
end
- def display_result_message
- shell.say 'Your site has been deployed.', :green
+ def print_result_message
+ shell.say "\n\nYour site has been deployed.", :green
if remote_site.respond_to?(:preview_url)
shell.say "\nTo preview your site, visit: #{remote_site.preview_url.light_white}"
locomotive/wagon/commands/sync_command.rb b/lib/locomotive/wagon/commands/sync_command.rb +21 -6
@@ @@ -13,30 +13,40 @@ require_relative_all 'sync_sub_commands'
module Locomotive::Wagon
- class SyncCommand < Struct.new(:env, :path, :options)
+ class SyncCommand < Struct.new(:env, :path, :options, :shell)
RESOURCES = %w(pages content_entries translations).freeze
include ApiConcern
include DeployFileConcern
include InstrumentationConcern
+ include SpinnerConcern
- def self.sync(env, path, options)
- self.new(env, path, options).sync
+ def self.sync(env, path, options, shell)
+ self.new(env, path, options, shell).sync
end
def sync
- SyncLogger.new if options[:verbose]
+ if options[:verbose]
+ SyncLogger.new
+ _sync
+ else
+ show_wait_spinner('Syncing content...') { _sync }
+ end
+ end
+ private
+
+ def _sync
api_client = api_site_client(connection_information)
site = api_client.current_site.get
each_resource do |klass|
klass.sync(api_client, site, path)
end
- end
- private
+ print_result_message
+ end
def each_resource
RESOURCES.each do |name|
@@ @@ -52,6 +62,11 @@ module Locomotive::Wagon
read_deploy_settings(self.env, self.path)
end
+ def print_result_message
+ shell.say "\n\nThe content of your local Wagon site has been updated.", :green
+ true
+ end
+
end
end