Class: RighteousGitHooks::ContentFilesChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/hooks/content-files-are-in-repo.rb

Instance Method Summary collapse

Constructor Details

#initialize(git_root, project_dir, csproj_filename) ⇒ ContentFilesChecker

Returns a new instance of ContentFilesChecker.



9
10
11
12
13
# File 'lib/hooks/content-files-are-in-repo.rb', line 9

def initialize(git_root, project_dir, csproj_filename)
	@git_root = git_root
	@project_dir = project_dir
	@csproj_filename = csproj_filename
end

Instance Method Details

#adjudge!Object



15
16
17
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
50
51
52
53
54
55
56
57
58
# File 'lib/hooks/content-files-are-in-repo.rb', line 15

def adjudge!()
	
	puts 'Checking content files are in your repository...'
	csproj_path = File.join(@git_root, @project_dir, @csproj_filename)
	
	return Result.error("Cannot find #{csproj_path}") unless File.exists? csproj_path
	
	puts "Checking csproj: '#{csproj_path}'"
	csproj = Nokogiri::XML(File.read(csproj_path))
	sins = []
	
	# Make list of all files from repo
	# Make list of all files from csproj
	# Make sure they're the same!
	
	content_files = []
	repo_files = []
	
	Dir.chdir(@git_root) do
		repo_files = `git ls-files`.split("\n")
	end
	
	csproj.css('ItemGroup > Content[Include]').each do |content|
		# Issue: We are just checking individual projects, so we can't check
		# content files in other projects where they are generated in those projects
		# we need some way to include all csproj files in the search.
		next if content['Include'][0..1] == '..'
		next unless (content > "DependentUpon").empty?
		test_path = File.expand_path(File.join(@project_dir, URI.unescape(content['Include']).gsub('\\', '/'))).gsub(@git_root + '/', '')
		unless repo_files.include? test_path then
			sins.push(test_path)
		end
	end

	return Result.success("Congratulations, this is a righteous commit!") if sins.empty?

	message = "\nYou have sinned! The following #{sins.length} files are csproj content files, but do not exist in your repo..."
	sins.each do |sin|
		message = message + "\n#{sin}"
	end
	message = message + "\nPlease add all #{sins.length} files or specify that they're DependentUpon in the csproj before committing."
	
	return Result.error(message)
end