-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathtypes.go
206 lines (174 loc) · 5.11 KB
/
types.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
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.
package types
import (
"fmt"
"strconv"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/arduino/sketch"
paths "github.com/arduino/go-paths-helper"
)
type SourceFile struct {
// Sketch or Library pointer that this source file lives in
Origin interface{}
// Path to the source file within the sketch/library root folder
RelativePath *paths.Path
}
// Create a SourceFile containing the given source file path within the
// given origin. The given path can be absolute, or relative within the
// origin's root source folder
func MakeSourceFile(ctx *Context, origin interface{}, path *paths.Path) (SourceFile, error) {
if path.IsAbs() {
var err error
path, err = sourceRoot(ctx, origin).RelTo(path)
if err != nil {
return SourceFile{}, err
}
}
return SourceFile{Origin: origin, RelativePath: path}, nil
}
// Return the build root for the given origin, where build products will
// be placed. Any directories inside SourceFile.RelativePath will be
// appended here.
func buildRoot(ctx *Context, origin interface{}) *paths.Path {
switch o := origin.(type) {
case *Sketch:
return ctx.SketchBuildPath
case *libraries.Library:
return ctx.LibrariesBuildPath.Join(o.Name)
default:
panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
}
}
// Return the source root for the given origin, where its source files
// can be found. Prepending this to SourceFile.RelativePath will give
// the full path to that source file.
func sourceRoot(ctx *Context, origin interface{}) *paths.Path {
switch o := origin.(type) {
case *Sketch:
return ctx.SketchBuildPath
case *libraries.Library:
return o.SourceDir
default:
panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
}
}
func (f *SourceFile) SourcePath(ctx *Context) *paths.Path {
return sourceRoot(ctx, f.Origin).JoinPath(f.RelativePath)
}
func (f *SourceFile) ObjectPath(ctx *Context) *paths.Path {
return buildRoot(ctx, f.Origin).Join(f.RelativePath.String() + ".o")
}
func (f *SourceFile) DepfilePath(ctx *Context) *paths.Path {
return buildRoot(ctx, f.Origin).Join(f.RelativePath.String() + ".d")
}
type SketchFile struct {
Name *paths.Path
}
type SketchFileSortByName []SketchFile
func (s SketchFileSortByName) Len() int {
return len(s)
}
func (s SketchFileSortByName) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s SketchFileSortByName) Less(i, j int) bool {
return s[i].Name.String() < s[j].Name.String()
}
type Sketch struct {
MainFile SketchFile
OtherSketchFiles []SketchFile
AdditionalFiles []SketchFile
}
func SketchToLegacy(sketch *sketch.Sketch) *Sketch {
s := &Sketch{}
s.MainFile = SketchFile{
paths.New(sketch.MainFile.Path),
}
for _, item := range sketch.OtherSketchFiles {
s.OtherSketchFiles = append(s.OtherSketchFiles, SketchFile{
paths.New(item.Path),
})
}
for _, item := range sketch.AdditionalFiles {
s.AdditionalFiles = append(s.AdditionalFiles, SketchFile{
paths.New(item.Path),
})
}
return s
}
func SketchFromLegacy(s *Sketch) *sketch.Sketch {
others := []*sketch.Item{}
for _, f := range s.OtherSketchFiles {
i := sketch.NewItem(f.Name.String())
others = append(others, i)
}
additional := []*sketch.Item{}
for _, f := range s.AdditionalFiles {
i := sketch.NewItem(f.Name.String())
additional = append(additional, i)
}
return &sketch.Sketch{
MainFile: &sketch.Item{
Path: s.MainFile.Name.String(),
},
LocationPath: s.MainFile.Name.Parent().String(),
OtherSketchFiles: others,
AdditionalFiles: additional,
}
}
type PlatforKeysRewrite struct {
Rewrites []PlatforKeyRewrite
}
func (p *PlatforKeysRewrite) Empty() bool {
return len(p.Rewrites) == 0
}
type PlatforKeyRewrite struct {
Key string
OldValue string
NewValue string
}
type Prototype struct {
FunctionName string
File string
Prototype string
Modifiers string
Line int
}
func (proto *Prototype) String() string {
return proto.Modifiers + " " + proto.Prototype + " @ " + strconv.Itoa(proto.Line)
}
type LibraryResolutionResult struct {
Library *libraries.Library
NotUsedLibraries []*libraries.Library
}
type CTag struct {
FunctionName string
Kind string
Line int
Code string
Class string
Struct string
Namespace string
Filename string
Typeref string
SkipMe bool
Signature string
Prototype string
PrototypeModifiers string
}
type Command interface {
Run(ctx *Context) error
}