Class: Razor::Rake::Git::Generate

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/razor/rake/git_generate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = 'git:generate') {|_self| ... } ⇒ Generate

Returns a new instance of Generate.

Yields:

  • (_self)

Yield Parameters:



35
36
37
38
39
40
41
42
# File 'lib/razor/rake/git_generate.rb', line 35

def initialize(name='git:generate')
	@name = name
	@template_branch = 'master'
	@site_branch = 'site'
	@url_base = '/'
	yield self if block_given?
	define_task
end

Instance Attribute Details

#site_branchObject

Returns the value of attribute site_branch.



32
33
34
# File 'lib/razor/rake/git_generate.rb', line 32

def site_branch
  @site_branch
end

#template_branchObject

Returns the value of attribute template_branch.



31
32
33
# File 'lib/razor/rake/git_generate.rb', line 31

def template_branch
  @template_branch
end

#url_baseObject

Returns the value of attribute url_base.



33
34
35
# File 'lib/razor/rake/git_generate.rb', line 33

def url_base
  @url_base
end

Instance Method Details

#checkout(branch) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/razor/rake/git_generate.rb', line 75

def checkout(branch)
	if block_given?
		old = current_branch
		begin
			`git checkout #{branch}`
			yield
		ensure
			`git checkout #{old}`
		end
	else
		`git checkout #{branch}`
	end
end

#current_branchObject



70
71
72
73
# File 'lib/razor/rake/git_generate.rb', line 70

def current_branch
	`git status` =~ /^# On branch (.+)/
	return $1
end

#define_taskObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/razor/rake/git_generate.rb', line 44

def define_task
	task(@name, [:message]) { |t, args|
		ensure_on_branch template_branch
		ensure_branch_is_clean # needed ?
		# .gitinore workaround
		#TODO: Solve this problem
		gitignore = File.read '.gitignore' rescue ''
		Dir.mktmpdir { |tmp|
			Razor.generate(generate_options, '.', tmp)
			checkout site_branch do
				sh 'git rm -r .'
				sh "cp -r #{tmp}/* ."
				# .gitignore workaround
				File.open('.gitignore', 'w') {|f| f << gitignore }
				sh 'git add .'
				msg = args.message
				if msg.nil? or msg.empty?
					sh 'git commit'
				else
					sh "git commit -m #{msg}"
				end
			end
		}
	}
end

#ensure_branch_is_cleanObject

Raises:



97
98
99
100
101
# File 'lib/razor/rake/git_generate.rb', line 97

def ensure_branch_is_clean
	clean = (`git status` =~ /nothing to commit \(working directory clean\)\n$/ )
	clean and return
	raise CleanBranchError
end

#ensure_on_branch(branch) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/razor/rake/git_generate.rb', line 89

def ensure_on_branch(branch)
	current_branch == branch and return
	ensure_branch_is_clean
	checkout branch
rescue CleanBranchError
	raise BranchError.new(branch)
end

#generate_optionsObject



103
104
105
106
107
# File 'lib/razor/rake/git_generate.rb', line 103

def generate_options
	{
		:url_base => @url_base
	}
end