Sublime’s powerful Repository Template feature allows you to create templates that users can select when creating new repositories. These templates can include folder structures and files that should be created for each new repository. Creating a basic template is as easy as creating the folders and files, and then editing a few lines in a text file. However, one of the more powerful features of Sublime is to create templates customized to each new repository. This, can take a bit more work.
A customized template is one where repository details (name, description or owner) are used when the new repository is populated from the template. Typically this is used to simply rename files or folders to match the name of the new repository. But what if you wanted to go farther? In this post I’ll show you how to create a template containing full Visual Studio solution including multiple projects, all of which will be customized to each new repository.
1. Create a new Visual Studio Solution
Start up Visual Studio and create a solution to be used as your template. You can create multiple projects, default classes, etc.
New Visual Studio project
2. Rename Files and Folders
Next, close Visual Studio and navigate to the folder containing your solution. Rename your files and folders using the placeholders below so that they will be renamed when your new repository is created.
- %name% – name of the new repository
- %name-formatted% – name of the new repository formatted for a file system (removed spaces and special characters)
- %description% – description of the new repository
- %description-formatted% – description, formatted for a file system
- %owner% – owner of the new repository
- %owner-formatted% – owner, formatted for a file system
- %guid% – %guid% – unique guids which will be generated when the new repository is generated
Be sure to remove any unwanted files and folders such as bin, obj, or .suo and .user files.
Renamed files and folders
3. Modify Solution Files
The next step is a bit tricky. Open your Solution File (.sln) in a text editor such as Notepad. Locate the project names and again, use the placeholders above to rename the names and paths.
Modify project names and paths
Next you must replace the solution GUID and project GUIDs with placeholders so that new GUIDs will be generated for each new repository. Start with the solution GUID used for each project and replace it with %guid% (leave the GUID braces).
Replaced solution GUIDs
Next you must replace the GUIDs for each project. Assign numbers to each project starting with 1 and keep a note of which project has been assigned what number. In my case I chose to use alphabetical order. Now, select the GUID for the first project, and using Find and Replace, replace it with %guid% (again, leaving the braces). Do the same for each project, each time using a new guid.
Project GUIDs replaced
Save and close the modified solution file.
4. Modify Project Files
Next, open each project file (.csproj, .vbproj, etc) in a text editor such as Notepad.
First, replace the GUID in the <ProjectGuid> tag with the guid that you assigned in the last step (i.e. %guid%). If you set up project dependencies when you created your solution, you may need to update those as well.
Next, update the <AssemblyName> and <RootNamespace> tags to use the placeholder names for your new repository.
Modified project file
5. Modify Code Files
Next you may need to modify your source code files to replace namespaces with placeholders. For example, in this example we replaced the namespaces within the Global.asax and Global.asax.cs files.
Renamed Namespace in Global.asax.cs
6. Finalize Repository Template
Next build out the rest of your repository template structure. Remember that the files and folders you create will populate the entire repository, so you must create the “tags”, “branches” and “trunk” folders as well.
Complete repository template structure
7. Register the new Template on your Sublime Server
Finally, copy the entire template structure to the Templates folder on your Sublime server (typically C:\Program Files\Sublime\Templates).
New Template copied to Templates folder on Sublime server
Then open the “templates.txt” file and add the details for your new repository template. See the administrator guide for details on modifying the “templates.txt” file. Be sure to include the “replaceTokens = true” line for this new template. Otherwise the placeholders you created will not be replaced with actual values.
Add the new template to templates.txt
Save “templates.txt” and perform an IISRESET.
8. Test the Template
Finally, create a new repository using the new template to ensure it is working properly. It is a good idea to create a repository, check it out, and open the solution in Visual Studio to ensure that the project builds and that all names are being replaced properly. If you missed any files or namespaces you can simply edit the template on the server and try again.
Create a repository from the new template
Download the Sample
You can download the sample repository template created for this walkthrough from the link below.
Download the sample Template