Skip to content

Commit

Permalink
generator does less api requests, no longer using github library
Browse files Browse the repository at this point in the history
  • Loading branch information
NotLe0n committed Dec 5, 2023
1 parent 011c845 commit 356a798
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 51 deletions.
36 changes: 24 additions & 12 deletions gen/stdlib-generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"sort"
Expand All @@ -13,8 +15,6 @@ import (
"github.com/DDP-Projekt/Kompilierer/src/ast"
"github.com/DDP-Projekt/Kompilierer/src/ddperror"
"github.com/DDP-Projekt/Kompilierer/src/parser"

"github.com/google/go-github/v55/github"
)

var nameMap = map[string]map[string]string{
Expand Down Expand Up @@ -48,41 +48,53 @@ func clearDirectory(dir string) error {
})
}

type GitHubDir struct {
Name string `json:"name"`
Path string `json:"path"`
}

func main() {
outputDirDe := "content/DE/Programmierung/Standardbibliothek"
outputDirEn := "content/EN/Programmierung/Standardbibliothek"

// delete old articles
panicIfErr(clearDirectory(outputDirDe))

gh := github.NewClient(nil)
_, dir, _, err := gh.Repositories.GetContents(context.Background(), "DDP-Projekt", "Kompilierer", "lib/stdlib/Duden", nil)
// get all files in directory
resp, err := http.Get("https://api.github.com/repos/DDP-Projekt/Kompilierer/contents/lib/stdlib/Duden/")
panicIfErr(err)
defer resp.Body.Close()

dir := make([]GitHubDir, 0)
json.NewDecoder(resp.Body).Decode(&dir)

// iterate through all files and generate MD files
for _, entry := range dir {
outputPathDe := filepath.Join(outputDirDe, strings.Replace(entry.GetName(), "ddp", "md", 1))
outputPathEn := filepath.Join(outputDirEn, strings.Replace(entry.GetName(), "ddp", "md", 1))
outputPathDe := filepath.Join(outputDirDe, strings.Replace(entry.Name, "ddp", "md", 1))
outputPathEn := filepath.Join(outputDirEn, strings.Replace(entry.Name, "ddp", "md", 1))

file, _, _, err := gh.Repositories.GetContents(context.Background(), "DDP-Projekt", "Kompilierer", entry.GetPath(), nil)
resp, err := http.Get("https://raw.githubusercontent.com/DDP-Projekt/Kompilierer/master/" + entry.Path)
panicIfErr(err)
inputFile, err := file.GetContent()
defer resp.Body.Close()
inputFile, err := io.ReadAll(resp.Body)
panicIfErr(err)

MakeMdFiles(inputFile, outputPathDe, "DE")
MakeMdFiles(inputFile, outputPathEn, "EN")
}
}

func MakeMdFiles(inputFile, outputFilePath, lang string) {
func MakeMdFiles(inputFile []byte, outputFilePath, lang string) {
fmt.Printf("creating output file '%s'...\n", outputFilePath)

os.MkdirAll(filepath.Dir(outputFilePath), os.ModeDir|os.ModePerm)
outputFile, err := os.Create(outputFilePath)
panicIfErr(err)
defer outputFile.Close()

fmt.Printf("parsing module...\n")
module, err := parser.Parse(parser.Options{
Source: []byte(inputFile),
Source: inputFile,
ErrorHandler: ddperror.EmptyHandler,
})
panicIfErr(err)
Expand All @@ -100,7 +112,7 @@ func MakeMdFiles(inputFile, outputFilePath, lang string) {

fmt.Println("writing md file...")

writeMD(inputFile, outputFile, publicDecls, lang)
writeMD(string(inputFile), outputFile, publicDecls, lang)

fmt.Println("done writing md file.")
fmt.Println()
Expand Down
13 changes: 1 addition & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,4 @@ go 1.19

require github.com/DDP-Projekt/Kompilierer v0.1.0-alpha.0.20231204192905-7f9b0948acb0

require (
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/google/go-querystring v1.1.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/sys v0.11.0 // indirect
)

require (
github.com/google/go-github/v55 v55.0.0
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
)
require github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
27 changes: 0 additions & 27 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,31 +1,4 @@
github.com/DDP-Projekt/Kompilierer v0.1.0-alpha h1:+ypGLaIz7vw5G6M2prOn86No4PfXEylkP8Py+vzZVlY=
github.com/DDP-Projekt/Kompilierer v0.1.0-alpha/go.mod h1:OVRSCZNQ8LVV8ZH5TuAGTaf0skO06bgV5Y7Wgvdb+NY=
github.com/DDP-Projekt/Kompilierer v0.1.0-alpha.0.20231204192905-7f9b0948acb0 h1:RmgvyR3iJ3bNj8JxN2iGckOi6Xx6SVSV6TueAYv20c0=
github.com/DDP-Projekt/Kompilierer v0.1.0-alpha.0.20231204192905-7f9b0948acb0/go.mod h1:OVRSCZNQ8LVV8ZH5TuAGTaf0skO06bgV5Y7Wgvdb+NY=
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA=
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g=
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I=
github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs=
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-github/v55 v55.0.0 h1:4pp/1tNMB9X/LuAhs5i0KQAE40NmiR/y6prLNb9x9cg=
github.com/google/go-github/v55 v55.0.0/go.mod h1:JLahOTA1DnXzhxEymmFF5PP2tSS9JVNj68mSZNDwskA=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 comments on commit 356a798

Please sign in to comment.