class JekyllPush::Branch

Attributes

target[R]

Public Class Methods

new(target, opts) click to toggle source

@param target [String] the name of the Git branch to deploy to

# File lib/jekyll_push/branch.rb, line 10
def initialize(target, opts)
  @target = JekyllPush::Utils.slugify target
  @time   = Time.now.strftime('%H:%M on %Y-%m-%d')

  (class << self; include JekyllPush::Travis; end) if on_travis?
  (class << self; include JekyllPush::Local; end)  if local?

  @commit = commit
  @origin = origin opts
  @msg    = msg

  raise JekyllPush::Error::NoOrigin, 'No remote origin was found for the project GitHub repository.' if @origin.empty?
end

Public Instance Methods

git_commands() click to toggle source

@return [Array]

# File lib/jekyll_push/branch.rb, line 38
def git_commands
  ['git init && git add .', "git commit -m '#{@commit}'", "git remote add origin #{@origin}", "git push origin master:refs/heads/#{@target} --force"]
end
local?() click to toggle source

@return [Boolean]

# File lib/jekyll_push/branch.rb, line 32
def local?
  !on_travis?
end
on_travis?() click to toggle source

@return [Boolean]

# File lib/jekyll_push/branch.rb, line 26
def on_travis?
  !!ENV.fetch('CI', false)
end
pretty_list(items) click to toggle source

@return [Nil]

# File lib/jekyll_push/branch.rb, line 60
def pretty_list(items)
  items.each { |i| puts "\t+ #{i.strip}" }
end
push(dir) click to toggle source

@return [Nil]

# File lib/jekyll_push/branch.rb, line 44
def push(dir)
  files = Dir.glob "#{dir}/**/*"
  raise JekyllPush::Error::NoFilesBuilt, "Found no files inside site directory '#{dir}' to push." if files.empty?

  puts Rainbow("\nFound the following _site files:").cyan
  pretty_list files
  puts Rainbow(@msg).cyan

  Dir.chdir dir
  File.open('.info', 'w') { |f| f.write @time }

  git_commands.each { |cmd| system_try cmd }
end
system_try(command) click to toggle source

@return [Nil]

# File lib/jekyll_push/branch.rb, line 66
def system_try(command)
  puts Rainbow("\nTrying `#{command}`").cyan
  success = system command
  return if success

  raise JekyllPush::Error::SystemCall, Rainbow("JekyllPush Failed on command '#{command}'.")
end