|
| 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 | +} |
0 commit comments