Skip to content

Commit cef90e5

Browse files
committed
Read plugin-owned paths from manifest during the apps init
1 parent edf6088 commit cef90e5

File tree

4 files changed

+98
-10
lines changed

4 files changed

+98
-10
lines changed

cmd/apps/init.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ func runCreate(ctx context.Context, opts createOptions) error {
794794

795795
// Apply plugin-specific post-processing (e.g., remove config/queries if analytics not selected)
796796
runErr = prompt.RunWithSpinnerCtx(ctx, "Configuring plugins...", func() error {
797-
return applyPlugins(absOutputDir, selectedPlugins)
797+
return applyPlugins(absOutputDir, selectedPlugins, m.GetTemplatePaths())
798798
})
799799
if runErr != nil {
800800
return runErr
@@ -946,20 +946,14 @@ func buildPluginStrings(pluginNames []string) (pluginImport, pluginUsage string)
946946
return pluginImport, pluginUsage
947947
}
948948

949-
// pluginOwnedPaths maps plugin names to directories they own.
950-
// When a plugin is not selected, its owned paths are removed from the project.
951-
var pluginOwnedPaths = map[string][]string{
952-
"analytics": {"config/queries"},
953-
}
954-
955-
// applyPlugins removes directories owned by unselected plugins.
956-
func applyPlugins(projectDir string, pluginNames []string) error {
949+
// applyPlugins removes template directories owned by unselected plugins.
950+
func applyPlugins(projectDir string, pluginNames []string, templatePaths map[string][]string) error {
957951
selectedSet := make(map[string]bool)
958952
for _, name := range pluginNames {
959953
selectedSet[name] = true
960954
}
961955

962-
for plugin, paths := range pluginOwnedPaths {
956+
for plugin, paths := range templatePaths {
963957
if selectedSet[plugin] {
964958
continue
965959
}

cmd/apps/init_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,59 @@ func TestAppendUniqueNoValues(t *testing.T) {
458458
assert.Equal(t, []string{"a", "b"}, result)
459459
}
460460

461+
func TestApplyPlugins(t *testing.T) {
462+
tests := []struct {
463+
name string
464+
selected []string
465+
templatePaths map[string][]string
466+
expectRemoved []string
467+
expectKept []string
468+
}{
469+
{
470+
name: "unselected plugin directory is removed",
471+
selected: []string{"server"},
472+
templatePaths: map[string][]string{"analytics": {"config/queries"}},
473+
expectRemoved: []string{"config/queries"},
474+
},
475+
{
476+
name: "selected plugin directory is kept",
477+
selected: []string{"analytics", "server"},
478+
templatePaths: map[string][]string{"analytics": {"config/queries"}},
479+
expectKept: []string{"config/queries"},
480+
},
481+
{
482+
name: "empty templatePaths is a no-op",
483+
selected: []string{"server"},
484+
templatePaths: map[string][]string{},
485+
},
486+
}
487+
488+
for _, tc := range tests {
489+
t.Run(tc.name, func(t *testing.T) {
490+
dir := t.TempDir()
491+
492+
// Create all directories referenced in templatePaths
493+
for _, paths := range tc.templatePaths {
494+
for _, p := range paths {
495+
require.NoError(t, os.MkdirAll(filepath.Join(dir, p), 0o755))
496+
}
497+
}
498+
499+
err := applyPlugins(dir, tc.selected, tc.templatePaths)
500+
require.NoError(t, err)
501+
502+
for _, p := range tc.expectRemoved {
503+
_, statErr := os.Stat(filepath.Join(dir, p))
504+
assert.True(t, os.IsNotExist(statErr), "expected %s to be removed", p)
505+
}
506+
for _, p := range tc.expectKept {
507+
_, statErr := os.Stat(filepath.Join(dir, p))
508+
assert.NoError(t, statErr, "expected %s to exist", p)
509+
}
510+
})
511+
}
512+
}
513+
461514
func TestRunManifestOnlyFound(t *testing.T) {
462515
dir := t.TempDir()
463516
manifestPath := filepath.Join(dir, manifest.ManifestFileName)

libs/apps/manifest/manifest.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type Plugin struct {
6767
Description string `json:"description"`
6868
Package string `json:"package"`
6969
RequiredByTemplate bool `json:"requiredByTemplate"`
70+
TemplatePaths []string `json:"templatePaths,omitempty"`
7071
Resources Resources `json:"resources"`
7172
}
7273

@@ -205,6 +206,18 @@ func (m *Manifest) CollectResources(pluginNames []string) []Resource {
205206
return resources
206207
}
207208

209+
// GetTemplatePaths returns a map of plugin name to template directory paths.
210+
// Only plugins that declare at least one path are included.
211+
func (m *Manifest) GetTemplatePaths() map[string][]string {
212+
result := make(map[string][]string)
213+
for name, p := range m.Plugins {
214+
if len(p.TemplatePaths) > 0 {
215+
result[name] = p.TemplatePaths
216+
}
217+
}
218+
return result
219+
}
220+
208221
// CollectOptionalResources returns all optional resources for the given plugin names.
209222
func (m *Manifest) CollectOptionalResources(pluginNames []string) []Resource {
210223
seen := make(map[string]bool)

libs/apps/manifest/manifest_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,34 @@ func TestResourceKey(t *testing.T) {
320320
assert.Equal(t, "sql_warehouse", r.VarPrefix())
321321
}
322322

323+
func TestGetTemplatePaths(t *testing.T) {
324+
m := &manifest.Manifest{
325+
Plugins: map[string]manifest.Plugin{
326+
"analytics": {
327+
Name: "analytics",
328+
TemplatePaths: []string{"config/queries"},
329+
},
330+
"server": {
331+
Name: "server",
332+
},
333+
},
334+
}
335+
336+
paths := m.GetTemplatePaths()
337+
assert.Equal(t, map[string][]string{"analytics": {"config/queries"}}, paths)
338+
}
339+
340+
func TestGetTemplatePathsEmpty(t *testing.T) {
341+
m := &manifest.Manifest{
342+
Plugins: map[string]manifest.Plugin{
343+
"server": {Name: "server"},
344+
},
345+
}
346+
347+
paths := m.GetTemplatePaths()
348+
assert.Empty(t, paths)
349+
}
350+
323351
func TestCollectOptionalResources(t *testing.T) {
324352
m := &manifest.Manifest{
325353
Plugins: map[string]manifest.Plugin{

0 commit comments

Comments
 (0)