diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2075da --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +index.js +release +linux diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..30a8d91 --- /dev/null +++ b/Dockerfile @@ -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} / diff --git a/README.md b/README.md index 82ee780..05e511a 100644 --- a/README.md +++ b/README.md @@ -1 +1,100 @@ -# snippets \ No newline at end of file +# 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 +``` + + diff --git a/build-release.sh b/build-release.sh new file mode 100755 index 0000000..f6e9a32 --- /dev/null +++ b/build-release.sh @@ -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 diff --git a/create-release.sh b/create-release.sh new file mode 100755 index 0000000..ceb0370 --- /dev/null +++ b/create-release.sh @@ -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} diff --git a/docker-build-image.sh b/docker-build-image.sh new file mode 100755 index 0000000..9ae120d --- /dev/null +++ b/docker-build-image.sh @@ -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 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1ec929c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module snippet + +go 1.21.1 diff --git a/help.md b/help.md new file mode 100644 index 0000000..e2a0b17 --- /dev/null +++ b/help.md @@ -0,0 +1,3 @@ +# Help on `snippets` + +This is a work in progress 🚧 diff --git a/main.go b/main.go new file mode 100644 index 0000000..8f2013d --- /dev/null +++ b/main.go @@ -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:]) + +} diff --git a/release.env b/release.env new file mode 100644 index 0000000..c50c93a --- /dev/null +++ b/release.env @@ -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" diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..a994e58 --- /dev/null +++ b/run.sh @@ -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 + diff --git a/samples/js.code-snippets b/samples/js.code-snippets new file mode 100644 index 0000000..f22a0a4 --- /dev/null +++ b/samples/js.code-snippets @@ -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" + } +} \ No newline at end of file diff --git a/samples/js.yml b/samples/js.yml new file mode 100644 index 0000000..2b2f69b --- /dev/null +++ b/samples/js.yml @@ -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}") + } diff --git a/test-container.sh b/test-container.sh new file mode 100755 index 0000000..deabf4a --- /dev/null +++ b/test-container.sh @@ -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 diff --git a/version.txt b/version.txt new file mode 100644 index 0000000..c0ff80a --- /dev/null +++ b/version.txt @@ -0,0 +1 @@ +snippets v0.0.0 🍉 [watermelon] \ No newline at end of file