Creating groups using a custom template xcode 8
Asked Answered
P

2

40

I have successfully created an Xcode 8 template that will respond to these actions:

  1. You already have a working project
  2. You want to add your custom swift file to this project

Step by step, how to achieve that:

I have created under:

/Users/*YOUR_USER_NAME*/Library/Developer/Xcode/Templates/File Templates

The folder name Custom/Interactor.xctemplate

Files inside that folder:

So when I right click and create new file, I will choose the custom file:

create new file

Creating custom file

The above example works and creates my custom file:

successfully creating a custom file

What I am trying to achieve:

I am trying to create a group that will contain the new custom file. No need for an actual folder or complicated tasks, simply a group that contains the actual file.

So the end result will be:

enter image description here

Since there is no documentation explaining how to create a custom template I have used many references: ref1, ref2, ref3, ref4.

Here is the TemplateInfo.plist file:

<?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>DefaultCompletionName</key>
    <string>MyCustomInteractor</string>
  <key>Description</key>
  <string>Custom interactor</string>
  <key>Kind</key>
  <string>Xcode.IDEKit.TextSubstitutionFileTemplateKind</string>
  <key>Options</key>
  <array>
        <dict>
            <key>Description</key>
            <string>Custom interactor</string>
            <key>Identifier</key>
            <string>fileName</string>
            <key>Name</key>
            <string>Custom name</string>
            <key>NotPersisted</key>
            <true/>
            <key>Required</key>
            <true/>
            <key>Type</key>
            <string>text</string>
        </dict>
    <dict>
            <key>Default</key>
            <string>___VARIABLE_fileName:identifier___</string>
            <key>Identifier</key>
            <string>productName</string>
            <key>Type</key>
            <string>static</string>
    </dict>
    <dict>
            <key>Default</key>
            <string>___VARIABLE_fileName:identifier___Interactor</string>
            <key>Description</key>
            <string>The interactor name</string>
            <key>Identifier</key>
            <string>interactorName</string>
            <key>Name</key>
            <string>Interactor Name:</string>
            <key>Required</key>
            <true/>
            <key>Type</key>
            <string>static</string>
    </dict>
  </array>
  <key>Platforms</key>
  <array>
        <string>com.apple.platform.iphoneos</string>
  </array>
    <key>SortOrder</key>
    <string>99</string>
</dict>
</plist>

What I have tried:

I have tried to insert this answer into my TemplateInfo.plist but nothing happens. In my opinion it could be related to a scope issue - perhaps I am not inserting the Definitions or the Nodes keys in the right place. The xml Definitions and Nodes code snip I have been struggling with:

<key>Definitions</key>
<dict>
    <key>___FILEBASENAME___Interactor.swift</key>
    <dict>
        <key>Group</key>
        <string>Custom Interactor</string>
        <key>Path</key>
        <string>___FILEBASENAME___Interactor.swift</string>
    </dict>
</dict>

<key>Nodes</key>
<array>
    <string>___FILEBASENAME___Interactor.swift</string>
</array>

So where should I insert those keys or what am I doing wrong ?

Any help would be much appreciated.

Thanks.

Papst answered 5/1, 2017 at 12:12 Comment(15)
Exactly same problem here... it seems Definitions and Nodes are only taken into account for project templates, and adding the file into a folder in the Template makes XCode to import the folder instead of creating a group...Remodel
I have reached to the same conclusionPapst
have you managed to do group template?Quake
@wm.p1us, Unfortunately, no.Papst
I found group template (clean-swift.com) and will try to use it for example. May be it will be useful for you as well.Quake
@wm.p1us, Creating a group template when creating a new project IS possible, but creating a bunch of files and group them in an existing project isn't possible at this moment unless you will show me otherwise :)Papst
If you try add scene (from clean-swift) you will see that it is not one file but 7 at once.Quake
Which is exactly what I have asked in this thread and need...Papst
@Papst any luck with this?Raisaraise
@George, unfortunately no. It seems that it is only possible when creating a new project - still looking for a hack/solution :)Papst
@Papst Please do post answer if you findRaisaraise
I would like to know as wellIdette
Yep, over here in 2020--still looking for an answer to this...Apple should really document this.Expectorant
2021: still looking =)Roarke
Yes I am :) Perhaps Apple will give us the solution in near Xcode versionPapst
R
1

You can create groups as well as folders in the project directory using a Nodes dictionary:

<key>Swift</key>
<dict>
    <key>Nodes</key>
    <array>
         <string>ViewController.swift:comments</string>
         <string>ViewController.swift:imports:importCocoa</string>
         <string>ViewController.swift:implementation(___FILEBASENAME___: UIViewController)</string>
         <string>ViewController.swift:implementation:methods:viewDidLoad(override func viewDidLoad(\))</string>
         <string>ViewController.swift:implementation:methods:viewDidLoad:super</string>
         <string>ViewController.swift:implementation:methods:didReceiveMemoryWarning(override func didReceiveMemoryWarning(\))</string>
         <string>ViewController.swift:implementation:methods:didReceiveMemoryWarning:super</string>
         <string>Constant/CommonExtension.swift</string>
    </array>
</dict>

You'll also need to supply Definitions, just like this:

<key>Definitions</key>
<dict>
    <key>Base.lproj/Main.storyboard</key>
    <dict>
        <key>Path</key>
        <string>Main.storyboard</string>
        <key>SortOrder</key>
        <integer>99</integer>
    </dict>
    <key>Group</key>
    <array>
        <string>Singletone</string>
    </array>
    <key>Constant/CommonExtension.swift</key>
    <dict>
        <key>Path</key>
        <string>Constant/CommonExtension.swift</string>
        <key>Group</key>
        <array>
            <string>Constant</string>
        </array>
    </dict>
</dict>
Rubinrubina answered 27/6, 2017 at 5:5 Comment(4)
Does this works on an existing project or only when creating a new project ?Papst
this is work with new project with our custom templateRubinrubina
Thanks but I am aware that it works for a new project. I am looking for a solution of an existing projectPapst
@PratikLad do you know how to set a SortOrder to a Group?Misdoing
R
1

In case this helps someone, here's a project template that works with Xcode 12.4:

<key>Nodes</key>
<array>
    <string>ViewController.swift:comments</string>
    <string>ViewController.swift:imports:importCocoa</string>
    <string>ViewController.swift:implementation(___FILEBASENAME___: UIViewController)</string>
    <string>ViewController.swift:implementation:methods:viewDidLoad(override func viewDidLoad(\))</string>
    <string>ViewController.swift:implementation:methods:viewDidLoad:super</string>
    <string>Info.plist:UIMainStoryboardFile</string>
    <string>Info.plist:UIApplicationSceneManifest:UISceneStoryboardFile</string>
    <string>Some/MainCoordinator.swift</string>
    <string>Some/Other/TestClass.swift</string>
</array>
<key>Definitions</key>
<dict>
    <key>Some/MainCoordinator.swift</key>
    <dict>
        <key>Group</key>
        <array>
            <string>Some</string>
        </array>
        <key>Path</key>
        <string>MainCoordinator.swift</string>
    </dict>
    <key>Some/Other/TestClass.swift</key>
    <dict>
        <key>Group</key>
        <array>
            <string>Some</string>
            <string>Other</string>
        </array>
        <key>Path</key>
        <string>TestClass.swift</string>
    </dict>
</dict>

The group is Some. There's a file inside it. There's also a subgroup inside it called Other. And there's a file inside Other called TestClass.


The Result

Screenshot of the Xcode file navigator showing a top-level folder, Some, with a nested file and folder, Other.

Rolan answered 5/2, 2021 at 5:18 Comment(2)
how do you include more than one file in a single group?Kibler
Where should Definitions and Nodes be? In the root?Tampere

© 2022 - 2024 — McMap. All rights reserved.