This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
plugin_migrator.go
242 lines (216 loc) · 6.13 KB
/
plugin_migrator.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package sceneops
import (
"context"
"errors"
"github.com/reearth/reearth-backend/pkg/dataset"
"github.com/reearth/reearth-backend/pkg/layer"
"github.com/reearth/reearth-backend/pkg/plugin"
"github.com/reearth/reearth-backend/pkg/property"
"github.com/reearth/reearth-backend/pkg/rerror"
"github.com/reearth/reearth-backend/pkg/scene"
)
type PluginMigrator struct {
Property property.Loader
PropertySchema property.SchemaLoader
Dataset dataset.Loader
Layer layer.LoaderByScene
Plugin plugin.Loader
}
type MigratePluginsResult struct {
Scene *scene.Scene
Layers layer.List
Properties []*property.Property
RemovedLayers []layer.ID
RemovedProperties []property.ID
}
var (
ErrPluginNotInstalled error = errors.New("plugin not installed")
ErrInvalidPlugins error = errors.New("invalid plugins")
)
func (s *PluginMigrator) MigratePlugins(ctx context.Context, sc *scene.Scene, oldPluginID, newPluginID plugin.ID) (MigratePluginsResult, error) {
if s == nil {
return MigratePluginsResult{}, rerror.ErrInternalBy(errors.New("scene is nil"))
}
if oldPluginID.Equal(newPluginID) || oldPluginID.Name() != newPluginID.Name() {
return MigratePluginsResult{}, ErrInvalidPlugins
}
if !sc.Plugins().Has(oldPluginID) {
return MigratePluginsResult{}, ErrPluginNotInstalled
}
plugins, err := s.Plugin(ctx, []plugin.ID{oldPluginID, newPluginID})
if err != nil || len(plugins) < 2 {
return MigratePluginsResult{}, ErrInvalidPlugins
}
oldPlugin := plugins[0]
newPlugin := plugins[1]
// 全レイヤーの取得
layers, err := s.Layer(ctx, sc.ID())
if err != nil {
return MigratePluginsResult{}, err
}
modifiedLayers := layer.List{}
removedLayers := []layer.ID{}
propertyIDs := []property.ID{}
removedPropertyIDs := []property.ID{}
schemaMap := map[property.SchemaID]*property.Schema{}
// プロパティスキーマの取得と、古いスキーマと新しいスキーマのマップ作成
schemaIDs := []property.SchemaID{}
if oldPlugin.Schema() != nil {
if pps := newPlugin.Schema(); pps != nil {
schemaIDs = append(schemaIDs, *pps)
}
}
for _, e := range newPlugin.Extensions() {
schemaIDs = append(schemaIDs, e.Schema())
}
schemas, err := s.PropertySchema(ctx, schemaIDs...)
if err != nil {
return MigratePluginsResult{}, err
}
if oops := oldPlugin.Schema(); oops != nil {
if pps := newPlugin.Schema(); pps != nil {
for _, s := range schemas {
if s.ID() == *pps {
schemaMap[*oops] = s
}
}
}
}
for _, e := range oldPlugin.Extensions() {
if ne := newPlugin.Extension(e.ID()); ne != nil {
for _, s := range schemas {
if s.ID() == ne.Schema() {
schemaMap[e.Schema()] = s
}
}
}
}
// シーンのプラグイン
sc.Plugins().Upgrade(oldPluginID, newPluginID, nil, false)
for _, sp := range sc.Plugins().Plugins() {
if sp.Plugin().Equal(newPluginID) && sp.Property() != nil {
propertyIDs = append(propertyIDs, *sp.Property())
}
}
// シーンのウィジェット
sc.Widgets().UpgradePlugin(oldPluginID, newPluginID)
for _, w := range sc.Widgets().Widgets() {
if w.Plugin().Equal(newPluginID) {
if newPlugin.Extension(w.Extension()) == nil {
sc.Widgets().RemoveAllByPlugin(oldPluginID, w.Extension().Ref())
} else {
propertyIDs = append(propertyIDs, w.Property())
}
}
}
// レイヤー
for _, l := range layers {
if l == nil {
continue
}
ll := *l
llp := ll.Plugin()
lle := ll.Extension()
// 不正なレイヤーの検出
if llp != nil && lle != nil && (*llp).Equal(oldPluginID) {
if newPlugin.Extension(*lle) == nil {
// 削除
removedLayers = append(removedLayers, ll.ID())
if p := ll.Property(); p != nil {
removedPropertyIDs = append(removedPropertyIDs, *p)
}
if ib := ll.Infobox(); ib != nil {
removedPropertyIDs = append(removedPropertyIDs, ib.Property())
for _, f := range ib.Fields() {
removedPropertyIDs = append(removedPropertyIDs, f.Property())
}
}
continue
}
}
if p := ll.Property(); p != nil {
propertyIDs = append(propertyIDs, *p)
}
// 不正なInfoboxFieldの削除
if ib := ll.Infobox(); ib != nil {
removeFields := []layer.InfoboxFieldID{}
for _, f := range ib.Fields() {
if newPlugin.Extension(f.Extension()) == nil {
removeFields = append(removeFields, f.ID())
removedPropertyIDs = append(removedPropertyIDs, f.Property())
} else {
propertyIDs = append(propertyIDs, f.Property())
}
}
for _, f := range removeFields {
ib.Remove(f)
}
}
ll.SetPlugin(&newPluginID)
modifiedLayers = append(modifiedLayers, l)
}
// 不正なレイヤーのグループからの削除
for _, lg := range layers.ToLayerGroupList() {
modified := false
canceled := false
for _, l := range removedLayers {
if l == lg.ID() {
canceled = true
break
}
if lg.Layers().HasLayer(l) {
lg.Layers().RemoveLayer(l)
modified = true
}
}
if canceled {
continue
}
if modified {
already := false
for _, l := range modifiedLayers {
if l != nil && (*l).ID() == lg.ID() {
already = true
break
}
}
if already {
continue
}
var lg2 layer.Layer = lg
modifiedLayers = append(modifiedLayers, &lg2)
}
}
// プロパティの取得
properties, err := s.Property(ctx, propertyIDs...)
if err != nil {
return MigratePluginsResult{}, err
}
// データセットの取得
datasetIDs := collectDatasetIDs(properties)
datasets, err := s.Dataset(ctx, datasetIDs...)
if err != nil {
return MigratePluginsResult{}, err
}
datasetLoader := datasets.Map().Loader()
// プロパティの移行作業
for _, p := range properties {
if schema := schemaMap[p.Schema()]; schema != nil {
p.MigrateSchema(ctx, schema, datasetLoader)
}
}
return MigratePluginsResult{
Scene: sc,
Layers: modifiedLayers,
Properties: properties,
RemovedLayers: removedLayers,
RemovedProperties: removedPropertyIDs,
}, nil
}
func collectDatasetIDs(properties []*property.Property) []property.DatasetID {
res := []property.DatasetID{}
for _, p := range properties {
res = append(res, p.Datasets()...)
}
return res
}