Add utility to parse maven identifiers.

This commit is contained in:
Daniel Scalzi
2019-07-29 20:53:26 -04:00
parent 1a9762daea
commit 55afd5fb04
3 changed files with 51 additions and 5 deletions

View File

@@ -2,6 +2,13 @@
import { resolve } from 'path' import { resolve } from 'path'
import yargs from 'yargs' import yargs from 'yargs'
function positionalRoot(yargs: yargs.Argv) {
return yargs.positional('root', {
describe: 'File structure root',
type: 'string'
})
}
// Registering yargs configuration. // Registering yargs configuration.
// tslint:disable-next-line:no-unused-expression // tslint:disable-next-line:no-unused-expression
yargs yargs
@@ -10,14 +17,11 @@ yargs
root: resolve root: resolve
}) })
.command({ .command({
command: 'generate <root>', command: 'generate server <root>',
aliases: ['g'], aliases: ['g'],
describe: 'Generate a distribution.json', describe: 'Generate a distribution.json',
builder: (yargs) => { builder: (yargs) => {
return yargs.positional('root', { return positionalRoot(yargs)
describe: 'File structure root',
type: 'string'
})
}, },
handler: (argv) => { handler: (argv) => {
console.log(`got generate with root=${argv.root}`) console.log(`got generate with root=${argv.root}`)

View File

@@ -4,6 +4,9 @@ export const Types: {[property: string]: Type} = {
id: 'Library', id: 'Library',
defaultExtension: 'jar' defaultExtension: 'jar'
}, },
/**
* @deprecated Will be replaced by Types.Forge.
*/
ForgeHosted: { ForgeHosted: {
id: 'ForgeHosted', id: 'ForgeHosted',
defaultExtension: 'jar' defaultExtension: 'jar'

39
src/util/maven.ts Normal file
View File

@@ -0,0 +1,39 @@
import { normalize } from 'path'
import { URL } from 'url'
export class MavenUtil {
public static readonly ID_REGEX = /(.+):(.+):([^@]+)(?:@{1}(.+)$)?/
public static isMavenIdentifier(id: string) {
return this.ID_REGEX.test(id)
}
public static mavenToString(id: string, extension = 'jar') {
if (!this.isMavenIdentifier(id)) {
return null
}
const result = this.ID_REGEX.exec(id)
if (result != null) {
const group = result[1]
const artifact = result[2]
const version = result[3]
const ext = result[4] || extension
return `${group.replace(/\./g, '/')}/${artifact}/${version}/${artifact}-${version}.${ext}`
}
return null
}
public static mavenToUrl(id: string, extension = 'jar') {
const res = this.mavenToString(id, extension)
return res == null ? null : new URL(res)
}
public static mavenToPath(id: string, extension = 'jar') {
const res = this.mavenToString(id, extension)
return res == null ? null : normalize(res)
}
}