2023-02-12 20:59:55 +00:00
|
|
|
//go:build mage
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-07-29 01:05:33 +01:00
|
|
|
"io"
|
|
|
|
"io/fs"
|
2023-02-24 18:07:08 +00:00
|
|
|
"log"
|
2023-02-12 20:59:55 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
2023-02-24 18:07:08 +00:00
|
|
|
|
|
|
|
"github.com/magefile/mage/mg"
|
2023-02-12 20:59:55 +00:00
|
|
|
)
|
|
|
|
|
2023-07-29 01:05:33 +01:00
|
|
|
// Prepare prepares the service's build directory.
|
|
|
|
func Prepare(service string) error {
|
2023-02-24 18:07:08 +00:00
|
|
|
cfg, err := newConfig(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to load the configuration; %v", err)
|
|
|
|
}
|
|
|
|
|
2023-07-29 01:05:33 +01:00
|
|
|
if service == "all" {
|
2023-02-24 18:07:08 +00:00
|
|
|
objects, err := os.ReadDir(rootTemplatesDir)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to read the templates directory; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range objects {
|
|
|
|
if !o.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-07-29 01:05:33 +01:00
|
|
|
service := o.Name()
|
2023-02-24 18:07:08 +00:00
|
|
|
|
2023-07-31 00:40:19 +01:00
|
|
|
buildDir := filepath.Join(rootBuildDir, service)
|
|
|
|
|
|
|
|
if _, err := os.Stat(buildDir); err != nil {
|
|
|
|
if err := os.Mkdir(buildDir, 0o700); err != nil {
|
|
|
|
return fmt.Errorf("unable to make %s; %w", buildDir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 01:05:33 +01:00
|
|
|
if service != "compose" {
|
2023-02-26 17:14:39 +00:00
|
|
|
mg.Deps(
|
2023-07-29 01:05:33 +01:00
|
|
|
mg.F(Download, service),
|
2023-02-26 17:14:39 +00:00
|
|
|
)
|
2023-07-31 00:40:19 +01:00
|
|
|
|
|
|
|
log.Printf("Copying assets for %s.\n", service)
|
|
|
|
if err := copyAssets(service); err != nil {
|
|
|
|
return fmt.Errorf("unable to copy the assets for %s; %w", service, err)
|
|
|
|
}
|
2023-02-26 17:14:39 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 01:05:33 +01:00
|
|
|
log.Printf("Rendering templates for %s.\n", service)
|
|
|
|
if err := render(cfg, service); err != nil {
|
|
|
|
return fmt.Errorf("unable to render templates for %s; %w", service, err)
|
|
|
|
}
|
|
|
|
|
2023-02-24 18:07:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-07-31 00:40:19 +01:00
|
|
|
buildDir := filepath.Join(rootBuildDir, service)
|
|
|
|
|
|
|
|
if _, err := os.Stat(buildDir); err != nil {
|
|
|
|
if err := os.Mkdir(buildDir, 0o700); err != nil {
|
|
|
|
return fmt.Errorf("unable to make %s; %w", buildDir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 01:05:33 +01:00
|
|
|
if service != "compose" {
|
2023-02-24 18:07:08 +00:00
|
|
|
mg.Deps(
|
2023-07-29 01:05:33 +01:00
|
|
|
mg.F(Download, service),
|
|
|
|
mg.F(Prepare, "compose"),
|
2023-02-24 18:07:08 +00:00
|
|
|
)
|
2023-07-31 00:40:19 +01:00
|
|
|
|
|
|
|
log.Printf("Copying assets for %s.\n", service)
|
|
|
|
if err := copyAssets(service); err != nil {
|
|
|
|
return fmt.Errorf("unable to copy the assets for %s; %w", service, err)
|
|
|
|
}
|
2023-02-24 18:07:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 01:05:33 +01:00
|
|
|
if err := render(cfg, service); err != nil {
|
2023-02-24 18:07:08 +00:00
|
|
|
return fmt.Errorf("an error occurred whilst rendering the templates; %w", err)
|
|
|
|
}
|
2023-07-29 01:05:33 +01:00
|
|
|
|
2023-02-24 18:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-12 20:59:55 +00:00
|
|
|
func render(cfg config, component string) error {
|
|
|
|
buildDirName := filepath.Join(rootBuildDir, component)
|
|
|
|
|
|
|
|
templateDirName := filepath.Join(rootTemplatesDir, component)
|
|
|
|
|
2023-08-24 15:30:14 +01:00
|
|
|
_, err := os.Stat(templateDirName)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
fmt.Printf("There's no template directory for %q.\n", component)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-12 20:59:55 +00:00
|
|
|
files, err := os.ReadDir(templateDirName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to read files from %s; %w ", templateDirName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
err := func() error {
|
|
|
|
templateFilename := f.Name()
|
|
|
|
|
|
|
|
if f.IsDir() || !strings.HasSuffix(templateFilename, templateExtension) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
outputFilename := strings.TrimSuffix(templateFilename, templateExtension)
|
|
|
|
outputPath := filepath.Join(buildDirName, outputFilename)
|
|
|
|
|
|
|
|
file, err := os.Create(outputPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create the file '%s'; %w", outputPath, err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
templatePath := filepath.Join(templateDirName, templateFilename)
|
|
|
|
tmpl, err := template.New(templateFilename).ParseFiles(templatePath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create a new template value from '%s'; %w", templateFilename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tmpl.Execute(file, cfg); err != nil {
|
|
|
|
return fmt.Errorf("unable to render the template to '%s'; %w", outputPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("an error occurred whilst rendering the templates for '%s'; %w", component, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-29 01:05:33 +01:00
|
|
|
|
|
|
|
func copyAssets(service string) error {
|
|
|
|
assetsDirName := filepath.Join(rootAssetsDir, service)
|
|
|
|
if _, err := os.Stat(assetsDirName); err != nil {
|
2023-08-24 15:30:14 +01:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
fmt.Printf("There's no assets directory for %q.\n", service)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2023-07-29 01:05:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
buildDirName := filepath.Join(rootBuildDir, service, "assets")
|
|
|
|
|
|
|
|
walkDirFunc := func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if path == assetsDirName {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
buildAssetPath := filepath.Join(buildDirName, strings.TrimPrefix(path, assetsDirName))
|
|
|
|
|
|
|
|
if d.IsDir() {
|
|
|
|
if err := os.MkdirAll(buildAssetPath, 0o750); err != nil {
|
|
|
|
return fmt.Errorf("unable to make %s; %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
source, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer source.Close()
|
|
|
|
|
|
|
|
dest, err := os.Create(buildAssetPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dest.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(dest, source); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := filepath.WalkDir(assetsDirName, walkDirFunc)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error walking the path '%s'; %w", assetsDirName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|