forked from bitrise-steplib/steps-recreate-user-schemes
/
project.go
132 lines (106 loc) · 3.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 (
"fmt"
"github.com/bitrise-io/go-utils/pathutil"
xcodeproject "github.com/bitrise-io/go-xcode/xcodeproject/xcodeproj"
"github.com/bitrise-io/go-xcode/xcodeproject/xcscheme"
"github.com/bitrise-io/go-xcode/xcodeproject/xcworkspace"
)
type container interface {
// schemes returns schemes mapped to the project or workspace path
schemes() (map[string][]xcscheme.Scheme, error)
projects() ([]xcodeproject.XcodeProj, []string, error)
}
// projectContainer ...
type projectContainer struct {
project xcodeproject.XcodeProj
}
func newProject(path string) (projectContainer, error) {
if !xcodeproject.IsXcodeProj(path) {
return projectContainer{}, fmt.Errorf("%s is not an Xcode project", path)
}
project, err := xcodeproject.Open(path)
if err != nil {
return projectContainer{}, fmt.Errorf("opening the Xcode project at %s failed: %w", path, err)
}
return projectContainer{
project: project,
}, nil
}
func (p projectContainer) schemes() (map[string][]xcscheme.Scheme, error) {
projectSchemes, err := p.project.Schemes()
if err != nil {
return nil, fmt.Errorf("listing schemes in Xcode project at %s failed: %w", p.project.Path, err)
}
containerToSchemes := make(map[string][]xcscheme.Scheme)
containerToSchemes[p.project.Path] = projectSchemes
return containerToSchemes, nil
}
func (p projectContainer) projects() ([]xcodeproject.XcodeProj, []string, error) {
return []xcodeproject.XcodeProj{p.project}, []string{}, nil
}
// workspaceContainer ...
type workspaceContainer struct {
workspace xcworkspace.Workspace
}
func newWorkspace(path string) (workspaceContainer, error) {
if !xcworkspace.IsWorkspace(path) {
return workspaceContainer{}, fmt.Errorf("%s is not an Xcode workspace", path)
}
workspace, err := xcworkspace.Open(path)
if err != nil {
return workspaceContainer{}, fmt.Errorf("opening the Xcode workspace at %s failed: %w", path, err)
}
return workspaceContainer{
workspace: workspace,
}, nil
}
func (w workspaceContainer) schemes() (map[string][]xcscheme.Scheme, error) {
containerToSchemes, err := w.workspace.Schemes()
if err != nil {
return nil, fmt.Errorf("listing schemes in Xcode workspace at %s failed: %w", w.workspace.Path, err)
}
return containerToSchemes, nil
}
func (w workspaceContainer) projects() ([]xcodeproject.XcodeProj, []string, error) {
projPaths, err := w.workspace.ProjectFileLocations()
if err != nil {
return nil, nil, err
}
var projects []xcodeproject.XcodeProj
var missingProjects []string
for _, projPath := range projPaths {
if exist, err := pathutil.IsPathExists(projPath); err != nil {
return nil, nil, fmt.Errorf("list Xcode projects in the workspace at %s failed: can not check if path (%s) exists: %w", w.workspace.Path, projPath, err)
} else if !exist {
missingProjects = append(missingProjects, projPath)
continue
}
project, err := xcodeproject.Open(projPath)
if err != nil {
return nil, nil, fmt.Errorf("opening the Xcode project (%s) in the workspace at %s failed: %w", projPath, w.workspace.Path, err)
}
projects = append(projects, project)
}
return projects, missingProjects, nil
}
func openContainer(path string) (container, error) {
if xcodeproject.IsXcodeProj(path) {
container, err := newProject(path)
if err != nil {
return nil, fmt.Errorf("failed to open Xcode project: %w", err)
}
return container, nil
} else if xcworkspace.IsWorkspace(path) {
container, err := newWorkspace(path)
if err != nil {
return nil, fmt.Errorf("failed to open Xcode workspace: %w", err)
}
return container, nil
}
return nil, fmt.Errorf("project path (%s) has an invalid extension, excepted '%s' or '%s'",
path,
xcodeproject.XcodeProjExtension,
xcworkspace.XCWorkspaceExtension,
)
}