Skip to content

Commit

Permalink
load namespace, deduce filepath from config (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinming-Hu authored Dec 2, 2021
1 parent 098e493 commit 60f467b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/cppFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CodeGenerationContext } from "./cppGenerationContext";

export class CppFile {
filename: string;
relatedHeader?: string;
stuff: Array<
| CppTypes.ModelStruct
| CppTypes.ModelBitwiseEnum
Expand Down Expand Up @@ -59,7 +60,11 @@ export class CppFile {
else otherHeaders.push(h);
}
let includeHeaders = "";
// TODO: corresponding .hpp file should be the first group if this is a .cpp file
// We follow the rule in Google C++ Style Guide for the order of headers https://google.github.io/styleguide/cppguide.html#Names_and_Order_of_Includes
if (context.isSource() && this.relatedHeader) {
includeHeaders +=
new CodeBlocks.IncludeHeader(this.relatedHeader).print(context) + "\n";
}
includeHeaders +=
cSystemHeaders
.map(h => new CodeBlocks.IncludeHeader(h).print(context))
Expand Down
26 changes: 14 additions & 12 deletions src/cppGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT

import * as path from "path";
import {
ArraySchema,
ChoiceSchema,
Expand Down Expand Up @@ -34,8 +35,7 @@ export async function generateCppLibrary(
codeModel: CodeModel,
host: Host
): Promise<void> {
// TODO: load this from config
const defaultNamespace = "::Azure::Storage::Blobs";
const defaultNamespace = "::" + (await host.GetValue("namespace"));

let models = new Array();
let modelsNameSet = new Set();
Expand Down Expand Up @@ -594,19 +594,21 @@ export async function generateCppLibrary(
clients.push(clientDefinition);
}

// TODO: load generate root path from config
/* Basically we need two paths, one is the root directory for output files, the other is the relative path of the header file to the include root.
* For example, we will generate two files
* - generated/blob_rest_client.hpp
* - generated/blob_rest_client.cpp
* In .cpp file, we need to include the header with `#include "azure/storage/blobs/protocol/blob_rest_client.hpp"`
* So the generate root directory is `./generated/`, the relative path to include root is `azure/storage/blobs/protocol/`
*/
let hppFile = new CppFile("1.hpp");
let headerPathSegments = defaultNamespace
.split("::")
.map(i => i.toLowerCase());
headerPathSegments.shift();
headerPathSegments.unshift("inc");
headerPathSegments.push("rest_client.hpp");
const headerPath = path.join(...headerPathSegments);
const sourcePath = path.join("src", "rest_client.cpp");

let hppFile = new CppFile(headerPath);
hppFile.stuff = models.concat(clients);
host.WriteFile(hppFile.filename, hppFile.print());

let cppFile = new CppFile("1.cpp");
let cppFile = new CppFile(sourcePath);
cppFile.relatedHeader = path.posix.join(...headerPathSegments.slice(1));
cppFile.stuff = models.concat(clients);
host.WriteFile(cppFile.filename, cppFile.print());
}

0 comments on commit 60f467b

Please sign in to comment.