This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
project.go
132 lines (107 loc) · 2.69 KB
/
project.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
package main
import (
"acdc/diagram"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
type Project struct {
Info Info `json:"Info"`
Model *Model `json:"Model"`
Analysis *Analysis `json:"Analysis"`
Evaluate *Evaluate `json:"Evaluate"`
Results *Results `json:"Results"`
Diagram *diagram.Diagram `json:"Diagram"`
}
type Info struct {
Date string `json:"Date"`
Path string `json:"Path"`
}
func NewProject() *Project {
return &Project{
Evaluate: NewEvaluate(),
Model: NewModel(),
}
}
func LoadProject(path string) (*Project, error) {
// Read project file
bs, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error reading project: %w", err)
}
// Parse project file
p := NewProject()
if err := json.Unmarshal(bs, p); err != nil {
return nil, fmt.Errorf("error parsing project: %w", err)
}
// Save project path
p.Info.Path = path
return p, nil
}
func (p *Project) Save() (*Project, error) {
if p == nil {
return nil, fmt.Errorf("project not loaded")
}
// Update time
p.Info.Date = time.Now().Format(time.RFC850)
// Create temporary project to save relevant parts
pSave := Project{
Info: p.Info,
Model: p.Model,
Analysis: p.Analysis,
Evaluate: p.Evaluate,
}
// Convert project to json
bs, err := json.MarshalIndent(pSave, "", "\t")
if err != nil {
return nil, fmt.Errorf("error encoding project: %w", err)
}
// Write project file
err = os.WriteFile(p.Info.Path, bs, 0777)
if err != nil {
return nil, fmt.Errorf("error reading project: %w", err)
}
// If project results has a path, write file
if p.Results != nil && p.Results.LinDir != "" {
// Write results file
bs, err := json.MarshalIndent(p.Results, "", "\t")
if err != nil {
return nil, err
}
err = os.WriteFile(filepath.Join(p.Results.LinDir, "results.json"), bs, 0777)
if err != nil {
return nil, err
}
// If project diagram is not nil, write file
if p.Diagram != nil {
// Write Diagram file
bs, err := json.MarshalIndent(p.Diagram, "", "\t")
if err != nil {
return nil, err
}
err = os.WriteFile(filepath.Join(p.Results.LinDir, "diagram.json"), bs, 0777)
if err != nil {
return nil, err
}
}
}
return p, nil
}
func (p *Project) RootPath() string {
return strings.TrimSuffix(p.Info.Path, filepath.Ext(p.Info.Path))
}
func (p *Project) Case(caseID int) (*Case, error) {
if p.Analysis == nil {
return nil, fmt.Errorf("no analysis is project")
}
// Loop through cases and find matching ID
for i := range p.Analysis.Cases {
if p.Analysis.Cases[i].ID == caseID {
return &p.Analysis.Cases[i], nil
}
}
return nil, fmt.Errorf("Case ID %d not found", caseID)
}