Skip to content

Commit

Permalink
Download web dependencies on build (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 authored Feb 3, 2023
1 parent da32ffc commit 76305c3
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 1,236 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ LICENSE
Dockerfile
config.json
config.json.example
assets/highlight.min.js
assets/highlightjs-line-numbers.min.js
assets/styles/
1 change: 1 addition & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}

deploy:
needs:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.idea/
config.json
gobin.db
assets/highlight.min.js
assets/highlightjs-line-numbers.min.js
assets/styles/
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
FROM golang:1.19-alpine AS build

ARG GITHUB_TOKEN

WORKDIR /build

COPY tools/go.mod tools/go.sum tools/

RUN cd tools && go mod download

COPY tools/ tools/

RUN mkdir assets && \
mkdir assets/styles && \
cd tools && \
go run . --github-token $GITHUB_TOKEN

COPY go.mod go.sum ./

RUN go mod download
Expand Down
1,202 changes: 0 additions & 1,202 deletions assets/highlight.min.js

This file was deleted.

1 change: 0 additions & 1 deletion assets/highlightjs-line-numbers.min.js

This file was deleted.

1 change: 0 additions & 1 deletion assets/styles/atom-one-dark.min.css

This file was deleted.

1 change: 0 additions & 1 deletion assets/styles/atom-one-light.min.css

This file was deleted.

10 changes: 0 additions & 10 deletions assets/styles/github-dark.min.css

This file was deleted.

10 changes: 0 additions & 10 deletions assets/styles/github.min.css

This file was deleted.

3 changes: 2 additions & 1 deletion tools/highlightjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"

"github.com/google/go-github/v50/github"
)
Expand All @@ -22,7 +23,7 @@ func downloadHighlightJS(client *github.Client) error {

prefix := zipReader.File[0].Name
for _, file := range highlightJSFiles {
println("Copying: ", prefix+file)
log.Println("Copying: ", prefix+file)
if err = copyToAssets(zipReader, prefix, "build/", file); err != nil {
return fmt.Errorf("failed to copy file: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion tools/highlightjs_line_numbers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"

"github.com/google/go-github/v50/github"
)
Expand All @@ -16,7 +17,7 @@ func downloadHighlightJSLineNumbers(client *github.Client) error {

prefix := zipReader.File[0].Name

println("Copying: ", prefix+highlightJSLineNumbersFiles)
log.Println("Copying: ", prefix+highlightJSLineNumbersFiles)
if err = copyToAssets(zipReader, prefix, "dist/", highlightJSLineNumbersFiles); err != nil {
return fmt.Errorf("failed to copy file: %w", err)
}
Expand Down
27 changes: 18 additions & 9 deletions tools/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"archive/zip"
"bytes"
"context"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
Expand All @@ -15,31 +17,38 @@ import (
)

func main() {
println("Downloading latest version...")

client := github.NewClient(nil)
githubToken := flag.String("github-token", "", "GitHub token (required)")
flag.Parse()
log.Println("Downloading latest version...")

var client *github.Client
if *githubToken != "" {
client = github.NewTokenClient(context.Background(), *githubToken)
} else {
client = github.NewClient(nil)
}

if err := downloadHighlightJS(client); err != nil {
println("Failed to download HighlightJS:", err.Error())
log.Panicln("Failed to download HighlightJS:", err.Error())
return
}

if err := downloadHighlightJSLineNumbers(client); err != nil {
println("Failed to download HighlightJS Line Numbers:", err.Error())
log.Panicln("Failed to download HighlightJS Line Numbers:", err.Error())
return
}

println("Done")
log.Println("Done")
}

func downloadLatestTagZipball(client *github.Client, owner string, repo string) (*zip.Reader, error) {
latestTag, err := getLatestTag(client, owner, repo)
if err != nil {
return nil, fmt.Errorf("failed to get latest tag: %w", err)
}
println("Latest", owner+"/"+repo, "version:", latestTag.GetName())
log.Println("Latest", owner+"/"+repo, "version:", latestTag.GetName())

println(owner+"/"+repo, "Zipball URL:", latestTag.GetZipballURL())
log.Println(owner+"/"+repo, "Zipball URL:", latestTag.GetZipballURL())

zipReader, err := downloadZip(latestTag.GetZipballURL())
if err != nil {
Expand Down Expand Up @@ -99,7 +108,7 @@ func copyToAssets(zipReader *zip.Reader, prefix string, buildPrefix string, file
}
defer zipFile.Close()

assetsFile, err := os.OpenFile("assets/"+strings.TrimPrefix(filename, buildPrefix), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
assetsFile, err := os.OpenFile("../assets/"+strings.TrimPrefix(filename, buildPrefix), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("failed to open assets file: %w", err)
}
Expand Down

0 comments on commit 76305c3

Please sign in to comment.