diff --git a/lib/generator.js b/lib/generator.js index 5ffe914..e9549c6 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -581,13 +581,36 @@ setup( } else { this.emit('def __init__(self):\n', level + 2); } + const wirePop = this.shouldWirePopGateway(); if (ast.initBody && ast.initBody.stmts.length !== 0) { this.isStaticFunction = false; this.visitStmts(ast.initBody, level + 4); this.isStaticFunction = true; - } else { + } else if (!wirePop) { this.emit('pass\n', level + 4); } + if (wirePop) { + this.imports.push({ + packageName: 'alibabacloud_gateway_pop.client', + className: 'Client', + aliasName: 'PopClient', + }); + this.package.dependencies.alibabacloud_gateway_pop = '0.1.4'; + this.emit(`self._product_id = '${this.config.productId}'\n`, level + 4); + this.emit('self._spi = PopClient()\n', level + 4); + } + } + + shouldWirePopGateway() { + const productId = this.config && this.config.productId; + if (typeof productId !== 'string' || !/^[A-Za-z][A-Za-z0-9]*$/.test(productId)) { + return false; + } + if (!this.parentModule) { + return false; + } + const info = this.packageInfo && this.packageInfo[_name(this.parentModule)]; + return !!(info && info.namemespace === 'alibabacloud_tea_openapi'); } eachImport(imports, usedModels, innerModule, filepath, level) { diff --git a/tests/fixtures/popv4/.libraries.json b/tests/fixtures/popv4/.libraries.json new file mode 100644 index 0000000..f791f5a --- /dev/null +++ b/tests/fixtures/popv4/.libraries.json @@ -0,0 +1,3 @@ +{ + "alibabacloud:OpenApi:*": "libraries/OpenApi" +} diff --git a/tests/fixtures/popv4/Darafile b/tests/fixtures/popv4/Darafile new file mode 100644 index 0000000..879f89b --- /dev/null +++ b/tests/fixtures/popv4/Darafile @@ -0,0 +1,14 @@ +{ + "scope": "alibabacloud", + "name": "main", + "version": "0.0.1", + "main": "./main.dara", + "productId": "Ecs", + "libraries": { + "OpenApi": "alibabacloud:OpenApi:*" + }, + "python": { + "package": "tea_python_tests", + "clientName": "client" + } +} diff --git a/tests/fixtures/popv4/libraries/OpenApi/Darafile b/tests/fixtures/popv4/libraries/OpenApi/Darafile new file mode 100644 index 0000000..de49256 --- /dev/null +++ b/tests/fixtures/popv4/libraries/OpenApi/Darafile @@ -0,0 +1,13 @@ +{ + "scope": "alibabacloud", + "name": "openapi", + "version": "0.0.1", + "main": "./main.dara", + "python": { + "package": "alibabacloud_tea_openapi", + "clientName": "client" + }, + "releases": { + "python": "alibabacloud_tea_openapi:0.4.5" + } +} diff --git a/tests/fixtures/popv4/libraries/OpenApi/main.dara b/tests/fixtures/popv4/libraries/OpenApi/main.dara new file mode 100644 index 0000000..a03f075 --- /dev/null +++ b/tests/fixtures/popv4/libraries/OpenApi/main.dara @@ -0,0 +1,3 @@ +model Config = {} + +init(config: Config); diff --git a/tests/fixtures/popv4/main.dara b/tests/fixtures/popv4/main.dara new file mode 100644 index 0000000..a8a063e --- /dev/null +++ b/tests/fixtures/popv4/main.dara @@ -0,0 +1,7 @@ +import OpenApi; + +extends OpenApi; + +init(config: OpenApi.Config) { + super(config); +} diff --git a/tests/main.tests.js b/tests/main.tests.js index 62a2aea..bd258e5 100644 --- a/tests/main.tests.js +++ b/tests/main.tests.js @@ -350,4 +350,26 @@ describe('Python Generator', function () { } }); }); + + it('openapi product wires pop gateway', function () { + const moduleName = 'popv4'; + const prefixDir = path.join(fixturesDir, moduleName); + const pkg = JSON.parse(fs.readFileSync(path.join(prefixDir, 'Darafile'), 'utf8')); + const moduleOutputDir = path.join(outputDir, moduleName); + const config = { + outputDir: moduleOutputDir, + pkgDir: prefixDir, + ...pkg, + ...pkg.python + }; + const generator = new Generator(config, lang); + const mainFilePath = path.join(prefixDir, 'main.dara'); + generator.visit(DSL.parse(fs.readFileSync(mainFilePath, 'utf8'), mainFilePath)); + const client = fs.readFileSync(path.join(moduleOutputDir, 'tea_python_tests', 'client.py'), 'utf8'); + assert.ok(client.includes('from alibabacloud_gateway_pop.client import Client as PopClient')); + assert.ok(client.includes("self._product_id = 'Ecs'")); + assert.ok(client.includes('self._spi = PopClient()')); + const setup = fs.readFileSync(path.join(moduleOutputDir, 'setup.py'), 'utf8'); + assert.ok(setup.includes('alibabacloud_gateway_pop>=0.1.4')); + }); }); \ No newline at end of file