summaryrefslogtreecommitdiff
path: root/.github/workflows/scripts/check-files.sh
blob: 428649bf174650c3b87aaaf747a63172e558e6b1 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/sh
set -eu

SIZE_LIMIT=150000
FAIL=0

check_size() {
	size="$(stat --printf="%s" "$1")"
	if [ "$size" -gt "$SIZE_LIMIT" ]; then
		echo "File $1 is bigger than specified $SIZE_LIMIT limit"
		FAIL=1
	fi
}

check_file_name() {
	fileName="$1"
	expectedFolder="$2"

	shouldname="${expectedFolder}/$(basename "$fileName" |
		iconv --to-code=utf-8 |
		tr '[:upper:]' '[:lower:]' |
		tr '_ ' '-')"

	if [ "$shouldname" != "$fileName" ]; then
		echo "$1 should be named $shouldname."
		FAIL=1
	fi
}

check_webp_name() {
	check_file_name "$1" "data/pix"
}

check_recipe_name() {
	check_file_name "$1" "src"
}

check_recipe_content() {
	errMsgs="$(awk '
		BEGIN {
			HAS_TITLE                   = 0;
			HAS_TAGS                    = 0;
			HAS_INVALID_TAGS            = 0;
			NUM_TAGS                    = 0;
			HAS_INGREDIENTS             = 0;
			HAS_DIRECTIONS              = 0;
			HAS_CONSECUTIVE_EMPTY_LINES = 0;

			CONSECUTIVE_EMPTY_LINES = 0;
		}

		# First line should be the title
		NR == 1 && /^# / {
			HAS_TITLE = 1;
			next;
		}

		$0 == "## Ingredients" {
			HAS_INGREDIENTS = 1;
		}

		$0 == "## Directions" {
			HAS_DIRECTIONS = 1;
		}

		$0 == "" {
			CONSECUTIVE_EMPTY_LINES++
			if (CONSECUTIVE_EMPTY_LINES >= 2) {
				HAS_CONSECUTIVE_EMPTY_LINES = 1;
			}
		}

		$0 != "" {
			CONSECUTIVE_EMPTY_LINES = 0;
		}

		END {
			# Last line should be the tags list
			if ($1 == ";tags:") {
				HAS_TAGS = 1;
				NUM_TAGS = NF - 1;

				# Loop through all the tags
				for (i = 2; i <= NF; i++) {
					# Make sure that each tag only contains lowercase letters and hyphens
					if ($i !~ "^[a-z-]+$") {
						HAS_INVALID_TAGS = 1;
						break;
					}
				}
			}

			if (!HAS_TITLE) {
				print "Recipe does not have a properly formatted title on the first line."
			}

			if (!HAS_TAGS) {
				print "Recipe does not have a properly formatted tags on the last line."
			} else {
				if (HAS_INVALID_TAGS) {
					print "Recipe has invalid tags. Tags must be separated by spaces and contain only lowercase letters or hyphens (-)";
				}

				if (NUM_TAGS < 2) {
					print "Recipe only has " NUM_TAGS " tags. Add some more."
				} else if (NUM_TAGS > 5) {
					print "Recipe has " NUM_TAGS " tags which is too many. Remove some tags."
				}
			}

			if (!HAS_INGREDIENTS) {
				print "Recipe does not have an ingredients list."
			}

			if (!HAS_DIRECTIONS) {
				print "Recipe does not have a directions section."
			}

			if (HAS_CONSECUTIVE_EMPTY_LINES) {
				print "Recipe has at least 2 consecutive empty lines.";
			}
		}
	' "$1")"

	if [ -n "$errMsgs" ]; then
		echo "$errMsgs"
		FAIL=1
	fi
}

while IFS= read -r file; do
	echo "Checking '$file'"
	case "$file" in
		# Ignore these files
		index.md) ;;
		.github/*.md) ;;

		*.webp)
			check_size "$file"
			check_webp_name "$file"
			;;
		*.md)
			check_recipe_name "$file"
			check_recipe_content "$file"
			;;
	esac
	# Separate each file for easier reading.
	echo ""
done <<EOF
$(git diff --name-only "$(git merge-base origin/master HEAD)")
EOF

exit $FAIL