Class: Usmu::Github::Pages::Commands::Init

Inherits:
Object
  • Object
show all
Defined in:
lib/usmu/github/pages/commands/init.rb

Instance Method Summary collapse

Constructor Details

#initializeInit

Returns a new instance of Init.



3
4
5
# File 'lib/usmu/github/pages/commands/init.rb', line 3

def initialize(*)
  @log = Logging.logger[self]
end

Instance Method Details

#checkout_destination_branch(destination, branch) ⇒ void (protected)



76
77
78
79
80
# File 'lib/usmu/github/pages/commands/init.rb', line 76

def checkout_destination_branch(destination, branch)
  Dir.chdir destination do
    `git checkout -f #{Shellwords.escape branch} 2>&1`
  end
end

#create_destination_branch(destination, branch) ⇒ void (protected)



68
69
70
71
72
73
74
# File 'lib/usmu/github/pages/commands/init.rb', line 68

def create_destination_branch(destination, branch)
  Dir.chdir destination do
    `git checkout -f --orphan #{Shellwords.escape branch} 2>&1`
    `git rm -r . 2>&1`
    `git clean -fd 2>&1`
  end
end

#ensure_destination!(destination) ⇒ void (protected)



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/usmu/github/pages/commands/init.rb', line 51

def ensure_destination!(destination)
  if File.exist? destination
    unless File.file? File.expand_path('./.git', destination)
      if File.exist? File.expand_path('./.git', destination)
        @log.fatal('Destination directory looks like a git clone not a worktree: ' + destination)
      else
        @log.fatal('Destination directory exists but doesn\'t look like it is controlled by git: ' + destination)
      end
      exit 1
    end
  else
    @log.info("Configuring git worktree at: #{destination}")
    `git worktree prune 2>&1`
    `git worktree add #{Shellwords.escape destination} HEAD 2>&1`
  end
end

#ensure_git!void (protected)



43
44
45
46
47
48
49
# File 'lib/usmu/github/pages/commands/init.rb', line 43

def ensure_git!
  git_version = `git version 2>&1`.split(' ').last
  if Gem::Version.new(git_version) < Gem::Version.new('2.5.0')
    @log.fatal('The Github Pages plugin requires at least git version 2.5.0')
    exit 1
  end
end

#run(ui, config) ⇒ void



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/usmu/github/pages/commands/init.rb', line 7

def run(ui, config)
  # Ensure git (>= 2.5).
  ensure_git!

  # Work out what the correct gh-pages branch is.
  remote = config['remote', default: 'origin']
  branch = config['branch', default: config.default_branch(remote)]
  @log.info("Configuring to deploy to #{remote}/#{branch}")

  # Ensure destination is gitignored.
  destination = ui.configuration['destination', default: 'site']
  gitignore_path = File.expand_path './.gitignore', ui.configuration.config_dir
  gitignore = File.read(gitignore_path).lines.map(&:chomp)
  if gitignore.grep(%r{^/?#{File.basename destination}$}).empty?
    @log.info("Adding #{destination} to gitignore at #{gitignore_path}")
    gitignore.push("#{File.basename destination}")
    File.write gitignore_path, gitignore.join("\n") + "\n"
  end

  # Ensure the destination directory is configured correctly.
  destination = ui.configuration.destination_path
  ensure_destination! destination

  # Check if branch exists locally and remotely.
  branches = `git branch -a`.lines.map{|l| l[2..-1]}.map(&:chomp)
  local = !branches.select{|b| b == branch }.empty?
  remote = !branches.select{|b| b == "remotes/#{remote}/#{branch}" }.empty?
  if !local && !remote
    create_destination_branch destination, branch
  else
    checkout_destination_branch destination, branch
  end
end