-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Oracle Cloud parser #158
base: master
Are you sure you want to change the base?
Oracle Cloud parser #158
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import * as fs from "fs"; | ||
import { createSourceFile, ScriptTarget, SyntaxKind } from "typescript"; | ||
import { getAST } from "../../parsers/oracle/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/do.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 generateOracleClass(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/DO/" + dir)) { | ||
fs.mkdirSync(process.cwd() + "/generatedClasses/DO/" + dir); | ||
} | ||
if (/^[A-Z]*$/.test(serviceName)) { | ||
filePath = | ||
process.cwd() + | ||
"/generatedClasses/DO/" + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why saving the generated classes in the DO folder? |
||
dir + | ||
"/do-" + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ?? |
||
serviceName + | ||
".js"; | ||
} else { | ||
filePath = | ||
process.cwd() + | ||
"/generatedClasses/DO/" + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here also. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not have any prior experience with the dummy classes for Oracle cloud at the time of my earlier response. However, I have since worked on them, made necessary corrections, and currently, I am working on the transformer component. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dummy class for oracle #161 And I am currently working on building the transformer for Oracle Cloud. |
||
dir + | ||
"/do-" + | ||
serviceName.charAt(0).toLowerCase() + | ||
serviceName.slice(1) + | ||
".js"; | ||
} | ||
printFile(filePath, output); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
import { createSourceFile, ScriptTarget, SyntaxKind } from "typescript"; | ||
|
||
export function getAST(sdkpkgName) { | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
const file = path.join( | ||
__dirname, | ||
`../../../node_modules/oci-${sdkpkgName.toLowerCase()}/lib/client.d.ts` | ||
); | ||
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("PACKAGE NOT FOUND")); | ||
} else { | ||
reject(error); | ||
} | ||
} | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
import { expect } from "chai"; | ||
import { SyntaxKind } from "typescript"; | ||
|
||
import { getAST } from "../../../parsers/oracle/parser"; | ||
|
||
describe("Oracle Cloud parser getAST", () => { | ||
context("With existing file", () => { //checks if getAST returns the AST of the file when given a valid file. | ||
it("Should return Abstract syntax tree of the class", async () => { | ||
const ast: any = await getAST("dns"); | ||
expect(ast).to.be.an("object"); | ||
expect(SyntaxKind[ast.kind] === "ClassDeclaration").to.be.true; | ||
}); | ||
}); | ||
|
||
context("With non-existing file", () => { //checking if throws error when a non existent file is passed. | ||
it("should return File not found Error", async () => { | ||
try { | ||
await getAST("unknown.d.ts"); | ||
} catch (error) { | ||
expect(error.message).to.eql("File not found!"); | ||
} | ||
}); | ||
}); | ||
|
||
context("With wrong format file", () => { //if the file dors not contain a class declaration. | ||
it("Should return class not found Error", async () => { | ||
try { | ||
await getAST("../types/common.d.ts"); | ||
} catch (error) { | ||
expect(error.message).to.eql("Class not found!"); | ||
} | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why you are using the dummy class of digital ocean here.Is the dummy class for Oracle same as the class of digital ocean?