-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcenter.js
183 lines (152 loc) · 5.14 KB
/
center.js
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
'use strict'
const paths = require('path')
const ops = require('./ops')
const libs = require('./libs')
const mods = require('./mods')
const buck = require('./buck')
const mvn = require('./mvn')
const javaRules = {java_library: true, kotlin_library: true}
function targetOf(a) { return a[buck.attr.qname] }
function pathOf(a) { return a[buck.attr.path] }
function jarOf(a) { return a[buck.attr.mavenCoords] }
function ruleOf(a) { return a[buck.attr.type] }
function quote(a) { return `'${a}'` }
let conf, artifacts, artifactModules
const defaultVersion = '0-SNAPSHOT', defaultGroup = 'group'
function parentVersion() {
return conf['maven.publish_ver'] || defaultVersion
}
function parentGroup() {
return conf['maven.publish_group'] || defaultGroup
}
function parentArtifact(parentPom) {
let p = parentPom.replace('.xml', '')
.replace('.pom', '')
.replace(/\//g, '')
.replace(/\./g, '')
return p === 'pom' ? 'parent' : p
}
function prepare() {
if (artifacts) return // noop if already consumed stagedObjects
conf = buck.conf()
artifacts = buck.info(`//...`)
.filter(t => ruleOf(t) in javaRules && jarOf(t))
.reduce((acc, rule) => (acc[pathOf(rule)] = rule, acc), {})
artifactModules = mods.all.filter(m => m.path in artifacts)
}
function genProjects(parentPom) {
console.log('Parent POM:', parentPom)
generateParentTemplate(parentPom)
for (let a of artifactModules) {
generatePom(a, artifacts[a.path], parentPom)
}
}
function generateParentTemplate(parentPom) {
let template = parentPom.replace('.xml', '.template.xml')
if (ops.exists(template)) {
let content = ops.read(template)
.replace(/G<!--GROUP-->/g, parentGroup())
.replace(/A<!--ARTIFACT-->/g, parentArtifact(parentPom))
.replace(/V<!--VERSION-->/g, parentVersion())
.replace('<!--MODULES-->', artifactModules.map(m => `
<module>${m.path}</module>`).join(''))
ops.write(parentPom, content)
}
}
function generatePom(module, rule, parentPom) {
let coords = mvn.coords(jarOf(rule))
// for each
let parentPath = '../' + parentPom
for (let ch of module.path) {
if (ch === '/') parentPath = '../' + parentPath
}
let pom = paths.join(module.path, 'pom.xml')
let template = paths.join(module.path, 'pom.template.xml')
let content
if (ops.exists(template)) {
content = ops.read(template)
.replace(/G<!--GROUP-->/g, coords.group)
.replace(/A<!--ARTIFACT-->/g, coords.artifact)
.replace(/V<!--VERSION-->/g, coords.version)
.replace(/G<!--PARENT-GROUP-->/g, parentGroup())
.replace(/A<!--PARENT-ARTIFACT-->/g, parentArtifact(parentPom))
.replace(/V<!--PARENT-VERSION-->/g, parentVersion())
.replace(/P<!--PARENT-PATH-->/g, parentPath)
.replace('<!--DEPENDENCIES-->', pomDependencies(module))
} else {
content = `<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>${coords.group}</groupId>
<artifactId>${coords.artifact}</artifactId>
<version>${coords.version}</version>
<packaging>jar</packaging>
<name>\${project.groupId}.\${project.artifactId}</name>
<parent>
<groupId>${parentGroup()}</groupId>
<artifactId>${parentArtifact(parentPom)}</artifactId>
<version>${parentVersion()}</version>
<relativePath>${parentPath}</relativePath>
</parent>
<dependencies>${pomDependencies(module)}
</dependencies>
</project>
`
}
ops.write(pom, content)
}
function pomDependencies(module) {
let deps = []
for (let [path, dep] of Object.entries(module.depmods)) {
let jar = jarOf(artifacts[path] || {})
if (jar) {
let coords = mvn.coords(jar)
deps.push(`
<dependency>
<groupId>${coords.group}</groupId>
<artifactId>${coords.artifact}</artifactId>
<version>${coords.version}</version>${classifier(coords)}${scope(dep)}
</dependency>`)
}
}
for (let [path, dep] of Object.entries(module.deplibs)) {
for (let coords of dep.lib.jars) {
deps.push(`
<dependency>
<groupId>${coords.group}</groupId>
<artifactId>${coords.artifact}</artifactId>
<version>${coords.version}</version>${classifier(coords)}${scope(dep)}
</dependency>`)
}
}
return deps.join('')
function scope(dep) {
if (dep.test) return `
<scope>test</scope>`
if (dep.provided && dep.exported) return `
<scope>provided</scope>`
if (dep.provided) return `
<scope>provided</scope>
<optional>true</optional>`
if (dep.exported) return `
<scope>compile</scope>`
return `
<scope>compile</scope>
<optional>true</optional>`
}
function classifier(coords) {
return coords.classifier ? `
<classifier>${coords.classifier}</classifier>` : ''
}
}
module.exports = {
prepare, genProjects,
toString() {
return [
'Pom Modules',
...artifactModules.map(m => '\t' + String(m)),
'Artifacts',
...Object.values(artifacts).map(t => '\t' + targetOf(t) + ' [' + jarOf(t) + ']'),
].join('\n')
}
}