Skip to content

Commit

Permalink
fixed bug where router would prompt twice
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleypliu committed Aug 11, 2020
1 parent 0f56eac commit c2a2464
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
17 changes: 10 additions & 7 deletions lib/modules/sync_seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ def download_files(source, dest)
end

def create_paths(relative_path)
{ local_path: File.join(LOCAL, relative_path), remote_path: File.join(REMOTE, relative_path) }
{ local_path: File.join(@local_base, relative_path), remote_path: File.join(@remote_base, relative_path) }
end

def check_if_newer(parent_folder, local_item, remote_item, remote_path, local_path, base = LOCAL)
def check_if_newer(parent_folder, local_item, remote_item, remote_path, local_path, base = @local_base)
if parent_folder.include?(local_item)
yield if block_given?

is_newer = Time.at(remote_item.attributes.mtime) >= File.stat(local_path).mtime
# If there are any outdated files, will trigger download
if is_newer
if Dir.glob('*', base: LOCAL).include?(local_item)
if Dir.glob('*', base: @local_base).include?(local_item)
download_files(remote_path, local_path)
# Will be hit if local_item is a file or folder inside a directory
elsif Dir.glob('**/*', base: base).include?(local_item)
Expand All @@ -66,14 +66,14 @@ def check_if_newer(parent_folder, local_item, remote_item, remote_path, local_pa
end
else
# Just download it if it doesn't exist at all
download_files(remote_path, LOCAL)
download_files(remote_path, @local_base)
downloaded = true
end

downloaded
end

def compare_folders(wildcard, local, remote)
def compare_folders(wildcard, local, remote, base = @local_base)
puts "Checking to see what files need to be deleted from #{local}"

remote_list = @session.sftp.dir.glob(remote, wildcard).map do |f|
Expand All @@ -82,7 +82,7 @@ def compare_folders(wildcard, local, remote)

local_list = Dir.glob(wildcard, base: local)

files_for_deletion(local_list, remote_list, local)
files_for_deletion(local_list, remote_list, base)
end

def check_inside_folder(folder, local_list)
Expand All @@ -107,7 +107,10 @@ def check_inside_folder(folder, local_list)
end
end

def main_task(local_list, remote_list)
def main_task(local_list, remote_list, local_base, remote_base)
@local_base = local_base
@remote_base = remote_base

remote_list.each do |object|
# There are files with non-ASCII characters (i.e. accented) in the CMS files
name = object.name.force_encoding('UTF-8')
Expand Down
38 changes: 24 additions & 14 deletions lib/tasks/staging_seeds.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,34 @@ namespace :comfy do
PP_USER = 'wcmc'.freeze

def user_input
puts question = "What would you like to import? 'All/Files/Layouts/Pages' or 'Nothing' to quit"
valid_answers = ['All', 'Files', 'Layouts', 'Pages', 'Nothing']
answer = STDIN.gets.chomp.downcase.capitalize
valid_answers = %w(all files layouts pages nothing)
puts question = "What would you like to import? 'all/files/layouts/pages' or 'nothing' to quit"
answer = STDIN.gets.chomp.downcase

until valid_answers.include?(answer)
puts question
answer = STDIN.gets.chomp.downcase
end

# Fast-tracked unhappy path
abort('Goodbye') if answer == 'Nothing'
abort('Goodbye') if answer == 'nothing'

{ answer: answer, destination: File.join(ComfortableMexicanSofa.config.seeds_path, 'protected-planet') }
end

desc "Import CMS Seed data from staging. Can be run with arguments [<destination>, files/pages/layouts/all] or can accept user input if no argument is supplied"
task :staging_import, %i[to folder] => [:environment] do |_t, args|
to = args[:to].nil? ? user_input[:destination] : File.join(ComfortableMexicanSofa.config.seeds_path, args[:to])
answer = args[:answer].nil? ? user_input[:answer] : args[:folder].downcase.capitalize!

to = nil
answer = nil

if args.length.nil?
answers = user_input
to = answers[:destination]
answer = answers[:answer]
else
to = File.join(ComfortableMexicanSofa.config.seeds_path, args[:to])
answer = args[:folder].downcase
end

puts "Importing CMS Seed data from Staging Folder to #{to} ..."

new_session = SyncSeeds.new(PP_STAGING, PP_USER)
Expand All @@ -38,17 +48,17 @@ namespace :comfy do
local_list = new_session.list_local_files(to)
remote_list = new_session.list_remote_files(FROM)

if answer == 'All'
new_session.main_task(local_list, remote_list)
if answer == 'all'
new_session.main_task(local_list, remote_list, to, FROM)
else
local_list.filter! { |f| f == answer.downcase }
local_list.filter! { |f| f == answer }
remote_list.filter! do |f|
f.name.force_encoding('UTF-8') == answer.downcase
f.name.force_encoding('UTF-8') == answer
end

puts "Downloading a new set of #{answer.downcase}..."
puts "Downloading a new set of #{answer}..."

new_session.main_task(local_list, remote_list)
new_session.main_task(local_list, remote_list, to, FROM)
end

puts "Finished downloads, now replacing your local seed data with your selection..."
Expand Down

0 comments on commit c2a2464

Please sign in to comment.