Minify JSON (or XML) in XCode build phase - xcode

I'm building an iOS app that frequently loads JSON configuration files at runtime.
However, the files are very generous with comments and indenting.
How can I tell XCode to copy minified versions of the files to the bundle during build?

You just need to add a build phase to your target. Here is an example, my build script that converts a multimarkdown file to an HTML file.
# Create the HTML file from the Markdown File
/usr/local/bin/multimarkdown --process-html --output="${SCRIPT_OUTPUT_FILE_0}" --to=html "${SCRIPT_INPUT_FILE_0}"
# Publish the Help Text and Image to Dropbox
if [ -d ~/Dropbox/Public/DCWS-Help-Text ]; then
rsync -t "${SRCROOT}/DC Wire Sizer/en.lproj/"* ~/Dropbox/Public/DCWS-Help-Text/
fi
I create the file in the source directory but added it to my git ignore file. It is a build product and not in source control, but you need to make sure it is in your project and part of the target, so it gets copied into the bundle. Also make sure your build script runs before your copy bundle phase.

Related

premake5: add files to "Copy Bundle Resources" build phase

I have some image files I need to add to an Xcode project. I need the files to be included in the Copy Bundle Resources build phase. I also need Type to be set to Data to avoid any compression or processing by Xcode.
Is this possible with premake5?
You can use a filter to select the files and then do an embed action
filter "files:%{prj.name}/Res/**"
buildaction "Embed"
This example adds all of the files in the Res folder to the Copy Bundle Resources. Although XCode won't show them in the Copy Bundle Resources list they should still show up in the target directory after you build and run the project.
https://github.com/premake/premake-core/wiki/buildaction

Xcode 10 fails to copy file generated by target dependency

I have one target A that just runs a script and generates a bunch of files (localization files via twine). Other targets that copy these localization files have a target dependency on A so that they don't run before the files are generated.
Somehow, this doesn't work. Even though the files are all generated successfully, and I see that the copy files phases are running later, the files aren't found.
Also this only fails when I do a clean build. On the second build, the generated files already exist and the copies complete successfully.
For example, my script runs this:
bundle exec twine generate-all-localization-files Strings/ExtensionStrings.txt MyExtension/Resources --format apple --create-folders --tags ios
find /Users/me/proj/myapp -name Localizable.strings
I see it output /Users/me/proj/myapp/MyExtension/Resources/zh-Hans.lproj/Localizable.strings early on in the build process, then later on I see this:
builtin-copyStrings --validate --outputencoding binary --outdir /Users/me/Library/Developer/Xcode/DerivedData/myapp-adfoisdfnasdao/Build/Products/Debug-iphonesimulator/MyExtension.appex/zh-Hans.lproj -- /Users/me/proj/myapp/MyExtension/Resources/zh-Hans.lproj/Localizable.strings
error: Build input file cannot be found: '/Users/me/proj/myapp/MyExtension/Resources/zh-Hans.lproj/Localizable.strings'
And again, if I build a second time it works. What the heck is going on? I'm guessing this has something to do with Xcode 10's parallel builds but I thought that putting the generation/copy in separate targets with a dependency between them would solve that problem.

Xcode: add files to project in a build phase

In my Xcode project I have a custom build phase which runs a script and downloads some images for use by the app. What I want to do is to automatically add those image to the project during the build. Right now, I have to build once (which downloads the files), and then manually add those files to the project. It works as long as the file names don't change. Instead, I'd like to add all the files in a specific directory to the project.
I've tried setting the Output Files value, as suggested here, like this:
$(PROJECT_DIR)/$(PROJECT_NAME)/External Assets/*
but it doesn't work. Any idea if this can be done?
Create a directory with the .bundle extension. Add this bundle to your app's resources. When the project builds, it will automatically copy every file in the bundle, even if they are changed or added after you first add the bundle to the project.

What is the difference between Copy Bundle Resources and Copy Files for Xcode build phases?

Could someone explain the difference between the Copy Bundle Resources phase of Xcode and a Copy Files phase? When would I use "Copy Files"?
Copy Bundle Resources phase copies files that you want to be available in your bundle (.app). On the other hand Copy Files phase copies files to other (standard) locations accessible from your application (for example to /Library/Fonts) giving you also the option to copy them only when installing. You can also see relevant documentation here
Xcode Copy Files vs Copy Bundle Resources
Copy Files - Copies files from a project to an external specified location.
It is useful, for example, when you are creating an Objective-C Static Library where you should expose .modulemap[About] or/and umbrella.h[About] files
Copy Bundle Resources - Copies files that support your source code into internal structure. If you want to make sure the files you added will get copied to the application bundle
It is useful, when you are creating an app where you can add images and other resources
Vocablurary

Xcode - build phase or script for copying only modified files to App Bundle

Mac OS X 10.5 compatibility required. Xcode 3.2.5.
My app looks in the bundle's Resources folder, for a "data" folder.
"data" contains a hierarchy of subfolders and data. The contents are constantly being modified.
I don't need to navigate or modify this data within Xcode.
Q. When building, how can I copy "data" (say, from the Xcode project's folder) to the Resources folder, but only copying those files within "data" that have been modified since the last build?
(Simply copying all files every time I build is not feasible; the file sizes are too large, slowing build times.)
Thanks for any help.
Shouldn't be a problem to use rsync in a shell script build phase. Something like this:
rsync -a "${SRCROOT}/data" "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"
That does a bit more than date comparison but it should still be quite a bit faster than what you're doing now.
The above doesn't delete any files you removed from the source directory; to do so, you can add --delete but I'd only suggest doing that after you're sure it's working properly.

Resources