📜 ⬆️ ⬇️

How to mark your TODO, FIXME and ERROR in Xcode


This post is a free translation of the article How to highlight your TODOs, FIXMEs, & ERRORs in Xcode by Hector Matos


It was a very ordinary day: I wrote the code, fixed the bugs, and in general everything was fine. It was then that I wrote a block of code to which I had to return later. This is a common case, which you probably also came across: you had to interact with an API that was not yet ready. I knew the general structure of the object, which I will receive by the API, but I still could not test working with it. Like any other developer, I wrote a comment that looks like this:



At this point, I would like to create a warning in Xcode, the same as we used to do in Objective-C using compiler directives:



But alas, it did not work out and I was sad.



As a man of action, I did what I should: I acted. It turns out you can add a run-script to get the necessary functionality.


RUN SCRIPT BUILD PHASES


Xcode supports internal bash commands or scripts in various phases of your development cycle. You can run a bash script at any time before or after building, running, testing, profiling, analyzing, or even archiving!


To do this, go to the “Build Phases” of your project in Xcode, click on the “+” in the upper left, and then select the “New Run Script Phase” from the drop-down menu:



Then you will see a new section in which you can write a bash script. If you are already an expert in writing Swift scripts after reading the scripting in swift post, you can simply put the script file in the project root directory and call it from your new run script.



# Mark your TODO, FIXME and ERROR with the help of Xcode's native warnings


Put this wonderful code in the body of your run-script:


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

From now on, you will see warnings when you put the TODO: label or FIXME: in the comments! See how this magic works:



We will not stop there and fix the script in such a way as to highlight errors using // ERROR: in the comments. As you know, there are situations when we want to pay special attention by highlighting the error using // ERROR:. To do this, modify your bash script like this:


 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/" 

I don’t know about you, but I’m probably the most forgetful person in the world. At the end of the day, the current code snippet is not always complete and I like to use // ERROR: to remind myself what to work on tomorrow.



When my IDE looks like this , I immediately feel that I need to finish what I have already begun. And do not worry, errors generated by this script do not hinder the assembly of the project.


CONCLUSION


In your daily work, you will always come across a block of code to which you will need to return later, but now you have to patch and move on. Unfortunately, even a simple // TODO:, // FIXME: or // ERROR: comment is just not enough. You'd be surprised how many people forget about their // TODO:, // FIXME: and // ERROR: in the project. Using a run script in this situation is a great way to make sure you don’t miss anything in your development cycle. Hope this helps!


Happy coding, botany comrades!


P.S. Another modification of the script has been extracted from the comments to the original post, in case you still need to prevent the project from being assembled if there are marks // ERROR: in the code:


 TAGS="TODO:|FIXME:" ERRORTAG="ERROR:" OUTPUT=$(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/") ECHO "$OUTPUT" if [[ $OUTPUT == *" error: "* ]] then exit 1 fi 

')

Source: https://habr.com/ru/post/306612/


All Articles