Xcode - change project templates - xcode

So I can add new templates for single files, but I want Xcode to uses these files when setting up the new project (most noticeably the Controller and Delegate .h and .m files). How do I do that?

In addition to the templates for individual classes that you found, there are also templates for the various project types. If you just want to replace a single class file that is already being added in the projects you can just replace the file in each of the project template files (as long as the names are the same). You will need to copy the template files into each project template that you want to change it in. The other option is to make your own project templates with whatever default files you want.

Related

How to support Xcode Templates for SwiftPM?

I have Xcode Template that generate a bunch of files (not project) with some predefined repetitive code (for example classes for MVVM pattern). When I try to create these files in Xcode Project all works fine (I can find and create my templates in New File… menu), but when I try to do the same thing in SwiftPM I can't find my templates in the New File… menu.
All my templates are leaving in ~/Library/Developer/Xcode/Templates/**/*.xctemplate.
What should I do to show templates for SwiftPM as well?
After some research of /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/ folder I found that some default templates have the property SupportsSwiftPackage that set to YES. I added it to my templates and it finally works.

How to create custom SwiftUI View playground template in Xcode?

I want to create a custom Xcode playground template for SwiftUI View.
What file formats are used?
How can I find examples?
Where should I put those files?
You can make template by duplicating file templates at path
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Playground
Note that each platform has it's own templates.
Copy two folders TEMPLATE_NAME.xctemplate and TEMPLATE_NAME___FileMenu.xctemplate
to ~/Library/Developer/Xcode/Templates/File Templates and rename them as you want.
(you can put it in subfolder SwiftUI, it's name will be used to separate your templates from others)
To change the displayed name of template you can change Name field in TemplateInfo.plist
Paste the code of your template into ___FILEBASENAME___.playground file (you can use placeholders to generate code base on parameters, see for example this article)
Here are my templates as an example: https://github.com/DanSkeel/XcodeTemplates/tree/main/Playground
Note: ...___FileMenu.xctemplate files will be used when you create new file from opened playground.

Is it possible to configure a csproj file to allow any project item type?

I'm working on a unique project in that I would like to be able to have CSHTML Razor Views, Windows Forms, and various other "non project type crossing" project item templates in one single project in visual studio.
Is there a way I can override the project type so that it compiles as a Windows Form's project but still allows me to add CSHTML files and other template types to it?
When you edit the .csproj using notepad or an xml editor you'll find that <ProjectTypeGuids> element.
You can then edit it to have an entry like below
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Note: these are Guids from a portable class library project. You need to find correct ProjectTypeGuids for your requirements and add it.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/d9d05cdc-96a1-4044-95d8-a4f8885a660a/what-is-the-significance-of-projecttypeguids-tag-in-the-visual-studio-project-file?forum=vsx

How do I disconnect code generated by a T4 template from that template?

When I 'run' a T4 template, the class files it generates for me all appear under the template. I cannot remove the template from the project without the files going away too. I also can't copy the files out from 'under' the template into the project root because the are already in the project root. How can I emancipate these files from the template that created them?
Get creative:
Right click and open containing folder.
Drag-and-drop the generated file to another location in the solution.
Delete the original template and file from Visual Studio.
Presto.
If you want to do one-time code-generation with T4 then I would suggest using T4 scaffolding. This tool was originally created to support the generation of views, controllers etc. in MVC3 but you can use it on pretty much anything.

ASP.NET MVC3 project does not always publish all views/content

This is crazy, but I can't seem to get all my views/content/scripts published when I publish the site. This seems to happen, I believe, when the view or content is not directly referenced by my project, but used by another assembly in my project. So I might have:
ExternalAssembly.dll referenced (it gets published)
I'll need ExternalLogin.cshtml in my main project, under my views folder
ExternalLogin.cshtml doesn't get published
Right now I have a script that copies everything in the Views folder and dumps it to where I want it deployed, but VS should do this for me. What am I doing wrong?
When you click on one if these files what is the build action for it on the properties? Content....or? Set to content.
So your views files are in another project or folder outside your current project? Normally the files have to exist in the web site project, in it's views folder, not externally, and the build action should be set to Content and not to copy to the output folder. But there are some workarounds:
Duplicate them in to your site views folder and make sure they are marked content (as stated in another answer). One thing to note though is that you can add them as "Linked Files" in visual studio which actually allows them to exist in two places in the hierarchy without having to exist in two places on disk: http://support.microsoft.com/kb/306234
If you have control over the external library, you can compile them in as embedded resources or use Razor Generator or something similar and use a custom view engine to return them: How can I make ASP.NET MVC 3 use views (aspx, ascx) files from an external assembly in my website?
Manually put the copies in the .csproj build XML using the Copy task: http://msdn.microsoft.com/en-us/library/3e54c37h.aspx (Note that this will make it work in visual studio doing essentially what you are doing now, as it will then be part of the Visual Studio build if you add it to the AfterBuild target or something)

Resources