concatenate_swift_files.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. #
  3. # Combines *.swift files into a single file. Used in Xcode to build a single swift distributive file.
  4. #
  5. # Here is how to use it in Xcode:
  6. #
  7. # 1. Create an "External build system" target.
  8. # 2. Click "Info" tab in target settings.
  9. # 3. In "Build Tool" field specify the path to this script file, for example: $PROJECT_DIR/scripts/concatenate_swift_files.sh
  10. # 4. In "Arguments" field specify the arguments, for example $PROJECT_DIR/YourSubDir $PROJECT_DIR/Distrib/Distrib.swift "// Your header"
  11. # 5. Build the target and it will concatenate your swift files into a single swift file.
  12. #
  13. # You can see an example of using the script in this project: https://github.com/evgenyneu/moa
  14. #
  15. # Usage
  16. # ------
  17. #
  18. # ./combine_swift_files.sh source_dir destination_file [optional_header_text] [remove_line_text]
  19. #
  20. #
  21. # Example
  22. # --------
  23. #
  24. # Use in external build tool in Xcode.
  25. #
  26. # Build tool:
  27. #
  28. # $PROJECT_DIR/scripts/concatenate_swift_files.sh
  29. #
  30. # Arguments:
  31. #
  32. # $PROJECT_DIR/MyProject $PROJECT_DIR/Distrib/MyDistrib.swift "// My header" "remove this line"
  33. #
  34. # Handle paths with spaces (http://unix.stackexchange.com/a/9499)
  35. IFS=$'\n'
  36. destination=$2
  37. headermessage=$3
  38. remove_text=$4
  39. if [ "$#" -lt 2 ]
  40. then
  41. echo "\nUsage:\n"
  42. echo " ./combine_swift_files.sh source_dir destination_file [optional_header_text] [remove text]\n"
  43. exit 1
  44. fi
  45. # Create empty destination file
  46. echo > "$destination";
  47. text=""
  48. destination_filename=$(basename "$destination")
  49. for swift in `find $1 ! -name "$destination_filename" -name "*.swift"`;
  50. do
  51. filename=$(basename "$swift")
  52. text="$text\n// ----------------------------";
  53. text="$text\n//";
  54. text="$text\n// ${filename}";
  55. text="$text\n//";
  56. text="$text\n// ----------------------------\n\n";
  57. if [ -n "$remove_text" ]
  58. then
  59. filecontent="$(cat "${swift}"| sed "/${remove_text}/d";)"
  60. else
  61. filecontent="$(cat "${swift}";)"
  62. fi
  63. text="$text$filecontent\n\n";
  64. echo "Combining $swift";
  65. done;
  66. # Add header message
  67. if [ -n "$headermessage" ]
  68. then
  69. text="$headermessage\n\n$text"
  70. fi
  71. # Write to destination file
  72. echo -e "$text" > "$destination"
  73. echo -e "\nSwift files combined into $destination"