Class: JekyllPush::Branch

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll_push/branch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Branch

Returns a new instance of Branch.

Parameters:

  • target (String)

    the name of the Git branch to deploy to

  • time (String)

    message with the time of deployment

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jekyll_push/branch.rb', line 11

def initialize(target)
  @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
  @msg    = msg

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

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



7
8
9
# File 'lib/jekyll_push/branch.rb', line 7

def target
  @target
end

Instance Method Details

#git_commandsObject



39
40
41
# File 'lib/jekyll_push/branch.rb', line 39

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?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/jekyll_push/branch.rb', line 33

def local?
  !on_travis?
end

#on_travis?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/jekyll_push/branch.rb', line 27

def on_travis?
  !!ENV.fetch('CI', false)
end

#pretty_list(items) ⇒ Object



61
62
63
# File 'lib/jekyll_push/branch.rb', line 61

def pretty_list(items)
  items.each { |i| puts "\t+ #{i.strip}" }
end

#push(dir) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jekyll_push/branch.rb', line 45

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) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/jekyll_push/branch.rb', line 67

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