Skip to content

Instantly share code, notes, and snippets.

@nightcycle
Last active May 31, 2024 19:51
Show Gist options
  • Save nightcycle/72f047d051097d7c4db0ed55466e86f2 to your computer and use it in GitHub Desktop.
Save nightcycle/72f047d051097d7c4db0ed55466e86f2 to your computer and use it in GitHub Desktop.
Generate an array of tags for a muse project, just run in the vscodium integrated bash terminal (specifically bash, not powershell) "sh tag-array.sh MythNameHere" and it will generate a script called "TagArray.cs" in the appropriate myth.
#!/bin/bash
myth_name="$1"
codegen_path="Codegen/Tags.cs"
codegen_contents=$(<$codegen_path) # the contents of the codegen script
# Regular expression to match lines containing "public static readonly string".
codegen_regex="public const string[[:space:]]+[[:alnum:]_]+[[:space:]]*=[[:space:]]*\"([^\"]+)\";"
# Array to hold the extracted string values.
declare -a tags_array
# Read each line from the file.
while IFS= read -r line
do
# Check if the line matches the regular expression.
if [[ $line =~ $codegen_regex ]]
then
# Extract the string value using the captured group from the regex.
string_value="${BASH_REMATCH[1]}"
# Append the string value to the array.
tags_array+=("$string_value")
fi
done < $codegen_path
output_path="$myth_name/TagArray.cs"
# Write the starting part of the C# class structure to the file.
echo "// DO NOT EDIT! This is generated by tag-array.sh" > "$output_path"
echo "// If you need to update, run 'sh tag-array.sh' in the bash (not powershell) terminal" >> "$output_path"
echo "namespace $myth_name" >> "$output_path"
echo '{' >> "$output_path"
echo ' public class TagArray' >> "$output_path"
echo ' {' >> "$output_path"
echo ' public static readonly string[] Tags = [' >> "$output_path"
# Loop through each tag in the array and write it to the file.
for tag in "${tags_array[@]}"
do
echo " \"$tag\"," >> "$output_path"
done
# Write the ending part of the array and class structure to the file.
echo ' ];' >> "$output_path"
echo ' }' >> "$output_path"
echo '}' >> "$output_path"
echo "Tags written to $output_path"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment