Last active
September 29, 2022 00:39
-
-
Save rubeniskov/b039a9a37827421e7eb3981a29d36c01 to your computer and use it in GitHub Desktop.
Concatenate bash sources once to create standalone file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env awk | |
# usage | |
# echo "main.sh" | awk -v header='# HEADER COMMENT' -v footer='# FOOTER COMMENT' -f concat-bash-sources.awk | |
function read_file_content(file) { | |
while ((getline line < file) > 0) { | |
if ( line ~ /^source / ) { | |
print parse_bash_source(line) | |
# remove comments and hashbangs | |
} else if ( line !~ /^#/ ) { | |
print line | |
} | |
} | |
} | |
function match_source_path(line) { | |
return substr(line, 9, length(line) - 9) | |
} | |
function parse_bash_source(line) { | |
src = match_source_path(line) | |
if (cached[src] != 1) { | |
read_file_content(src) | |
} | |
cached[src] = 1 | |
} | |
BEGIN { | |
print "#!/usr/bin/env bash" | |
if (header) { | |
print header | |
} | |
} | |
{ | |
read_file_content($0) | |
} | |
END { | |
if (footer) { | |
print footer | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment