generate-project.sh (2964B)
1 #!/bin/sh 2 set -eu 3 4 project_file=Tera.xcodeproj/project.pbxproj 5 localizations_id=A11CE10CA11A710A00000001 6 root_package_id=9F54FC4930051FC4611B37D3 7 root_package_name=tera 8 9 xcodegen generate --spec project.yml 10 11 root_package_reference=$(perl -ne ' 12 if (/^\s*([0-9A-F]{24}) \/\* ([^*]+) \*\/ = \{isa = PBXFileReference; lastKnownFileType = folder; name = (?:"[^"]+"|[^";]+); path = \.; sourceTree = SOURCE_ROOT; \};$/) { 13 print "$1\t$2\n"; 14 } 15 ' "$project_file") 16 root_package_count=$(printf '%s\n' "$root_package_reference" | grep -c . || true) 17 if [ "$root_package_count" -ne 1 ]; then 18 echo "error: expected exactly one root Swift package file reference" >&2 19 exit 1 20 fi 21 generated_root_package_id=$(printf '%s\n' "$root_package_reference" | cut -f1) 22 generated_root_package_name=$(printf '%s\n' "$root_package_reference" | cut -f2-) 23 GENERATED_ROOT_PACKAGE_ID=$generated_root_package_id \ 24 GENERATED_ROOT_PACKAGE_NAME=$generated_root_package_name \ 25 ROOT_PACKAGE_ID=$root_package_id \ 26 ROOT_PACKAGE_NAME=$root_package_name \ 27 perl -0pi -e ' 28 s/\Q$ENV{GENERATED_ROOT_PACKAGE_ID}\E \/\* \Q$ENV{GENERATED_ROOT_PACKAGE_NAME}\E \*\//$ENV{ROOT_PACKAGE_ID} \/\* $ENV{ROOT_PACKAGE_NAME} \*\//g; 29 s/name = "?\Q$ENV{GENERATED_ROOT_PACKAGE_NAME}\E"?; path = \.;/name = $ENV{ROOT_PACKAGE_NAME}; path = .;/g; 30 ' "$project_file" 31 32 if ! grep -Fq "$root_package_id /* $root_package_name */" "$project_file"; then 33 echo "error: deterministic root Swift package reference is absent" >&2 34 exit 1 35 fi 36 if grep -Fq "$generated_root_package_id /* $generated_root_package_name */" "$project_file" && \ 37 { [ "$generated_root_package_id" != "$root_package_id" ] || \ 38 [ "$generated_root_package_name" != "$root_package_name" ]; } 39 then 40 echo "error: generated worktree-specific Swift package reference remains" >&2 41 exit 1 42 fi 43 perl -0pi -e ' 44 s{ 45 (/\*\ Begin\ PBXFileReference\ section\ \*/\n) 46 (.*?) 47 (/\*\ End\ PBXFileReference\ section\ \*/) 48 }{ 49 my ($start, $body, $end) = ($1, $2, $3); 50 my @lines = split(/(?<=\n)/, $body); 51 $start . join("", sort @lines) . $end; 52 }gsex; 53 ' "$project_file" 54 55 temporary_count=$(grep -Ec '"TEMP_[0-9A-F-]+" /\* Localizations \*/' "$project_file" || true) 56 case "$temporary_count" in 57 0) ;; 58 1) 59 perl -0pi -e \ 60 's/"TEMP_[0-9A-F-]+"( \/\* Localizations \*\/)/A11CE10CA11A710A00000001$1/g' \ 61 "$project_file" 62 ;; 63 *) 64 echo "error: unexpected XcodeGen Localizations group inventory" >&2 65 exit 1 66 ;; 67 esac 68 69 if grep -Eq '"TEMP_[0-9A-F-]+" /\* Localizations \*/' "$project_file"; then 70 echo "error: XcodeGen temporary Localizations identifier remains" >&2 71 exit 1 72 fi 73 deterministic_count=$(grep -Ec "$localizations_id /\* Localizations \*/" "$project_file" || true) 74 if [ "$deterministic_count" -gt 1 ]; then 75 echo "error: deterministic Localizations identifier is duplicated" >&2 76 exit 1 77 fi