Skip to content

Commit d2452f1

Browse files
committed
refactor: let 'volt migrate plugconf/config-func' use JSON DSL
1 parent 2b19509 commit d2452f1

File tree

2 files changed

+116
-66
lines changed

2 files changed

+116
-66
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package ops
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"io/ioutil"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
11+
"github.com/pkg/errors"
12+
"github.com/vim-volt/volt/config"
13+
"github.com/vim-volt/volt/dsl/dslctx"
14+
"github.com/vim-volt/volt/dsl/ops/util"
15+
"github.com/vim-volt/volt/dsl/types"
16+
"github.com/vim-volt/volt/lockjson"
17+
"github.com/vim-volt/volt/pathutil"
18+
"github.com/vim-volt/volt/plugconf"
19+
"github.com/vim-volt/volt/subcmd/builder"
20+
)
21+
22+
func init() {
23+
opsMap[MigratePlugconfConfigFuncOp.String()] = MigratePlugconfConfigFuncOp
24+
}
25+
26+
type migratePlugconfConfigFuncOp struct {
27+
funcBase
28+
}
29+
30+
// MigratePlugconfConfigFuncOp is "migrate/plugconf/config-func" operator
31+
var MigratePlugconfConfigFuncOp = &migratePlugconfConfigFuncOp{
32+
funcBase("migrate/plugconf/config-func"),
33+
}
34+
35+
func (*migratePlugconfConfigFuncOp) Bind(args ...types.Value) (types.Expr, error) {
36+
if err := util.Signature().Check(args); err != nil {
37+
return nil, err
38+
}
39+
retType := types.VoidType
40+
return types.NewExpr(MigratePlugconfConfigFuncOp, args, retType), nil
41+
}
42+
43+
func (*migratePlugconfConfigFuncOp) InvertExpr(_ context.Context, args []types.Value) (types.Value, error) {
44+
return MigratePlugconfConfigFuncOp.Bind(args...)
45+
}
46+
47+
func (*migratePlugconfConfigFuncOp) EvalExpr(ctx context.Context, args []types.Value) (_ types.Value, rollback func(), result error) {
48+
rollback = NoRollback
49+
lockJSON := ctx.Value(dslctx.LockJSONKey).(*lockjson.LockJSON)
50+
cfg := ctx.Value(dslctx.ConfigKey).(*config.Config)
51+
52+
parseResults, parseErr := plugconf.ParseMultiPlugconf(lockJSON.Repos)
53+
if parseErr.HasErrs() {
54+
var errMsg bytes.Buffer
55+
errMsg.WriteString("Please fix the following errors before migration:")
56+
for _, err := range parseErr.Errors().Errors {
57+
for _, line := range strings.Split(err.Error(), "\n") {
58+
errMsg.WriteString(" ")
59+
errMsg.WriteString(line)
60+
}
61+
}
62+
result = errors.New(errMsg.String())
63+
return
64+
}
65+
66+
type plugInfo struct {
67+
path string
68+
content []byte
69+
}
70+
infoList := make([]plugInfo, 0, len(lockJSON.Repos))
71+
72+
// Collects plugconf infomations and check errors
73+
parseResults.Each(func(reposPath pathutil.ReposPath, info *plugconf.ParsedInfo) {
74+
if !info.ConvertConfigToOnLoadPreFunc() {
75+
return // no s:config() function
76+
}
77+
content, err := info.GeneratePlugconf()
78+
if err != nil {
79+
result = errors.Wrap(err, "could not generate converted plugconf")
80+
return
81+
}
82+
infoList = append(infoList, plugInfo{
83+
path: reposPath.Plugconf(),
84+
content: content,
85+
})
86+
})
87+
if result != nil {
88+
return
89+
}
90+
91+
// After checking errors, write the content to files
92+
for _, info := range infoList {
93+
os.MkdirAll(filepath.Dir(info.path), 0755)
94+
err := ioutil.WriteFile(info.path, info.content, 0644)
95+
if err != nil {
96+
result = errors.Wrapf(err, "could not write to file %s", info.path)
97+
return
98+
}
99+
}
100+
101+
// Build ~/.vim/pack/volt dir
102+
result = builder.Build(false, lockJSON, cfg)
103+
if result != nil {
104+
result = errors.Wrap(result, "could not build "+pathutil.VimVoltDir())
105+
}
106+
return
107+
}

migration/plugconf-config-func.go

Lines changed: 9 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
package migration
22

33
import (
4-
"io/ioutil"
5-
"os"
6-
"path/filepath"
7-
"strings"
4+
"context"
85

96
"github.com/pkg/errors"
107
"github.com/vim-volt/volt/config"
8+
"github.com/vim-volt/volt/dsl"
9+
"github.com/vim-volt/volt/dsl/dslctx"
10+
"github.com/vim-volt/volt/dsl/ops"
1111
"github.com/vim-volt/volt/lockjson"
12-
"github.com/vim-volt/volt/logger"
13-
"github.com/vim-volt/volt/pathutil"
14-
"github.com/vim-volt/volt/plugconf"
15-
"github.com/vim-volt/volt/subcmd/builder"
16-
"github.com/vim-volt/volt/transaction"
1712
)
1813

1914
func init() {
@@ -41,63 +36,11 @@ Description
4136
}
4237

4338
func (*plugconfConfigMigrater) Migrate(lockJSON *lockjson.LockJSON, cfg *config.Config) (result error) {
44-
results, parseErr := plugconf.ParseMultiPlugconf(lockJSON.Repos)
45-
if parseErr.HasErrs() {
46-
logger.Error("Please fix the following errors before migration:")
47-
for _, err := range parseErr.Errors().Errors {
48-
for _, line := range strings.Split(err.Error(), "\n") {
49-
logger.Errorf(" %s", line)
50-
}
51-
}
52-
return nil
53-
}
54-
55-
type plugInfo struct {
56-
path string
57-
content []byte
58-
}
59-
infoList := make([]plugInfo, 0, len(lockJSON.Repos))
60-
61-
// Collects plugconf infomations and check errors
62-
results.Each(func(reposPath pathutil.ReposPath, info *plugconf.ParsedInfo) {
63-
if !info.ConvertConfigToOnLoadPreFunc() {
64-
return // no s:config() function
65-
}
66-
content, err := info.GeneratePlugconf()
67-
if err != nil {
68-
logger.Errorf("Could not generate converted plugconf: %s", err)
69-
return
70-
}
71-
infoList = append(infoList, plugInfo{
72-
path: reposPath.Plugconf(),
73-
content: content,
74-
})
75-
})
76-
77-
// After checking errors, write the content to files
78-
for _, info := range infoList {
79-
os.MkdirAll(filepath.Dir(info.path), 0755)
80-
err := ioutil.WriteFile(info.path, info.content, 0644)
81-
if err != nil {
82-
return err
83-
}
84-
}
85-
86-
// Begin transaction
87-
trx, err := transaction.Start()
88-
if err != nil {
89-
return err
90-
}
91-
defer func() {
92-
if err := trx.Done(); err != nil {
93-
result = err
94-
}
95-
}()
96-
97-
// Build ~/.vim/pack/volt dir
98-
err = builder.Build(false, lockJSON, cfg)
39+
ctx := dslctx.WithDSLValues(context.Background(), lockJSON, cfg)
40+
expr, err := ops.MigratePlugconfConfigFuncOp.Bind()
9941
if err != nil {
100-
return errors.Wrap(err, "could not build "+pathutil.VimVoltDir())
42+
return errors.Wrapf(err, "cannot bind %s operator", ops.LockJSONWriteOp.String())
10143
}
102-
return nil
44+
_, err = dsl.Execute(ctx, expr)
45+
return err
10346
}

0 commit comments

Comments
 (0)