alfred-yubico-auth/magefile.go

134 lines
2.5 KiB
Go
Raw Normal View History

2020-12-10 16:42:50 +00:00
// +build mage
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"github.com/deanishe/awgo/util"
"github.com/deanishe/awgo/util/build"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var (
info *build.Info
buildDir = "./build"
distDir = "./dist"
// Default mage target
Default = Run
)
func init() {
var err error
if info, err = build.NewInfo(); err != nil {
panic(err)
}
}
2021-01-08 21:45:05 +00:00
// Build workflow.
2020-12-10 16:42:50 +00:00
func Build() error {
mg.Deps(cleanBuild)
fmt.Println("Building...")
2021-01-08 21:45:05 +00:00
err := sh.RunWith(info.Env(), "go", "build", "-o", buildDir+"/alfred-yubico-auth", ".")
2020-12-10 16:42:50 +00:00
if err != nil {
2021-01-08 21:45:05 +00:00
return fmt.Errorf("error building binary %w", err)
2020-12-10 16:42:50 +00:00
}
globs := build.Globs(
"*.png",
"info.plist",
"README.md",
"LICENSE.txt",
"password-prompt.js",
)
return build.SymlinkGlobs(buildDir, globs...)
}
2021-01-08 21:45:05 +00:00
// Run workflow.
2020-12-10 16:42:50 +00:00
func Run() error {
mg.Deps(Build)
fmt.Println("Running...")
2021-01-08 21:45:05 +00:00
return sh.RunWith(info.Env(), buildDir+"/alfred-yubico-auth")
2020-12-10 16:42:50 +00:00
}
2021-01-08 21:45:05 +00:00
// Dist packages workflow for distribution.
2020-12-10 16:42:50 +00:00
func Dist() error {
mg.SerialDeps(Clean, Build)
2021-01-08 21:45:05 +00:00
2020-12-10 16:42:50 +00:00
p, err := build.Export(buildDir, distDir)
if err != nil {
return err
}
2021-01-08 21:45:05 +00:00
2020-12-10 16:42:50 +00:00
fmt.Printf("Exported %q\n", p)
2021-01-08 21:45:05 +00:00
2020-12-10 16:42:50 +00:00
return nil
}
2021-01-08 21:45:05 +00:00
// Install symlinked workflow to Alfred.
2020-12-10 16:42:50 +00:00
func Install() error {
mg.Deps(Build)
fmt.Printf("Installing (linking) %q to %q...\n", buildDir, info.InstallDir)
2021-01-08 21:45:05 +00:00
2020-12-10 16:42:50 +00:00
if err := sh.Rm(info.InstallDir); err != nil {
2021-01-08 21:45:05 +00:00
return fmt.Errorf("error cleaning previously installed workflow: %w", err)
2020-12-10 16:42:50 +00:00
}
2021-01-08 21:45:05 +00:00
2020-12-10 16:42:50 +00:00
return build.Symlink(info.InstallDir, buildDir, true)
}
2021-01-08 21:45:05 +00:00
// InstallHooks will install pre-commit hooks.
2020-12-10 16:42:50 +00:00
func InstallHooks() error {
return sh.RunV("pre-commit", "install", "--overwrite", "--install-hooks")
}
2021-01-08 21:45:05 +00:00
// Check will run all pre-commit hooks.
2020-12-10 16:42:50 +00:00
func Check() error {
return sh.RunV("pre-commit", "run", "--all-files")
}
2021-01-08 21:45:05 +00:00
// Clean build files.
2020-12-10 16:42:50 +00:00
func Clean() error {
fmt.Println("Cleaning...")
mg.Deps(cleanBuild, cleanMage)
2021-01-08 21:45:05 +00:00
2020-12-10 16:42:50 +00:00
return nil
}
2021-01-08 21:45:05 +00:00
// DistClean build files and distribution files.
2020-12-10 16:42:50 +00:00
func DistClean() error {
mg.Deps(Clean, cleanDist)
2021-01-08 21:45:05 +00:00
2020-12-10 16:42:50 +00:00
return nil
}
func cleanDir(name string) error {
if !util.PathExists(name) {
return nil
}
infos, err := ioutil.ReadDir(name)
if err != nil {
2021-01-08 21:45:05 +00:00
return fmt.Errorf("cleanDir could not read folder: %w", err)
2020-12-10 16:42:50 +00:00
}
for _, fi := range infos {
if err := sh.Rm(filepath.Join(name, fi.Name())); err != nil {
2021-01-08 21:45:05 +00:00
return fmt.Errorf("cleanDir could not remove file: %w", err)
2020-12-10 16:42:50 +00:00
}
}
2021-01-08 21:45:05 +00:00
2020-12-10 16:42:50 +00:00
return nil
}
func cleanBuild() error { return cleanDir(buildDir) }
func cleanDist() error { return cleanDir(distDir) }
func cleanMage() error { return sh.Run("mage", "-clean") }