2020-07-05 01:02:57 +02:00
|
|
|
//+build mage
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-07-16 01:20:08 +02:00
|
|
|
"net/http"
|
2020-07-05 01:02:57 +02:00
|
|
|
"os"
|
|
|
|
"strings"
|
2020-12-30 02:37:14 +01:00
|
|
|
"time"
|
2020-07-17 05:58:46 +02:00
|
|
|
|
|
|
|
"github.com/magefile/mage/mg"
|
|
|
|
"github.com/magefile/mage/sh"
|
|
|
|
"github.com/shurcooL/vfsgen"
|
2020-07-05 01:02:57 +02:00
|
|
|
)
|
|
|
|
|
2020-12-30 02:37:14 +01:00
|
|
|
const (
|
|
|
|
packageName = "github.com/jordanknott/taskcafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ldflags = "-X $PACKAGE/internal/utils.commitHash=$COMMIT_HASH -X $PACKAGE/internal/utils.buildDate=$BUILD_DATE -X $PACKAGE/internal/utils.version=$VERSION"
|
|
|
|
|
|
|
|
func runWith(env map[string]string, cmd string, inArgs ...interface{}) error {
|
|
|
|
s := argsToStrings(inArgs...)
|
|
|
|
return sh.RunWith(env, cmd, s...)
|
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// Aliases is a list of short names for often used commands
|
2020-07-05 01:02:57 +02:00
|
|
|
var Aliases = map[string]interface{}{
|
2020-07-19 00:28:25 +02:00
|
|
|
"s": Backend.Schema,
|
|
|
|
"up": Docker.Up,
|
2020-07-17 05:58:46 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// Frontend is the namespace for all commands that interact with the frontend
|
2020-07-17 05:58:46 +02:00
|
|
|
type Frontend mg.Namespace
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// Install the npm dependencies for the React frontend
|
2020-07-17 05:58:46 +02:00
|
|
|
func (Frontend) Install() error {
|
2020-07-18 22:34:33 +02:00
|
|
|
return sh.RunV("yarn", "--cwd", "frontend", "install")
|
2020-07-17 05:58:46 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 19:27:56 +02:00
|
|
|
// Eslint runs eslint on the frontend source
|
|
|
|
func (Frontend) Eslint() error {
|
|
|
|
return sh.RunV("yarn", "--cwd", "frontend", "lint")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tsc runs tsc on the frontend source
|
|
|
|
func (Frontend) Tsc() error {
|
|
|
|
return sh.RunV("yarn", "--cwd", "frontend", "tsc")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lint the frontend source
|
|
|
|
func (Frontend) Lint() {
|
|
|
|
mg.SerialDeps(Frontend.Eslint, Frontend.Tsc)
|
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// Build the React frontend
|
2020-07-17 05:58:46 +02:00
|
|
|
func (Frontend) Build() error {
|
2020-07-18 22:34:33 +02:00
|
|
|
return sh.RunV("yarn", "--cwd", "frontend", "build")
|
2020-07-05 01:02:57 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// Backend is the namespace for all commands that interact with the backend
|
2020-07-17 05:58:46 +02:00
|
|
|
type Backend mg.Namespace
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// GenMigrations embeds schema migration files into Go source code
|
2020-07-19 23:10:07 +02:00
|
|
|
func (Backend) GenMigrations() error {
|
|
|
|
if _, err := os.Stat("internal/migrations"); os.IsNotExist(err) {
|
|
|
|
os.Mkdir("internal/migrations/", 0755)
|
|
|
|
}
|
|
|
|
var fs http.FileSystem = http.Dir("migrations")
|
|
|
|
err := vfsgen.Generate(fs, vfsgen.Options{
|
|
|
|
Filename: "internal/migrations/migrations_generated.go",
|
|
|
|
PackageName: "migrations",
|
|
|
|
VariableName: "Migrations",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// GenFrontend embeds built frontend client into Go source code
|
2020-07-17 05:58:46 +02:00
|
|
|
func (Backend) GenFrontend() error {
|
2020-07-19 20:28:02 +02:00
|
|
|
if _, err := os.Stat("internal/frontend/"); os.IsNotExist(err) {
|
|
|
|
os.Mkdir("internal/frontend/", 0755)
|
|
|
|
}
|
2020-07-16 01:20:08 +02:00
|
|
|
var fs http.FileSystem = http.Dir("frontend/build")
|
|
|
|
err := vfsgen.Generate(fs, vfsgen.Options{
|
|
|
|
Filename: "internal/frontend/frontend_generated.go",
|
|
|
|
PackageName: "frontend",
|
|
|
|
VariableName: "Frontend",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-30 02:37:14 +01:00
|
|
|
func flagEnv() map[string]string {
|
|
|
|
hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
|
|
|
|
fmt.Println("[ignore] fatal: no tag matches")
|
|
|
|
tag, err := sh.Output("git", "describe", "--exact-match", "--tags")
|
|
|
|
if err != nil {
|
|
|
|
tag = "nightly"
|
|
|
|
}
|
|
|
|
return map[string]string{
|
|
|
|
"PACKAGE": packageName,
|
|
|
|
"COMMIT_HASH": hash,
|
|
|
|
"BUILD_DATE": time.Now().Format("2006-01-02T15:04:05Z0700"),
|
|
|
|
"VERSION": tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// Build the Go api service
|
2020-07-17 05:58:46 +02:00
|
|
|
func (Backend) Build() error {
|
2020-08-07 03:50:35 +02:00
|
|
|
fmt.Println("compiling binary dist/taskcafe")
|
2020-12-30 02:37:14 +01:00
|
|
|
return runWith(flagEnv(), "go", "build", "-ldflags", ldflags, "-tags", "prod", "-o", "dist/taskcafe", "cmd/taskcafe/main.go")
|
2020-07-17 05:58:46 +02:00
|
|
|
}
|
2020-07-05 01:02:57 +02:00
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// Schema merges GraphQL schema files into single schema & runs gqlgen
|
2020-07-17 05:58:46 +02:00
|
|
|
func (Backend) Schema() error {
|
2020-07-12 09:06:11 +02:00
|
|
|
files, err := ioutil.ReadDir("internal/graph/schema/")
|
2020-07-05 01:02:57 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
var schema strings.Builder
|
|
|
|
for _, file := range files {
|
2020-07-12 09:06:11 +02:00
|
|
|
filename := "internal/graph/schema/" + file.Name()
|
2020-07-05 01:02:57 +02:00
|
|
|
fmt.Println(filename)
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
content, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(&schema, string(content))
|
|
|
|
}
|
2020-07-12 09:06:11 +02:00
|
|
|
err = ioutil.WriteFile("internal/graph/schema.graphqls", []byte(schema.String()), os.FileMode(0755))
|
2020-07-05 01:02:57 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return sh.Run("gqlgen")
|
|
|
|
}
|
2020-07-17 05:58:46 +02:00
|
|
|
|
2020-12-26 19:44:47 +01:00
|
|
|
func (Backend) Test() error {
|
|
|
|
fmt.Println("running taskcafe backend unit tests")
|
|
|
|
return sh.RunV("go", "test", "./...")
|
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// Install runs frontend:install
|
2020-07-18 22:34:33 +02:00
|
|
|
func Install() {
|
|
|
|
mg.SerialDeps(Frontend.Install)
|
|
|
|
}
|
2020-07-19 00:28:25 +02:00
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// Build runs frontend:build, backend:genMigrations, backend:genFrontend, backend:build
|
2020-07-17 05:58:46 +02:00
|
|
|
func Build() {
|
2020-07-19 23:10:07 +02:00
|
|
|
mg.SerialDeps(Frontend.Build, Backend.GenMigrations, Backend.GenFrontend, Backend.Build)
|
2020-07-17 05:58:46 +02:00
|
|
|
}
|
2020-07-19 00:28:25 +02:00
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// Docker is namespace for commands interacting with docker
|
2020-07-19 00:28:25 +02:00
|
|
|
type Docker mg.Namespace
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// Up runs the main docker compose file
|
2020-07-19 00:28:25 +02:00
|
|
|
func (Docker) Up() error {
|
2020-08-07 03:50:35 +02:00
|
|
|
return sh.RunV("docker-compose", "-p", "taskcafe", "up", "-d")
|
2020-07-19 00:28:25 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// Migrate runs the migration command for the docker-compose network
|
2020-07-19 00:28:25 +02:00
|
|
|
func (Docker) Migrate() error {
|
2020-08-07 03:50:35 +02:00
|
|
|
return sh.RunV("docker-compose", "-p", "taskcafe", "-f", "docker-compose.yml", "-f", "docker-compose.migrate.yml", "run", "--rm", "migrate")
|
2020-07-19 00:28:25 +02:00
|
|
|
}
|
2020-12-30 02:37:14 +01:00
|
|
|
|
|
|
|
func argsToStrings(v ...interface{}) []string {
|
|
|
|
var args []string
|
|
|
|
for _, arg := range v {
|
|
|
|
switch v := arg.(type) {
|
|
|
|
case string:
|
|
|
|
if v != "" {
|
|
|
|
args = append(args, v)
|
|
|
|
}
|
|
|
|
case []string:
|
|
|
|
if v != nil {
|
|
|
|
args = append(args, v...)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("invalid type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return args
|
|
|
|
}
|