XcodeGen icon indicating copy to clipboard operation
XcodeGen copied to clipboard

Custom file header comments

Open sgarg17 opened this issue 5 years ago • 4 comments

I am using xcodegen in my project and I would like to change the default file header comment for files in my project as per the following article: https://oleb.net/blog/2017/07/xcode-9-text-macros. The process described requires adding a plist file that will be located at <WorkspaceName>.xcworkspace/xcshareddata/IDETemplateMacros.plist. I am not sure how or if this is achievable with the existing xcodegen project spec. Any advice or suggestions would be appreciated!

sgarg17 avatar Apr 29 '19 18:04 sgarg17

We're facing a similar obstacle as it seems like this isn't currently supported.

We're running XcodeGen via the Swift Package Manager and are hoping that SPM will add support for post run script execution ala NPM, which would allow us to simply copy the file, but this seems to be a controversial feature with an unclear future.

A workaround would be to either wrap your XcodeGen execution in a shell script or put together a snippet that you can execute that does the same. A snippet would look like...

kill $(ps aux | grep 'Xcode' | awk '{print $2}'); swift run xcodegen -p Source/ -s Source/project.yml; open Source/*.xcodeproj; <copy custom header file to project file subdirectory>;

I run this as a snippet, rather than as a script, because the initial kill command can kill the script. 😅

mylesbarros avatar May 06 '19 09:05 mylesbarros

I think moving forward we should perhaps modify the behaviour of XcodeProj to leave the xcshareddata directory alone. So to write files to it but not clear it before doing so. Until then I would suggest wrapping your call to xcodegen in a script that copies this file into the correct place

yonaskolb avatar May 11 '19 08:05 yonaskolb

The postGenCommand below may be used to configure the property list value(s) whenever the project is generated.

[!IMPORTANT] Be sure to set XCSHAREDDATA, HEADER and ORGANIZATION to the correct values for the project.

[!TIP] If ___ORGANIZATIONNAME___ is not used in the HEADER, then ORGANIZATIONNAME does not need to be deleted from and added to the property list, so those two commands and the ORGANIZATION variable may be omitted in this case.

  postGenCommand: |
    XCSHAREDDATA="Example.xcodeproj/xcshareddata"
    HEADER=$'\n//  Copyright © ___YEAR___ ___ORGANIZATIONNAME___\n//'
    ORGANIZATION="Example"
    echo "⚙️  Installing file header template..."
    mkdir -p "$XCSHAREDDATA"
    /usr/libexec/PlistBuddy -c \
        "Delete :FILEHEADER" \
        "${XCSHAREDDATA}/IDETemplateMacros.plist" >/dev/null 2>&1 || true
    /usr/libexec/PlistBuddy -c \
        "Delete :ORGANIZATIONNAME" \
        "${XCSHAREDDATA}/IDETemplateMacros.plist" >/dev/null 2>&1 || true
    /usr/libexec/PlistBuddy -c \
        "Add :FILEHEADER string ${HEADER}" \
        "${XCSHAREDDATA}/IDETemplateMacros.plist" >/dev/null 2>&1
    /usr/libexec/PlistBuddy -c \
        "Add :ORGANIZATIONNAME string ${ORGANIZATION}" \
        "${XCSHAREDDATA}/IDETemplateMacros.plist" >/dev/null 2>&1

tinder-cfuller avatar Jan 01 '24 20:01 tinder-cfuller

Any updates on this? We've recently moved from tuist, which has FileHeaderTemplate and missing this feature in XcodeGen.

levochkaa avatar Jul 05 '24 17:07 levochkaa