What You can Do Using Xcode Building Scripts

Highlight TODO: | FIXME: | ERROR: Annotations

We usually add some special annotations for marking something we will do later, such as TODO:, FIXME: and ERROR:. However, Xcode doesn’t provide any reminders for us to complete these todo tasks. Some debug or unfinished code maybe released to the production environment.

In Xcode projects, we can add custom scripts to pre-process our code files and print some log information.

Customize Building Scripts

Creating path: TARGETS -> Building Settings -> + Add Button -> New Run Script Phase.
Once you complete the operations above, you will get a Run Script phase for executing custom scripts during building like below.

The script will be executed when building the target, lets fulfill the shell textarea with the script below as an example.

1
echo "Hello, my first building script."

Our scripts will execute when the target builds and all echoed messages will be printed into the building process logs.

Log Warning, Error and Note

Some logs with specified formats can display highlight messages onto our source code.

Generic Expression:

1
[filename]:[linenumber]: error | warning | note: [message]
  • filename: The source code file path (not only the filename).
  • linenumber: Highlighted line number.
  • error | warning | note: The Xcode can display three kinds of reminder messages with different highlight styles.
  • message: The description message.

Examples:

We add a warning and an error log to our test.swift source code arbitrarily.

1
2
echo "${SRCROOT}/CommandLine-Swift/test.swift:10: warning: this is a warning message."
echo "${SRCROOT}/CommandLine-Swift/test.swift:11: error: this is an error message."

After building, we can see the highlight reminding banners in our Xcode.

If we can analysis all special annotations (FIXME, TODO, ERROR) in our code and echo the log message through our custom scripts, we can get explicitly compiling warnings or errors from the Xcode.

Here is the magic shell:

1
2
3
TAGS="TODO:|FIXME:"
ERRORTAG="ERROR:"
find "${SRCROOT}" \( -name "*.h" -or -name "*.m" -or -name "*.swift" \) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*\$|($ERRORTAG).*\$" | perl -p -e "s/($TAGS)/ warning: \$1/" | perl -p -e "s/($ERRORTAG)/ error: \$1/"

References

Creating build rules for custom file types