Skip to content

Commit

Permalink
Merge pull request #177 from partik03/linode_transformer
Browse files Browse the repository at this point in the history
Linode transformer
  • Loading branch information
mbcse authored Aug 25, 2023
2 parents f2ef9ee + 30c65a8 commit 6a25385
Show file tree
Hide file tree
Showing 6 changed files with 902 additions and 77 deletions.
2 changes: 1 addition & 1 deletion generator/generators/googleCloud/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function extractClassBasedSDKData(methods, sdkFiles) {
params: parameters,
returnType: returnType,
returnTypeName: null,
client: classAst.name.text, // Class name
client: classAst.name.text,
};
if (returnType === 'TypeReference') {
method_1.returnTypeName =
Expand Down
239 changes: 174 additions & 65 deletions generator/generators/linode/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,19 @@ var __generator =
}
};
exports.__esModule = true;
exports.generateLinodeClass = exports.extractSDKData = void 0;
exports.generateLinodeClass = exports.getFunctions = exports.extractSDKData = void 0;
var fs = require('fs');
var typescript_1 = require('typescript');
var parser_1 = require('../../parsers/linode/parser');
var transformer_1 = require('../../transformers/linode/transformer');
var helper_1 = require('../lib/helper');
var dummyFile = process.cwd() + '/dummyClasses/linode.js';
var dummyAst = typescript_1.createSourceFile(
dummyFile,
fs.readFileSync(dummyFile).toString(),
typescript_1.ScriptTarget.Latest,
true
);
function extractSDKData(sdkAst, serviceClass) {
var methods = [];
var functions = [];
Expand Down Expand Up @@ -170,93 +180,192 @@ function extractSDKData(sdkAst, serviceClass) {
};
// common type
if (param.type.typeName) {
parameter.typeName = param.type.typeName.excapedText;
parameter.typeName = param.type.typeName.text;
}
parameters_1.push(parameter);
}
});
// console.log(parameters);
methods.push({
functionName: name_1.toString(),
SDKFunctionName: methodName,
params: parameters_1,
});
}
});
var classData = {
className: '',
functions: methods,
serviceName: null,
};
return classData;
return methods;
}
exports.extractSDKData = extractSDKData;
function generateLinodeClass(serviceClass, serviceName) {
function getFunctions(sdkFiles, serviceClass) {
return __awaiter(this, void 0, void 0, function() {
var methods, files, sdkFiles;
var functionsArray;
var _this = this;
return __generator(this, function(_a) {
methods = [];
if (serviceClass == null) return [2 /*return*/];
Object.keys(serviceClass).map(function(key, index) {
methods.push({
pkgName: serviceClass[key].split(' ')[0],
fileName: serviceClass[key].split(' ')[1],
functionName: key,
SDKFunctionName: serviceClass[key].split(' ')[2],
params: [],
returnType: null,
client: null,
});
});
files = Array.from(
new Set(
methods.map(function(method) {
return method.fileName;
})
)
);
sdkFiles = files.map(function(file) {
return {
fileName: file,
pkgName: methods[0].pkgName,
ast: null,
client: null,
sdkFunctionNames: methods
.filter(function(method) {
return method.fileName === file;
})
.map(function(method) {
return method.SDKFunctionName;
}),
};
});
sdkFiles.map(function(file) {
return __awaiter(_this, void 0, void 0, function() {
var _this = this;
return __generator(this, function(_a) {
parser_1.getAST(file).then(function(result) {
switch (_a.label) {
case 0:
functionsArray = [];
return [
4 /*yield*/,
sdkFiles.map(function(file) {
return __awaiter(_this, void 0, void 0, function() {
var sdkAst, classData;
var _this = this;
return __generator(this, function(_a) {
sdkAst = result;
try {
classData = extractSDKData(
sdkAst,
serviceClass
);
classData.serviceName = serviceName;
} catch (e) {
console.error(e);
}
parser_1
.getAST(file)
.then(function(result) {
return __awaiter(
_this,
void 0,
void 0,
function() {
var sdkAst, functions;
return __generator(
this,
function(_a) {
sdkAst = result;
try {
functions = extractSDKData(
sdkAst,
serviceClass
);
functions.map(
function(
method,
index
) {
functionsArray.push(
method
);
}
);
} catch (e) {
console.error(
e
);
}
return [
2 /*return*/,
];
}
);
}
);
});
return [2 /*return*/];
});
});
}),
];
case 1:
_a.sent();
return [2 /*return*/, functionsArray];
}
});
});
}
exports.getFunctions = getFunctions;
function generateLinodeClass(serviceClass, serviceName) {
return __awaiter(this, void 0, void 0, function() {
var methods_1,
files,
sdkFiles,
functionsArray,
classData,
output,
filePath,
dir,
e_1;
return __generator(this, function(_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, , 4]);
methods_1 = [];
if (serviceClass == null) return [2 /*return*/];
Object.keys(serviceClass).map(function(key, index) {
methods_1.push({
pkgName: serviceClass[key].split(' ')[0],
fileName: serviceClass[key].split(' ')[1],
functionName: key,
SDKFunctionName: serviceClass[key].split(' ')[2],
params: [],
returnType: null,
client: null,
});
return [2 /*return*/];
});
});
});
return [2 /*return*/];
files = Array.from(
new Set(
methods_1.map(function(method) {
return method.fileName;
})
)
);
sdkFiles = files.map(function(file) {
return {
fileName: file,
pkgName: methods_1[0].pkgName,
ast: null,
client: null,
sdkFunctionNames: methods_1
.filter(function(method) {
return method.fileName === file;
})
.map(function(method) {
return method.SDKFunctionName;
}),
};
});
return [4 /*yield*/, getFunctions(sdkFiles, serviceClass)];
case 1:
functionsArray = _a.sent();
classData = {
className: serviceName + 'LinodeClass',
functions: functionsArray,
serviceName: serviceName,
};
return [
4 /*yield*/,
transformer_1.transform(dummyAst, classData),
];
case 2:
output = _a.sent();
filePath = void 0;
dir = helper_1.getDir(serviceName);
if (
!fs.existsSync(
process.cwd() + '/generatedClasses/Linode/' + dir
)
) {
fs.mkdirSync(
process.cwd() + '/generatedClasses/Linode/' + dir
);
}
if (/^[A-Z]*$/.test(serviceName)) {
filePath =
process.cwd() +
'/generatedClasses/Linode/' +
dir +
'/linode-' +
serviceName +
'.js';
} else {
filePath =
process.cwd() +
'/generatedClasses/Linode/' +
dir +
'/linode-' +
serviceName.charAt(0).toLowerCase() +
serviceName.slice(1) +
'.js';
}
helper_1.printFile(filePath, output);
return [3 /*break*/, 4];
case 3:
e_1 = _a.sent();
console.error(e_1);
return [3 /*break*/, 4];
case 4:
return [2 /*return*/];
}
});
});
}
Expand Down
33 changes: 31 additions & 2 deletions generator/generators/linode/generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as fs from 'fs';
import { createSourceFile, ScriptTarget,SyntaxKind } from 'typescript';
import { createSourceFile, ScriptTarget, SyntaxKind } from 'typescript';

import { getAST } from '../../parsers/linode/parser';
import { transform } from '../../transformers/linode/transformer';
import { getDir, printFile } from '../lib/helper';

interface SDKClassData {
Expand Down Expand Up @@ -78,6 +79,8 @@ export function extractSDKData(sdkAst, serviceClass) {
parameters.push(parameter);
}
});
// console.log(parameters);

methods.push({
functionName: name.toString(),
SDKFunctionName: methodName,
Expand Down Expand Up @@ -152,7 +155,33 @@ export async function generateLinodeClass(serviceClass, serviceName) {
functions: functionsArray,
serviceName: serviceName,
};
const output = await transform(dummyAst, classData);
let filePath;
const dir = getDir(serviceName);

if (!fs.existsSync(process.cwd() + '/generatedClasses/Linode/' + dir)) {
fs.mkdirSync(process.cwd() + '/generatedClasses/Linode/' + dir);
}
if (/^[A-Z]*$/.test(serviceName)) {
filePath =
process.cwd() +
'/generatedClasses/Linode/' +
dir +
'/linode-' +
serviceName +
'.js';
} else {
filePath =
process.cwd() +
'/generatedClasses/Linode/' +
dir +
'/linode-' +
serviceName.charAt(0).toLowerCase() +
serviceName.slice(1) +
'.js';
}
printFile(filePath, output);
} catch (e) {
console.error(e);
}
}
}
9 changes: 0 additions & 9 deletions generator/parsers/linode/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,6 @@ function getAST(sdkFileInfo) {
return [
4 /*yield*/,
ast.forEachChild(function(child) {
// fs.writeFile('test.txt', SyntaxKind[child.kind], null);
// console.log(SyntaxKind[child.kind]);
// console.log('Linode', SyntaxKind[child.kind]);
// console.log("child",child);
if (
typescript_1.SyntaxKind[child.kind] ===
'FirstStatement'
Expand All @@ -185,16 +181,11 @@ function getAST(sdkFileInfo) {
cloned_1.push(
tmp_1.declarationList.declarations[0]
);
// console.log(
// 'name',
// tmp.declarationList.declarations[0].type.parameters[0]
// );
}
}),
];
case 1:
_a.sent();
// console.log('cloned', cloned);
if (!cloned_1) {
reject(new Error('Function not found!'));
} else {
Expand Down
Loading

0 comments on commit 6a25385

Please sign in to comment.