-
Notifications
You must be signed in to change notification settings - Fork 6
/
ctagger.rb
91 lines (73 loc) · 1.94 KB
/
ctagger.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class CTagger
TMP_PATH = 'tmp'
ZIPS_PATH = "#{TMP_PATH}/zips"
COMMITS_PATH = "#{TMP_PATH}/commits"
TAGS_PATH = "#{TMP_PATH}/tags"
def initialize(repo_slug, commit_hash)
@repo_slug = repo_slug
@commit_hash = commit_hash
end
def self.create_directories
`mkdir #{TMP_PATH}`
`mkdir #{ZIPS_PATH}`
`mkdir #{COMMITS_PATH}`
`mkdir #{TAGS_PATH}`
end
def tags_exist?
File.file?(tags_path)
end
def generate_tags
download_commit
build_ctags
end
def lookup_tag(tag)
reader = CtagsReader.read(tags_path)
reader.find_all(tag).map do |result|
filename = clean_filename(result.filename)
{
:filename => filename,
:url => "https://github.com/#{@repo_slug}/blob/#{filename}",
:line_number => result.line_number,
:line => result.ex_command
}
end
end
def cleanup
# TODO: beware... check to make sure this commit path is not evil.
`rm -rf #{commit_path}`
`rm #{zip_path}`
end
private
def path_to_commit_zip
"https://codeload.github.com/#{@repo_slug}/zip/#{@commit_hash}"
end
def download_file(url, destination)
IO.copy_stream(open(url), File.open(destination, 'w+'))
end
def unzip_file(zip_path, destination_path)
`unzip #{zip_path} -d #{destination_path}`
end
def move_up_one_directory(directory)
`mv #{directory}/*/* #{directory}/`
end
def download_commit
download_file(path_to_commit_zip, zip_path)
unzip_file(zip_path, commit_path)
move_up_one_directory(commit_path)
end
def build_ctags
`ctags -o #{tags_path} -R --excmd=number #{commit_path}`
end
def zip_path
"#{ZIPS_PATH}/#{@commit_hash}.zip"
end
def commit_path
"#{COMMITS_PATH}/#{@commit_hash}"
end
def clean_filename(commit_path)
commit_path.gsub("#{COMMITS_PATH}/", '')
end
def tags_path
"#{TAGS_PATH}/#{@commit_hash}.tags"
end
end