feat: add jdk support

This commit is contained in:
MiaoWoo 2019-11-09 18:34:54 +08:00
parent 7b67dc7719
commit b60259c345
2 changed files with 37 additions and 19 deletions

View File

@ -1,11 +1,12 @@
# sponge bukkit # sponge bukkit jdk
TYPE=sponge TYPE=jdk
TARGET=../types/dist/typings
npx tsc src/build.ts --outDir dist npx tsc src/build.ts --outDir dist
cd dist cd dist
rm -rf temp rm -rf temp
mkdir -p temp mkdir -p temp
node build.js ${TYPE} node build.js ${TYPE}
cd ../ cd ../
rm -rf src/typings rm -rf ${TARGET}/${TYPE}
mkdir -p src/typings/${TYPE} mkdir -p ${TARGET}/${TYPE}
cp dist/temp/* src/typings/${TYPE}/ -R cp dist/temp/* ${TARGET}/${TYPE}/ -R

View File

@ -10,7 +10,7 @@ function convertJson2TypeDefiend(infile: string, outDir: string) {
closeBuk++; closeBuk++;
const nms = qnas.slice(1, qnas.length - 1); const nms = qnas.slice(1, qnas.length - 1);
for (const nm of nms) { for (const nm of nms) {
temp += `${' '.repeat(closeBuk)}namespace ${nm} {\n`; temp += `${' '.repeat(closeBuk)}namespace ${nm.replace('function', 'function$')} {\n`;
closeBuk++; closeBuk++;
} }
let classModifier = formatClassModifier(obj.modifiers) let classModifier = formatClassModifier(obj.modifiers)
@ -33,7 +33,7 @@ function convertJson2TypeDefiend(infile: string, outDir: string) {
temp = temp.substr(0, temp.length - 2); temp = temp.substr(0, temp.length - 2);
} }
} else { } else {
temp += `${(obj.superclass && obj.superclass.qualifiedName.startsWith('org.')) ? (' extends ' + obj.superclass.qualifiedName) : ''}`; temp += `${(obj.superclass) ? (' extends ' + obj.superclass.qualifiedName) : ''}`;
if (safeInterface.length > 0) { if (safeInterface.length > 0) {
temp += ' implements ' temp += ' implements '
for (const ifs of safeInterface) { for (const ifs of safeInterface) {
@ -46,7 +46,7 @@ function convertJson2TypeDefiend(infile: string, outDir: string) {
temp += ' {\n' temp += ' {\n'
closeBuk++; closeBuk++;
for (const constructor of obj.constructors) { for (const constructor of obj.constructors) {
temp += `${formatDoc(constructor.docString, closeBuk)}${' '.repeat(closeBuk)}constructor(${formatParameters(constructor.parameters)})\n`; temp += `${formatDoc(constructor.docString, closeBuk)}${' '.repeat(closeBuk)}// @ts-ignore\n${' '.repeat(closeBuk)}constructor(${formatParameters(constructor.parameters)})\n`;
} }
let members = []; let members = [];
@ -77,8 +77,8 @@ function convertJson2TypeDefiend(infile: string, outDir: string) {
temp += `${' '.repeat(closeBuk - index - 1)}}\n`; temp += `${' '.repeat(closeBuk - index - 1)}}\n`;
} }
fs.writeFileSync(`${outDir}/${file}.ts`, temp); fs.writeFileSync(`${outDir}/${file}.${suffix}`, temp);
return `${file}.ts`; return `${file}.${suffix}`;
} }
function formatClassModifier(modifiers: string) { function formatClassModifier(modifiers: string) {
@ -98,7 +98,11 @@ function formatDoc(doc: string, closeBuk: number) {
} }
function replaceModifiers(modifiers: string, absClass = false): string { function replaceModifiers(modifiers: string, absClass = false): string {
// modifiers = modifiers.replace(' final', ' readonly');
modifiers = modifiers.split(" final")[0]; modifiers = modifiers.split(" final")[0];
modifiers = modifiers.split(" native")[0];
modifiers = modifiers.split(" volatile")[0];
modifiers = modifiers.split(" transient")[0];
modifiers = modifiers.split(" synchronized")[0]; modifiers = modifiers.split(" synchronized")[0];
if (!absClass) { if (!absClass) {
modifiers = modifiers.split(" abstract")[0]; modifiers = modifiers.split(" abstract")[0];
@ -117,6 +121,7 @@ function formatParameters(params: any[]) {
const nameMap = []; const nameMap = [];
nameMap['function'] = 'func' nameMap['function'] = 'func'
nameMap['in'] = 'input' nameMap['in'] = 'input'
nameMap['var'] = 'variable'
function mappingName(name: string) { function mappingName(name: string) {
if (whiteKey.includes(name)) { return name } if (whiteKey.includes(name)) { return name }
@ -124,7 +129,7 @@ function mappingName(name: string) {
return outName; return outName;
} }
let whiteKey = ['shift', "map", 'filter', 'values', 'valueOf', 'toString', 'length', 'includes', 'entries','keys'] let whiteKey = ['shift', "map", 'filter', 'values', 'valueOf', 'toString', 'length', 'includes', 'entries', 'keys', 'join', 'fill']
const typeMap = []; const typeMap = [];
typeMap['int'] = 'number'; typeMap['int'] = 'number';
@ -133,17 +138,22 @@ typeMap['int[][]'] = 'number[][]';
typeMap['byte'] = 'number'; typeMap['byte'] = 'number';
typeMap['byte[]'] = 'number[]'; typeMap['byte[]'] = 'number[]';
typeMap['double'] = 'number'; typeMap['double'] = 'number';
typeMap['double[]'] = 'number[]';
typeMap['short'] = 'number'; typeMap['short'] = 'number';
typeMap['short[]'] = 'number[]';
typeMap['float'] = 'number'; typeMap['float'] = 'number';
typeMap['float[]'] = 'number[]';
typeMap['long'] = 'number'; typeMap['long'] = 'number';
typeMap['long[]'] = 'number[]';
typeMap['<any>'] = 'any'; typeMap['<any>'] = 'any';
typeMap['char'] = 'string'; typeMap['char'] = 'string';
typeMap['char[]'] = 'string[]';
typeMap['java.lang.String'] = "string"; typeMap['java.lang.String'] = "string";
typeMap['java.util.Date'] = 'any /*java.util.Date*/' // typeMap['java.util.Date'] = 'any /*java.util.Date*/'
typeMap['java.util.List'] = 'any[] /*java.util.List*/' // typeMap['java.util.List'] = 'any[] /*java.util.List*/'
typeMap['java.util.Set'] = 'any[] /*java.util.Set*/' // typeMap['java.util.Set'] = 'any[] /*java.util.Set*/'
typeMap['java.util.Collection'] = 'any[] /*java.util.Collection*/' // typeMap['java.util.Collection'] = 'any[] /*java.util.Collection*/'
typeMap['java.util.Map'] = 'Map<any, any> /*java.util.Map*/' // typeMap['java.util.Map'] = 'Map<any, any> /*java.util.Map*/'
// Sponge // Sponge
typeMap['Vectori'] = 'any /*Vector3i*/' typeMap['Vectori'] = 'any /*Vector3i*/'
typeMap['Vectord'] = 'any /*Vector3d*/' typeMap['Vectord'] = 'any /*Vector3d*/'
@ -167,12 +177,19 @@ typeMap['Matrix4d'] = 'any /*Matrix4d*/'
function mappingType(type: string): string { function mappingType(type: string): string {
let outType = typeMap[type] || type || 'any'; let outType = typeMap[type] || type || 'any';
outType = outType.startsWith('java.') || outType.startsWith('javax.') ? 'any' : outType; if (outType.indexOf('.') != -1) {
return outType; if (outType.startsWith('java.') || outType.startsWith('org.')) {
} else {
outType = `any /*${outType}*/`
}
}
return outType.replace('function', 'function$');
} }
var args = process.argv.splice(2) var args = process.argv.splice(2)
const suffix = 'd.ts'
const inDir = `../docs/${args[0]}` const inDir = `../docs/${args[0]}`
const outDir = "./temp"; const outDir = "./temp";
const files = fs.readdirSync(inDir); const files = fs.readdirSync(inDir);
@ -180,4 +197,4 @@ let index = '';
for (const file of files) { for (const file of files) {
index += `/// <reference path="./${convertJson2TypeDefiend(file, outDir)}" />\n`; index += `/// <reference path="./${convertJson2TypeDefiend(file, outDir)}" />\n`;
} }
fs.writeFileSync(`${outDir}/index.ts`, index); fs.writeFileSync(`${outDir}/index.${suffix}`, index);