-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Partik SIngh <partik.sbumrah.cse21@itbhu.ac.in>
- Loading branch information
Showing
6 changed files
with
36,597 additions
and
7,602 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class ClassName { | ||
/** | ||
* | ||
* @param {module} do Linode SDK | ||
* @param {object} options SDK options | ||
*/ | ||
constructor(linodeSdk, linodeToken) { | ||
this._linode = linodeSdk; | ||
this._linodeToken = linodeToken; | ||
} | ||
|
||
function() { | ||
this._linode.setToken(this._linodeToken) | ||
return new Promise((resolve, reject) => { | ||
this._linode | ||
.SDKFunctionName() | ||
.then(data => resolve(data)) | ||
.catch(err => reject(err)); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = ClassName; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import * as fs from "fs"; | ||
import { createSourceFile, ScriptTarget, SyntaxKind } from "typescript"; | ||
|
||
import { getAST } from "../../parsers/do/parser"; | ||
import { transform } from "../../transformers/do/transformer"; | ||
import { getDir,printFile } from "../lib/helper"; | ||
|
||
interface FunctionData { | ||
functionName: string; | ||
SDKFunctionName: string; | ||
params: param[]; | ||
} | ||
|
||
interface param { | ||
name: string; | ||
type: string; | ||
typeName: string; | ||
} | ||
|
||
interface ClassData { | ||
className: string; | ||
functions: FunctionData[]; | ||
serviceName: string; | ||
} | ||
|
||
const dummyFile = process.cwd() + "/dummyClasses/linode.js"; | ||
|
||
const dummyAst = createSourceFile( | ||
dummyFile, | ||
fs.readFileSync(dummyFile).toString(), | ||
ScriptTarget.Latest, | ||
true | ||
); | ||
|
||
export function extractSDKData(sdkClassAst, serviceClass) { | ||
let methods: FunctionData[] = []; | ||
const functions = []; | ||
|
||
Object.keys(serviceClass).map((key, index) => { | ||
functions.push(serviceClass[key].split(" ")[1]); | ||
}); | ||
|
||
sdkClassAst.members.map(method => { | ||
if (method.name && functions.includes(method.name.text)) { | ||
let name; | ||
Object.keys(serviceClass).map((key, index) => { | ||
if (serviceClass[key].split(" ")[1] === method.name.text) { | ||
name = key; | ||
} | ||
}); | ||
|
||
const parameters = []; | ||
method.parameters.map(param => { | ||
if (param.name.text !== "callback") { | ||
const parameter = { | ||
name: param.name.text, | ||
optional: param.questionToken ? true : false, | ||
type: SyntaxKind[param.type.kind], | ||
typeName: null | ||
}; | ||
|
||
if (parameter.type === "TypeReference" && param.type.typeName) { | ||
parameter.typeName = param.type.typeName.text; | ||
} | ||
|
||
parameters.push(parameter); | ||
} | ||
}); | ||
|
||
methods.push({ | ||
functionName: name.toString(), | ||
SDKFunctionName: method.name.text.toString(), | ||
params: parameters | ||
}); | ||
} | ||
}); | ||
|
||
const classData: ClassData = { | ||
className: sdkClassAst.name.text, | ||
functions: methods, | ||
serviceName: null | ||
}; | ||
|
||
return classData; | ||
} | ||
|
||
export function generateLinodeClass(serviceClass, serviceName) { | ||
const sdkFile = serviceClass[Object.keys(serviceClass)[0]].split(" ")[0]; | ||
getAST(sdkFile).then(async result => { | ||
const sdkClassAst = result; | ||
try { | ||
const classData: ClassData = extractSDKData(sdkClassAst, serviceClass); | ||
classData.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 + | ||
"/do-" + | ||
serviceName + | ||
".js"; | ||
} else { | ||
filePath = | ||
process.cwd() + | ||
"/generatedClasses/Linode/" + | ||
dir + | ||
"/do-" + | ||
serviceName.charAt(0).toLowerCase() + | ||
serviceName.slice(1) + | ||
".js"; | ||
} | ||
printFile(filePath, output); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
import { createSourceFile, ScriptTarget, SyntaxKind } from "typescript"; | ||
|
||
export function getAST(sdkFileName) { | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
const file = path.join( | ||
__dirname, | ||
"../../../node_modules/linode/dist/modules/" + | ||
sdkFileName.toLowerCase() | ||
); | ||
const ast = createSourceFile( | ||
file, | ||
fs.readFileSync(file).toString(), | ||
ScriptTarget.Latest, | ||
true | ||
); | ||
|
||
let cloned = null; | ||
|
||
await ast.forEachChild(child => { | ||
if (SyntaxKind[child.kind] === "ClassDeclaration") { | ||
cloned = Object.assign({}, child); | ||
} | ||
}); | ||
|
||
if (!cloned) { | ||
reject(new Error("Class not found!")); | ||
} else { | ||
resolve(cloned); | ||
} | ||
} catch (error) { | ||
if (error.code === "ENOENT") { | ||
reject(new Error("File not found!")); | ||
} else { | ||
reject(error); | ||
} | ||
} | ||
}); | ||
} |
Oops, something went wrong.