Class: Sprout::GitTask

Inherits:
Rake::Task
  • Object
show all
Defined in:
lib/sprout/tasks/git_task.rb

Overview

A Rake task for continous integration and automated deployment. This task will automatically load a version_file, increment the last digit (revision), create a new tag in Git with the full version number, and push tags to the remote Git repository.

To use this task, simply add the following to your rakefile:

desc 'Increment revision, tag and push with git'
git :tag do |t|
  t.version_file = 'version.txt'
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, app) ⇒ GitTask

Returns a new instance of GitTask.



32
33
34
35
36
37
# File 'lib/sprout/tasks/git_task.rb', line 32

def initialize(name, app)
  super
  @remote = 'origin'
  @branch = 'master'
  @commit_message = 'Incremented revision number'
end

Instance Attribute Details

#branchObject

The local branch to send, defaults to ‘master’.



27
28
29
# File 'lib/sprout/tasks/git_task.rb', line 27

def branch
  @branch
end

#commit_messageObject

Message to use when committing after incrementing revision number. Defaults to ‘Incremented revision number’.



30
31
32
# File 'lib/sprout/tasks/git_task.rb', line 30

def commit_message
  @commit_message
end

#remoteObject

The remote branch to use, defaults to ‘origin’.



25
26
27
# File 'lib/sprout/tasks/git_task.rb', line 25

def remote
  @remote
end

#version_fileObject

Path to a plain text file that contains a three-part version number.

See Also:



23
24
25
# File 'lib/sprout/tasks/git_task.rb', line 23

def version_file
  @version_file
end

Class Method Details

.define_task(args) {|t| ... } ⇒ Object

Yields:

  • (t)


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sprout/tasks/git_task.rb', line 59

def self.define_task(args, &block)
  begin
    require 'git'
  rescue LoadError => e
    puts "You need to install the 'git' gem. Try running: sudo gem install git"
    raise e
  end
  t = super
  yield t if block_given?
  t.define
  return t
end

Instance Method Details

#defineObject



43
44
45
46
# File 'lib/sprout/tasks/git_task.rb', line 43

def define
  validate
  @version = VersionFile.new(version_file)
end

#execute(*args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/sprout/tasks/git_task.rb', line 48

def execute(*args)
  super
  # Fix numeric comparison....
  while(get_tags.index(@version.to_tag)) do
    @version.increment_revision
  end
  create_tag(@version.to_tag)
  commit
  push
end

#scmObject

Will open on path if no SCM exists yet.



78
79
80
81
82
83
84
85
86
# File 'lib/sprout/tasks/git_task.rb', line 78

def scm
  if(@scm.nil?)
    path = path_to_git
    raise GitTaskError.new("We don't appear to be inside of a git repository") if path.nil?
    @scm = Git.open(path)
  end

  @scm
end

#scm=(scm) ⇒ Object

Accessor for mocking the git gem.



73
74
75
# File 'lib/sprout/tasks/git_task.rb', line 73

def scm=(scm)
  @scm = scm
end

#versionObject



39
40
41
# File 'lib/sprout/tasks/git_task.rb', line 39

def version
  @version.to_s
end