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



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

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



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

def baseurl
  @baseurl
end

#commit_msgString

the commit message to use on push

Returns:

  • (String)

    the current value of commit_msg



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

def commit_msg
  @commit_msg
end

#originString

the current repository remote

Returns:

  • (String)

    the current value of origin



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

def origin
  @origin
end

#successObject (readonly)

Returns the value of attribute success.



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

def success
  @success
end

#success_msgString

informative message to be output to console

Returns:

  • (String)

    the current value of success_msg



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

def success_msg
  @success_msg
end

#targetString

the name of the Git branch to deploy to

Returns:

  • (String)

    the current value of target



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

def target
  @target
end

Class Method Details

.inheritedObject

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



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

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 #{WaxTasks::SITE_DIR}" unless Dir.exist? WaxTasks::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)


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

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?(WaxTasks::SITE_DIR)
  opts = {
    source: @site[:source_dir] || '.',
    destination: WaxTasks::SITE_DIR,
    config: WaxTasks::DEFAULT_CONFIG,
    baseurl:  @baseurl,
    verbose: true
  }
  Jekyll::Site.new(Jekyll.configuration(opts)).process
end