Skip to content

Question: How to sync templates and avoid duplicates for custom host #9859

@GustavEikaas

Description

@GustavEikaas

Issue with managing SDK templates for custom host - duplicates and updates

Hey folks! I'm working on a C# IDE plugin for neovim, therefore I am trying to integrate with Microsoft.TemplateEngine. I'm trying to define a custom host using Microsoft.TemplateEngine.IDE.Bootstrapper and running into some challenges around template management. Would love some guidance on the best approach here.

What I'm trying to do

I want to create my own custom host (makes sense since some templates have constraints like if!dotnet-cli), and I'd like to:

  1. Automatically install all the templates that show up when running dotnet new --list
  2. Avoid having duplicate versions of the same template package installed
  3. Update templates when newer versions are available

The problem

I'm finding packages from the SDK at C:\Program Files\dotnet\templates\{version}\*.nupkg and installing them, but I keep running into issues with duplicates. It seems like:

  • When I install packages, they get copied to C:\Users\{user}\.templateengine\packages\
  • Some package identifiers include version numbers (like Microsoft.DotNet.Common.ProjectTemplates.10.0 vs Microsoft.DotNet.Common.ProjectTemplates.9.0)
  • This makes it tricky to dedupe by Identifier since different major versions have different identifiers
  • I'm getting stuck in a loop where certain packages (like WinForms and WPF templates) keep getting installed and then uninstalled

What I'm currently doing

// Install missing packages
var installedFilenames = installedPackages.Select(p => Path.GetFileName(new Uri(p.MountPointUri).LocalPath));
var missing = nupkgs.Where(nupkg => !installedFilenames.Contains(Path.GetFileName(nupkg)));
await bootstrapper.InstallTemplatePackagesAsync(missing, InstallationScope.Global, cancellationToken);

// Try to dedupe by Identifier
var dupes = installedPackages.GroupBy(p => p.Identifier).Where(g => g.Count() > 1);
await bootstrapper.UninstallTemplatePackagesAsync(dupes.SelectMany(g => g.Skip(1)), cancellationToken);

// Check for updates
await bootstrapper.UpdateTemplatePackagesAsync(...);

Questions

  1. What's the recommended way to sync SDK templates to a custom host without creating duplicates?
  2. Should I be comparing by Identifier, filename, or something else?
  3. How should I handle packages where the major version is part of the identifier (like *.ProjectTemplates.9.0 vs *.ProjectTemplates.10.0)?
  4. Is there a simpler/better pattern I'm missing here?

Any pointers would be super helpful! Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions