mirror of
https://github.com/Forceu/Gokapi.git
synced 2026-02-04 07:28:32 -06:00
Change all versions for preventing caching on every new release
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
minify "github.com/tdewolff/minify/v2"
|
||||
"github.com/tdewolff/minify/v2/css"
|
||||
@@ -16,11 +15,11 @@ import (
|
||||
const pathPrefix = "../../internal/webserver/web/static/"
|
||||
|
||||
type converter struct {
|
||||
InputPath string
|
||||
OutputPath string
|
||||
PreviousVersion string
|
||||
Type string
|
||||
Name string
|
||||
InputPath string
|
||||
OutputPath string
|
||||
GlobPreviousFiles string
|
||||
Type string
|
||||
Name string
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -32,32 +31,32 @@ func main() {
|
||||
func getPaths() []converter {
|
||||
var result []converter
|
||||
result = append(result, converter{
|
||||
InputPath: pathPrefix + "css/*.css",
|
||||
OutputPath: pathPrefix + "css/min/gokapi.min." + strconv.Itoa(cssMainVersion) + ".css",
|
||||
PreviousVersion: pathPrefix + "css/min/gokapi.min." + strconv.Itoa(cssMainVersion-1) + ".css",
|
||||
Type: "text/css",
|
||||
Name: "Main CSS",
|
||||
InputPath: pathPrefix + "css/*.css",
|
||||
OutputPath: pathPrefix + "css/min/gokapi.min." + cssMainVersion + ".css",
|
||||
GlobPreviousFiles: pathPrefix + "css/min/gokapi.min.*.css",
|
||||
Type: "text/css",
|
||||
Name: "Main CSS",
|
||||
})
|
||||
result = append(result, converter{
|
||||
InputPath: pathPrefix + "js/admin_*.js",
|
||||
OutputPath: pathPrefix + "js/min/admin.min." + strconv.Itoa(jsAdminVersion) + ".js",
|
||||
PreviousVersion: pathPrefix + "js/min/admin.min." + strconv.Itoa(jsAdminVersion-1) + ".js",
|
||||
Type: "text/javascript",
|
||||
Name: "Admin JS",
|
||||
InputPath: pathPrefix + "js/admin_*.js",
|
||||
OutputPath: pathPrefix + "js/min/admin.min." + jsAdminVersion + ".js",
|
||||
GlobPreviousFiles: pathPrefix + "js/min/admin.min.*.js",
|
||||
Type: "text/javascript",
|
||||
Name: "Admin JS",
|
||||
})
|
||||
result = append(result, converter{
|
||||
InputPath: pathPrefix + "js/end2end_admin.js",
|
||||
OutputPath: pathPrefix + "js/min/end2end_admin.min." + strconv.Itoa(jsE2EVersion) + ".js",
|
||||
PreviousVersion: pathPrefix + "js/min/end2end_admin.min." + strconv.Itoa(jsE2EVersion-1) + ".js",
|
||||
Type: "text/javascript",
|
||||
Name: "Admin E2E JS",
|
||||
InputPath: pathPrefix + "js/end2end_admin.js",
|
||||
OutputPath: pathPrefix + "js/min/end2end_admin.min." + jsE2EVersion + ".js",
|
||||
GlobPreviousFiles: pathPrefix + "js/min/end2end_admin.min.*.js",
|
||||
Type: "text/javascript",
|
||||
Name: "Admin E2E JS",
|
||||
})
|
||||
result = append(result, converter{
|
||||
InputPath: pathPrefix + "js/end2end_download.js",
|
||||
OutputPath: pathPrefix + "js/min/end2end_download.min." + strconv.Itoa(jsE2EVersion) + ".js",
|
||||
PreviousVersion: pathPrefix + "js/min/end2end_download.min." + strconv.Itoa(jsE2EVersion-1) + ".js",
|
||||
Type: "text/javascript",
|
||||
Name: "Download E2E JS",
|
||||
InputPath: pathPrefix + "js/end2end_download.js",
|
||||
OutputPath: pathPrefix + "js/min/end2end_download.min." + jsE2EVersion + ".js",
|
||||
GlobPreviousFiles: pathPrefix + "js/min/end2end_download.min.*.js",
|
||||
Type: "text/javascript",
|
||||
Name: "Download E2E JS",
|
||||
})
|
||||
result = append(result, converter{
|
||||
InputPath: pathPrefix + "js/streamsaver.js",
|
||||
@@ -91,6 +90,10 @@ func minifyContent(conv converter) {
|
||||
m.AddFunc("text/css", css.Minify)
|
||||
m.AddFunc("text/javascript", js.Minify)
|
||||
|
||||
if conv.GlobPreviousFiles != "" {
|
||||
removeOldFiles(conv.GlobPreviousFiles)
|
||||
}
|
||||
|
||||
files, err := m.Bytes(conv.Type, getAllFiles(conv.InputPath))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@@ -103,15 +106,28 @@ func minifyContent(conv converter) {
|
||||
os.Exit(5)
|
||||
}
|
||||
fmt.Println("Minified " + conv.Name)
|
||||
if conv.PreviousVersion != "" && fileExists(conv.PreviousVersion) {
|
||||
fmt.Println("Removing old version of " + conv.Name)
|
||||
err = os.Remove(conv.PreviousVersion)
|
||||
if err != nil {
|
||||
fmt.Println("Could not remove old " + conv.Name + " file")
|
||||
fmt.Println(err)
|
||||
os.Exit(6)
|
||||
}
|
||||
}
|
||||
|
||||
func removeOldFiles(pattern string) {
|
||||
matches, err := filepath.Glob(pattern)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if len(matches) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if len(matches) > 1 {
|
||||
fmt.Println("Multiple matching files found for " + pattern + ", refusing to delete")
|
||||
os.Exit(6)
|
||||
}
|
||||
|
||||
err = os.Remove(matches[0])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func getAllFiles(pattern string) []byte {
|
||||
@@ -150,6 +166,6 @@ func fileExists(filename string) bool {
|
||||
// Auto-generated content below, do not modify
|
||||
// Version codes can be changed in updateVersionNumbers.go
|
||||
|
||||
const jsAdminVersion = 15
|
||||
const jsE2EVersion = 8
|
||||
const cssMainVersion = 5
|
||||
const jsAdminVersion = "d01c4e9bf1"
|
||||
const jsE2EVersion = "d01c4e9bf1"
|
||||
const cssMainVersion = "d01c4e9bf1"
|
||||
|
||||
@@ -4,6 +4,8 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
@@ -11,11 +13,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const versionJsAdmin = 15
|
||||
const versionJsDropzone = 5
|
||||
const versionJsE2EAdmin = 8
|
||||
const versionCssMain = 5
|
||||
|
||||
const fileMain = "../../cmd/gokapi/Main.go"
|
||||
const fileMinify = "../../build/go-generate/minifyStaticContent.go"
|
||||
const fileVersionConstants = "../../internal/webserver/web/templates/string_constants.tmpl"
|
||||
@@ -109,15 +106,22 @@ func checkFileExists(filename string) {
|
||||
|
||||
func insertVersionNumbers(input string) string {
|
||||
versionGokapi := parseGokapiVersion()
|
||||
hashedVersion := getShortSHA256Hash(versionGokapi)
|
||||
result := strings.ReplaceAll(input, "%gokapiversion%", versionGokapi)
|
||||
result = strings.ReplaceAll(result, "%gokapiversionInt%", parseVersionAsInt(versionGokapi))
|
||||
result = strings.ReplaceAll(result, "%jsadmin%", strconv.Itoa(versionJsAdmin))
|
||||
result = strings.ReplaceAll(result, "%jsdropzone%", strconv.Itoa(versionJsDropzone))
|
||||
result = strings.ReplaceAll(result, "%jse2e%", strconv.Itoa(versionJsE2EAdmin))
|
||||
result = strings.ReplaceAll(result, "%css_main%", strconv.Itoa(versionCssMain))
|
||||
result = strings.ReplaceAll(result, "%jsadmin%", hashedVersion)
|
||||
result = strings.ReplaceAll(result, "%jsdropzone%", hashedVersion)
|
||||
result = strings.ReplaceAll(result, "%jse2e%", hashedVersion)
|
||||
result = strings.ReplaceAll(result, "%css_main%", hashedVersion)
|
||||
return result
|
||||
}
|
||||
|
||||
func getShortSHA256Hash(data string) string {
|
||||
hasher := sha256.New()
|
||||
hasher.Write([]byte(data))
|
||||
return hex.EncodeToString(hasher.Sum(nil))[:10]
|
||||
}
|
||||
|
||||
func parseVersionAsInt(version string) string {
|
||||
versionNoAppendix := strings.Split(version, "-")[0]
|
||||
versionSplit := strings.Split(versionNoAppendix, ".")
|
||||
@@ -174,9 +178,9 @@ const templateVersions = `// File contains auto-generated values. Do not change
|
||||
const autoGenComment = "// Auto-generated content below, do not modify"
|
||||
const templateMinify = `// Version codes can be changed in updateVersionNumbers.go
|
||||
|
||||
const jsAdminVersion = %jsadmin%
|
||||
const jsE2EVersion = %jse2e%
|
||||
const cssMainVersion = %css_main%
|
||||
const jsAdminVersion = "%jsadmin%"
|
||||
const jsE2EVersion = "%jse2e%"
|
||||
const cssMainVersion = "%css_main%"
|
||||
`
|
||||
|
||||
const templateVersionApi = `// Code generated by updateApiRouting.go - DO NOT EDIT.
|
||||
|
||||
@@ -34,7 +34,6 @@ import (
|
||||
)
|
||||
|
||||
// versionGokapi is the current version in readable form.
|
||||
// Other version numbers can be modified in /build/go-generate/updateVersionNumbers.go
|
||||
const versionGokapi = "2.2.1"
|
||||
|
||||
// The following calls update the version numbers, update documentation, minify Js/CSS and build the WASM modules
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
// Specifies the version of JS files, so that the browser doesn't
|
||||
// use a cached version, if the file has been updated
|
||||
{{define "js_admin_version"}}15{{end}}
|
||||
{{define "js_dropzone_version"}}5{{end}}
|
||||
{{define "js_e2eversion"}}8{{end}}
|
||||
{{define "css_main"}}5{{end}}
|
||||
{{define "js_admin_version"}}d01c4e9bf1{{end}}
|
||||
{{define "js_dropzone_version"}}d01c4e9bf1{{end}}
|
||||
{{define "js_e2eversion"}}d01c4e9bf1{{end}}
|
||||
{{define "css_main"}}d01c4e9bf1{{end}}
|
||||
Reference in New Issue
Block a user