-
Notifications
You must be signed in to change notification settings - Fork 0
/
desktopEntry.go
332 lines (270 loc) · 7.79 KB
/
desktopEntry.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Generate and update .desktop (desktop entry) files for Go binaries automatically.
//
// # Install
//
// Add it to your project
//
// go get https://github.com/mrf345/desktop-entry@latest
//
// # How it works
//
// With the default settings shown in [desktopEntry.DesktopEntry] the method [desktopEntry.DesktopEntry.Create]
// will check your [desktopEntry.DesktopEntry.AppsPath] for a .desktop file, that matches your
// [desktopEntry.DesktopEntry.Name], if it can't find it, it'll create a new one.
// That will later on be updated it only when the binary path changes.
// See test [example].
//
// [example]: https://pkg.go.dev/github.com/mrf345/desktop-entry#example-DesktopEntry.Create
package desktopEntry
import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"slices"
"strings"
)
type DesktopEntry struct {
// Application name (required)
Name string
// Application version (required)
Version string
// Application .png icon data (required)
Icon []byte
// Executable type (default: Application)
Type string
// Semicolon separated list of categories (default: '')
Categories string
// Description of the app (default: '')
Comment string
// Architecture (default: x86_64)
Arch string
// Desktop applications path (default: ~/.local/share/applications)
AppsPath string
// Desktop icons path (default: ~/.icons)
IconsPath string
// Default permission for created files and directories (default: 0776)
Perm fs.FileMode
// Supported operating systems (default: []string{"linux"})
OSs []string
// Update if executable path has changed (default: true)
UpdateIfChanged bool
// Rerun the app if desktop entry has changed (default: true)
RerunIfChanged bool
// Mime type to associate the desktop entry with
MimeType MimeType
}
type MimeType struct {
Type string
Path string
Comment string
GenericIcon string
Patterns []string
}
// Create a new [desktopEntry.DesktopEntry] instance with the default options
func New(name, version string, icon []byte) *DesktopEntry {
return &DesktopEntry{
Name: name,
Version: version,
Icon: icon,
Type: "Application",
Arch: "x86_64",
Perm: 0755,
AppsPath: fmt.Sprintf("%s/.local/share/applications", os.Getenv("HOME")),
IconsPath: fmt.Sprintf("%s/.icons", os.Getenv("HOME")),
OSs: []string{"linux"},
UpdateIfChanged: true,
RerunIfChanged: true,
}
}
// Creates a new desktop entry or updates an existing one if the executable paths mismatch
func (de *DesktopEntry) Create() (err error) {
var changed bool
isDevBuild := strings.HasPrefix(os.Args[0], os.TempDir())
isSupportedOs := slices.Contains(de.OSs, runtime.GOOS)
shouldCreateMimeType, err := de.shouldCreateMimeType()
if isDevBuild || !isSupportedOs || err != nil {
return
}
if err = de.createPaths(); err != nil {
err = fmt.Errorf("failed to create app or icon paths > %w", err)
return
}
if err = de.createIcon(); err != nil {
err = fmt.Errorf("failed to create icon file > %w", err)
return
}
if changed, err = de.createEntry(); err != nil {
err = fmt.Errorf("failed to create or update desktop entry file > %w", err)
return
}
if shouldCreateMimeType {
if err = de.createMimeType(); err != nil {
err = fmt.Errorf("failed to create mime type file > %w", err)
return
}
}
if changed && de.RerunIfChanged {
err = restart()
}
return
}
func (de DesktopEntry) createPaths() (err error) {
paths := []string{de.AppsPath, de.IconsPath}
if len(de.MimeType.Path) > 0 {
paths = append(paths, de.MimeType.Path)
}
for _, path := range paths {
if _, err = os.Stat(path); os.IsNotExist(err) {
if err = os.MkdirAll(path, de.Perm); err != nil {
return
}
err = nil
} else if err != nil {
return
}
}
return
}
func (de DesktopEntry) createIcon() (err error) {
var iconPath = de.getIconPath()
if _, err = os.Stat(iconPath); !os.IsNotExist(err) {
return
}
return os.WriteFile(iconPath, de.Icon, de.Perm)
}
func (de DesktopEntry) getIconPath() string {
return filepath.Join(de.IconsPath, de.getID()+".png")
}
func (de DesktopEntry) getID() string {
return strings.ToLower(de.Name)
}
func (de DesktopEntry) createEntry() (changed bool, err error) {
var entryPath = filepath.Join(de.AppsPath, de.getID()+".desktop")
var entryData string
var doUpdate = de.UpdateIfChanged
if _, err = os.Stat(entryPath); err != nil && !os.IsNotExist(err) {
return
}
if _, err = os.Stat(entryPath); err == nil && doUpdate {
if doUpdate, err = de.shouldUpdate(entryPath); err != nil {
return
}
} else if !os.IsNotExist(err) {
return
}
if doUpdate {
if entryData, err = de.getEntryContent(); err != nil {
return
}
changed = true
err = os.WriteFile(entryPath, []byte(entryData), de.Perm)
_ = exec.Command(fmt.Sprintf(`update-desktop-database "%s"`, de.AppsPath)).Run()
return
}
return
}
func (de DesktopEntry) shouldUpdate(entryPath string) (yes bool, err error) {
var entryFile *os.File
var execRegex, classRegex *regexp.Regexp
var existingData []byte
var execLine string
if execRegex, err = regexp.Compile("Exec=sh -c '.*'"); err != nil {
return
}
if classRegex, err = regexp.Compile("StartupWMClass=.*"); err != nil {
return
}
if entryFile, err = os.Open(entryPath); err != nil {
return
}
defer entryFile.Close()
if existingData, err = io.ReadAll(entryFile); err != nil {
return
}
if execLine, err = getExecLine(); err != nil {
return
}
if match := execRegex.Find(existingData); match == nil || string(match) != execLine {
yes = true
}
if match := classRegex.Find(existingData); match == nil || string(match) != getStartupClassLine() {
yes = true
}
return
}
func (de DesktopEntry) getEntryContent() (content string, err error) {
var execLine string
if execLine, err = getExecLine(); err != nil {
return
}
lines := []string{
"[Desktop Entry]",
"Type=" + de.Type,
"Name=" + de.Name,
execLine,
"Icon=" + de.getIconPath(),
getStartupClassLine(),
}
if de.Categories != "" {
lines = append(lines, "Categories="+de.Categories)
}
if de.Comment != "" {
lines = append(lines, "Comment="+de.Comment)
}
if len(de.MimeType.Type) > 0 {
lines = append(lines, "MimeType="+de.MimeType.Type)
}
return strings.Join(lines, "\n"), nil
}
func (de DesktopEntry) shouldCreateMimeType() (yes bool, err error) {
var path string
if len(de.MimeType.Type) == 0 || len(de.MimeType.Path) == 0 {
return
}
if path, err = de.getMimeTypePath(); err != nil {
return
}
if _, err = os.Stat(path); err != nil && !os.IsNotExist(err) {
return
}
return true, nil
}
func (de DesktopEntry) getMimeTypePath() (string, error) {
typeChunks := strings.Split(de.MimeType.Type, "/")
if 1 >= len(typeChunks) {
return "", errors.New("invalid MimeType.Type")
}
return filepath.Join(de.MimeType.Path, "packages/"+typeChunks[1]+".xml"), nil
}
func (de DesktopEntry) createMimeType() (err error) {
var path string
lines := []string{
`<?xml version="1.0" encoding="utf-8"?>`,
`<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">`,
fmt.Sprintf(` <mime-type type="%s">`, de.MimeType.Type),
}
for _, p := range de.MimeType.Patterns {
lines = append(lines, fmt.Sprintf(` <glob pattern="%s"/>`, p))
}
if len(de.MimeType.Comment) > 0 {
lines = append(lines, fmt.Sprintf(` <comment>%s</comment>`, de.MimeType.Comment))
}
if len(de.MimeType.GenericIcon) > 0 {
lines = append(lines, fmt.Sprintf(` <generic-icon name="%s"/>`, de.MimeType.GenericIcon))
}
lines = append(lines, []string{" </mime-type>", "</mime-info>"}...)
if path, err = de.getMimeTypePath(); err != nil {
return
}
if err = os.WriteFile(path, []byte(strings.Join(lines, "\n")), de.Perm); err != nil {
return
}
_ = exec.Command(fmt.Sprintf(`update-mime-database "%s"`, de.MimeType.Path)).Run()
return
}