Class: WaxTasks::Branch

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

Overview

Parent class representing a Git Branch that cannot be created directly. Only child classes (LocalBranch, TravisBranch) can be initialized.

Direct Known Subclasses

LocalBranch, TravisBranch

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, target) ⇒ Branch

Returns a new instance of Branch.

Parameters:

  • site (Hash)

    the site config from (TaskRunner.site)

  • target (String)

    the name of the Git branch to deploy to



28
29
30
31
# File 'lib/wax_tasks/branch.rb', line 28

def initialize(site, target)
  @site   = site
  @target = target
end

Instance Attribute Details

#baseurlString

the site baseurl to build with (if on gh-pages)

Returns:

  • (String)

    the current value of baseurl



16
17
18
# File 'lib/wax_tasks/branch.rb', line 16

def baseurl
  @baseurl
end

#commit_msgString

the commit message to use on push

Returns:

  • (String)

    the current value of commit_msg



16
17
18
# File 'lib/wax_tasks/branch.rb', line 16

def commit_msg
  @commit_msg
end

#originString

the current repository remote

Returns:

  • (String)

    the current value of origin



16
17
18
# File 'lib/wax_tasks/branch.rb', line 16

def origin
  @origin
end

#successObject (readonly)

Returns the value of attribute success.



17
18
19
# File 'lib/wax_tasks/branch.rb', line 17

def success
  @success
end

#success_msgString

informative message to be output to console

Returns:

  • (String)

    the current value of success_msg



16
17
18
# File 'lib/wax_tasks/branch.rb', line 16

def success_msg
  @success_msg
end

#targetString

the name of the Git branch to deploy to

Returns:

  • (String)

    the current value of target



16
17
18
# File 'lib/wax_tasks/branch.rb', line 16

def target
  @target
end

Class Method Details

.inheritedObject

This method ensures child classes can be instantiated eventhough Branch.new cannot be.



22
23
24
# File 'lib/wax_tasks/branch.rb', line 22

def self.inherited(*)
  public_class_method :new
end

Instance Method Details

#pushNil

Add, commmit, and push compiled Jekyll site to @target branch

Returns:

  • (Nil)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wax_tasks/branch.rb', line 52

def push
  if @site[:env] == 'prod'
    rebuild if @target == 'gh-pages'
    raise Error::MissingSite, "Cannot find #{SITE_DIR}" unless Dir.exist? SITE_DIR
    Dir.chdir(SITE_DIR)
    system 'git init && git add .'
    system "git commit -m '#{@commit_msg}'"
    system "git remote add origin #{@origin}"
    puts @success_msg.cyan
    system "git push origin master:refs/heads/#{@target} --force"
  else
    puts "Skipping build for branch '#{@target}' on env='test'".orange
  end
end

#rebuildNil

Rebuild the Jekyll site with branch @baseurl

Returns:

  • (Nil)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wax_tasks/branch.rb', line 35

def rebuild
  if @baseurl.empty?
    msg = 'Building the gh-pages _site without a baseurl is not recommended'
    Logger.new($stdout).warn(msg.orange)
  end
  FileUtils.rm_r(SITE_DIR) if File.directory?(SITE_DIR)
  opts = {
    source: '.',
    destination: SITE_DIR,
    baseurl:  @baseurl,
    verbose: true
  }
  Jekyll::Site.new(Jekyll.configuration(opts)).process
end