happyDomain/generators/gen_icon.go

95 lines
2.4 KiB
Go

// This file is part of the happyDomain (R) project.
// Copyright (c) 2020-2024 happyDomain
// Authors: Pierre-Olivier Mercier, et al.
//
// This program is offered under a commercial and under the AGPL license.
// For commercial licensing, contact us at <contact@happydomain.org>.
//
// For AGPL licensing:
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//go:build ignore
// +build ignore
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)
const tplIcon = `// Code generated by go generate. DO NOT EDIT.
// sources:
{{ range $idx, $path := .Sources }}// {{ $path }}
{{ end }}
package {{ .Package }} // import "git.happydns.org/happyDomain/{{ .Directory }}"
var Icons = map[string][]byte{
{{ range $file, $content := .Map }} {{printf "%q" $file}}: []byte({{printf "%q" $content}}),
{{ end }}}
`
var bundleTplIcon = template.Must(template.New("").Parse(tplIcon))
type valTpl struct {
Directory string
Package string
Sources []string
Map map[string][]byte
}
func main() {
dir := os.Args[1]
pkg := os.Args[2]
d := valTpl{
Directory: dir,
Package: pkg,
Sources: []string{},
Map: map[string][]byte{},
}
err := filepath.Walk(dir, func(srcFile string, info os.FileInfo, err error) error {
if filepath.Ext(srcFile) == ".png" {
data, err := ioutil.ReadFile(srcFile)
if err != nil {
return err
}
d.Sources = append(d.Sources, srcFile)
fPath := strings.Split(strings.TrimPrefix(strings.TrimSuffix(srcFile, ".png"), dir+"/"), "/")
fPathLen := len(fPath) - 2
if fPathLen < 0 {
fPathLen = 0
}
d.Map[strings.Join(fPath[fPathLen:], ".")] = data
}
return nil
})
if err != nil {
panic(err)
}
f, err := os.Create(dir + "/icons.go")
if err != nil {
panic(err)
}
defer f.Close()
bundleTplIcon.Execute(f, d)
}