This repository has been archived by the owner on Jun 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modder.go
109 lines (88 loc) · 3.63 KB
/
modder.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
package modder
import (
"fmt"
"os"
"github.com/go-git/go-billy/v5"
"cuelang.org/go/cue"
"github.com/hofstadter-io/mvs/lib/util"
)
// This modder is for more complex, yet configurable module processing.
// You can have system wide and local custom configurations.
// The fields in this struct are alpha and are likely to change
type Modder struct {
// MetaConfiguration
Name string `yaml:"Name"`
Version string `yaml:"Version",omitempty`
// Module information
ModFile string `yaml:"ModFile",omitempty`
SumFile string `yaml:"SumFile",omitempty`
ModsDir string `yaml:"ModsDir",omitempty`
MappingFile string `yaml:"MappingFile",omitempty`
// Commands override default, configuragble processing
// for things like golang
NoLoad bool `yaml:"NoLoad",omitempty`
CommandInit [][]string `yaml:"CommandInit",omitempty`
CommandGraph [][]string `yaml:"CommandGraph",omitempty`
CommandTidy [][]string `yaml:"CommandTidy",omitempty`
CommandVendor [][]string `yaml:"CommandVendor",omitempty`
CommandVerify [][]string `yaml:"CommandVerify",omitempty`
CommandStatus [][]string `yaml:"CommandStatus",omitempty`
// Init related fields
// we need to create things like directories and files beyond the
InitTemplates map[string]string `yaml:"InitTemplates",omitempty`
InitPreCommands [][]string `yaml:"InitPreCommands",omitempty`
InitPostCommands [][]string `yaml:"InitPostCommands",omitempty`
// Vendor related fields
// filesystem globs for discovering files we should copy over
VendorIncludeGlobs []string `yaml:"VendorIncludeGlobs",omitempty`
VendorExcludeGlobs []string `yaml:"VendorExcludeGlobs",omitempty`
// Any files we need to generate
VendorTemplates map[string]string `yaml:"VendorTemplates",omitempty`
VendorPreCommands [][]string `yaml:"VendorPreCommands",omitempty`
VendorPostCommands [][]string `yaml:"VendorPostCommands",omitempty`
// Some more vendor controls
ManageFileOnly bool `yaml:"ManageFileOnly",omitempty`
SymlinkLocalReplaces bool `yaml:"SymlinkLocalReplaces",omitempty`
// Introspection Configuration(s)
// filesystem globs for discovering files we should introspect
// regexs for extracting package information
IntrospectIncludeGlobs []string `yaml:"IntrospectIncludeGlobs",omitempty`
IntrospectExcludeGlobs []string `yaml:"IntrospectExcludeGlobs",omitempty`
IntrospectExtractRegex []string `yaml:"IntrospectExtractRegex",omitempty`
PackageManagerDefaultPrefix string `yaml:"PackageManagerDefaultPrefix",omitempty`
// filesystem
FS billy.Filesystem `yaml:"-"`
// root module
module *Module `yaml:"-"`
errors []error `yaml:"-"`
// dependency modules (requires/replace)
// dependencies shoule respect any .mvsconfig it finds along side the module files
// module writers can then have local control over how their module is handeled during vendoring
depsMap map[string]*Module `yaml:"-"`
// compiled cue, used for merging
CueInstance *cue.Instance `yaml:"-"`
}
func NewFromFile(lang, filepath string, FS billy.Filesystem) (*Modder, error) {
bytes, err := util.BillyReadAll(filepath, FS)
if err != nil {
if _, ok := err.(*os.PathError); !ok && err.Error() != "file does not exist" && err.Error() != "no such file or directory" {
return nil, err
}
// The user has not setup a global $HOME/.mvs/mvsconfig file
return nil, nil
}
var mdrMap map[string]*Modder
i, err := util.CueRuntime.Compile(filepath, string(bytes))
if err != nil {
return nil, err
}
err = i.Value().Decode(&mdrMap)
if err != nil {
return nil, err
}
mdr, ok := mdrMap[lang]
if !ok {
return nil, fmt.Errorf("lang %q not found in %s", lang, filepath)
}
return mdr, nil
}