Module: Git::Story::Setup

Extended by:
Utils
Includes:
Utils
Defined in:
lib/git/story/setup.rb

Constant Summary collapse

MARKER =
'Installed by the git-story gem'
HOOKS_DIR =
'.git/hooks'
PREPARE_COMMIT_MESSAGE_SRC =
File.join(__dir__, 'prepare-commit-msg')
PREPARE_COMMIT_MESSAGE_DST =
File.join(HOOKS_DIR, 'prepare-commit-msg')
CONFIG_TEMPLATE =
<<~end
  ---
  pivotal_token:   <%= ENV['PIVOTAL_TOKEN'] %>
  pivotal_project: 123456789
  pivotal_reference_prefix: pivotal
  deploy_tag_prefix: production_deploy_
  semaphore_auth_token: <%= ENV['SEMAPHORE_AUTH_TOKEN'] %>
  semaphore_project_url: https://betterplace.semaphoreci.com/projects/betterplace
  todo_nudging: <%= ENV['TODO_NUDGING'].to_i == 1 %>
end

Class Method Summary collapse

Methods included from Utils

ask, capture, sh

Class Method Details

.file_installed?(filename) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
# File 'lib/git/story/setup.rb', line 57

def file_installed?(filename)
  path = File.join(HOOKS_DIR, filename)
  if File.exist?(path)
    path
  end
end

.install_config(filename, force: false) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/git/story/setup.rb', line 70

def install_config(filename, force: false)
  filename = File.expand_path(filename)
  if !force && File.exist?(filename)
    ask(
      prompt: "File #{filename.to_s.inspect} exists."\
      " Overwrite? (y/n, default is %s) ",
      default: ?n,
    ) do |response|
      if response != ?y
        puts "Skipping creation of #{filename.to_s.inspect}."
        return
      end
    end
  end
  mkdir_p File.dirname(filename)
  File.secure_write(filename) do |io|
    io.puts CONFIG_TEMPLATE
  end
  puts "#{filename.to_s.inspect} was created."
end

.install_file(filename) ⇒ Object



64
65
66
67
68
# File 'lib/git/story/setup.rb', line 64

def install_file(filename)
  File.exist?(HOOKS_DIR) or mkdir_p(HOOKS_DIR)
  cp File.join(__dir__, filename), dest = File.join(HOOKS_DIR, filename)
  puts "#{filename.to_s.inspect} was installed to #{dest.to_s.inspect}."
end

.install_hooks(force: false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/git/story/setup.rb', line 35

def install_hooks(force: false)
  for filename in %w[ prepare-commit-msg pre-push ]
    if path = file_installed?(filename)
      if force || File.read(path).match?(MARKER)
        install_file filename
      else
        ask(
          prompt: "File #{path.inspect} not created by git-story."\
            " Overwrite? (y/n, default is %s) ",
          default: ?n,
        ) do |response|
          if response == ?y
            install_file filename
          end
        end
      end
    else
      install_file filename
    end
  end
end

.perform(force: false) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/git/story/setup.rb', line 25

def perform(force: false)
  unless File.directory?('.git')
    puts "No directory .git found, you need an initialized git repo for this to work"
    return
  end
  install_config('config/story.yml', force: force)
  install_hooks(force: force)
  "Setup was performed."
end