Class: Begin::GitTemplate

Inherits:
Template show all
Defined in:
lib/begin/template.rb

Overview

Encapsulates the logic for templates that are installed as cloned git repositories on the user’s machine.

Constant Summary

Constants inherited from Template

Template::CONFIG_NAME

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Template

#config, #config_path, #ensure_name_not_empty, #ensure_no_back_references, #ensure_no_conflicts, #process_file, #process_files, #process_path_name, #process_path_names, #process_path_names_in_dir, #run

Constructor Details

#initialize(path) ⇒ GitTemplate

Returns a new instance of GitTemplate.



153
154
155
156
# File 'lib/begin/template.rb', line 153

def initialize(path)
  super path
  @repository = Git.open(path.to_s)
end

Class Method Details

.format_git_error_message(err) ⇒ Object



158
159
160
161
# File 'lib/begin/template.rb', line 158

def self.format_git_error_message(err)
  partition = err.message.partition('2>&1:')
  partition[2]
end

.install(source_uri, path) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/begin/template.rb', line 163

def self.install(source_uri, path)
  Git.clone(source_uri, path.to_s)
  Output.success 'Template source was successfully git cloned'
  GitTemplate.new path
rescue Git::GitExecuteError => e
  raise format_git_error_message(e)
end

Instance Method Details

#check_pending_changesObject



199
200
201
202
203
204
205
206
# File 'lib/begin/template.rb', line 199

def check_pending_changes
  not_added = @repository.status.added.empty?
  not_deleted = @repository.status.deleted.empty?
  not_changed = @repository.status.changed.empty?
  message = 'Local repository contains modified / staged files. ' \
            "Please fix: #{@path}"
  raise message unless not_added && not_deleted && not_changed
end

#check_repositoryObject



175
176
177
178
179
180
181
182
183
184
# File 'lib/begin/template.rb', line 175

def check_repository
  @repository.revparse('HEAD')
  if @repository.current_branch.include? 'detached'
    raise "HEAD is detached in local repository. Please fix: #{@path}"
  end
rescue Git::GitExecuteError => e
  error = "HEAD is not valid in local repository. Please fix: #{@path}\n"
  error += format_git_error_message(e)
  raise error
end

#check_tracking_branchObject



186
187
188
189
190
191
# File 'lib/begin/template.rb', line 186

def check_tracking_branch
  @repository.revparse('@{u}')
rescue StandardError
  raise "Local branch '#{@repository.current_branch}' does not track " \
        "an upstream branch in local repository: #{@path}"
end

#check_untracked_changesObject



193
194
195
196
197
# File 'lib/begin/template.rb', line 193

def check_untracked_changes
  message = 'Local repository contains untracked changes. ' \
            "Please fix: #{@path}"
  raise message unless @repository.status.untracked.empty?
end

#uninstallObject



171
172
173
# File 'lib/begin/template.rb', line 171

def uninstall
  FileUtils.rm_rf @path
end

#updateObject



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/begin/template.rb', line 208

def update
  check_repository
  check_tracking_branch
  check_untracked_changes
  check_pending_changes
  begin
    @repository.pull
  rescue Git::GitExecuteError => e
    raise format_git_error_message(e)
  end
end