Class: Gitkeep

Inherits:
Object
  • Object
show all
Defined in:
lib/gitkeep.rb

Constant Summary collapse

VERSION =
'0.3.2'
@@ignores =
['.git']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGitkeep

Returns a new instance of Gitkeep.



11
12
13
14
15
16
17
18
19
# File 'lib/gitkeep.rb', line 11

def initialize	
	@__test = false
	@dryrun = false
	@interactive = false
	@autoclean = false
	@deindex = false
	@file_count = 0	
	@error_count = 0
end

Instance Attribute Details

#__testObject

Returns the value of attribute __test.



6
7
8
# File 'lib/gitkeep.rb', line 6

def __test
  @__test
end

#autocleanObject

Returns the value of attribute autoclean.



6
7
8
# File 'lib/gitkeep.rb', line 6

def autoclean
  @autoclean
end

#deindexObject

Returns the value of attribute deindex.



6
7
8
# File 'lib/gitkeep.rb', line 6

def deindex
  @deindex
end

#dryrunObject

Returns the value of attribute dryrun.



6
7
8
# File 'lib/gitkeep.rb', line 6

def dryrun
  @dryrun
end

#error_countObject (readonly)

Returns the value of attribute error_count.



7
8
9
# File 'lib/gitkeep.rb', line 7

def error_count
  @error_count
end

#file_countObject (readonly)

Returns the value of attribute file_count.



7
8
9
# File 'lib/gitkeep.rb', line 7

def file_count
  @file_count
end

#interactiveObject

Returns the value of attribute interactive.



6
7
8
# File 'lib/gitkeep.rb', line 6

def interactive
  @interactive
end

Instance Method Details

#add_to_index(files) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/gitkeep.rb', line 77

def add_to_index(files)
	puts "adding files to git index..."
	puts files
	files = files.join(' ') if files.kind_of?(Array)
	`git add -f #{files}`
	puts green("files were added!") 
	puts `git status`
end

#blue(text) ⇒ Object



132
133
134
# File 'lib/gitkeep.rb', line 132

def blue(text)
	colorize(text, 36)
end

#colorize(text, color_code) ⇒ Object



120
121
122
# File 'lib/gitkeep.rb', line 120

def colorize(text, color_code)
	"\e[#{color_code}m#{text}\e[0m"
end

#create(path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitkeep.rb', line 21

def create(path)
	path = "." if path.empty?

	unless File.directory?(path)
		puts red("[error] directory \"#{path}\" not found! abort...")
		return false
	end		

	puts "gitkeep is creating files..."

	Find.find(path) do |p|
		name = File.basename(p)
		if File.directory?(p)
			if File.readable?(p)
				if @@ignores.include?(name)
					Find.prune
				else		  		
					if Dir.entries(p).size == 2
						save(p)
					end

					if @autoclean && Dir.entries(p).size >= 3
						File.delete("#{p}/.gitkeep") if File.exists?("#{p}/.gitkeep")
							
						if @deindex
							`git rm -rf #{p}/.gitkeep`
						end					
					end
				end
			else
				puts red("[error] could not READ in #{p}/ -> check permissions")
				@error_count += 1
			end					
		end 		   	   
	end

	puts "finished. #{@file_count} file(s) created!"
	puts red("#{@error_count} error(s)...") if @error_count > 0
	true if @error_count == 0
end

#green(text) ⇒ Object



124
125
126
# File 'lib/gitkeep.rb', line 124

def green(text)
	colorize(text, 32)
end

#red(text) ⇒ Object



128
129
130
# File 'lib/gitkeep.rb', line 128

def red(text)
	colorize(text, 31)
end

#save(path) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gitkeep.rb', line 88

def save(path)
gitkeep = "#{path}/.gitkeep"

unless File.writable?(path)
	puts red("[error] could not WRITE in #{path}/ -> check permissions")
	@error_count += 1
	return false
end

return true if File.exists?(gitkeep)

if @interactive
	puts "create #{gitkeep} ? [Yn]"
		a = $stdin.gets # read from stdin to avoid collision with params ARGV
		return false unless a == "\n" || a.downcase == "y\n"
	end

	@file_count += 1

	unless @dryrun								
		f = File.new(gitkeep, "w+")
		f.close			
		puts green("created #{gitkeep}")  	    
		true
	else
		puts blue("[dryrun] created #{gitkeep}")
		nil
	end				
end

#search(path) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gitkeep.rb', line 62

def search(path)
	files = []

	Find.find(path) do |p|
		name = File.basename(p)
		if @@ignores.include?(name)
			Find.prune
		else		  
			files << p if name == '.gitkeep'
		end	   	   
	end

	add_to_index(files)
end