-
Notifications
You must be signed in to change notification settings - Fork 4
/
save.go
109 lines (98 loc) · 3.13 KB
/
save.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 project
import (
"fmt"
"github.com/pkg/errors"
"projectforge.dev/projectforge/app/lib/filesystem"
"projectforge.dev/projectforge/app/lib/metamodel/enum"
"projectforge.dev/projectforge/app/lib/metamodel/model"
"projectforge.dev/projectforge/app/lib/theme"
"projectforge.dev/projectforge/app/util"
)
func (s *Service) Save(prj *Project, logger util.Logger) error {
fn := ConfigDir + "/project.json"
if prj.Icon == DefaultIcon {
prj.Icon = ""
}
if prj.Exec == prj.Key {
prj.Exec = ""
}
if prj.Theme != nil && prj.Theme.Equals(theme.Default) {
prj.Theme = nil
defer func() {
prj.Theme = theme.Default
}()
}
tgtFS, err := s.GetFilesystem(prj)
if err != nil {
return err
}
j := util.ToJSON(prj)
err = tgtFS.WriteFile(fn, []byte(j), filesystem.DefaultMode, true)
if err != nil {
return errors.Wrapf(err, "unable to write project file to [%s]", fn)
}
if _ = prj.ModuleArgExport(s, logger); prj.ExportArgs != nil {
if len(prj.ExportArgs.Config) > 0 {
fn := ConfigDir + "/export/config.json"
err = tgtFS.WriteFile(fn, util.ToJSONBytes(prj.ExportArgs.Config, true), filesystem.DefaultMode, true)
if err != nil {
return errors.Wrapf(err, "unable to write project file to [%s]", fn)
}
}
for _, m := range prj.ExportArgs.Models {
err = s.SaveExportModel(tgtFS, m)
if err != nil {
return err
}
}
}
if prj.Path != "" && prj.Path != "." {
fs, _ := filesystem.NewFileSystem(".", false, "")
s.addAdditional(prj.Path, fs, logger)
}
return nil
}
func (s *Service) SaveExportGroups(fs filesystem.FileLoader, g model.Groups) error {
fn := fmt.Sprintf("%s/export/groups.json", ConfigDir)
j := util.ToJSON(g)
err := fs.WriteFile(fn, []byte(j), filesystem.DefaultMode, true)
if err != nil {
return errors.Wrapf(err, "unable to write export groups file to [%s]", fn)
}
return nil
}
func (s *Service) SaveExportModel(fs filesystem.FileLoader, mdl *model.Model) error {
if mdl.HasTag("json") {
return nil
}
fn := fmt.Sprintf("%s/export/models/%s.json", ConfigDir, mdl.Name)
j := util.ToJSONBytes(mdl, true)
err := fs.WriteFile(fn, j, filesystem.DefaultMode, true)
if err != nil {
return errors.Wrapf(err, "unable to write export model file to [%s]", fn)
}
return nil
}
func (s *Service) DeleteExportModel(fs filesystem.FileLoader, mdl string, logger util.Logger) error {
fn := fmt.Sprintf("%s/export/models/%s.json", ConfigDir, mdl)
if err := fs.Remove(fn, logger); err != nil {
return errors.Wrapf(err, "unable to delete export model file [%s]", fn)
}
return nil
}
func (s *Service) SaveExportEnum(fs filesystem.FileLoader, e *enum.Enum) error {
fn := fmt.Sprintf("%s/export/enums/%s.json", ConfigDir, e.Name)
j := util.ToJSONBytes(e, true)
err := fs.WriteFile(fn, j, filesystem.DefaultMode, true)
if err != nil {
return errors.Wrapf(err, "unable to write export enum file to [%s]", fn)
}
return nil
}
func (s *Service) DeleteExportEnum(fs filesystem.FileLoader, e string, logger util.Logger) error {
fn := fmt.Sprintf("%s/export/enums/%s.json", ConfigDir, e)
if err := fs.Remove(fn, logger); err != nil {
return errors.Wrapf(err, "unable to delete export enum file [%s]", fn)
}
return nil
}