Skip to content

Commit

Permalink
POM file generation / enables upload to Central
Browse files Browse the repository at this point in the history
  • Loading branch information
elucash committed Nov 19, 2020
1 parent 26aa695 commit c305eb1
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 2 deletions.
165 changes: 165 additions & 0 deletions center.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
'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-->', parentGroup())
.replace('A<!--ARTIFACT-->', parentArtifact(parentPom))
.replace('V<!--VERSION-->', 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 relativePath = '../' + parentPom
for (let ch of module.path) {
if (ch === '/') relativePath = '../' + relativePath
}
let 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>${relativePath}</relativePath>
</parent>
<dependencies>${pomDependencies(module)}
</dependencies>
</project>
`
ops.write(paths.join(module.path, 'pom.xml'), 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')
}
}
4 changes: 2 additions & 2 deletions idea.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function libraryXml(lib) {
<root url="jar://$PROJECT_DIR$/${lib.symlinkSrc(j)}!/" />`)

if (lib.options.includeGeneratedSrcs) {
// This is very ad-hoc, convenience stuff so we can intellij idea
// This is very ad-hoc, convenience stuff so we can tell intellij idea
// to show not only original sources, but also derived sources,
// which otherwise are not available, only decompilable classes in IDE
let genSrcsRules = [].concat(lib.options.includeGeneratedSrcs)
Expand Down Expand Up @@ -60,7 +60,7 @@ function moduleMainXml(excludes) {
}

function moduleXml(mod) {
let excludesFolders = ['.out', '.ecj', '.cache'] // bin output + eclipse classes output + parcel etc
let excludesFolders = ['.out', '.ecj', '.cache', 'target'] // bin output + eclipse classes output + maybe maven + parcel etc
.filter(dir => ops.exists(paths.join(mod.path, dir)))
.map(dir => `
<excludeFolder url="file://$MODULE_DIR$/../../${mod.path}/${dir}" isTestSource="false" />`)
Expand Down
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const args = require('./args')
const ops = require('./ops')
const pub = require('./pub')
const grab = require('./grab')
const center = require('./center')

const opts = args({
'--help': ['Prints usage and option hints', function() {
Expand Down Expand Up @@ -67,6 +68,14 @@ const opts = args({
let info = mvn.coords(coords).info
console.log(JSON.stringify(info, null, 2))
}],
'--center': ['Generateds maven POMs/modules to deploy to Central and other stuff', (parentPom) => {
libs.prepare()
mods.discover()
center.prepare()
console.error(String(center))
syms.linkGenSrc()
center.genProjects(parentPom)
}],
}, {
before: (_, hint) => ops.info(`${hint}`),
end: () => ops.ok('OK'),
Expand Down
4 changes: 4 additions & 0 deletions mods.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ const mods = {
let m = moduleByPath[t.path]
if (!m) continue

// dependencies of annotation processor definition are
// not a regular compile dependencies
if (rule[buck.attr.type] === 'java_annotation_processor') continue

let isTest = isTestRule(rule)

addSourceFolders(m, rule, isTest)
Expand Down

0 comments on commit c305eb1

Please sign in to comment.