-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_package.go
96 lines (85 loc) · 2.11 KB
/
install_package.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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/marguerite/go-stdlib/dir"
"github.com/marguerite/go-stdlib/fileutils"
)
func (pkg Package) install() {
if len(pkg.Rx) > 0 {
r := filepath.Join(pkg.WorkingDirectory, pkg.Rx+".recipe.yaml")
if _, err := os.Stat(r); os.IsNotExist(err) {
fmt.Printf("no recipe %s found, typo?\n", pkg.Rx)
os.Exit(1)
}
b, err := os.ReadFile(r)
if err != nil {
fmt.Printf("failed to open file %s: %s\n", r, err)
os.Exit(1)
}
recipe, err := NewRecipe(b, pkg.RxOptions)
if err != nil {
fmt.Printf("failed to parse recipe: %s\n", err)
os.Exit(1)
}
checkRecipe(recipe, pkg)
recipe.download(pkg)
recipe.install(pkg)
recipe.patch(pkg)
return
}
if _, err := os.Stat(filepath.Join(pkg.WorkingDirectory, "recipe.yaml")); !os.IsNotExist(err) {
r := filepath.Join(pkg.WorkingDirectory, "recipe.yaml")
b, err := os.ReadFile(r)
if err != nil {
fmt.Printf("failed to read file %s: %s\n", r, err)
os.Exit(1)
}
recipe, err := NewRecipe(b, pkg.RxOptions)
if err != nil {
fmt.Printf("failed to parse recipe: %s\n", err)
os.Exit(1)
}
checkRecipe(recipe, pkg)
recipe.download(pkg)
recipe.install(pkg)
recipe.patch(pkg)
return
}
installFilesFromDir(pkg.WorkingDirectory)
}
func checkRecipe(r Recipe, pkg Package) {
if len(pkg.Rx) == 0 {
return
}
if pkg.Rx != r.Recipe.Rx {
fmt.Printf("invalid recipe: %s does not match file name %s\n", r.Recipe.Rx, pkg.Rx)
os.Exit(1)
}
}
func installFilesFromDir(d string) {
pattern := [][]string{{"*.yaml", "*{custom,recipe}.yaml"}, {"*.txt", "opencc/"}, {"*.gram"}, {"opencc/*.*", "*.{json,ocd,txt}"}}
var files []string
for _, v := range pattern {
var matches []string
var err error
if len(v) > 1 {
matches, err = dir.Glob(v[0], d, v[1])
} else {
matches, err = dir.Glob(v[0], d)
}
if err != nil {
fmt.Printf("can not find qualified files in %s\n", d)
os.Exit(1)
}
files = append(files, matches...)
}
for _, v := range files {
err := fileutils.Copy(v, RIME_DIR)
if err != nil {
fmt.Printf("failed to copy %s to %s\n", v, RIME_DIR)
os.Exit(1)
}
}
}