-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathassets.go
72 lines (64 loc) · 2.63 KB
/
assets.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
package ffcss
import (
"fmt"
"path/filepath"
"strings"
"github.com/bmatcuk/doublestar"
"github.com/hoisie/mustache"
)
func renderFileTemplate(f FileTemplate, operatingSystem string, variant Variant, osRenameMap map[string]string) string {
if strings.Contains(strings.Trim(f, " "), "{{variant}}") && variant.Name == "" {
LogWarning("%q uses {{variant}} which is empty\n", f)
}
var osName string
if osRenameMap[operatingSystem] == "" {
osName = operatingSystem
} else {
osName = osRenameMap[operatingSystem]
}
return mustache.Render(f, map[string]string{
"os": osName,
"variant": variant.Name,
})
}
// DestinationPathOfAsset computes the destination path of some asset from its path and the destination profile directory
// It is assumed that assetPath is absolute.
func (t Theme) DestinationPathOfAsset(assetPath string, profileDir string, operatingSystem string, variant Variant) (string, error) {
if !strings.HasPrefix(assetPath, t.DownloadedTo) {
return "", fmt.Errorf("asset %q is outside of the theme's root %q", assetPath, t.DownloadedTo)
}
relativeTo := filepath.Clean(filepath.Join(t.DownloadedTo, filepath.Clean(renderFileTemplate(t.CopyFrom, operatingSystem, variant, t.OSNames))))
if !strings.HasPrefix(relativeTo, t.DownloadedTo) {
return "", fmt.Errorf("copy from %q is outside of the theme's root %q", relativeTo, t.DownloadedTo)
}
relativised, err := filepath.Rel(relativeTo, assetPath)
if err != nil {
return "", fmt.Errorf("couldn't make %s relative to %s: %w", assetPath, filepath.Join(t.DownloadedTo, filepath.Clean(t.CopyFrom)), err)
}
return filepath.Join(profileDir, "chrome", relativised), nil
}
// AssetsPaths returns the individual file paths of all assets.
func (t Theme) AssetsPaths(os string, variant Variant) ([]string, error) {
resolvedFiles := make([]string, 0)
for _, template := range t.Assets {
glob := renderFileTemplate(template, os, variant, t.OSNames)
LogDebug("looking for assets: globbing %q", filepath.Join(t.DownloadedTo, glob))
glob = filepath.Clean(filepath.Join(t.DownloadedTo, glob))
files, err := doublestar.Glob(glob)
if err != nil {
return resolvedFiles, fmt.Errorf("while getting all matches of glob %s: %w", glob, err)
}
// If no matches
if len(files) < 1 {
// If it's _really_ a glob pattern
if strings.Contains(glob, "*") {
return resolvedFiles, fmt.Errorf("glob pattern %s matches no files", glob)
}
// If it's just a regular file (that was treated as a glob pattern)
return resolvedFiles, fmt.Errorf("file %s not found", glob)
}
// For each match of the glob pattern
resolvedFiles = append(resolvedFiles, files...)
}
return resolvedFiles, nil
}