-
Notifications
You must be signed in to change notification settings - Fork 0
/
.xcgen
executable file
·158 lines (138 loc) · 5.95 KB
/
.xcgen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/zsh
# XCGEN – Automated Bootstrapping for XcodeGen-based Xcode Workflow
#
# This shell script streamlines the setup and maintenance of Xcode projects
# that leverage XcodeGen and the Swift Package Manager (SPM). It executes a
# series of tasks, including:
#
# * Dependency Verification: Checks and ensures that critical system
# dependencies—Homebrew, XcodeGen, and Mint—are installed correctly.
# * Xcode Project Generation: Utilizes XcodeGen to generate an Xcode project file
# from the project specification.
# * Xcode Project Launch: Automatically opens the generated Xcode project.
#
# The script uses two subcommands, 'bootstrap' and 'fetch', to control its execution.
#
# This script encapsulates critical setup and maintenance tasks for XcodeGen-based
# Xcode projects with SPM, significantly simplifying the iOS development workflow.
#
# Usage:
#
# - Place this shell script in the project root directory alongside the XcodeGen manifest.
# - Fine-tune for your use case, e.g., remove GraphQL or SwiftGen related parts if you
# don't need them, or add those specific to your project.
# - Ensure the script is executable with 'chmod +x .xcgen'.
# - Add 'alias xcgen='./.xcgen'' to your '.zshrc' profile.
# - Run 'xcgen bootstrap'.
# - After pulling new code from the repository, regenerate the Xcode project using the 'xcgen' command.
#
# The 'bootstrap' subcommand is geared towards preparing the development environment.
# It installs necessary tools, with a focus on Mint, which is then used for installing
# and managing XcodeGen and other scripts crucial to the project generation pipeline.
#
# In this setup, we utilize the following scripts (should be defined in the Mintfile):
#
# * xcodegen: Generates the Xcode project file from the project specification (in JSON format).
# * xcgbootstrap: Fetches Swift packages as defined in the XcodeGen project specification.
# * apollo-ios-cli: Generates native Swift types from a GraphQL schema.
# * swiftgen: Generates Swift namespaces and values for resources such as colors, fonts,
# images, and localization strings.
#
# The 'fetch' subcommand updates the repositories of dependencies. This command should be
# used when the XcodeGen project specification is updated with new version tags.
#
# The 'clean' subcommand can be used to wipe all the Xcode and SPM related caches.
#
# Ensure packages are proxied by local paths in your XcodeGen manifest, for example:
#
# packages:
# local-swift-collections:
# path: "../Packages/swift-collections/"
# remote-swift-collections:
# url: https://github.com/apple/swift-collections.git
# version: 1.0.2
#
# The xcgbootstrap script automatically creates the 'Packages' folder in the parent directory
# of your project directory. This setup allows Xcode to accommodate local changes in packages,
# track these changes in its GUI, and launch much faster. As a result, the overall developer
# experience is significantly enhanced compared to when remote packages are used.
set -e
set -o pipefail
trap 'echo "An error occurred. Exiting."; exit 1' ERR
PROJECT_NAME="YOUR-PROJECT-NAME"
PROJECT_DIR=$PWD
GRAPHQL_CODEGEN_CONFIG_DIR=$PROJECT_DIR/Autogen/Networking/GraphQL/apollo-codegen-config.json
# Check if homebrew is installed
if ! command -v brew &> /dev/null then
echo "Homebrew is not installed on your system. It is required to bootstrap the project"
echo "Install Homebrew first and try again"
echo "Visit https://brew.sh or use this command:"
echo "/bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
exit 1
fi
export PATH="/opt/homebrew/bin:$PATH"
# Check if realpath is available
if ! command -v realpath &> /dev/null then
echo "'realpath' command is not available on your system. It is required for resolving symlinks"
echo "Install 'coreutils' via Homebrew first and try again"
echo "Use this command:"
echo "brew install coreutils"
exit 1
fi
# Check if mint is installed
if ! command -v mint &> /dev/null then
# Install mint using homebrew
brew install mint
fi
export PATH="$HOME/.mint/bin:$PATH"
# Check if 'bootstrap' subcommand is passed
if [[ $1 == "bootstrap" ]]; then
# Run mint bootstrap
mint bootstrap --link
xcgbootstrap submodules $PROJECT_NAME.xcodeprojless
fi
# Assuming swiftgen is in the PATH, find its symlink.
# Replace 'swiftgen' with the actual symlink if it's different.
SWIFTGEN_SYMLINK=$(which swiftgen)
# Resolve the symlink to the actual path.
SWIFTGEN_PATH=$(realpath $SWIFTGEN_SYMLINK)
# Check for the existence of 'Packages' directory in the parent directory
if [[ ! -d "../Packages" ]]; then
# If 'Packages' directory does not exist, run xcgbootstrap
xcgbootstrap submodules $PROJECT_NAME.xcodeprojless
fi
# Check if 'fetch' subcommand is passed
if [[ $1 == "fetch" ]]; then
# xcgbootstrap submodules $PROJECT_NAME.xcodeprojless
echo "⚙️ Cloning dependencies..."
xcgbootstrap submodules $PROJECT_NAME.xcodeprojless
fi
if [[ $1 == "cleanall" ]]; then
echo "⚙️ Cleaning Xcode, SPM and Xcodegen caches..."
rm -rf ~/Library/org.swift.swiftpm
rm -rf ~/Library/Caches/org.swift.swiftpm
rm -rf ~/Library/Developer/Xcode/DerivedData
rm -rf ~/.xcodegen/cache/
echo "✅ Done"
fi
if [[ $1 == "cleandd" ]]; then
echo "⚙️ Cleaning Xcode caches..."
rm -rf ~/Library/Developer/Xcode/DerivedData
echo "✅ Done"
fi
if [[ $1 == "cleanspm" ]]; then
echo "⚙️ Cleaning SPM caches..."
rm -rf ~/Library/org.swift.swiftpm
rm -rf ~/Library/Caches/org.swift.swiftpm
echo "✅ Done"
fi
echo "⚙️ Generating Swift GraphQL files"
apollo-ios-cli generate -p $GRAPHQL_CODEGEN_CONFIG_DIR
# Use the full path of swiftgen,
# we need this to overcome the bug where
# swift executable cannot access its bundle
# when launched from a symlink
echo "⚙️ Generating Swift Wrappers for assets and localization"
$SWIFTGEN_PATH config run --config "$PROJECT_DIR/Autogen/Assets/swiftgen.yml"
xcodegen generate --spec $PROJECT_NAME.xcodeprojless
open $PROJECT_NAME.xcodeproj