In part one, we were introduced to how run scripts can be utilized as part of Xcode build process with a trivial example..
In this post we will further demonstrate on run scripts but through very meaningful use case...
If you've ever worked with localizable files in iOS, I'm sure you have, You may have notice, I'm not sure you may, that Xcode doesn't detect duplicated keys in the files, i.e. you can have multiple keys with different or even same values and Xcode won't complain about this..
This of course is not a recommended practice at all, and must be avoided, and that's our duty in this post..
Not only that, but we're going to highlight duplicated keys in Xcode as follows..
Insert the following code inside into the code block in your run phase step, this code can be found here..
Now let's go over it line by line...
- We find the
Localizable.strings
files in the directory of the project, and iterate over them. -
SRCROOT
is an environment variable exposed by Xcode during the build process, thusFILE_PATH
represents the absolute path to theLocalizable.strings
file. - We extract only the keys from the file.
- We filter the keys to the only duplicated ones.
- We iterate over the duplicated keys to get the key and its occurrences in the file.
- We iterate over these duplicated occurrences and get the line number then we print what key is duplicated and at which line.
The cool part is when it comes to highlighting this error message in Xcode..
You just need to print the following message during the build process.
echo "${filePath}:${lineNumber}:${columnNumber}: error: {errorMessage}"
This highlights the message in Xcode as follows..
If instead you want a warning instead of error in Xcode, you just need to change it to the following..
echo "${filePath}:${lineNumber}:${columnNumber}: warning: {errorMessage}"
This yields the following output..
Run scripts are such powerful features that can utilize to run any script you can think of... not only can you run shel scripts there, but you can run any sort of script, Your imagination is the limit 🚀
Top comments (0)