-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sh
98 lines (74 loc) · 3.37 KB
/
script.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
#!/bin/bash
set -e
# set output filename
OUTPUT_FILENAME="metadata.md"
# if datapckage.yaml exists, convert to json
if [ -f datapackage.yaml ]; then
cat datapackage.yaml | yq . > datapackage.json
package_format="yaml"
else
package_format="json"
fi
# copy template
cp template.md $OUTPUT_FILENAME
# add title
perl -i -p -e 's/{{{title}}}/'"$(cat datapackage.json | jq -r '.title')"'/g' $OUTPUT_FILENAME
# add repo description
perl -i -p -e 's/{{{repository-description}}}/'"$(cat datapackage.json | jq -r '.description')"'/g' $OUTPUT_FILENAME
# add tree
perl -i -p -e 's/{{{repository-structure}}}/'"$(tree | head -n -2)"'/g' $OUTPUT_FILENAME
# add license info
sed -i "s|{{{license}}}|$(echo "Quest'opera è distribuita con Licenza ["$(cat datapackage.json | jq -r '.licenses[0].title') "]($(cat datapackage.json | jq -r '.licenses[0].path')) ("$(cat datapackage.json | jq -r '.licenses[0].name')")")|g" $OUTPUT_FILENAME
# add contributors table
# contributors_table=$(cat datapackage.json | jq '[.contributors[] | {Name: .title, Role: .role, Email: .email}]' | mlr --j2m cat)
# perl -i -p -e 's/{{{contributors}}}/'"$contributors_table"'/g' $OUTPUT_FILENAME
cat datapackage.json | jq '[.contributors[] | {Name: .title, Role: .role, Email: .email}]' | mlr --j2m cat > frct-contributors.md
sed -i -e '/{{{contributors}}}/r frct-contributors.md' -e '//d' metadata.md
# if frct-contributors.md exists, delete it
if [ -f frct-contributors.md ]; then
rm frct-contributors.md
fi
# data dictionary
# count the number of resources
n_resources=$(cat datapackage.json | jq -r '.resources[].name' | wc -l)
# if dictionary.md exists, delete it
if [ -f dictionary.md ]; then
rm dictionary.md
fi
# create dictionary.md
touch dictionary.md
# test n_resources
# n_resources=2
# loop over resources
for (( i=0; i<$n_resources; i++ ))
do
# get resource infos
filename=$(cat datapackage.json | jq -r '.resources['$i'].name')
filepath=$(cat datapackage.json | jq -r '.resources['$i'].path')
title=$(cat datapackage.json | jq -r '.resources['$i'].title')
# print resource infos
echo "### 📄 [$filename]($filepath)" >> dictionary.md
echo "- Path: \`$filepath\`" >> dictionary.md
echo "- URL:" >> dictionary.md
echo "- Delimiter:" >> dictionary.md
echo "- Encoding: \`$(cat datapackage.json | jq -r '.resources['$i'].encoding')\`" >> dictionary.md
echo "" >> dictionary.md
# create csv table
cat datapackage.json | jq '[.resources['$i'].schema.fields[] | {Field: .name, Type: .type, Description: .description}]' | mlr --j2c cat > frct-schema-$i.csv
# cat datapackage.json | jq '[.resources[0].schema.fields[] | {name, type, title, description} | {Field: .name, Type: .type, Description: ((.title // "") + if .description then ": " else "" end + (.description // ""))}]' | mlr --j2c cat > frct-schema-$i.csv
# join frct-schema-$i.csv with first 2 line of $path (transposed) to get the column example
# convert frct-schema-$i.csv to markdown
mlr --c2m cat frct-schema-$i.csv >> dictionary.md
# rm frct-schema-$i.csv
rm frct-schema-$i.csv
# add a line break
echo "" >> dictionary.md
done
# substitute {{{data-dictionary}}} with dictionary.md
sed -i -e '/{{{data-dictionary}}}/r dictionary.md' -e '//d' $OUTPUT_FILENAME
# delete dictionary.md
rm dictionary.md
# delete unnecessary files
if package_format="yaml"; then
rm datapackage.json
fi