Skip to content

Commit

Permalink
feat: reorganize code so it can be used as a library
Browse files Browse the repository at this point in the history
  • Loading branch information
kamermans committed Jul 25, 2024
1 parent b4a8dc1 commit 58f15e0
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 45 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes
File renamed without changes
55 changes: 18 additions & 37 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -31,7 +30,7 @@ var (
openscadPath string

aspectRatioInts [2]int
outputFileType string
outputFileType skyline.OutputType
)

func init() {
Expand Down Expand Up @@ -76,24 +75,24 @@ 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")
}
}

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)
Expand All @@ -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)
}
}
40 changes: 37 additions & 3 deletions cad.go → pkg/skyline/cad.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion github.go → pkg/skyline/github.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package skyline

import (
"context"
Expand Down

0 comments on commit 58f15e0

Please sign in to comment.