Skip to content

Instantly share code, notes, and snippets.

@evgenyneu
Last active March 27, 2023 00:35
Show Gist options
  • Save evgenyneu/78c188c0940fd3d16935 to your computer and use it in GitHub Desktop.
Save evgenyneu/78c188c0940fd3d16935 to your computer and use it in GitHub Desktop.
Concatenates all Swift files into one file. Used in Xcode to build a single swift distributive file. In Xcode it is done by creating an external build tool configuration.
#!/bin/bash
#
# Combines *.swift files into a single file. Used in Xcode to build a single swift distributive file.
#
# Here is how to use it in Xcode:
#
# 1. Create an "External build system" target.
# 2. Click "Info" tab in target settings.
# 3. In "Build Tool" field specify the path to this script file, for example: $PROJECT_DIR/scripts/concatenate_swift_files.sh
# 4. In "Arguments" field specify the arguments, for example $PROJECT_DIR/YourSubDir $PROJECT_DIR/Distrib/Distrib.swift "// Your header"
# 5. Build the target and it will concatenate your swift files into a single swift file.
#
# You can see an example of using the script in this project: https://github.com/evgenyneu/moa
#
# Usage
# ------
#
# ./combine_swift_files.sh source_dir destination_file [optional_header_text] [remove_line_text]
#
#
# Example
# --------
#
# Use in external build tool in Xcode.
#
# Build tool:
#
# $PROJECT_DIR/scripts/concatenate_swift_files.sh
#
# Arguments:
#
# $PROJECT_DIR/MyProject $PROJECT_DIR/Distrib/MyDistrib.swift "// My header" "remove this line"
#
# Handle paths with spaces (http://unix.stackexchange.com/a/9499)
IFS=$'\n'
destination=$2
headermessage=$3
remove_text=$4
if [ "$#" -lt 2 ]
then
echo "\nUsage:\n"
echo " ./combine_swift_files.sh source_dir destination_file [optional_header_text] [remove text]\n"
exit 1
fi
# Create empty destination file
echo > "$destination";
text=""
destination_filename=$(basename "$destination")
for swift in `find $1 ! -name "$destination_filename" -name "*.swift"`;
do
filename=$(basename "$swift")
text="$text\n// ----------------------------";
text="$text\n//";
text="$text\n// ${filename}";
text="$text\n//";
text="$text\n// ----------------------------\n\n";
if [ -n "$remove_text" ]
then
filecontent="$(cat "${swift}"| sed "/${remove_text}/d";)"
else
filecontent="$(cat "${swift}";)"
fi
text="$text$filecontent\n\n";
echo "Combining $swift";
done;
# Add header message
if [ -n "$headermessage" ]
then
text="$headermessage\n\n$text"
fi
# Write to destination file
echo -e "$text" > "$destination"
echo -e "\nSwift files combined into $destination"
@glassoff
Copy link

glassoff commented Jan 8, 2018

Hello!
You write content to a file with flag "-e" and if file contains line break in code (for example: var foo = "some text\n"), the script will make line break in result file and we get
var foo = "some text
"

You have to use "echo" without "-e" when write content of swift file.

@Adobels
Copy link

Adobels commented Dec 31, 2019

I get this error message error: unable to spawn process (Permission denied) (in target 'toto' from project 'Rx') after setting a project with your " Here is how to use it in Xcode"

Xcode Version 11.2.1 (11B500)

@dimohamdy
Copy link

dimohamdy commented Mar 21, 2023

@Adobels Make the script executable using chmod 755 YourScriptName.sh

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