Skip to content

Commit 6b17191

Browse files
committed
πŸ“¦ create release v0.0.0
1 parent 48ff9c7 commit 6b17191

15 files changed

+372
-1
lines changed

β€Ž.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
index.js
2+
release
3+
linux

β€ŽDockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#FROM golang:latest as build-env
2+
FROM scratch
3+
#COPY --from=build-env /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
4+
ARG TARGETOS
5+
ARG TARGETARCH
6+
ADD /${TARGETOS}/${TARGETARCH} /

β€ŽREADME.md

+100-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,100 @@
1-
# snippets
1+
# Snippets
2+
3+
**Snippets** generates VSCode code snippets from snippets yam file.
4+
5+
> Snippets is a small tool (without dependency)
6+
7+
## Usage
8+
9+
```bash
10+
snippets generate \
11+
--input samples/js.yml \
12+
--output ../.vscode/js.code-snippets
13+
```
14+
15+
### Input `yaml` file (example)
16+
17+
```yaml
18+
snippet hello world:
19+
prefix: "js-plugin-hello-world"
20+
name: "hello world"
21+
description: "This is the hello world plugin"
22+
scope: "javascript"
23+
body: |
24+
function helloWorld() {
25+
console.log("πŸ‘‹ hello world 🌍")
26+
}
27+
28+
snippet good morning:
29+
prefix: "js-plugin-good-morning"
30+
name: "good morning"
31+
description: "this is the good morning plugin"
32+
scope: "javascript"
33+
body: |
34+
function goodMorning(name) {
35+
console.log("πŸ‘‹", name, "${1:message}")
36+
console.log("${2:message}")
37+
}
38+
39+
```
40+
41+
### Output `json` file (example)
42+
43+
```json
44+
{
45+
"good morning": {
46+
"body": [
47+
"function goodMorning(name) {",
48+
" console.log(\"πŸ‘‹\", name, \"${1:message}\")",
49+
" console.log(\"${2:message}\")",
50+
"}",
51+
""
52+
],
53+
"description": "this is the good morning plugin",
54+
"prefix": "js-plugin-good-morning",
55+
"scope": "javascript"
56+
},
57+
"hello world": {
58+
"body": [
59+
"function helloWorld() {",
60+
" console.log(\"πŸ‘‹ hello world 🌍\")",
61+
"}",
62+
""
63+
],
64+
"description": "This is the hello world plugin",
65+
"prefix": "js-plugin-hello-world",
66+
"scope": "javascript"
67+
}
68+
}
69+
```
70+
71+
## Install
72+
73+
### Linux or MacOS
74+
75+
```bash
76+
SNIPPETS_VERSION="0.0.0"
77+
SNIPPETS_OS="linux" # or darwin
78+
SNIPPETS_ARCH="arm64" # or amd64
79+
wget https://github.com/bots-garden/snippets/releases/download/v${SNIPPETS_VERSION}/minism-v${SNIPPETS_VERSION}-${SNIPPETS_OS}-${SNIPPETS_ARCH}
80+
cp snippets-v${SNIPPETS_VERSION}-${SNIPPETS_OS}-${SNIPPETS_ARCH} snippets
81+
chmod +x snippets
82+
rm snippets-v${SNIPPETS_VERSION}-${SNIPPETS_OS}-${SNIPPETS_ARCH}
83+
sudo cp ./snippets /usr/bin
84+
rm snippets
85+
# check the version
86+
snippets version
87+
```
88+
89+
### Docker
90+
91+
```bash
92+
docker run \
93+
-v $(pwd)/samples:/samples \
94+
--rm botsgarden/snippets:0.0.0 \
95+
./snippets generate \
96+
--input samples/js.yml \
97+
--output samples/js.code-snippets
98+
```
99+
100+

β€Žbuild-release.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
set -o allexport; source release.env; set +o allexport
3+
4+
echo -n "${APPLICATION_NAME} ${TAG} ${NICK_NAME}" > ./version.txt
5+
6+
mkdir -p release
7+
8+
env CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o ${APPLICATION_NAME}-${TAG}-darwin-arm64
9+
env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o ${APPLICATION_NAME}-${TAG}-darwin-amd64
10+
env CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o ${APPLICATION_NAME}-${TAG}-linux-arm64
11+
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ${APPLICATION_NAME}-${TAG}-linux-amd64
12+
mv ${APPLICATION_NAME}-${TAG}-* ./release

β€Žcreate-release.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
set -o allexport; source release.env; set +o allexport
3+
echo "TAG: ${TAG}"
4+
echo "IMAGE_TAG: ${IMAGE_TAG}"
5+
echo "IMAGE_BASE_NAME: ${IMAGE_BASE_NAME}"
6+
7+
echo "πŸ“¦ Create release..."
8+
git add .
9+
git commit -m "πŸ“¦ create release ${TAG}"
10+
git tag ${TAG}
11+
git push origin main ${TAG}

β€Ždocker-build-image.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
set -o allexport; source release.env; set +o allexport
3+
4+
sudo chmod 666 /var/run/docker.sock
5+
ls -l /var/run/docker.sock
6+
sudo systemctl restart docker.service
7+
8+
echo -n "${APPLICATION_NAME} ${TAG} ${NICK_NAME}" > ./version.txt
9+
#mkdir -p release
10+
11+
env CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o linux/arm64/${APPLICATION_NAME}
12+
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o linux/amd64/${APPLICATION_NAME}
13+
14+
docker login -u ${DOCKER_USER} -p ${DOCKER_PWD}
15+
16+
docker buildx create --use
17+
docker buildx build -t ${DOCKER_USER}/${IMAGE_BASE_NAME}:${IMAGE_TAG} --platform=linux/arm64,linux/amd64 . --push

β€Žgo.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module snippet
2+
3+
go 1.21.1

β€Žhelp.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Help on `snippets`
2+
3+
This is a work in progress 🚧

β€Žmain.go

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"flag"
6+
"fmt"
7+
"os"
8+
"strings"
9+
10+
_ "embed"
11+
12+
"gopkg.in/yaml.v3"
13+
)
14+
15+
type YamlSnippet struct {
16+
Name string
17+
Description string
18+
Prefix string
19+
Scope string
20+
Body string
21+
}
22+
23+
//go:embed version.txt
24+
var version []byte
25+
26+
//go:embed help.md
27+
var help []byte
28+
29+
func readYamlFile(yamlFilePath string) (map[string]YamlSnippet, error) {
30+
31+
yamlFile, err := os.ReadFile(yamlFilePath)
32+
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
data := make(map[string]YamlSnippet)
38+
39+
err = yaml.Unmarshal(yamlFile, &data)
40+
41+
if err != nil {
42+
return nil, err
43+
}
44+
return data, nil
45+
}
46+
47+
func generateJsonSnippets(yamlSnippets map[string]YamlSnippet) ([]byte, error) {
48+
VSCodeSnippets := make(map[string]interface{})
49+
50+
for _, snippet := range yamlSnippets {
51+
body := strings.Split(snippet.Body, "\n")
52+
53+
VSCodeSnippets[snippet.Name] = map[string]interface{}{
54+
"prefix": snippet.Prefix,
55+
"description": snippet.Description,
56+
"scope": snippet.Scope,
57+
"body": body,
58+
}
59+
60+
}
61+
62+
bSnippets, err := json.MarshalIndent(VSCodeSnippets, "", " ")
63+
64+
if err != nil {
65+
return nil, err
66+
}
67+
return bSnippets, nil
68+
}
69+
70+
func writeJsonFile(jsonFilePath string, jsonData []byte) error {
71+
72+
f, err := os.Create(jsonFilePath)
73+
74+
if err != nil {
75+
return err
76+
}
77+
78+
defer f.Close()
79+
80+
_, err = f.WriteString(string(jsonData))
81+
82+
if err != nil {
83+
return err
84+
}
85+
return nil
86+
}
87+
88+
func parse(command string, args []string) error {
89+
switch command {
90+
91+
case "generate", "gen":
92+
93+
flagSet := flag.NewFlagSet("generate", flag.ExitOnError)
94+
95+
input := flagSet.String("input", "", "input yaml file path")
96+
output := flagSet.String("output", "", "output json file path")
97+
98+
flagSet.Parse(args[0:])
99+
100+
yamlData, err := readYamlFile(*input)
101+
if err != nil {
102+
fmt.Println("😑", err.Error())
103+
os.Exit(1)
104+
}
105+
jsonData, err := generateJsonSnippets(yamlData)
106+
if err != nil {
107+
fmt.Println("😑", err.Error())
108+
os.Exit(1)
109+
}
110+
111+
err = writeJsonFile(*output, jsonData)
112+
113+
if err != nil {
114+
fmt.Println("😑", err.Error())
115+
os.Exit(1)
116+
}
117+
fmt.Println("πŸ™‚", *output, "generated")
118+
//os.Exit(0)
119+
return nil
120+
121+
case "version":
122+
fmt.Println(string(version))
123+
//os.Exit(0)
124+
return nil
125+
126+
case "help":
127+
fmt.Println(string(help))
128+
//os.Exit(0)
129+
return nil
130+
131+
default:
132+
return fmt.Errorf("πŸ”΄ invalid command")
133+
}
134+
}
135+
136+
func main() {
137+
138+
flag.Parse()
139+
140+
if len(flag.Args()) < 1 {
141+
fmt.Println("πŸ”΄ invalid command")
142+
os.Exit(0)
143+
}
144+
145+
command := flag.Args()[0]
146+
147+
parse(command, flag.Args()[1:])
148+
149+
}

β€Žrelease.env

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
APPLICATION_NAME="snippets"
2+
VERSION="0.0.0"
3+
NICK_NAME="πŸ‰ [watermelon]"
4+
TAG="v${VERSION}" # for git
5+
IMAGE_TAG="${VERSION}"
6+
IMAGE_BASE_NAME="${APPLICATION_NAME}"
7+
DOCKER_USER="botsgarden"

β€Žrun.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
go run main.go generate --input samples/js.yml --output ../.vscode/js.code-snippets
3+
go run main.go generate --input samples/js.yml --output samples/js.code-snippets
4+

β€Žsamples/js.code-snippets

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"good morning": {
3+
"body": [
4+
"function goodMorning(name) {",
5+
" console.log(\"πŸ‘‹\", name, \"${1:message}\")",
6+
" console.log(\"${2:message}\")",
7+
"}",
8+
""
9+
],
10+
"description": "this is the good morning plugin",
11+
"prefix": "js-plugin-good-morning",
12+
"scope": "javascript"
13+
},
14+
"hello world": {
15+
"body": [
16+
"function helloWorld() {",
17+
" console.log(\"πŸ‘‹ hello world 🌍\")",
18+
"}",
19+
""
20+
],
21+
"description": "This is the hello world plugin",
22+
"prefix": "js-plugin-hello-world",
23+
"scope": "javascript"
24+
}
25+
}

β€Žsamples/js.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
snippet hello world:
2+
prefix: "js-plugin-hello-world"
3+
name: "hello world"
4+
description: "This is the hello world plugin"
5+
scope: "javascript"
6+
body: |
7+
function helloWorld() {
8+
console.log("πŸ‘‹ hello world 🌍")
9+
}
10+
11+
snippet good morning:
12+
prefix: "js-plugin-good-morning"
13+
name: "good morning"
14+
description: "this is the good morning plugin"
15+
scope: "javascript"
16+
body: |
17+
function goodMorning(name) {
18+
console.log("πŸ‘‹", name, "${1:message}")
19+
console.log("${2:message}")
20+
}

β€Žtest-container.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
set -o allexport; source release.env; set +o allexport
3+
4+
docker rmi -f ${DOCKER_USER}/${IMAGE_BASE_NAME}:${IMAGE_TAG}
5+
6+
docker run \
7+
-v $(pwd)/samples:/samples \
8+
--rm ${DOCKER_USER}/${IMAGE_BASE_NAME}:${IMAGE_TAG} \
9+
./snippets generate \
10+
--input samples/js.yml \
11+
--output samples/js.code-snippets

β€Žversion.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
snippets v0.0.0 πŸ‰ [watermelon]

0 commit comments

Comments
Β (0)