api(dotnet): generate api in a specific folder (#6708)

This commit is contained in:
Pavel Feldman 2021-05-22 11:47:13 -07:00 committed by GitHub
parent 7d0b4c263f
commit 3bdb1c356f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 24 deletions

View file

@ -44,13 +44,14 @@ const customTypeNames = new Map([
['File', 'FilePayload'], ['File', 'FilePayload'],
]); ]);
const typesDir = process.argv[2] || path.join(__dirname, 'generate_types', 'csharp'); const outputDir = process.argv[2] || path.join(__dirname, 'generate_types', 'csharp');
const modelsDir = path.join(typesDir, 'Models'); const apiDir = path.join(outputDir, 'API', 'Generated');
const enumsDir = path.join(typesDir, 'Enums'); const optionsDir = path.join(outputDir, 'API', 'Generated', 'Options');
const optionsDir = path.join(typesDir, 'Options'); const enumsDir = path.join(outputDir, 'API', 'Generated', 'Enums');
const baseDir = path.join(typesDir, 'Base'); const typesDir = path.join(outputDir, 'API', 'Generated', 'Types');
const adaptersDir = path.join(outputDir, 'Generated', 'Adapters');
for (const dir of [typesDir, modelsDir, enumsDir, optionsDir, baseDir]) for (const dir of [apiDir, optionsDir, enumsDir, typesDir, adaptersDir])
fs.mkdirSync(dir, { recursive: true }); fs.mkdirSync(dir, { recursive: true });
const documentation = parseApi(path.join(PROJECT_DIR, 'docs', 'src', 'api')); const documentation = parseApi(path.join(PROJECT_DIR, 'docs', 'src', 'api'));
@ -70,8 +71,7 @@ documentation.setLinkRenderer(item => {
}); });
// get the template for a class // get the template for a class
const template = fs.readFileSync(path.join(__dirname, 'templates', 'interface.cs'), 'utf-8') const template = fs.readFileSync(path.join(__dirname, 'templates', 'interface.cs'), 'utf-8');
.replace('[PW_TOOL_VERSION]', `${__filename.substring(path.join(__dirname, '..', '..').length).split(path.sep).join(path.posix.sep)}`);
// we have some "predefined" types, like the mixed state enum, that we can map in advance // we have some "predefined" types, like the mixed state enum, that we can map in advance
enumTypes.set("MixedState", ["On", "Off", "Mixed"]); enumTypes.set("MixedState", ["On", "Off", "Mixed"]);
@ -128,7 +128,7 @@ function writeFile(kind, name, spec, body, folder, extendsName = null) {
out.push('}'); out.push('}');
let content = template.replace('[CONTENT]', out.join(EOL)); let content = template.replace('[CONTENT]', out.join(EOL));
fs.writeFileSync(path.join(folder, name + '.generated.cs'), content); fs.writeFileSync(path.join(folder, name + '.cs'), content);
} }
/** /**
@ -148,7 +148,7 @@ function renderClass(clazz) {
name, name,
clazz.spec, clazz.spec,
body, body,
typesDir, apiDir,
clazz.extends ? `I${toTitleCase(clazz.extends)}` : null); clazz.extends ? `I${toTitleCase(clazz.extends)}` : null);
} }
@ -180,7 +180,7 @@ function renderBaseClass(clazz) {
name, name,
[], [],
body, body,
baseDir, adaptersDir,
null); null);
} }
@ -208,7 +208,7 @@ function renderModelType(name, type) {
console.log(type); console.log(type);
throw new Error(`Not sure what to do in this case.`); throw new Error(`Not sure what to do in this case.`);
} }
writeFile('public partial class', name, null, body, modelsDir); writeFile('public partial class', name, null, body, typesDir);
} }
/** /**
@ -261,7 +261,7 @@ for (let [name, literals] of enumTypes)
if (process.argv[3] !== "--skip-format") { if (process.argv[3] !== "--skip-format") {
// run the formatting tool for .net, to ensure the files are prepped // run the formatting tool for .net, to ensure the files are prepped
execSync(`dotnet format -f "${typesDir}" --include-generated --fix-whitespace`); execSync(`dotnet format -f "${outputDir}" --include-generated --fix-whitespace`);
} }
/** /**

View file

@ -20,18 +20,8 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*
*
* ------------------------------------------------------------------------------
* <auto-generated>
* This code was generated by a tool at:
* [PW_TOOL_VERSION]
*
* Changes to this file may cause incorrect behavior and will be lost if
* the code is regenerated.
* </auto-generated>
* ------------------------------------------------------------------------------
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;