Skip to content

Instantly share code, notes, and snippets.

@kshaa
Last active May 24, 2019 18:17
Show Gist options
  • Select an option

  • Save kshaa/7e22dc9d1c0e0b3948fb5b89a68b66c6 to your computer and use it in GitHub Desktop.

Select an option

Save kshaa/7e22dc9d1c0e0b3948fb5b89a68b66c6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Usage:
#
# # Compile main.cpp to merged_main.cpp
# ./merge.sh
#
# # Compile index.cpp to merged_main.cpp
# ./merge.sh index.cpp
#
# # Compile index.cpp to output.cpp
# ./merge.sh index.cpp output.cpp
#
# # Compile index.cpp to merged_main.cpp and prepend a notice for programming teacher as to why I use this
# ./merge.sh index.cpp output.cpp --add-notice
mergefile="Mergefile"
defaultMain="${1:-main.cpp}"
outfile="${2:-merged_main.cpp}"
# Support for default Mergefile
usingDefaultMergefile=false
if [ ! -f $mergefile ]
then
echo "$defaultMain" > $mergefile
echo "Default merge file not found, supposing '$defaultMain' is the main file to be preprocessed!"
usingDefaultMergefile=true
fi
# Download Node merge script and run it
merge="merge.js"
curl https://raw.githubusercontent.com/kshaa/single-c-file/master/single-c-file.js > $merge
node $merge $mergefile $outfile
# Flag-based (--add-notice) notice for teacher regarding how this merge works
if [[ $* == *--add-notice* ]]
then
printf '%s\n%s\n' "// This was to work around the following issue - https://github.com/PPakalns/APTS/issues/8" "$(cat $outfile)" > $outfile
printf '%s\n%s\n' "// P.S. The original author of the script is Adrian Dawid, I personally forked it and added some extra features" "$(cat $outfile)" > $outfile
printf '%s\n%s\n' "// Source of the NodeJS script - https://github.com/kshaa/single-c-file" "$(cat $outfile)" > $outfile
printf '%s\n%s\n' "// This content was generated using a tiny NodeJS script which acts as a C++ preprocessor" "$(cat $outfile)" > $outfile
fi
# Remove default Mergefile
if [ "$usingDefaultMergefile" = true ]
then
rm $mergefile
fi
# Remove downloaded Node merge script
rm $merge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment