Created
January 27, 2017 20:25
-
-
Save bobmcwhirter/8e0d4fb220522e14c709370a786a3a03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict" | |
var version = 'unknown'; | |
var fs = require('fs'); | |
var http = require('http'); | |
var https = require('https'); | |
var path = require('path'); | |
var Q = require('q'); | |
var unzip = require('unzip'); | |
var SNAPSHOT_REPO = "https://oss.sonatype.org/content/repositories/snapshots/"; | |
var RELEASE_REPO = "http://repo.maven.apache.org/maven2/"; | |
const generateDocs = (book, page)=>{ | |
console.log("Processing: ", page.title, page.groupId, page.artifactId); | |
var groupId = 'org.wildfly.swarm'; | |
if ( page.groupId ) { | |
groupId = page.groupId; | |
} | |
var artifactId = path.basename( page.path, '.adoc' ); | |
if ( page.artifactId ) { | |
artifactId = page.artifactId; | |
} | |
return locateJar(groupId, artifactId) | |
.then( (path)=>{ | |
var deferred = Q.defer(); | |
fs.createReadStream(path) | |
.pipe(unzip.Parse()) | |
.on('entry', function (entry) { | |
var fileName = entry.path; | |
var type = entry.type; // 'Directory' or 'File' | |
var size = entry.size; | |
if (fileName === "META-INF/configuration-meta.properties" ) { | |
//entry.pipe(fs.createWriteStream('output/path')); | |
deferred.resolve( entry ); | |
} else { | |
entry.autodrain(); | |
} | |
}); | |
return deferred.promise; | |
}) | |
.then( (entry)=>{ | |
var deferred = Q.defer(); | |
var body = ""; | |
entry.on('data', (chunk)=>{ | |
body += chunk; | |
}) | |
entry.on('end', ()=>{ | |
deferred.resolve(body); | |
}) | |
return deferred.promise; | |
} ) | |
.then( (docs)=>{ | |
console.log( "page", page ); | |
page.content += ("= " + page.title + "\n\n"); | |
docs.split('\n').forEach( (line)=>{ | |
var parts = line.split("="); | |
var key = parts[0]; | |
var doc = parts[1]; | |
console.log( "key-1:", key, typeof key ); | |
if ( ! key.startsWith('#') && key != 'fraction' && doc ) { | |
key = key.replace( /\*/g, '_KEY_'); | |
page.content += (key + ":: " + doc + "\n"); | |
} | |
}); | |
return page; | |
}) | |
} | |
const locateJar = (groupId, artifactId)=>{ | |
var path = groupId.replace(/\./g, '/') + '/' + artifactId + '/' + version + '/' + artifactId + '-' + version + '.jar'; | |
console.log( "locate jar: " + groupId + ':' + artifactId + ':' + version ); | |
console.log( "path: " + path ); | |
return locateJarInRemoteRepo(path); | |
} | |
const locateJarInLocalRepo = (path)=>{ | |
var localPath = process.env.HOME + '/.m2/repository/' + path; | |
if ( fs.existsSync( localPath ) ) { | |
console.log( "found locally" ); | |
} | |
return Q.resolve(localPath); | |
} | |
const locateJarInRemoteRepo = (path)=>{ | |
if ( path.includes('SNAPSHOT')) { | |
return determineSnapshotPath(SNAPSHOT_REPO,path) | |
.then( (mavenPath)=>{ | |
return fetchJar( SNAPSHOT_REPO, mavenPath ); | |
} ); | |
} | |
return fetchJar( RELEASE_REPO, path ); | |
} | |
const determineSnapshotPath = (repo,mavenPath)=>{ | |
return fetchMavenMetadata(repo,mavenPath) | |
.then( (content)=>{ | |
var results = new RegExp("<timestamp>([0-9.]+)</timestamp>").exec( content ); | |
var tstamp = results[1]; | |
results = new RegExp("<buildNumber>([0-9]+)</buildNumber>").exec( content ); | |
var buildNumber = results[1]; | |
var dirname = path.dirname( mavenPath ); | |
var filename = path.basename( mavenPath ); | |
return dirname + '/' + filename.replace('SNAPSHOT', tstamp + '-' + buildNumber ); | |
}) | |
} | |
const fetchMavenMetadata = (repo,mavenPath)=>{ | |
var dir = path.dirname(mavenPath); | |
var metadata = dir + '/maven-metadata.xml'; | |
var deferred = Q.defer(); | |
var url = repo + '/' + metadata; | |
var request = https.get(url, function(response) { | |
var body = ""; | |
response.on('data', (chunk)=>{ | |
body += chunk; | |
}) | |
response.on('end', ()=>{ | |
deferred.resolve(body); | |
}) | |
}); | |
return deferred.promise; | |
} | |
const fetchJar = (repo, mavenPath)=>{ | |
if ( ! fs.existsSync( "_tmp" ) ) { | |
fs.mkdirSync( "_tmp" ); | |
} | |
var url = repo + '/' + mavenPath; | |
var deferred = Q.defer(); | |
var filename = path.basename(mavenPath); | |
var outpath = "_tmp/" + filename; | |
var file = fs.createWriteStream(outpath); | |
var request = https.get(url, function(response) { | |
response.pipe(file); | |
file.on('finish', ()=>{ | |
deferred.resolve(outpath); | |
} ); | |
}); | |
return deferred.promise; | |
} | |
module.exports = { | |
hooks: { | |
'init': function() { | |
version = this.config.values.variables.versions.swarm; | |
}, | |
'page:before': function(page) { | |
if ( ! page.path.startsWith('fractions/')) { | |
return; | |
} | |
return generateDocs(this, page); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment