Repository navigation
Expand file tree
/
Copy pathpackage.gradle
More file actions
227 lines (191 loc) · 5.37 KB
/
Copy pathpackage.gradle
File metadata and controls
227 lines (191 loc) · 5.37 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import java.nio.file.Files
import java.nio.file.Path
import java.util.stream.Collectors
import java.util.stream.Stream
final class GeneratedService {
String name
Path directory
Stream<GeneratedFile> list() {
def existing = listExistingFiles()
def index = createIndexFile()
return Stream.concat(existing, Stream.of(index))
}
private def createIndexFile() {
return new IndexFile(serviceName: name)
}
private def listExistingFiles() {
return Files.list(directory)
.filter { Files.isRegularFile(it) }
.map {
return new SourceFile(
path: it,
serviceName: name
)
}
}
}
interface GeneratedFile {
void writeTo(Path directory)
}
interface GeneratedServiceFile extends GeneratedFile {
Path createPath(Path sourceDirectory)
}
final class SourceFile implements GeneratedServiceFile {
String serviceName
Path path
@Override
void writeTo(Path directory) {
def content = processContent()
def filePath = createPath(directory)
MoreFiles.writeString(filePath, String.valueOf(content))
}
@Override
Path createPath(Path directory) {
return directory
.resolve(serviceName)
.resolve(selectGeneratedName())
}
private def processContent() {
def content = Files.readString(path)
return process(content)
}
private static def process(String content) {
return '// Processed by JSheets Protobuf Packager\n' + content
}
private def selectGeneratedName() {
return path.fileName.toString()
}
}
final class IndexFile implements GeneratedServiceFile {
String serviceName
@Override
void writeTo(Path directory) {
def filePath = createPath(directory)
MoreFiles.writeString(filePath, generateContent())
}
@Override
Path createPath(Path directory) {
return directory.resolve(serviceName).resolve('index.ts')
}
private def generateContent() {
return """// Generated by protobuf p
export * from './${serviceName}_service_grpc_web_pb'
export * from './${serviceName}_service_pb'
"""
}
}
final class StaticContentFile implements GeneratedFile {
Path name
String content
@Override
void writeTo(Path directory) {
def filePath = directory.resolve(name)
MoreFiles.writeString(filePath, content)
}
}
final class GeneratedLibrary {
Path outputDirectory
Path destination
String version
void generate() {
generateServiceFiles().each { it.writeTo(destination.resolve ("src/jsheets")) }
generateLibraryFiles().each { it.writeTo(destination) }
}
private Collection<GeneratedFile> generateLibraryFiles() {
def files = new ArrayList<GeneratedFile>()
files.addAll(Arrays.asList(generateConfigurations()))
files.addAll(Arrays.asList(generateSpecialFiles()))
return files
}
private GeneratedFile[] generateServiceFiles() {
return createFilesForService().stream()
.map { Arrays.stream(it) }
.flatMap { it }
.toArray { new GeneratedFile[it] }
}
private GeneratedFile[] generateConfigurations() {
return [generatePackageConfig()]
}
private static GeneratedFile[] generateSpecialFiles() {
return [generateIndexFile()]
}
private static GeneratedFile generateIndexFile() {
return new StaticContentFile(
content: '',
name: Path.of('index.js')
)
}
private def generatePackageConfig() {
def content = """
{
"name": "@jsheets/protocol",
"version": "$version",
"description": "JSheets protocol",
"license": "MIT",
"main": "index.js",
"files": ["src"]
}
""".stripIndent().strip()
return new StaticContentFile(
content: content,
name: Path.of('package.json')
)
}
private static def toUnixPath(Path path) {
return path.toString().replace("\\", "/")
}
private Collection<GeneratedFile> createFilesForService() {
return listServicesInDirectory(outputDirectory)
.map { it.list() }
.flatMap { it }
.toArray { new GeneratedFile[it] }
}
private static boolean isServiceContainer(Path path) {
return Files.isDirectory(path) && containsFiles(path)
}
private static boolean containsFiles(Path path) {
return Files.list(path).withCloseable { directory ->
directory.anyMatch { Files.isRegularFile(it) }
}
}
private Stream<GeneratedService> listServicesInDirectory(Path directory) {
if (!Files.isDirectory(directory)) {
return Stream.empty()
}
return Files.walk(directory).withCloseable { files ->
files.filter { isServiceContainer(it) }
.map {
def fileName = it.fileName.toString()
new GeneratedService(name: fileName, directory: it)
}
.collect(Collectors.toList())
.stream()
}
}
}
final class MoreFiles {
private MoreFiles() {}
static void writeString(Path path, String content) {
createIfNotExists(path)
Files.writeString(path, content)
}
static void createIfNotExists(Path path) {
if (Files.exists(path)) {
return
}
if (path.getParent() != null) {
Files.createDirectories(path.getParent())
}
Files.createFile(path)
}
}
task generateJavascriptPackage {
doLast {
def generation =new GeneratedLibrary(
version: version,
outputDirectory: project.projectDir.toPath().resolve('build/generated/source/proto/main/js'),
destination: project.projectDir.toPath().resolve ("generated/js-protocol"),
)
generation.generate()
}
}