-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathsupport.go
170 lines (145 loc) · 3.97 KB
/
support.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
// Copyright 2017 Seamia Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"errors"
"flag"
"io"
"os"
"path"
"path/filepath"
"strings"
"github.com/seamia/tools/support"
)
//----------------------------------------------------------------------------------------------------------------------
var legalSimpleTypes = map[string]bool{
"bool": true,
"bytes": true,
"double": true,
"fixed32": true,
"fixed64": true,
"float": true,
"int32": true,
"int64": true,
"sfixed32": true,
"sfixed64": true,
"sint32": true,
"sint64": true,
"string": true,
"uint32": true,
"uint64": true,
}
var legalSimpleMapTypes = map[string]bool{
"bool": true,
"fixed32": true,
"fixed64": true,
"int32": true,
"int64": true,
"sfixed32": true,
"sfixed64": true,
"sint32": true,
"sint64": true,
"string": true,
"uint32": true,
"uint64": true,
}
func isSimpleType(name string) bool {
_, found := legalSimpleTypes[name]
return found
}
func isSimpleMapType(name string) bool {
_, found := legalSimpleMapTypes[name]
return found
}
//------------------------------------------------------------------------ support
func Exists(name string) bool {
if _, err := os.Stat(name); err != nil {
return os.IsExist(err)
}
return true
}
//----------------------------------------------------------------------------------------------------------------------
var (
g_includes []string = nil
g_incs = flag.String("inc", "", "Include directories (semicolon separated)")
)
func openLocalFile(file string) (io.Reader, error) {
return os.Open(file)
}
func locate(name, rootDir string) string {
components := strings.Split(rootDir, string(os.PathSeparator))
for len(components) > 0 {
dir := strings.Join(components, string(os.PathSeparator))
fullname := path.Join(dir, name)
if Exists(fullname) {
return fullname
}
components = components[:len(components)-1]
}
return ""
}
func Find(name, rootDir string) (io.Reader, error) {
if g_includes == nil {
// if it is the first time we're called: init the 'inclides' list
g_includes = make([]string, 0)
// 1. get the includes from the config file
if includes, found := g_config["includes"].([]interface{}); found {
for _, include := range includes {
candidate := os.ExpandEnv(include.(string))
if len(candidate) > 0 {
g_includes = append(g_includes, candidate)
}
}
}
// 2. get the includes from the command line
parts := strings.Split(*g_incs, ";")
for _, part := range parts {
candidate := os.ExpandEnv(part)
if len(candidate) > 0 {
g_includes = append(g_includes, candidate)
}
}
}
if Exists(name) {
return openLocalFile(name)
}
includes := make([]string, len(g_includes), len(g_includes)+1)
copy(includes, g_includes)
includes = append(includes, rootDir)
for _, include := range includes {
candidate := path.Join(include, name)
if Exists(candidate) {
debug("-- found file", name, "in one of the include folders:", include)
return openLocalFile(candidate)
}
}
trace("!! file", name, "was not found in any of the include folders:", g_includes)
{
// let's try to find the required file somewhere in the (partial) root directory
found := locate(name, rootDir)
if len(found) > 0 {
return openLocalFile(found)
}
status("*** failed to find file [", name, "] with root [", rootDir, "]")
}
// todo: enable downloads later?
// return downloadFile(name)
return nil, errors.New("Failed to find file [" + name + "].")
}
func getProtoName(raw, suffix string) string {
if len(suffix) > 0 {
raw += ":" + suffix
}
return support.NameToId(raw, 16)
}
func pathSplit(path string) (dir, file string) {
if abs, err := filepath.Abs(path); err == nil {
if abs != path {
debug("*** replacing [", path, "] with [", abs, "]")
path = abs
}
}
i := strings.LastIndex(path, string(os.PathSeparator))
return path[:i+1], path[i+1:]
}