-
Notifications
You must be signed in to change notification settings - Fork 2
/
comment_shardfile.rb
executable file
·109 lines (76 loc) · 2.44 KB
/
comment_shardfile.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env ruby
##########################################
###
## File: comment_shardfile.rb
## Desc: Do things to the "shard.yml" file
##
#
GITHUB_API_TOKEN = ENV['HOMEBREW_GITHUB_API_TOKEN']
SHARD_FILENAME = 'shard.yml'
puts "processing #{SHARD_FILENAME} ..."
require 'amazing_print' # Pretty print Ruby objects with proper indentation and colors
require 'github_api'
GITHUB = Github.new( oauth_token: GITHUB_API_TOKEN)
require 'pathname' # STDLIB
require 'yaml'
# TODO: Add cli_helper and some command line options.
def normalize?
true
end
$shard_hash = Hash.new
last_desc_start_col = 30 # Pick an acceptable starting place
if ARGV.empty?
shardfile_path = Pathname.pwd
else
shardfile_path = Pathname.new ARGV.first
end
shardfile = shardfile_path.directory? ? shardfile_path + SHARD_FILENAME : shardfile_path
unless shardfile.exist?
puts
puts "ERROR: Invalid path - '#{SHARD_FILENAME}' does not exist"
puts " #{shardfile.parent}"
puts
exit(1)
end
shardfile_bak = Pathname.new (shardfile.to_s + '.bak')
shardfile.rename( shardfile_bak )
SHARDFILE = shardfile.open('w')
# TODO: process the file as a YAML file instead of simple text.
lines = shardfile_bak.read.split("\n")
lines.each do |a_line|
if a_line.include?("\t")
STDERR.puts "ERROR: Found a tab ..."
STDERR.puts a_line
end
if a_line.include?('#')
# don't document something that is already documented
SHARDFILE.puts a_line.chomp
last_desc_start_col = a_line.index('#')
next
end
if normalize?
# if github: entry exists convert it to a plain git: entry
# and add a "desc:" element to the shard
if a_line.strip.start_with? 'github'
repo = a_line.split(':')[1].strip
repo = repo.gsub(/\.git$/,'') if repo.end_with?('.git')
repo_account, repo_name = repo.split('/')
begin
desc = GITHUB.repos.get(repo_account, repo_name)['description']
rescue Exception => e
STDERR.puts "ERROR: #{e}"
STDERR.puts "while processing #{repo}"
desc = "unknown"
end
desc = "unknown" if desc.nil?
puts "#{repo_account}/#{repo_name} #{desc}"
link = "https://github.com/#{repo}"
a_line.gsub!('github', 'git')
a_line.gsub!(repo, link)
# TOdO: what if there is already a "desc:" element?
a_line += "\n desc: '" + desc.gsub("'","") + "'"
end
end # if normalize?
SHARDFILE.puts a_line
end # lines.each do |a_line|
SHARDFILE.close