module EmDeploy class Deployment attr_accessor :app_name, :config, :revision def upload_assets bar = ProgressBar.create(title: 'Uploading Assets', progress_mark: '='.colorize(:light_blue), length: 80) count = 0 threads = [] Dir.glob('./dist/assets/*') do |path| t = Thread.new do p = File.expand_path(path) if File.directory?(p) Dir.glob("#{p}/*") { |sub| Uploader.new(sub, config) } else Uploader.new(p, config) end end threads.push(t) end bar.total = threads.count bar.progress = count while count < threads.count do count = threads.map { |t| t unless t.alive? }.compact.count bar.progress = count sleep 0.2 end threads.each(&:join) !threads.map { |t| t.alive? }.compact.include?(true) end def activate_revision redis = Redis.new(config[:redis]) redis.set(revision, File.read('./dist/index.html')) redis.set("#{app_name}:current", revision) end def build_application c = %w{ | / - \\ } t = Thread.new { %x(ember build --environment #{config[:environment]}) } print "\n" print "Building #{app_name.capitalize} " while t.alive? print c[0] sleep 0.1 print "\b" c.push(c.shift) end t.join puts "\n" puts "\n" end def initialize(environment='development') puts %x(tput civis) json = JSON.parse( File.read('./deploy.json') ) custom = {} options = {} options[:aws] = { region: 'us-east-1' } options[:redis] = { host: '127.0.0.1', port: 6379 } options[:environment] = environment if json[environment] json[environment].each do |k,v| if v.class == Hash custom[k.to_sym] = {} v.each { |_k,_v| custom[k.to_sym][_k.to_sym] = _v } else custom[k.to_sym] = v end end end self.config = options.merge!(custom) self.revision = %x(git rev-parse --short HEAD).chomp! self.app_name = JSON.parse( File.read('./package.json') )['name'].downcase.chomp puts "Preparing to deploy #{app_name.capitalize}. Environment: #{environment} Revision: #{"#{revision}".colorize(:light_blue)}" %x(rm -rf #{}/dist) if File.directory?('./dist') build_application if File.directory?('./dist') upload_assets activate_revision end puts "\nDeployment Successful!".colorize(:light_green) puts %x(tput cnorm) end end end