Skip to content

Instantly share code, notes, and snippets.

@palaniraja
Created July 4, 2024 22:47
Show Gist options
  • Save palaniraja/940e507a9b7b6833af6825c1f5a62a33 to your computer and use it in GitHub Desktop.
Save palaniraja/940e507a9b7b6833af6825c1f5a62a33 to your computer and use it in GitHub Desktop.
combine multiple .fbs to one
#!/bin/bash
# The output file
output_file='combined.fbs'
# Remove the output file if it already exists
if [ -f $output_file ]; then
rm $output_file
fi
# Variable to store the namespace
namespace=""
# Loop over all .fbs files in the current directory
for f in *.fbs
do
# Exclude the output file
if [ $f != $output_file ]; then
# Check if the file contains a namespace definition
if grep -q "^namespace" $f; then
# Extract the namespace and remove the trailing semicolon
namespace=$(grep "^namespace" $f | cut -d ' ' -f2 | tr -d ';')
fi
# Use grep to exclude lines that start with 'include' or 'namespace'
grep -v '^include\|^namespace' $f >> $output_file
# Append a newline to the output file to separate the contents of the .fbs files
printf "\n" >> $output_file
fi
done
# Prepend the namespace to the output file
printf "namespace $namespace;\n$(cat $output_file)" > $output_file
printf "Combined .fbs files into $output_file\n"
@palaniraja
Copy link
Author

PROMPT

write a bash script to combine all the necessary included files into a single .fbs file, remove ‘include’ if any of the file contains it, handle includes, handle namespaces, and handle namespaces in each of the files assuming all of them are using same namespace and make it compatible with mac without adding additional ; in namespace

original converstation

Sent by you: assume i have file3.fbs include file1.fbs, file2.fbs. what is the flatbuffer command to get all combined.fbs  that parses and embed all the necessary included files definition in it

Sent by you: since flatc cant do that, can you write an bash to do that?

Sent by you: do you see the prb? your for will only copy all the contents, it wont remove "include" if any of the file contains it

Sent by you: No worries! Can you provide an updated script that handles includes?

Sent by you: what about the namespaces in each of the files?

Sent by you: assume all of them are using same namespace

Sent by you: write a bash script to combine all the necessary included files into a single .fbs file, remove ‘include’ if any of the file contains it, handle includes, handle namespaces, and handle namespaces in each of the files assuming all of them are using same namespace

Sent by you: make it compatible with mac

Sent by you: it adds additional ; in namespace

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