summaryrefslogtreecommitdiff
path: root/magefile.go
blob: 4bf62c72c205025db03cc835ffbd504e8938328a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2021, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.

//go:build mage
// +build mage

package main

import (
	"github.com/magefile/mage/mg"
	"github.com/magefile/mage/sh"
	"github.com/sylabs/release-tools/pkg/cmd"
	"github.com/sylabs/release-tools/pkg/git"
)

// Aliases defines command-line aliases exposed by Mage.
//nolint:deadcode
var Aliases = map[string]interface{}{
	"build":   Build.All,
	"cover":   Cover.All,
	"install": Install.All,
	"test":    Test.All,
}

type Build mg.Namespace

// All compiles all assets.
func (ns Build) All() {
	mg.Deps(ns.Source)
}

// Source compiles all source code.
func (Build) Source() error {
	d, err := git.Describe(".")
	if err != nil {
		return err
	}

	c, err := cmd.NewBuildCommand(
		cmd.OptBuildWithBuiltBy("mage"),
		cmd.OptBuildWithGitDescription(d),
	)
	if err != nil {
		return err
	}

	return sh.RunWith(c.Env(), mg.GoCmd(), c.Args()...)
}

type Install mg.Namespace

// All installs all assets.
func (ns Install) All() {
	mg.Deps(ns.Bin)
}

// Bin installs binary to GOBIN.
func (Install) Bin() error {
	d, err := git.Describe(".")
	if err != nil {
		return err
	}

	c, err := cmd.NewInstallCommand(
		cmd.OptBuildPackages("./cmd/siftool"),
		cmd.OptBuildWithBuiltBy("mage"),
		cmd.OptBuildWithGitDescription(d),
	)
	if err != nil {
		return err
	}

	return sh.RunWith(c.Env(), mg.GoCmd(), c.Args()...)
}

type Test mg.Namespace

// All runs all tests.
func (ns Test) All() {
	mg.Deps(ns.Unit)
}

// Unit runs all unit tests.
func (Test) Unit() error {
	c, err := cmd.NewTestCommand()
	if err != nil {
		return err
	}

	return sh.RunWithV(c.Env(), mg.GoCmd(), c.Args()...)
}

type Cover mg.Namespace

// All runs all tests, writing coverage profile to the specified path.
func (ns Cover) All(path string) {
	mg.Deps(mg.F(ns.Unit, path))
}

// Unit runs all unit tests, writing coverage profile to the specified path.
func (Cover) Unit(path string) error {
	c, err := cmd.NewTestCommand(
		cmd.OptTestWithCoverPath(path),
	)
	if err != nil {
		return err
	}

	return sh.RunWithV(c.Env(), mg.GoCmd(), c.Args()...)
}