This repository has been archived by the owner on Jul 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkspace.go
76 lines (59 loc) · 1.85 KB
/
Workspace.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
package core
import (
"sort"
"github.com/inkyblackness/shocked-core/io"
"github.com/inkyblackness/shocked-core/release"
)
// Workspace describes a container of projects.
type Workspace struct {
autoSaveTimeoutMSec int
source release.Release
projectsContainer release.ReleaseContainer
projects map[string]*Project
}
// NewWorkspace takes a Release as a basis for existing resources and returns
// a new workspace instance. With this instance, projects from given projects container
// can be worked with.
func NewWorkspace(source release.Release, projects release.ReleaseContainer, autoSaveTimeoutMSec int) *Workspace {
ws := &Workspace{
autoSaveTimeoutMSec: autoSaveTimeoutMSec,
source: source,
projectsContainer: projects,
projects: make(map[string]*Project)}
return ws
}
// ProjectNames returns all currently known project identifiers.
func (ws *Workspace) ProjectNames() []string {
names := ws.projectsContainer.Names()
sort.Strings(names)
return names
}
// Project returns a project matching the given identifier.
func (ws *Workspace) Project(name string) (project *Project, err error) {
project, existing := ws.projects[name]
if !existing {
rel, relErr := ws.projectsContainer.Get(name)
if relErr == nil {
library := io.NewReleaseStoreLibrary(ws.source, rel, ws.autoSaveTimeoutMSec)
project, err = NewProject(name, library)
if err == nil {
ws.projects[name] = project
}
} else {
err = relErr
}
}
return
}
// NewProject tries to create a new project in the workspace.
func (ws *Workspace) NewProject(name string) (project *Project, err error) {
rel, err := ws.projectsContainer.New(name)
if err == nil {
library := io.NewReleaseStoreLibrary(ws.source, rel, ws.autoSaveTimeoutMSec)
project, err = NewProject(name, library)
if err == nil {
ws.projects[name] = project
}
}
return
}