-
Notifications
You must be signed in to change notification settings - Fork 5
/
magefile.go
149 lines (129 loc) · 3.26 KB
/
magefile.go
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// +build mage
package main
import (
"archive/zip"
"io/ioutil"
"os"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var LINUX_BIN = "sprig"
var LINUX_ARCHIVE = "sprig-linux.tar.xz"
var WINDOWS_BIN = "sprig.exe"
var WINDOWS_ARCHIVE = "sprig-windows.zip"
var FPNAME = "chat.arbor.Client.Sprig"
var FPCONFIG = FPNAME + ".yml"
var FPBUILD = "pakbuild"
var FPREPO = "/data/fp-repo"
var Aliases = map[string]interface{}{
"c": Clean,
"l": Linux,
"w": Windows,
"fp": Flatpak,
"run": FlatpakRun,
}
func goFlags() string {
return "-ldflags=-X=main.Version=" + embeddedVersion()
}
func embeddedVersion() string {
gitVersion, err := sh.Output("git", "describe", "--tags", "--dirty", "--always")
if err != nil {
return "git"
}
return gitVersion
}
// Build all binary targets
func All() {
mg.Deps(Linux, Windows)
}
// Build for specific platforms with a given binary name.
func BuildFor(platform, binary string) error {
_, err := sh.Exec(map[string]string{"GOOS": platform, "GOFLAGS": goFlags()},
os.Stdout, os.Stderr, "go", "build", "-o", binary, ".")
if err != nil {
return err
}
return nil
}
// Build Linux
func LinuxBin() error {
return BuildFor("linux", LINUX_BIN)
}
// Build Linux and archive/compress binary
func Linux() error {
mg.Deps(LinuxBin)
return sh.Run("tar", "-cJf", LINUX_ARCHIVE, LINUX_BIN, "desktop-assets", "install-linux.sh", "appicon.png", "LICENSE.txt")
}
// Build Windows
func WindowsBin() error {
return BuildFor("windows", WINDOWS_BIN)
}
// Build Windows binary and zip it up
func Windows() error {
mg.Deps(WindowsBin)
file, err := os.Create(WINDOWS_ARCHIVE)
if err != nil {
return err
}
zipWriter := zip.NewWriter(file)
f, err := zipWriter.Create(WINDOWS_BIN)
if err != nil {
return err
}
body, err := ioutil.ReadFile(WINDOWS_BIN)
if err != nil {
return err
}
_, err = f.Write(body)
if err != nil {
return err
}
err = zipWriter.Close()
if err != nil {
return err
}
return nil
}
// Build flatpak
func Flatpak() error {
mg.Deps(FlatpakInit)
return sh.Run("flatpak-builder", "--user", "--force-clean", FPBUILD, FPCONFIG)
}
// Get a shell within flatpak
func FlatpakShell() error {
mg.Deps(FlatpakInit)
return sh.Run("flatpak-builder", "--user", "--run", FPBUILD, FPCONFIG, "sh")
}
// Install flatpak
func FlatpakInstall() error {
mg.Deps(FlatpakInit)
return sh.Run("flatpak-builder", "--user", "--install", "--force-clean", FPBUILD, FPCONFIG)
}
// Run flatpak
func FlatpakRun() error {
return sh.Run("flatpak", "run", FPNAME)
}
// Flatpak into repo
func FlatpakRepo() error {
return sh.Run("flatpak-builder", "--user", "--force-clean", "--repo="+FPREPO, FPBUILD, FPCONFIG)
}
// Enable repos if this is your first time running flatpak
func FlatpakInit() error {
err := sh.RunV("flatpak", "remote-add", "--user", "--if-not-exists", "flathub", "https://flathub.org/repo/flathub.flatpakrepo")
if err != nil {
return err
}
err = sh.Run("flatpak", "install", "--user", "flathub", "org.freedesktop.Sdk/x86_64/19.08")
if err != nil {
return err
}
err = sh.Run("flatpak", "install", "--user", "flathub", "org.freedesktop.Platform/x86_64/19.08")
if err != nil {
return err
}
return nil
}
// Clean up
func Clean() error {
return sh.Run("rm", "-rf", WINDOWS_ARCHIVE, WINDOWS_BIN, LINUX_ARCHIVE, LINUX_BIN, FPBUILD)
}