-
Notifications
You must be signed in to change notification settings - Fork 21
/
magefile_alfred.go
151 lines (119 loc) · 3.11 KB
/
magefile_alfred.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
150
151
// Copyright (c) 2019 Dean Jackson <[email protected]>
// MIT Licence applies http://opensource.org/licenses/MIT
// +build mage
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"howett.net/plist"
)
var (
// Home = os.ExpandEnv("$HOME")
Library = os.ExpandEnv("$HOME/Library")
// Workflow configuration
// Read from info.plist
BundleID string
Version string
Name string
PrefsFile string
SyncFolder string
DataDir string
CacheDir string
WorkflowDir string
AppVersion string
defaultSyncFolder string
)
func init() {
ip := readInfo()
BundleID = ip.BundleID
if BundleID == "" {
panic("Bundle ID is unset")
}
Version = ip.Version
Name = ip.Name
PrefsFile = filepath.Join(Library, "Preferences/com.runningwithcrayons.Alfred-Preferences.plist")
if _, err := os.Stat(PrefsFile); err == nil {
CacheDir = filepath.Join(Library, "Caches/com.runningwithcrayons.Alfred/Workflow Data", ip.BundleID)
DataDir = filepath.Join(Library, "Application Support/Alfred/Workflow Data", ip.BundleID)
defaultSyncFolder = filepath.Join(Library, "Application Support/Alfred")
} else {
AppVersion = "3.8.1"
PrefsFile = filepath.Join(Library, "Preferences/com.runningwithcrayons.Alfred-Preferences-3.plist")
CacheDir = filepath.Join(Library, "Caches/com.runningwithcrayons.Alfred-3/Workflow Data", ip.BundleID)
DataDir = filepath.Join(Library, "Application Support/Alfred 3/Workflow Data", ip.BundleID)
defaultSyncFolder = filepath.Join(Library, "Application Support/Alfred 3")
}
SyncFolder = syncFolder()
WorkflowDir = filepath.Join(SyncFolder, "Alfred.alfredpreferences/workflows")
}
type infoPlist struct {
BundleID string `plist:"bundleid"`
Version string `plist:"version"`
Name string `plist:"name"`
}
func syncFolder() string {
var (
dirs = []string{defaultSyncFolder}
data []byte
err error
)
if data, err = ioutil.ReadFile(PrefsFile); err != nil {
panic(err)
}
p := struct {
SyncFolder string `plist:"syncfolder"`
}{}
if _, err = plist.Unmarshal(data, &p); err != nil {
panic(err)
}
if p.SyncFolder != "" {
dirs = append([]string{p.SyncFolder}, dirs...)
}
for _, p := range dirs {
p = expandPath(p)
if exists(p) {
return p
}
}
panic("syncfolder not found")
}
func readInfo() infoPlist {
data, err := ioutil.ReadFile("info.plist")
if err != nil {
panic(err)
}
ip := infoPlist{}
if _, err := plist.Unmarshal(data, &ip); err != nil {
panic(err)
}
return ip
}
func alfredEnv() map[string]string {
return map[string]string{
"alfred_workflow_bundleid": BundleID,
"alfred_workflow_version": Version,
"alfred_workflow_name": Name,
"alfred_workflow_cache": CacheDir,
"alfred_workflow_data": DataDir,
"alfred_version": AppVersion,
"GO111MODULE": "on", // for building
}
}
// expand ~ and variables in path.
func expandPath(path string) string {
if strings.HasPrefix(path, "~") {
path = "${HOME}" + path[1:]
}
return os.ExpandEnv(path)
}
func exists(path string) bool {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return false
}
panic(err)
}
return true
}