Skip to content

Commit

Permalink
πŸ“¦ create release v0.0.0
Browse files Browse the repository at this point in the history
k33g committed Oct 7, 2023
1 parent 48ff9c7 commit 6b17191
Showing 15 changed files with 372 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
index.js
release
linux
6 changes: 6 additions & 0 deletions Dockerfile
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} /
101 changes: 100 additions & 1 deletion README.md
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
```


12 changes: 12 additions & 0 deletions build-release.sh
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
11 changes: 11 additions & 0 deletions create-release.sh
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}
17 changes: 17 additions & 0 deletions docker-build-image.sh
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
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module snippet

go 1.21.1
3 changes: 3 additions & 0 deletions help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Help on `snippets`

This is a work in progress 🚧
149 changes: 149 additions & 0 deletions main.go
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:])

}
7 changes: 7 additions & 0 deletions release.env
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"
4 changes: 4 additions & 0 deletions run.sh
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

25 changes: 25 additions & 0 deletions samples/js.code-snippets
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"
}
}
20 changes: 20 additions & 0 deletions samples/js.yml
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}")
}
11 changes: 11 additions & 0 deletions test-container.sh
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
1 change: 1 addition & 0 deletions version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
snippets v0.0.0 πŸ‰ [watermelon]

0 comments on commit 6b17191

Please sign in to comment.