Skip to content
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

Add a vscode extension support #30

Merged
merged 9 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ bin/
.idea/
.vscode/
coverage.out
plugins/vscode/yaml-readme/node_modules/
*.vsix
*/**/.DS_Store
.DS_Store
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
build:
mkdir -p bin
go build -o bin/yaml-readme .
copy: build
cp bin/yaml-readme /usr/local/bin

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![codecov](https://codecov.io/gh/LinuxSuRen/yaml-readme/branch/master/graph/badge.svg?token=mnFyeD2IQ7)](https://codecov.io/gh/LinuxSuRen/yaml-readme)
[![vscode](https://vsmarketplacebadge.apphb.com/version/linuxsuren.yaml-readme.svg)](https://marketplace.visualstudio.com/items?itemName=linuxsuren.yaml-readme)

A helper to generate the READE file automatically.

Expand Down
2 changes: 2 additions & 0 deletions function/data/README-with-metadata.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!yaml-readme -p data/financing/*.yaml --output financing.md
a fake template
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -116,6 +117,8 @@ func loadTemplate(templateFile string, includeHeader bool) (readmeTpl string, er
filepath.Base(templateFile), filepath.Base(templateFile))
}
readmeTpl = readmeTpl + string(data)
s, err := regexp.Compile("#!yaml-readme .*\n")
readmeTpl = s.ReplaceAllString(readmeTpl, "")
return
}

Expand Down
13 changes: 13 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,19 @@ func Test_loadTemplate(t *testing.T) {
assert.Nil(t, err)
return true
},
}, {
name: "has metadata",
args: args{
templateFile: "function/data/README-with-metadata.tpl",
includeHeader: false,
},
wantReadmeTpl: func() string {
return `a fake template`
},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
assert.Nil(t, err)
return true
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
25 changes: 25 additions & 0 deletions plugins/vscode/yaml-readme/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"env": {
"browser": false,
"commonjs": true,
"es6": true,
"node": true,
"mocha": true
},
"parserOptions": {
"ecmaVersion": 2018,
"ecmaFeatures": {
"jsx": true
},
"sourceType": "module"
},
"rules": {
"no-const-assign": "warn",
"no-this-before-super": "warn",
"no-undef": "warn",
"no-unreachable": "warn",
"no-unused-vars": "warn",
"constructor-super": "warn",
"valid-typeof": "warn"
}
}
10 changes: 10 additions & 0 deletions plugins/vscode/yaml-readme/.vscodeignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.vscode/**
.vscode-test/**
test/**
.gitignore
.yarnrc
vsc-extension-quickstart.md
**/jsconfig.json
**/*.map
**/.eslintrc.json
node_modules
9 changes: 9 additions & 0 deletions plugins/vscode/yaml-readme/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Change Log

All notable changes to the "yaml-readme" extension will be documented in this file.

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [Unreleased]

- Initial release
21 changes: 21 additions & 0 deletions plugins/vscode/yaml-readme/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Rick

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
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
SOFTWARE.
21 changes: 21 additions & 0 deletions plugins/vscode/yaml-readme/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
This plugin could help you to maintain a complex README file.

## Prerequisite

Before get started, please install `yaml-readme` to you system. You can use [hd](https://github.com/LinuxSuRen/http-downloader/) or others.

```shell
hd i yaml-readme
```

## Get started

Put the following code in the first line of the [Go template](https://pkg.go.dev/text/template) file:

```
#!yaml-readme -p 'data/financing/*.yaml' --output financing.md
```

See also [this example file](https://github.com/LinuxSuRen/open-source-best-practice/blob/master/data/financing/financing.tpl).

then press `Ctrl+Shift+P` and type `yaml-readme` command to generate the Markdown file specific with `--output`.
73 changes: 73 additions & 0 deletions plugins/vscode/yaml-readme/extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const cp = require('child_process');
const fs = require('fs');

function getFirstLine(filePath) {
const data = fs.readFileSync(filePath);
return data.toString().split('\n')[0];
}

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed

/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {

// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "yaml-readme" is now active!');

// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand('yaml-readme.helloWorld', function () {
if(vscode.workspace.workspaceFolders !== undefined) {
let wf = vscode.workspace.workspaceFolders[0].uri.path ;

let filename = vscode.window.activeTextEditor.document.fileName
let metadata = getFirstLine(filename)
vscode.window.showInformationMessage(metadata + "===" + metadata.startsWith("#!yaml-readme"));
if (metadata.startsWith("#!yaml-readme")) {
metadata = metadata.replace("#!yaml-readme ", "")

let pattern = ""
let output = ""
const items = metadata.split(" ")
for (var i = 0; i < items.length; i++) {
const item = items[i]
if (item == "-p") {
pattern = wf + "/" + items[++i]
} else if (item == "--output") {
output = wf + "/" + items[++i]
}
}

vscode.window.showInformationMessage(`yaml-readme -p "${pattern}" -t "${filename}" > ${output}`)
cp.exec(`yaml-readme -p "${pattern}" -t "${filename}" > ${output}`, (err) => {
if (err) {
console.log('error: ' + err);
}
vscode.commands.executeCommand("markdown.showPreviewToSide", vscode.Uri.file(`${output}`));
});
}
} else {
let message = "YOUR-EXTENSION: Working folder not found, open a folder an try again" ;

vscode.window.showErrorMessage(message);
}
});

context.subscriptions.push(disposable);
}

// this method is called when your extension is deactivated
function deactivate() {}

module.exports = {
activate,
deactivate
}
13 changes: 13 additions & 0 deletions plugins/vscode/yaml-readme/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2020",
"checkJs": true, /* Typecheck .js files. */
"lib": [
"ES2020"
]
},
"exclude": [
"node_modules"
]
}
Loading