Skip to content

Instantly share code, notes, and snippets.

@thevirtuoso1973
Last active August 8, 2022 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thevirtuoso1973/399af39a3f3536d6be1ed2a2f124ee0a to your computer and use it in GitHub Desktop.
Save thevirtuoso1973/399af39a3f3536d6be1ed2a2f124ee0a to your computer and use it in GitHub Desktop.
Estimate Code Size
#!/usr/bin/env bash
# Use this script like `./code_size.sh file1 file2 ... > output.txt`
set -eu;
function estimate()
{
cat $1 | wc
# without comments and empty lines:
sed 's/#.*//' $1 \
| sed '/^\s*$/d' \
| wc
# Without the following tokens:
# - '\' at the end of a line.
# - '{{' (removed only if it's a template file)
# - '}}' or '-}}' (removed only if it's a template file)
# - '[ \t]+' replaced with a single space
# - trailing whitespaces
# as well as all the comments and empty lines removed.
case "$1" in
*template*)
sed 's/#.*//' $1 \
| sed 's/\\$//' \
| sed 's/{{//g' \
| sed 's/-\?}}//g' \
| sed 's/[ \t]\+/ /g' \
| sed 's/\s\+$//g' \
| sed '/^\s*$/d' \
| wc
;;
*)
sed 's/#.*//' $1 \
| sed 's/\\$//g' \
| sed 's/[ \t]\+/ /g' \
| sed 's/\s\+$//g' \
| sed '/^\s*$/d' \
| wc
;;
esac
}
for arg in $@
do
estimate $arg
done
@thevirtuoso1973
Copy link
Author

thevirtuoso1973 commented Mar 6, 2022

This should work on bash scripts, templated Dockerfiles (assuming filename contains 'template') and our Modusfiles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment