cadoles-profile/magefile.go

166 lines
3.4 KiB
Go

// +build mage
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"forge.cadoles.com/Cadoles/ldap-profile/mage/gitea"
"forge.cadoles.com/Cadoles/ldap-profile/mage/release"
)
var env = map[string]string{
"CGO_ENABLED": "0",
}
// Surveiller les sources avec "modd"
func Watch() error {
return sh.RunV("modd")
}
// Construire le binaire
func Build() error {
if mg.Verbose() {
fmt.Println("Building...")
}
return sh.RunWith(env, "go", "build", "-mod=vendor", "-v", "-o", "bin/server", "./cmd/server")
}
// Installer les dépendances du projet
func InstallDeps() error {
if mg.Verbose() {
fmt.Println("Installing Deps...")
}
if err := sh.RunWith(env, "go", "get", "-v", "./..."); err != nil {
return err
}
goTools := []string{
"golang.org/x/tools/cmd/godoc",
"github.com/cortesi/modd/cmd/modd",
"github.com/golangci/golangci-lint/cmd/golangci-lint",
}
disabledGoModules := map[string]string{
"GO111MODULE": "off",
}
for _, toolProject := range goTools {
if err := sh.RunWith(disabledGoModules, "go", "get", "-u", toolProject); err != nil {
return err
}
}
return nil
}
// Supprimer les dépendances obsolètes
func Tidy() error {
if mg.Verbose() {
fmt.Println("Tidying up dependencies...")
}
return sh.RunWith(env, "go", "mod", "tidy")
}
// Générer les archives de release
func Release() error {
defaultConfig, err := sh.Output("go", "run", "./cmd/server", "-dump-config")
if err != nil {
return err
}
handler := func(env map[string]string, destDir string) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
err = sh.RunWith(
env,
"go", "build",
"-mod=vendor",
"-ldflags", "-s -w",
fmt.Sprintf("-gcflags=-trimpath=\"%s\"", cwd),
fmt.Sprintf("-asmflags=-trimpath=\"%s\"", cwd),
"-o", fmt.Sprintf("%s/bin/server", destDir),
"./cmd/server",
)
if err != nil {
return err
}
configFile := path.Join(destDir, "server.conf")
if err := ioutil.WriteFile(configFile, []byte(defaultConfig), os.ModePerm); err != nil {
return err
}
err = release.MultiCopy(map[string]string{
"LICENSE": path.Join(destDir, "LICENSE"),
"README.md": path.Join(destDir, "README.md"),
"cmd/server/public": path.Join(destDir, "public"),
"cmd/server/templates": path.Join(destDir, "templates"),
})
if err != nil {
return err
}
return nil
}
return release.EachTarget(
handler,
release.WithEnv(env),
release.WithArchs(release.Arch386, release.ArchAMD64),
release.WithProjectName("cadoles-profile"),
)
}
// Générer une nouvelle release et la déployer sur Gitea (non fonctionnel)
// Bug sur Gitea https://github.com/go-gitea/gitea/issues/3842
func GiteaRelease() error {
mg.Deps(Release)
return gitea.Release(
"wpetit",
"cadoles-profile",
gitea.WithBaseURL("https://forge.cadoles.com"),
gitea.WithAutoUsername(),
gitea.WithAutoPassword(),
)
}
// Nettoyer le cache Go
func CleanGo() error {
return sh.RunWith(nil, "go", "clean", "-testcache")
}
// Exécuter les tests
func Test() error {
mg.Deps(CleanGo)
mg.Deps(Tidy)
return sh.RunWith(nil, "go", "test", "-race", "-v", "./...")
}
// Nettoyer l'espace de travail
func Clean() error {
if mg.Verbose() {
fmt.Println("Cleaning...")
}
dirs := []string{"data", "bin", "release"}
for _, d := range dirs {
if err := os.RemoveAll(d); err != nil {
return err
}
}
return nil
}