2023-10-23 03:44:38 -05:00
|
|
|
//go:build mage
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
build "github.com/grafana/grafana-plugin-sdk-go/build"
|
|
|
|
)
|
|
|
|
|
|
|
|
func find(dir string, name string) ([]string, error) {
|
|
|
|
files := []string{}
|
|
|
|
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if filepath.Base(path) == name {
|
|
|
|
files = append(files, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return files, err
|
|
|
|
}
|
|
|
|
|
2024-01-24 06:18:18 -06:00
|
|
|
func fileHasString(path string, s string) bool {
|
|
|
|
f, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return strings.Contains(string(f), s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func findPluginJSONDir(pluginID string) (string, error) {
|
|
|
|
pluginJSONMatches, err := filepath.Glob("../../public/app/plugins/datasource/*/plugin.json")
|
2023-10-23 03:44:38 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(pluginJSONMatches) == 0 {
|
|
|
|
return "", fmt.Errorf("Could not find plugin.json")
|
|
|
|
}
|
|
|
|
pluginJSONPath := ""
|
|
|
|
for _, pluginJSONMatch := range pluginJSONMatches {
|
2024-01-24 06:18:18 -06:00
|
|
|
if !fileHasString(pluginJSONMatch, fmt.Sprintf(`"id": "%s"`, pluginID)) {
|
|
|
|
continue
|
2023-10-23 03:44:38 -05:00
|
|
|
}
|
2024-01-24 07:01:15 -06:00
|
|
|
pluginJSONPath = pluginJSONMatch
|
|
|
|
break
|
2023-10-23 03:44:38 -05:00
|
|
|
}
|
|
|
|
pluginJSONPath, err = filepath.Abs(pluginJSONPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Dir(pluginJSONPath), nil
|
|
|
|
}
|
|
|
|
|
2024-01-24 06:18:18 -06:00
|
|
|
func findRootDir(pluginID string) (string, error) {
|
|
|
|
matches, err := find(".", "main.go")
|
2023-10-23 03:44:38 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return "", fmt.Errorf("Could not find main.go")
|
|
|
|
}
|
2024-01-24 06:18:18 -06:00
|
|
|
pluginDir := ""
|
|
|
|
for _, match := range matches {
|
|
|
|
if fileHasString(match, fmt.Sprintf(`datasource.Manage("%s"`, pluginID)) {
|
|
|
|
pluginDir = filepath.Dir(match)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pluginDir == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
2024-02-07 06:00:13 -06:00
|
|
|
return filepath.Abs(pluginDir)
|
2023-10-23 03:44:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildPlugin(rootDir, pluginJSONDir string) {
|
|
|
|
distDir := filepath.Join(pluginJSONDir, "dist")
|
|
|
|
|
|
|
|
configCallback := func(cfg build.Config) (build.Config, error) {
|
|
|
|
cfg.OutputBinaryPath = distDir
|
|
|
|
cfg.PluginJSONPath = pluginJSONDir
|
|
|
|
cfg.RootPackagePath = rootDir
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
build.SetBeforeBuildCallback(configCallback)
|
|
|
|
build.BuildAll()
|
|
|
|
}
|
|
|
|
|
2024-01-24 06:18:18 -06:00
|
|
|
func BuildPlugin(pluginID string) error {
|
|
|
|
rootDir, err := findRootDir(pluginID)
|
2023-10-23 03:44:38 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-24 06:18:18 -06:00
|
|
|
pluginJSONDir, err := findPluginJSONDir(pluginID)
|
2023-10-23 03:44:38 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
buildPlugin(rootDir, pluginJSONDir)
|
|
|
|
return nil
|
|
|
|
}
|