-
-
Notifications
You must be signed in to change notification settings - Fork 182
/
state.go
120 lines (104 loc) · 2.34 KB
/
state.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
package entrypoint
import (
"encoding/base64"
"os"
"path"
"path/filepath"
"strings"
"github.com/cespare/xxhash"
"github.com/matthewmueller/gotext"
)
type View struct {
Page Path // Path to the page
Type string // View extension
Route string
Frames []Path
Layout Path
Error Path
Client string
Hot string
}
func (v *View) ServerImports() (imports []Path) {
imports = append(imports, v.Page)
imports = append(imports, v.Frames...)
if v.Layout != "" {
imports = append(imports, v.Layout)
}
if v.Error != "" {
imports = append(imports, v.Error)
}
return imports
}
func (v *View) BrowserImports() (imports []Path) {
imports = append(imports, v.Page)
imports = append(imports, v.Frames...)
if v.Error != "" {
imports = append(imports, v.Error)
}
return imports
}
type Path string
func (p Path) Pascal() string {
return gotext.Pascal(string(p))
}
func (p Path) Camel() string {
return gotext.Camel(string(p))
}
func (p Path) Route() string {
name := strings.TrimPrefix(filepath.ToSlash(extless(string(p))), "view/")
dir, base := path.Split(name)
switch base {
case "show":
return "/" + path.Join(dir, ":id")
case "new":
return "/" + path.Join(dir, "new")
case "edit":
return "/" + path.Join(dir, ":id", "edit")
case "index":
return "/" + dir
default:
return "/" + path.Join(dir, strings.ToLower(base))
}
}
func (path Path) Ext() string {
return filepath.Ext(string(path))[1:]
}
func (path Path) Type() string {
switch extless(filepath.Base(string(path))) {
case "layout":
return "layout"
case "frame":
return "frame"
case "error":
return "error"
default:
return "page"
}
}
func (path Path) Hash() (string, error) {
contents, err := os.ReadFile(string(path))
if err != nil {
return "", err
}
hash := xxhash.New()
hash.Write(contents)
return base64.RawURLEncoding.EncodeToString(hash.Sum(nil)), nil
}
func (path Path) Layout() bool {
return extless(filepath.Base(string(path))) == "layout"
}
func (path Path) Frame() bool {
return extless(filepath.Base(string(path))) == "frame"
}
func (path Path) Error() bool {
return extless(filepath.Base(string(path))) == "error"
}
// Recursively trim file extensions until there aren't any left
func extless(path string) string {
ext := filepath.Ext(path)
for ext != "" {
path = strings.TrimSuffix(path, ext)
ext = filepath.Ext(path)
}
return path
}