Skip to content

Commit

Permalink
feature: Add lint-sprig script and missing games.
Browse files Browse the repository at this point in the history
  • Loading branch information
shanecelis committed Mar 15, 2023
1 parent 42a9fba commit d0fd150
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
79 changes: 79 additions & 0 deletions bin/lint-sprig
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env bash
# lint-sprig
#
# Do some sanity checks on the sprig games.

metadata=games/metadata.json

if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
echo "Usage: $0 [games/<name>.js ...]" >&2;
cat <<EOF >&2
Perform sanity check on sprig games. Ensures js files in games directory are
represented in metadata file. Checks name for invalid characters. Suggests a
record to add to metadata if not present, pulling title and author from
javascript file. Checks for associated image file.
Does not read metadata to determine if it references files not present.
EOF
exit 2;
fi

if [[ $# -eq 0 ]]; then
files=$(echo games/*.js);
else
files="$@";
fi


err=0
for game in $files; do
name=$(basename "$game" .js);
if [[ ! "$name" =~ ^[a-zA-Z0-9_\-]+$ ]]; then
echo "warning: Unexpected character in file name '$game'." >&2;
err=1;
fi

if ! grep -c "$name" $metadata > /dev/null; then
echo "warning: '$name' for $game not present in $metadata." >&2;
if ! grep '@title' $game >&-; then
echo "error: no @title in $game";
title="$name";
else
title=$(grep '@title' $game | awk '{ print $2 }');
fi
if ! grep '@author' $game >&-; then
echo "error: no @author in $game";
author="N/A";
else
author=$(grep '@author' $game | awk '{ print $2 }');
fi
img="games/img/$name.png";
if [[ ! -f "$img" ]]; then
echo "warning: no '$img' image found for $game." >&2;
img="";
err=1;
fi
date=$(date +%Y-%m-%d);
echo "Suggested fix: add this record to $metadata." >&2;
cat <<EOF >&2
{
"filename": "$name.js",
"title": "$title",
"author": "$author",
"img": "$img",
"tags": [],
"addedOn": "$date"
},
EOF
err=1
fi

# img="games/img/$name.png"
# if [[ ! -f "$img" ]]; then
# echo "warning: expect image '$img' for $game." >&2;
# err=1
# fi
done

exit $err
26 changes: 25 additions & 1 deletion games/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2350,5 +2350,29 @@
"img": "",
"tags": [],
"addedOn": "2023-03-14"
}
},
{
"filename": "Alien_Attack.js",
"title": "Alien_Attack",
"author": "whatware",
"img": "games/img/Alien_Attack.png",
"tags": [],
"addedOn": "2023-03-15"
},
{
"filename": "ElfSokoban.js",
"title": "ElfSokoban",
"author": "Markonije",
"img": "",
"tags": [],
"addedOn": "2023-03-15"
},
{
"filename": "It_Is_Coming_Out!.js",
"title": "It_Is_Coming_Out!",
"author": "N/A",
"img": "games/img/It_Is_Coming_Out!.png",
"tags": [],
"addedOn": "2023-03-15"
},
]

0 comments on commit d0fd150

Please sign in to comment.