From 58f15e09a3cbbaeb3451db1e9611e3f7addf9551 Mon Sep 17 00:00:00 2001 From: Steve Kamerman Date: Thu, 25 Jul 2024 10:16:09 -0400 Subject: [PATCH] feat: reorganize code so it can be used as a library --- README.md | 4 +- go.mod | 4 +- openscad.png => images/openscad.png | Bin .../skyline-example.png | Bin main.go | 55 ++++++------------ cad.go => pkg/skyline/cad.go | 40 ++++++++++++- github.go => pkg/skyline/github.go | 2 +- 7 files changed, 60 insertions(+), 45 deletions(-) rename openscad.png => images/openscad.png (100%) rename skyline-example.png => images/skyline-example.png (100%) rename cad.go => pkg/skyline/cad.go (89%) rename github.go => pkg/skyline/github.go (99%) diff --git a/README.md b/README.md index 750245d..0ee7949 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # github-skyline -![Skyline Example](skyline-example.png) +![Skyline Example](images/skyline-example.png) This program generates GitHub Skyline CAD files in OpenSCAD and STL format. @@ -73,7 +73,7 @@ maxBuildingHeight = 60.000000; buildingColor = "red"; ``` -![OpenSCAD Screenshot](openscad.png) +![OpenSCAD Screenshot](images/openscad.png) # Generating an STL file In order to generate an STL file, you msut have a recent version of [OpenSCAD](https://openscad.org/downloads.html) diff --git a/go.mod b/go.mod index b80ffd5..4c742f9 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ -module github-skyline +module github.com/kamermans/github-skyline -go 1.22.2 +go 1.22.5 require ( github.com/hasura/go-graphql-client v0.12.2 diff --git a/openscad.png b/images/openscad.png similarity index 100% rename from openscad.png rename to images/openscad.png diff --git a/skyline-example.png b/images/skyline-example.png similarity index 100% rename from skyline-example.png rename to images/skyline-example.png diff --git a/main.go b/main.go index b5e39c2..f6445d2 100644 --- a/main.go +++ b/main.go @@ -4,11 +4,10 @@ package main import ( "fmt" "os" - "os/exec" "path" "strings" - "time" + "github.com/kamermans/github-skyline/pkg/skyline" flag "github.com/spf13/pflag" ) @@ -31,7 +30,7 @@ var ( openscadPath string aspectRatioInts [2]int - outputFileType string + outputFileType skyline.OutputType ) func init() { @@ -76,8 +75,8 @@ func init() { panic("output file must have an extension") } - outputFileType = parts[len(parts)-1] - if outputFileType != "scad" && outputFileType != "stl" { + outputFileType = skyline.OutputType(parts[len(parts)-1]) + if outputFileType != skyline.OutputTypeSCAD && outputFileType != skyline.OutputTypeSTL { panic("output file must be .scad or .stl") } } @@ -85,15 +84,15 @@ func init() { func main() { var err error - var contribs *Contributions + var contribs *skyline.Contributions if contribsFile != "" && !saveContribs { - contribs, err = NewContributionsFromFile(contribsFile) + contribs, err = skyline.NewContributionsFromFile(contribsFile) if err != nil { panic(err) } } else { - fetcher := NewGitHubContributionsFetcher(username, token) + fetcher := skyline.NewGitHubContributionsFetcher(username, token) contribs, err = fetcher.FetchContributions(startYear, endYear) if err != nil { panic(err) @@ -110,46 +109,28 @@ func main() { fmt.Printf("Total contributions: %d between %v and %v\n", contribs.TotalContributions, contribs.FirstDate, contribs.LastDate) fmt.Printf("Generating OpenSCAD ...\n") - sg := NewSkylineGenerator(*contribs, aspectRatioInts, maxBuildingHeight, buildingWidth, buildingLength) - skyline := sg.Generate(interval) - skyline.BaseAngle = baseAngle - skyline.BaseHeight = baseHeight - skyline.BaseMargin = baseMargin - scad, err := skyline.ToOpenSCAD() - if err != nil { - panic(err) - } - - if outputFileType == "scad" { - err = os.WriteFile(outputFile, scad, 0644) + sg := skyline.NewSkylineGenerator(*contribs, aspectRatioInts, maxBuildingHeight, buildingWidth, buildingLength) + sl := sg.Generate(interval) + sl.BaseAngle = baseAngle + sl.BaseHeight = baseHeight + sl.BaseMargin = baseMargin + + if outputFileType == skyline.OutputTypeSCAD { + dur, err := sl.ToOpenSCAD(outputFile) if err != nil { panic(err) } - fmt.Printf("OpenSCAD file written to %s\n", outputFile) + fmt.Printf("OpenSCAD file %s generated in %v\n", outputFile, dur) - } else if outputFileType == "stl" { + } else if outputFileType == skyline.OutputTypeSTL { fmt.Printf("Generating STL ...\n") - start := time.Now() - - tmpFile, err := os.CreateTemp("", "skyline*.scad") - if err != nil { - panic(err) - } - - defer os.Remove(tmpFile.Name()) - _, err = tmpFile.Write(scad) - if err != nil { - panic(err) - } - cmd := exec.Command(openscadPath, "-o", outputFile, tmpFile.Name()) - err = cmd.Run() + dur, err := sl.ToSTL(outputFile, openscadPath) if err != nil { panic(err) } - dur := time.Since(start) fmt.Printf("STL file written to %s in %v\n", outputFile, dur) } } diff --git a/cad.go b/pkg/skyline/cad.go similarity index 89% rename from cad.go rename to pkg/skyline/cad.go index aabddc6..d080377 100644 --- a/cad.go +++ b/pkg/skyline/cad.go @@ -1,9 +1,12 @@ -package main +package skyline import ( "bytes" "fmt" "math" + "os" + "os/exec" + "time" // _ "github.com/go-gl/mathgl/mgl64" // _ "github.com/ljanyst/ghostscad/primitive" // osc "github.com/ljanyst/ghostscad/sys" @@ -13,8 +16,13 @@ const ( defaultBaseMargin = 1.0 defaultBaseHeight = 5.0 defaultBaseAngle = 22.5 + + OutputTypeSCAD = OutputType("scad") + OutputTypeSTL = OutputType("stl") ) +type OutputType string + type SkylineGenerator struct { contributions Contributions aspectRatio float64 @@ -217,7 +225,8 @@ var ( }` ) -func (sl *Skyline) ToOpenSCAD() ([]byte, error) { +func (sl *Skyline) ToOpenSCAD(filename string) (time.Duration, error) { + start := time.Now() out := &bytes.Buffer{} // Variables @@ -262,5 +271,30 @@ func (sl *Skyline) ToOpenSCAD() ([]byte, error) { fmt.Fprintf(out, "}\n") // end union - return out.Bytes(), nil + err := os.WriteFile(filename, out.Bytes(), 0644) + return time.Since(start), err +} + +func (sl *Skyline) ToSTL(filename string, openscadPath string) (time.Duration, error) { + start := time.Now() + + tmpFile, err := os.CreateTemp("", "skyline*.scad") + if err != nil { + return time.Since(start), err + } + + defer os.Remove(tmpFile.Name()) + + _, err = sl.ToOpenSCAD(tmpFile.Name()) + if err != nil { + return time.Since(start), err + } + + cmd := exec.Command(openscadPath, "-o", filename, tmpFile.Name()) + err = cmd.Run() + if err != nil { + return time.Since(start), err + } + + return time.Since(start), nil } diff --git a/github.go b/pkg/skyline/github.go similarity index 99% rename from github.go rename to pkg/skyline/github.go index f87c04e..8bfeb12 100644 --- a/github.go +++ b/pkg/skyline/github.go @@ -1,4 +1,4 @@ -package main +package skyline import ( "context"