Creating a Custom File Parser
Perhaps you would like to DRY some redundant code in your source?
Or simply make a little shortcut, e.g. something like !fn
or !command
?
Here is how you would configure caseEsac
to replace every instance of "foo"
in your code with "FOO BAR"
:
Create a Processor
foo-converter.sh
#! /bin/bash
# $1 This is the text of a file being parsed
echo "$1" | sed 's/foo/FOO BAR/g'
Now register it with caseEsac
./caseEsac compile --src src/ --out cmd.sh -p foo-converter
Thatโs it!
Every file will be run through your processor.
Available Variables
When processors are run, these environment variables are available:
$VARIABLE |
Description |
---|---|
$CASE_ESAC_SOURCE_ROOT_PATH |
The input path specified by --src |
$CASE_ESAC_OUTPUT_FILE_PATH |
The output path specified by --out |
$CASE_ESAC_FUNCTION_NAME |
The name of the function (inferred from --out or provided via --fn ) |
$CASE_ESAC_FULL_COMMAND_NAME |
The full command name associated with the file being parsed, e.g. myCommand greetings hello (note: this includes the top-level function name) |
$CASE_ESAC_SOURCE_FILE_PATH |
The full path of the current file being parsed |
$CASE_ESAC_SOURCE_RELATIVE_FILE_PATH |
The path of the current file being parsed relative to the root output path being compiled |
$CASE_ESAC_FILE_PROCESSOR |
The binary or function name of the current processor being run |
Parsing One Line at a Time
You might find this sample useful if you want to parse each line of a file, looking for a particular pattern!
foo-converter.sh
#! /bin/bash
soureCodeToReturn=""
while IFS="" read -r line || [ -n "$line" ]
do
if [[ "$line" =~ ^(some)-(pattern)$ ]]
then
# Extract matches from the line
interestingPart="${BASH_REMATCH[1]}"
# Replace this line of code with something interesting
sourceCodeToReturn+="Some code $interestingPart"
else
# Nothing interesting, 'skip' this line
sourceCodeToReturn+="$line"
fi
done < <( printf '%s' "$1" )
printf '%s' "$sourceCodeToReturn"