Class: Gitkeep

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGitkeep

Returns a new instance of Gitkeep.



8
9
10
11
12
13
14
15
16
# File 'lib/gitkeep.rb', line 8

def initialize	
	@__test = false
	@dryrun = false
	@interactive = false
	@file_count = 0	
	@error_count = 0

	@@ignores = ['.git']		
end

Instance Attribute Details

#__testObject

Returns the value of attribute __test.



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

def __test
  @__test
end

#dryrunObject

Returns the value of attribute dryrun.



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

def dryrun
  @dryrun
end

#error_countObject (readonly)

Returns the value of attribute error_count.



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

def error_count
  @error_count
end

#file_countObject (readonly)

Returns the value of attribute file_count.



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

def file_count
  @file_count
end

#interactiveObject

Returns the value of attribute interactive.



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

def interactive
  @interactive
end

Instance Method Details

#add_to_index(files) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/gitkeep.rb', line 66

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



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

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

#colorize(text, color_code) ⇒ Object



109
110
111
# File 'lib/gitkeep.rb', line 109

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

#create(path) ⇒ Object



18
19
20
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
# File 'lib/gitkeep.rb', line 18

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
				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



113
114
115
# File 'lib/gitkeep.rb', line 113

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

#red(text) ⇒ Object



117
118
119
# File 'lib/gitkeep.rb', line 117

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

#save(path) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gitkeep.rb', line 77

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
		print "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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gitkeep.rb', line 51

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