diff options
| author | to-s <tobias.weber@enisyst.de> | 2020-10-19 12:38:51 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-19 18:38:51 +0200 |
| commit | 771d246a96cd476fa9fddf600c532a28b2efd93a (patch) | |
| tree | bd03b4bdec9a6a5bb85baca7ca7d988cde64fe95 /src | |
| parent | d629202dd9419569c109475ad9f10405d8ab0d22 (diff) | |
| download | doxdocgen-771d246a96cd476fa9fddf600c532a28b2efd93a.tar.gz | |
Substitute author and email by git config (#186)
* Substitute author and email by git config
* Upgraded version of typescript
Due to https://github.com/cschlosser/doxdocgen/pull/186#discussion_r505813068
* Moved to CppDocGen.generateAuthorTag()
Due to https://github.com/cschlosser/doxdocgen/pull/186#discussion_r505819542
* Update README.md
Due to https://github.com/cschlosser/doxdocgen/pull/186#discussion_r505822908
* Update TestSetup.ts
* Added tests
* Changed return type to string
Due to https://ci.appveyor.com/project/cschlosser/doxdocgen/builds/35818180#L566
Co-authored-by: Christoph Schlosser <2466365+cschlosser@users.noreply.github.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/CodeParserController.ts | 5 | ||||
| -rw-r--r-- | src/Common/IDocGen.ts | 3 | ||||
| -rw-r--r-- | src/Config.ts | 4 | ||||
| -rw-r--r-- | src/GitConfig.ts | 30 | ||||
| -rw-r--r-- | src/Lang/Cpp/CppDocGen.ts | 21 | ||||
| -rw-r--r-- | src/test/CppTests/Config.test.ts | 28 | ||||
| -rw-r--r-- | src/test/CppTests/TestSetup.ts | 5 |
7 files changed, 91 insertions, 5 deletions
diff --git a/src/CodeParserController.ts b/src/CodeParserController.ts index 4490f69..f87e221 100644 --- a/src/CodeParserController.ts +++ b/src/CodeParserController.ts @@ -10,6 +10,7 @@ import { } from "vscode"; import CodeParser from "./Common/ICodeParser"; import { Config } from "./Config"; +import GitConfig from "./GitConfig"; import CppParser from "./Lang/Cpp/CppParser"; /** * @@ -21,6 +22,7 @@ import CppParser from "./Lang/Cpp/CppParser"; export default class CodeParserController { private disposable: Disposable; private cfg: Config; + private gitConfig: GitConfig; /** * Creates an instance of CodeParserController @@ -29,6 +31,7 @@ export default class CodeParserController { */ public constructor() { const subscriptions: Disposable[] = []; + this.gitConfig = new GitConfig(); // Hand off the event to the parser if a valid parser is found workspace.onDidChangeTextDocument((event) => { @@ -132,6 +135,6 @@ export default class CodeParserController { const nextLineText: string = window.activeTextEditor.document.lineAt(startReplace.line + 1).text; const endReplace = new Position(currentPos.line + 1, nextLineText.length); - parser.Parse(activeEditor).GenerateDoc(new Range(startReplace, endReplace)); + parser.Parse(activeEditor).GenerateDoc(new Range(startReplace, endReplace), this.gitConfig); } } diff --git a/src/Common/IDocGen.ts b/src/Common/IDocGen.ts index 1b5b1e1..5eaf8c1 100644 --- a/src/Common/IDocGen.ts +++ b/src/Common/IDocGen.ts @@ -1,9 +1,10 @@ import { Range } from "vscode"; +import GitConfig from "../GitConfig"; export interface IDocGen { /** * @brief Generate documentation string and write it to the active editor * @param {Range} rangeToReplace Range to replace with the generated comment. */ - GenerateDoc(rangeToReplace: Range); + GenerateDoc(rangeToReplace: Range, gitConfig: GitConfig); } diff --git a/src/Config.ts b/src/Config.ts index 51aebc3..e103b47 100644 --- a/src/Config.ts +++ b/src/Config.ts @@ -59,6 +59,8 @@ class Generic { public order: string[] = ["brief", "empty", "tparam", "param", "return"]; public customTags: string[] = []; public filteredKeywords: string[] = []; + public useGitUserName: boolean = false; + public useGitUserEmail: boolean = false; } export class Config { @@ -99,6 +101,8 @@ export class Config { values.Generic.order = Generic.getConfiguration().get<string[]>("order", values.Generic.order); values.Generic.customTags = Generic.getConfiguration().get<string[]>("customTags", values.Generic.customTags); values.Generic.filteredKeywords = Generic.getConfiguration().get<string[]>("filteredKeywords", values.Generic.filteredKeywords); + values.Generic.useGitUserName = Generic.getConfiguration().get<boolean>("useGitUserName", values.Generic.useGitUserName); + values.Generic.useGitUserEmail = Generic.getConfiguration().get<boolean>("useGitUserEmail", values.Generic.useGitUserEmail); return values; } diff --git a/src/GitConfig.ts b/src/GitConfig.ts new file mode 100644 index 0000000..2a425f6 --- /dev/null +++ b/src/GitConfig.ts @@ -0,0 +1,30 @@ +import simpleGit, {ConfigValues, SimpleGit} from "simple-git"; + +export default class GitConfig { + private gitConfig: ConfigValues; + + public constructor() { + const git: SimpleGit = simpleGit(); + git.listConfig().then((result) => { + this.gitConfig = result.all; + }); + } + + /** git config --get user.name */ + get UserName(): string { + try { + return this.gitConfig["user.name"].toString(); + } catch (error) { + return ""; + } + } + + /** git config --get user.email */ + get UserEmail(): string { + try { + return this.gitConfig["user.email"].toString(); + } catch (error) { + return ""; + } + } +} diff --git a/src/Lang/Cpp/CppDocGen.ts b/src/Lang/Cpp/CppDocGen.ts index 2795ec6..bc9f09a 100644 --- a/src/Lang/Cpp/CppDocGen.ts +++ b/src/Lang/Cpp/CppDocGen.ts @@ -3,6 +3,7 @@ import * as moment from "moment"; import { Position, Range, Selection, TextEditor } from "vscode"; import { IDocGen } from "../../Common/IDocGen"; import { Config } from "../../Config"; +import GitConfig from "../../GitConfig"; import { CppArgument } from "./CppArgument"; import * as CppParser from "./CppParser"; import { CppToken, CppTokenType } from "./CppToken"; @@ -47,6 +48,8 @@ export class CppDocGen implements IDocGen { protected vscodeAutoGeneratedComment: boolean; + private gitConfig; + /** * @param {TextEditor} actEdit Active editor window * @param {Position} cursorPosition Where the cursor of the user currently is @@ -84,8 +87,9 @@ export class CppDocGen implements IDocGen { /** * @inheritdoc */ - public GenerateDoc(rangeToReplace: Range) { + public GenerateDoc(rangeToReplace: Range, gitConfig: GitConfig) { let comment: string = ""; + this.gitConfig = gitConfig; if (this.commentType === CommentType.file) { comment = this.generateFileDescription(); } else if (this.commentType === CommentType.method) { @@ -291,13 +295,26 @@ export class CppDocGen implements IDocGen { } protected generateAuthorTag(lines: string[]) { + let authorName: string = this.cfg.Generic.authorName; + let authorEmail: string = this.cfg.Generic.authorEmail; + + // Check if set to use the git username + if (this.cfg.Generic.useGitUserName === true) { + authorName = this.gitConfig.UserName; + } + + // Check if set to use the git email + if (this.cfg.Generic.useGitUserEmail === true) { + authorEmail = this.gitConfig.UserEmail; + } + if (this.cfg.Generic.authorTag.trim().length !== 0) { // Allow substitution of {author} and {email} only lines.push( ...this.getMultiTemplatedString( [this.cfg.authorTemplateReplace, this.cfg.emailTemplateReplace], this.cfg.Generic.authorTag, - [this.cfg.Generic.authorName, this.cfg.Generic.authorEmail], + [authorName, authorEmail], ).split("\n"), ); } diff --git a/src/test/CppTests/Config.test.ts b/src/test/CppTests/Config.test.ts index aae09d4..fcf2550 100644 --- a/src/test/CppTests/Config.test.ts +++ b/src/test/CppTests/Config.test.ts @@ -247,4 +247,32 @@ suite("C++ - Configuration Tests", () => { assert.strictEqual("/**\n * @author MY_VARIABLE\n */", result); }); + test("Use git user.name as author", () => { + testSetup.cfg = new Config(); + testSetup.cfg.Generic.useGitUserName = true; + const result = testSetup.SetLine("").GetResult(); + assert.strictEqual("/**\n * @brief \n * \n * @file MockDocument.h\n * @author " + + testSetup.gitConfig.UserName + + " (you@domain.com)\n * @date " + moment().format("YYYY-MM-DD") + "\n */", result); + }); + + test("Use git user.email as email", () => { + testSetup.cfg = new Config(); + testSetup.cfg.Generic.useGitUserEmail = true; + const result = testSetup.SetLine("").GetResult(); + assert.strictEqual("/**\n * @brief \n * \n * @file MockDocument.h\n * @author your name (" + + testSetup.gitConfig.UserEmail + + ")\n * @date " + moment().format("YYYY-MM-DD") + "\n */", result); + }); + + test("Substitute author and email by git config", () => { + testSetup.cfg = new Config(); + testSetup.cfg.Generic.useGitUserName = true; + testSetup.cfg.Generic.useGitUserEmail = true; + const result = testSetup.SetLine("").GetResult(); + assert.strictEqual("/**\n * @brief \n * \n * @file MockDocument.h\n * @author " + + testSetup.gitConfig.UserName + " (" + testSetup.gitConfig.UserEmail+ + ")\n * @date " + moment().format("YYYY-MM-DD") + "\n */", result); + }); + }); diff --git a/src/test/CppTests/TestSetup.ts b/src/test/CppTests/TestSetup.ts index ad733dd..e38ae91 100644 --- a/src/test/CppTests/TestSetup.ts +++ b/src/test/CppTests/TestSetup.ts @@ -4,6 +4,7 @@ import CodeParser from "../../Common/ICodeParser"; import { IDocGen } from "../../Common/IDocGen"; import { Config } from "../../Config"; import * as myExtension from "../../extension"; +import GitConfig from "../../GitConfig"; import CppParser from "../../Lang/Cpp/CppParser"; import MockDocument from "../tools/MockDocument"; import MockEditor from "../tools/MockEditor"; @@ -14,9 +15,11 @@ import MockSelection from "../tools/MockSelection"; export default class TestSetup { public cfg: Config; public firstLine: number; + public gitConfig: GitConfig; private editor: MockEditor; constructor(method: string) { + this.gitConfig = new GitConfig(); this.cfg = new Config(); this.firstLine = 0; this.SetLine(method); @@ -58,7 +61,7 @@ export default class TestSetup { const gen: IDocGen = parser.Parse(this.editor); // tslint:disable-next-line:max-line-length - gen.GenerateDoc(new vscode.Range(new vscode.Position(this.firstLine, 0), new vscode.Position(this.firstLine, 0))); + gen.GenerateDoc(new vscode.Range(new vscode.Position(this.firstLine, 0), new vscode.Position(this.firstLine, 0)), this.gitConfig); return this.editor.editBuilder.text; } |