yonaskolb / XcodeGen

A Swift command line tool for generating your Xcode project
MIT License
7.03k stars 818 forks source link

Custom file header comments #575

Open sgarg17 opened 5 years ago

sgarg17 commented 5 years ago

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 .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!

mylesbarros commented 5 years ago

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. 😅

yonaskolb commented 5 years ago

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

tinder-cfuller commented 9 months ago

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
levochkaa commented 3 months ago

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