-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
372 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
index.js | ||
release | ||
linux |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#FROM golang:latest as build-env | ||
FROM scratch | ||
#COPY --from=build-env /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ | ||
ARG TARGETOS | ||
ARG TARGETARCH | ||
ADD /${TARGETOS}/${TARGETARCH} / |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,100 @@ | ||
# snippets | ||
# Snippets | ||
|
||
**Snippets** generates VSCode code snippets from snippets yam file. | ||
|
||
> Snippets is a small tool (without dependency) | ||
## Usage | ||
|
||
```bash | ||
snippets generate \ | ||
--input samples/js.yml \ | ||
--output ../.vscode/js.code-snippets | ||
``` | ||
|
||
### Input `yaml` file (example) | ||
|
||
```yaml | ||
snippet hello world: | ||
prefix: "js-plugin-hello-world" | ||
name: "hello world" | ||
description: "This is the hello world plugin" | ||
scope: "javascript" | ||
body: | | ||
function helloWorld() { | ||
console.log("π hello world π") | ||
} | ||
snippet good morning: | ||
prefix: "js-plugin-good-morning" | ||
name: "good morning" | ||
description: "this is the good morning plugin" | ||
scope: "javascript" | ||
body: | | ||
function goodMorning(name) { | ||
console.log("π", name, "${1:message}") | ||
console.log("${2:message}") | ||
} | ||
``` | ||
### Output `json` file (example) | ||
|
||
```json | ||
{ | ||
"good morning": { | ||
"body": [ | ||
"function goodMorning(name) {", | ||
" console.log(\"π\", name, \"${1:message}\")", | ||
" console.log(\"${2:message}\")", | ||
"}", | ||
"" | ||
], | ||
"description": "this is the good morning plugin", | ||
"prefix": "js-plugin-good-morning", | ||
"scope": "javascript" | ||
}, | ||
"hello world": { | ||
"body": [ | ||
"function helloWorld() {", | ||
" console.log(\"π hello world π\")", | ||
"}", | ||
"" | ||
], | ||
"description": "This is the hello world plugin", | ||
"prefix": "js-plugin-hello-world", | ||
"scope": "javascript" | ||
} | ||
} | ||
``` | ||
|
||
## Install | ||
|
||
### Linux or MacOS | ||
|
||
```bash | ||
SNIPPETS_VERSION="0.0.0" | ||
SNIPPETS_OS="linux" # or darwin | ||
SNIPPETS_ARCH="arm64" # or amd64 | ||
wget https://github.com/bots-garden/snippets/releases/download/v${SNIPPETS_VERSION}/minism-v${SNIPPETS_VERSION}-${SNIPPETS_OS}-${SNIPPETS_ARCH} | ||
cp snippets-v${SNIPPETS_VERSION}-${SNIPPETS_OS}-${SNIPPETS_ARCH} snippets | ||
chmod +x snippets | ||
rm snippets-v${SNIPPETS_VERSION}-${SNIPPETS_OS}-${SNIPPETS_ARCH} | ||
sudo cp ./snippets /usr/bin | ||
rm snippets | ||
# check the version | ||
snippets version | ||
``` | ||
|
||
### Docker | ||
|
||
```bash | ||
docker run \ | ||
-v $(pwd)/samples:/samples \ | ||
--rm botsgarden/snippets:0.0.0 \ | ||
./snippets generate \ | ||
--input samples/js.yml \ | ||
--output samples/js.code-snippets | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/bash | ||
set -o allexport; source release.env; set +o allexport | ||
|
||
echo -n "${APPLICATION_NAME} ${TAG} ${NICK_NAME}" > ./version.txt | ||
|
||
mkdir -p release | ||
|
||
env CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o ${APPLICATION_NAME}-${TAG}-darwin-arm64 | ||
env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o ${APPLICATION_NAME}-${TAG}-darwin-amd64 | ||
env CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o ${APPLICATION_NAME}-${TAG}-linux-arm64 | ||
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ${APPLICATION_NAME}-${TAG}-linux-amd64 | ||
mv ${APPLICATION_NAME}-${TAG}-* ./release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
set -o allexport; source release.env; set +o allexport | ||
echo "TAG: ${TAG}" | ||
echo "IMAGE_TAG: ${IMAGE_TAG}" | ||
echo "IMAGE_BASE_NAME: ${IMAGE_BASE_NAME}" | ||
|
||
echo "π¦ Create release..." | ||
git add . | ||
git commit -m "π¦ create release ${TAG}" | ||
git tag ${TAG} | ||
git push origin main ${TAG} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/bash | ||
set -o allexport; source release.env; set +o allexport | ||
|
||
sudo chmod 666 /var/run/docker.sock | ||
ls -l /var/run/docker.sock | ||
sudo systemctl restart docker.service | ||
|
||
echo -n "${APPLICATION_NAME} ${TAG} ${NICK_NAME}" > ./version.txt | ||
#mkdir -p release | ||
|
||
env CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o linux/arm64/${APPLICATION_NAME} | ||
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o linux/amd64/${APPLICATION_NAME} | ||
|
||
docker login -u ${DOCKER_USER} -p ${DOCKER_PWD} | ||
|
||
docker buildx create --use | ||
docker buildx build -t ${DOCKER_USER}/${IMAGE_BASE_NAME}:${IMAGE_TAG} --platform=linux/arm64,linux/amd64 . --push |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module snippet | ||
|
||
go 1.21.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Help on `snippets` | ||
|
||
This is a work in progress π§ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
_ "embed" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type YamlSnippet struct { | ||
Name string | ||
Description string | ||
Prefix string | ||
Scope string | ||
Body string | ||
} | ||
|
||
//go:embed version.txt | ||
var version []byte | ||
|
||
//go:embed help.md | ||
var help []byte | ||
|
||
func readYamlFile(yamlFilePath string) (map[string]YamlSnippet, error) { | ||
|
||
yamlFile, err := os.ReadFile(yamlFilePath) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
data := make(map[string]YamlSnippet) | ||
|
||
err = yaml.Unmarshal(yamlFile, &data) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
return data, nil | ||
} | ||
|
||
func generateJsonSnippets(yamlSnippets map[string]YamlSnippet) ([]byte, error) { | ||
VSCodeSnippets := make(map[string]interface{}) | ||
|
||
for _, snippet := range yamlSnippets { | ||
body := strings.Split(snippet.Body, "\n") | ||
|
||
VSCodeSnippets[snippet.Name] = map[string]interface{}{ | ||
"prefix": snippet.Prefix, | ||
"description": snippet.Description, | ||
"scope": snippet.Scope, | ||
"body": body, | ||
} | ||
|
||
} | ||
|
||
bSnippets, err := json.MarshalIndent(VSCodeSnippets, "", " ") | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
return bSnippets, nil | ||
} | ||
|
||
func writeJsonFile(jsonFilePath string, jsonData []byte) error { | ||
|
||
f, err := os.Create(jsonFilePath) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
defer f.Close() | ||
|
||
_, err = f.WriteString(string(jsonData)) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func parse(command string, args []string) error { | ||
switch command { | ||
|
||
case "generate", "gen": | ||
|
||
flagSet := flag.NewFlagSet("generate", flag.ExitOnError) | ||
|
||
input := flagSet.String("input", "", "input yaml file path") | ||
output := flagSet.String("output", "", "output json file path") | ||
|
||
flagSet.Parse(args[0:]) | ||
|
||
yamlData, err := readYamlFile(*input) | ||
if err != nil { | ||
fmt.Println("π‘", err.Error()) | ||
os.Exit(1) | ||
} | ||
jsonData, err := generateJsonSnippets(yamlData) | ||
if err != nil { | ||
fmt.Println("π‘", err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
err = writeJsonFile(*output, jsonData) | ||
|
||
if err != nil { | ||
fmt.Println("π‘", err.Error()) | ||
os.Exit(1) | ||
} | ||
fmt.Println("π", *output, "generated") | ||
//os.Exit(0) | ||
return nil | ||
|
||
case "version": | ||
fmt.Println(string(version)) | ||
//os.Exit(0) | ||
return nil | ||
|
||
case "help": | ||
fmt.Println(string(help)) | ||
//os.Exit(0) | ||
return nil | ||
|
||
default: | ||
return fmt.Errorf("π΄ invalid command") | ||
} | ||
} | ||
|
||
func main() { | ||
|
||
flag.Parse() | ||
|
||
if len(flag.Args()) < 1 { | ||
fmt.Println("π΄ invalid command") | ||
os.Exit(0) | ||
} | ||
|
||
command := flag.Args()[0] | ||
|
||
parse(command, flag.Args()[1:]) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
APPLICATION_NAME="snippets" | ||
VERSION="0.0.0" | ||
NICK_NAME="π [watermelon]" | ||
TAG="v${VERSION}" # for git | ||
IMAGE_TAG="${VERSION}" | ||
IMAGE_BASE_NAME="${APPLICATION_NAME}" | ||
DOCKER_USER="botsgarden" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
go run main.go generate --input samples/js.yml --output ../.vscode/js.code-snippets | ||
go run main.go generate --input samples/js.yml --output samples/js.code-snippets | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"good morning": { | ||
"body": [ | ||
"function goodMorning(name) {", | ||
" console.log(\"π\", name, \"${1:message}\")", | ||
" console.log(\"${2:message}\")", | ||
"}", | ||
"" | ||
], | ||
"description": "this is the good morning plugin", | ||
"prefix": "js-plugin-good-morning", | ||
"scope": "javascript" | ||
}, | ||
"hello world": { | ||
"body": [ | ||
"function helloWorld() {", | ||
" console.log(\"π hello world π\")", | ||
"}", | ||
"" | ||
], | ||
"description": "This is the hello world plugin", | ||
"prefix": "js-plugin-hello-world", | ||
"scope": "javascript" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
snippet hello world: | ||
prefix: "js-plugin-hello-world" | ||
name: "hello world" | ||
description: "This is the hello world plugin" | ||
scope: "javascript" | ||
body: | | ||
function helloWorld() { | ||
console.log("π hello world π") | ||
} | ||
snippet good morning: | ||
prefix: "js-plugin-good-morning" | ||
name: "good morning" | ||
description: "this is the good morning plugin" | ||
scope: "javascript" | ||
body: | | ||
function goodMorning(name) { | ||
console.log("π", name, "${1:message}") | ||
console.log("${2:message}") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
set -o allexport; source release.env; set +o allexport | ||
|
||
docker rmi -f ${DOCKER_USER}/${IMAGE_BASE_NAME}:${IMAGE_TAG} | ||
|
||
docker run \ | ||
-v $(pwd)/samples:/samples \ | ||
--rm ${DOCKER_USER}/${IMAGE_BASE_NAME}:${IMAGE_TAG} \ | ||
./snippets generate \ | ||
--input samples/js.yml \ | ||
--output samples/js.code-snippets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
snippets v0.0.0 π [watermelon] |