Upgrade updateassets command to use templates with manifest context

- Convert all asset files to be processed as Go templates
- Expose plugin manifest data via .Manifest key in template context
- Add template processing infrastructure with TemplateContext struct
- Update asset processing pipeline to execute templates instead of direct file copying
- Fix info command output format to use plain text instead of structured logging
- All tests passing with proper error handling and backward compatibility

This enables dynamic asset files that can access plugin manifest properties
like {{.Manifest.Id}}, {{.Manifest.Version}}, and conditional logic based
on plugin configuration.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Felipe M 2025-07-14 21:33:58 +02:00
parent 03c521f237
commit 04fa4154b3
No known key found for this signature in database
GPG key ID: 52E5D65FCF99808A
2 changed files with 77 additions and 31 deletions

41
info.go
View file

@ -28,28 +28,28 @@ func PrintPluginInfo(manifest *model.Manifest) error {
// printBasicInfo prints basic plugin information. // printBasicInfo prints basic plugin information.
func printBasicInfo(manifest *model.Manifest) { func printBasicInfo(manifest *model.Manifest) {
Logger.Info("Plugin Information:") fmt.Println("Plugin Information:")
Logger.Info("==================") fmt.Println("==================")
Logger.Info("ID:", "value", manifest.Id) fmt.Printf("ID: %s\n", manifest.Id)
Logger.Info("Name:", "value", manifest.Name) fmt.Printf("Name: %s\n", manifest.Name)
Logger.Info("Version:", "value", manifest.Version) fmt.Printf("Version: %s\n", manifest.Version)
minVersion := manifest.MinServerVersion minVersion := manifest.MinServerVersion
if minVersion == "" { if minVersion == "" {
minVersion = "Not specified" minVersion = "Not specified"
} }
Logger.Info("Min MM Version:", "value", minVersion) fmt.Printf("Min MM Version: %s\n", minVersion)
if manifest.Description != "" { if manifest.Description != "" {
Logger.Info("Description:", "value", manifest.Description) fmt.Printf("Description: %s\n", manifest.Description)
} }
} }
// printCodeComponents prints information about server and webapp code. // printCodeComponents prints information about server and webapp code.
func printCodeComponents(manifest *model.Manifest) { func printCodeComponents(manifest *model.Manifest) {
Logger.Info("Code Components:") fmt.Println("Code Components:")
Logger.Info("================") fmt.Println("================")
printServerCodeInfo(manifest) printServerCodeInfo(manifest)
printWebappCodeInfo(manifest) printWebappCodeInfo(manifest)
@ -58,28 +58,33 @@ func printCodeComponents(manifest *model.Manifest) {
// printServerCodeInfo prints server code information. // printServerCodeInfo prints server code information.
func printServerCodeInfo(manifest *model.Manifest) { func printServerCodeInfo(manifest *model.Manifest) {
if HasServerCode(manifest) { if HasServerCode(manifest) {
Logger.Info("Server Code:", "value", "Yes") fmt.Println("Server Code: Yes")
if manifest.Server != nil && len(manifest.Server.Executables) > 0 { if manifest.Server != nil && len(manifest.Server.Executables) > 0 {
var executables []string fmt.Print("Executables: ")
first := true
for platform := range manifest.Server.Executables { for platform := range manifest.Server.Executables {
executables = append(executables, platform) if !first {
fmt.Print(", ")
}
fmt.Print(platform)
first = false
} }
Logger.Info("Executables:", "platforms", executables) fmt.Println()
} }
} else { } else {
Logger.Info("Server Code:", "value", "No") fmt.Println("Server Code: No")
} }
} }
// printWebappCodeInfo prints webapp code information. // printWebappCodeInfo prints webapp code information.
func printWebappCodeInfo(manifest *model.Manifest) { func printWebappCodeInfo(manifest *model.Manifest) {
if HasWebappCode(manifest) { if HasWebappCode(manifest) {
Logger.Info("Webapp Code:", "value", "Yes") fmt.Println("Webapp Code: Yes")
if manifest.Webapp != nil && manifest.Webapp.BundlePath != "" { if manifest.Webapp != nil && manifest.Webapp.BundlePath != "" {
Logger.Info("Bundle Path:", "value", manifest.Webapp.BundlePath) fmt.Printf("Bundle Path: %s\n", manifest.Webapp.BundlePath)
} }
} else { } else {
Logger.Info("Webapp Code:", "value", "No") fmt.Println("Webapp Code: No")
} }
} }
@ -89,7 +94,7 @@ func printSettingsSchema(manifest *model.Manifest) {
if manifest.SettingsSchema != nil { if manifest.SettingsSchema != nil {
value = "Yes" value = "Yes"
} }
Logger.Info("Settings Schema:", "value", value) fmt.Printf("Settings Schema: %s\n", value)
} }
// InfoCommandWithPath implements the 'info' command with a custom path. // InfoCommandWithPath implements the 'info' command with a custom path.

View file

@ -8,6 +8,9 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"text/template"
"github.com/mattermost/mattermost/server/public/model"
) )
//go:embed assets/* //go:embed assets/*
@ -45,6 +48,7 @@ func RunUpdateAssetsCommand(args []string, pluginPath string) error {
hasWebapp: hasWebapp, hasWebapp: hasWebapp,
updatedCount: &updatedCount, updatedCount: &updatedCount,
pluginCtlConfig: pluginCtlConfig, pluginCtlConfig: pluginCtlConfig,
manifest: manifest,
} }
err = fs.WalkDir(assetsFS, "assets", func(path string, d fs.DirEntry, err error) error { err = fs.WalkDir(assetsFS, "assets", func(path string, d fs.DirEntry, err error) error {
@ -121,6 +125,12 @@ type AssetProcessorConfig struct {
hasWebapp bool hasWebapp bool
updatedCount *int updatedCount *int
pluginCtlConfig *PluginCtlConfig pluginCtlConfig *PluginCtlConfig
manifest *model.Manifest
}
// TemplateContext holds the data available to templates.
type TemplateContext struct {
Manifest *model.Manifest
} }
func processAssetEntry(path string, d fs.DirEntry, err error, config AssetProcessorConfig) error { func processAssetEntry(path string, d fs.DirEntry, err error, config AssetProcessorConfig) error {
@ -151,21 +161,21 @@ func processAssetEntry(path string, d fs.DirEntry, err error, config AssetProces
return createDirectory(targetPath) return createDirectory(targetPath)
} }
return processAssetFile(path, targetPath, relativePath, config.updatedCount) return processAssetFile(path, targetPath, relativePath, config)
} }
func processAssetFile(embeddedPath, targetPath, relativePath string, updatedCount *int) error { func processAssetFile(embeddedPath, targetPath, relativePath string, config AssetProcessorConfig) error {
shouldUpdate, err := shouldUpdateFile(embeddedPath, targetPath) shouldUpdate, err := shouldUpdateFile(embeddedPath, targetPath, config.manifest)
if err != nil { if err != nil {
return err return err
} }
if shouldUpdate { if shouldUpdate {
err = updateFile(embeddedPath, targetPath, relativePath) err = updateFile(embeddedPath, targetPath, relativePath, config.manifest)
if err != nil { if err != nil {
return err return err
} }
(*updatedCount)++ (*config.updatedCount)++
} }
return nil return nil
@ -179,10 +189,11 @@ func createDirectory(targetPath string) error {
return nil return nil
} }
func shouldUpdateFile(embeddedPath, targetPath string) (bool, error) { func shouldUpdateFile(embeddedPath, targetPath string, manifest *model.Manifest) (bool, error) {
content, err := assetsFS.ReadFile(embeddedPath) // Process the template to get the final content
processedContent, err := processTemplate(embeddedPath, manifest)
if err != nil { if err != nil {
return false, fmt.Errorf("failed to read embedded file %s: %w", embeddedPath, err) return false, fmt.Errorf("failed to process template %s: %w", embeddedPath, err)
} }
existingContent, err := os.ReadFile(targetPath) existingContent, err := os.ReadFile(targetPath)
@ -191,13 +202,14 @@ func shouldUpdateFile(embeddedPath, targetPath string) (bool, error) {
return true, nil //nolint:nilerr return true, nil //nolint:nilerr
} }
return !bytes.Equal(existingContent, content), nil return !bytes.Equal(existingContent, processedContent), nil
} }
func updateFile(embeddedPath, targetPath, relativePath string) error { func updateFile(embeddedPath, targetPath, relativePath string, manifest *model.Manifest) error {
content, err := assetsFS.ReadFile(embeddedPath) // Process the template to get the final content
processedContent, err := processTemplate(embeddedPath, manifest)
if err != nil { if err != nil {
return fmt.Errorf("failed to read embedded file %s: %w", embeddedPath, err) return fmt.Errorf("failed to process template %s: %w", embeddedPath, err)
} }
parentDir := filepath.Dir(targetPath) parentDir := filepath.Dir(targetPath)
@ -205,7 +217,7 @@ func updateFile(embeddedPath, targetPath, relativePath string) error {
return fmt.Errorf("failed to create parent directory %s: %w", parentDir, err) return fmt.Errorf("failed to create parent directory %s: %w", parentDir, err)
} }
if err := os.WriteFile(targetPath, content, filePermissions); err != nil { if err := os.WriteFile(targetPath, processedContent, filePermissions); err != nil {
return fmt.Errorf("failed to write file %s: %w", targetPath, err) return fmt.Errorf("failed to write file %s: %w", targetPath, err)
} }
@ -213,3 +225,32 @@ func updateFile(embeddedPath, targetPath, relativePath string) error {
return nil return nil
} }
// processTemplate processes a template file with the manifest context.
func processTemplate(embeddedPath string, manifest *model.Manifest) ([]byte, error) {
// Read the template content
templateContent, err := assetsFS.ReadFile(embeddedPath)
if err != nil {
return nil, fmt.Errorf("failed to read embedded file %s: %w", embeddedPath, err)
}
// Create template context
context := TemplateContext{
Manifest: manifest,
}
// Create and parse the template
tmpl, err := template.New(embeddedPath).Parse(string(templateContent))
if err != nil {
return nil, fmt.Errorf("failed to parse template %s: %w", embeddedPath, err)
}
// Execute the template
var buf bytes.Buffer
err = tmpl.Execute(&buf, context)
if err != nil {
return nil, fmt.Errorf("failed to execute template %s: %w", embeddedPath, err)
}
return buf.Bytes(), nil
}