-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkReadme.sh
executable file
·141 lines (117 loc) · 3.04 KB
/
mkReadme.sh
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
README_FILE='./README.md'
FRONTMATTER='./frontmatter.md'
BACKMATTER='./backmatter.md'
SCIENTIFIC_REPOSITORIES='./scientific_repositories'
EDUCATIONAL_REPOSITORIES='./educational_repositories'
CALL_DELAY=0.2
function ask_confirmation {
declare -i answer
while true; do
echo "$1"
read -p "Continue? (y/n) " yn
case $yn in
[Yy]* ) answer=0; break;;
* ) answer=1; break;;
esac
done
return $answer
}
function create_API_URIs {
# convert repositories URIs to API URIs and return as indexed array
declare -n uri_array="$1"
mapfile -t uri_array < <( sed -r 's|http(s)?://github\.com/(\w+)/(\w+)(/?)|https://api.github.com/repos/\2/\3|g;s|http(s)?://github\.com/(\w+)(/?)|https://api.github.com/orgs/\2|g' "$2" )
}
function retrieve_info {
# retrieve repository info from API and return JSON output
declare -r uri="$1"
echo $( curl -sS "$uri" )
}
function add_github_resources {
declare -a api_uris
create_API_URIs api_uris "$1"
for uri in "${api_uris[@]}"
do
echo -n '-'
repo_info=$( retrieve_info "$uri" )
name=$( jq -r .name < <( echo "$repo_info") )
desc=$( jq -r .description < <( echo "$repo_info") )
link=$( jq -r .html_url < <( echo "$repo_info") )
if [ -z "$name" ] || [ "$name" = "null" ]
then
continue
fi
echo "- [$name]($link)" >> "$README_FILE"
if [ ! -z "$desc" ] && [ "$desc" != "null" ]
then
desc_length="${#desc}"
if [ $desc_length -lt 110 ]
then
echo -e "\t> *$desc*" >> "$README_FILE"
else
# break line to avoid breaking italics on long lines
desc_array=( $desc )
echo -en "\t> *" >> "$README_FILE"
declare -i length=0
for word in "${desc_array[@]}"
do
if [ $length -ge 110 ]
then
echo -en '*\n\t *' >> "$README_FILE"
length=0
fi
# trim word and write to file
word=$( echo "$word" | xargs )
echo -n "$word " >> "$README_FILE"
word_length="${#word}"
let "length+=word_length"
done
echo '*' >> "$README_FILE"
fi
fi
sleep $CALL_DELAY
done
echo -e '\n' >> "$README_FILE"
}
function main {
cat "$FRONTMATTER" > "$README_FILE"
echo -en '\n' >> "$README_FILE"
echo -n 'API calls ['
if [ -f "$SCIENTIFIC_REPOSITORIES" ]
then
echo -e "### Scientific Resources\n" >> "$README_FILE"
add_github_resources "$SCIENTIFIC_REPOSITORIES"
else
echo "Cannot locate file: $SCIENTIFIC_REPOSITORIES"
fi
if [ -f "$EDUCATIONAL_REPOSITORIES" ]
then
echo -e "### Educational Resources\n" >> "$README_FILE"
add_github_resources "$EDUCATIONAL_REPOSITORIES"
else
echo "Cannot locate file: $EDUCATIONAL_REPOSITORIES"
fi
echo ']'
cat "$BACKMATTER" >> "$README_FILE"
echo -en "\n---\nList updated on" $(date +%F) >> "$README_FILE"
}
ask_confirmation "This operation will reduce your daily free maximum number of Github API calls."
declare -i answer=$?
if [ $answer -gt 0 ]
then
exit
fi
if [ -f "$README_FILE" ]
then
ask_confirmation "README exists. Overwrite $README_FILE ?"
declare -i answer=$?
if [ $answer -eq 0 ]
then
echo '' > "$README_FILE"
else
exit
fi
else
touch "$README_FILE"
fi
main