Why Doesn't Swift Package Manager Ask For A Filename?

576 Views Asked by At

In a regular Xcode project app, when I create a new file, it will offer me a template (eg. SwiftUI/Swift/Storyboard) to choose from. I choose a template, click on next, and the prompt will allow me to input the filename. After that, Xcode automatically generates the file with the filename given, the header filename, and the struct name in case of SwiftUI file.

However, in Swift Package Manager projects, creating a new file doesn't give me the chance to input a filename and creates a default Swift or SwiftUI file with placeholder names like file.swift.

Is this a known issue or bug? Why this behavior? Is there anyway to change this?

Question credit

1

There are 1 best solutions below

0
On

Somehow, as of Xcode 14, that's how the default template is configured for Swift package files.

You can change the behaviour by adding a custom template.

  • Create the directory ~/Library/Developer/Xcode/Templates/File Templates/Source/Better Swift.xctemplate/
  • Create a standard ___FILEBASENAME___.swift in it
  • Create IDETemplateMacros.plist in it with
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>SupportsSwiftPackage</key>
    <true/>
    <key>Kind</key>
    <string>Xcode.IDEFoundation.TextSubstitutionFileTemplateKind</string>
    <key>Description</key>
    <string>An empty Swift file.</string>
    <key>Summary</key>
    <string>An empty Swift file</string>
    <key>SortOrder</key>
    <string>1</string>
    <key>Image</key>
    <dict>
        <key>FileTypeIcon</key>
        <string>swift</string>
    </dict>
    <key>AllowedTypes</key>
    <array>
        <string>public.swift-source</string>
    </array>
    <key>Platforms</key>
    <array />
    <key>DefaultCompletionName</key>
    <string>File</string>
    <key>MainTemplateFile</key>
    <string>___FILEBASENAME___.swift</string>
    <key>Options</key>
    <array>
        <dict>
            <key>Identifier</key>
            <string>productName</string>
            <key>Required</key>
            <true/>
            <key>Name</key>
            <string>Name:</string>
            <key>Description</key>
            <string>The name of the file to create</string>
            <key>Type</key>
            <string>text</string>
            <key>NotPersisted</key>
            <true/>
        </dict>
    </array>
</dict>
</plist>

I wrote about it in https://samwize.com/2023/02/28/xcode-header-template-for-swift-packages/