Skip to content

Commit

Permalink
enhance migration
Browse files Browse the repository at this point in the history
  • Loading branch information
CMA2401PT committed Jun 1, 2022
1 parent 802eb87 commit 869450e
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 25 deletions.
1 change: 1 addition & 0 deletions omega/defines/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type OmegaConfig struct {
Name string `yaml:"系统名" json:"name,omitempty"`
Version string `yaml:"版本" json:"version,omitempty"`
Trigger *TriggerConfig `yaml:"触发词" json:"触发词"`
MigrationVersion int `yanl:"自动升级识别号" json:"自动升级识别号"`
CommandFeedBackByDefault bool `yaml:"默认情况下是否有commandfeedback" json:"默认情况下是否有commandfeedback,omitempty"`
}

Expand Down
9 changes: 0 additions & 9 deletions omega/mainframe/default_components.json
Original file line number Diff line number Diff line change
Expand Up @@ -660,15 +660,6 @@
"菜单末尾": "§b§l==============================END===============================\n前往 omega_storage/配置/组件-菜单显示.json 配置",
"菜单标题": "§b§l==========================Omega System==========================",
"目录结构":[
{
"触发词":"传送",
"菜单项":"[i]:输入 §l§c[systemTrigger] [defaultTrigger] §f§r以显示传送点相关操作",
"子节点":[
"移除",
"保存",
"所有地点"
]
},
{
"触发词":"建筑",
"菜单项":"[i]:输入 §l§d[systemTrigger] [defaultTrigger] §f§r以显示建筑相关操作",
Expand Down
1 change: 1 addition & 0 deletions omega/mainframe/default_config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"系统名": "Omega",
"版本": "0.0.2",
"自动升级识别号":542,
"触发词": {
"默认触发词": "omg",
"允许的触发词": [
Expand Down
73 changes: 73 additions & 0 deletions omega/mainframe/upgrade/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package upgrade

import (
"fmt"
"io/ioutil"
"path"
"phoenixbuilder/omega/defines"
"phoenixbuilder/omega/utils"
"strings"
)

func checkMigrationVersion(root string) (int, error) {
d := path.Join(root, "配置")
entries, err := ioutil.ReadDir(d)
if err != nil {
return 0, err
}
for _, entry := range entries {
if entry.Name() == "主系统.json" {
p := path.Join(root, "配置", entry.Name())
c := &defines.OmegaConfig{}
if err := utils.GetJsonData(p, c); err != nil {
return 0, fmt.Errorf("读取[" + p + "]时出错" + err.Error())
}
return c.MigrationVersion, nil
}
}
return 0, fmt.Errorf("配置/主系统.json 未找到,配置文件夹可能损坏了")
}

func setMigrationVersion(root string, version int) error {
d := path.Join(root, "配置")
entries, err := ioutil.ReadDir(d)
if err != nil {
return err
}
for _, entry := range entries {
if entry.Name() == "主系统.json" {
p := path.Join(root, "配置", entry.Name())
c := &defines.OmegaConfig{}
if err := utils.GetJsonData(p, c); err != nil {
return fmt.Errorf("读取[" + p + "]时出错" + err.Error())
}
c.MigrationVersion = version
return utils.WriteJsonData(p, c)
}
}
return fmt.Errorf("配置/主系统.json 未找到,配置文件夹可能损坏了")
}

func updateComponentConfig(root string, name string, handleFn func(c *defines.ComponentConfig)) error {
d := path.Join(root, "配置")
entries, err := ioutil.ReadDir(d)
if err != nil {
return err
}
for _, entry := range entries {
if !strings.HasPrefix(entry.Name(), "组件") {
continue
}
p := path.Join(root, "配置", entry.Name())
c := &defines.ComponentConfig{}
if err := utils.GetJsonData(p, c); err != nil {
return fmt.Errorf("读取[" + p + "]时出错" + err.Error())
}
if c.Name != name {
continue
}
handleFn(c)
return utils.WriteJsonData(p, c)
}
return fmt.Errorf("配置/组件-" + name + ".json 未找到,配置文件夹可能损坏了")
}
7 changes: 0 additions & 7 deletions omega/mainframe/upgrade/main/main.go

This file was deleted.

9 changes: 0 additions & 9 deletions omega/mainframe/upgrade/new_menu_structure_for_menu.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
[
{
"触发词":"传送",
"菜单项":"[i]:输入 §l§c[systemTrigger] [defaultTrigger] §f§r以显示传送点相关操作",
"子节点":[
"移除",
"保存",
"所有地点"
]
},
{
"触发词":"建筑",
"菜单项":"[i]:输入 §l§d[systemTrigger] [defaultTrigger] §f§r以显示建筑相关操作",
Expand Down
1 change: 1 addition & 0 deletions omega/mainframe/upgrade/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ func Upgrade() {
Policy_4(storageRoot)
Policy_5(storageRoot)
Policy_6(storageRoot)
Policy_7(storageRoot)
}
31 changes: 31 additions & 0 deletions omega/mainframe/upgrade/policy_7p.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package upgrade

import "phoenixbuilder/omega/defines"

func Policy_7(root string) {
if version, err := checkMigrationVersion(root); err == nil && version < 542 {
updateComponentConfig(root, "菜单显示", func(c *defines.ComponentConfig) {
if s := c.Configs["目录结构"]; s != nil {
if ts, success := s.([]interface{}); success {
newTS := make([]interface{}, 0)
for _, e := range ts {
if te, success := e.(map[string]interface{}); success {
if v, hasK := te["触发词"]; hasK {
if tv, success := v.(string); success {
if tv == "传送" {
continue
}
}
}
}
newTS = append(newTS, e)
}
c.Configs["目录结构"] = newTS
}
}
})
setMigrationVersion(root, 542)
} else if err != nil {
panic(err)
}
}

0 comments on commit 869450e

Please sign in to comment.