You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate-bindata.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. //go:build ignore
  5. // +build ignore
  6. package main
  7. import (
  8. "bytes"
  9. "crypto/sha1"
  10. "fmt"
  11. "log"
  12. "net/http"
  13. "os"
  14. "path/filepath"
  15. "strconv"
  16. "github.com/shurcooL/vfsgen"
  17. )
  18. func needsUpdate(dir, filename string) (bool, []byte) {
  19. needRegen := false
  20. _, err := os.Stat(filename)
  21. if err != nil {
  22. needRegen = true
  23. }
  24. oldHash, err := os.ReadFile(filename + ".hash")
  25. if err != nil {
  26. oldHash = []byte{}
  27. }
  28. hasher := sha1.New()
  29. err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  30. if err != nil {
  31. return err
  32. }
  33. _, _ = hasher.Write([]byte(info.Name()))
  34. _, _ = hasher.Write([]byte(info.ModTime().String()))
  35. _, _ = hasher.Write([]byte(strconv.FormatInt(info.Size(), 16)))
  36. return nil
  37. })
  38. if err != nil {
  39. return true, oldHash
  40. }
  41. newHash := hasher.Sum([]byte{})
  42. if bytes.Compare(oldHash, newHash) != 0 {
  43. return true, newHash
  44. }
  45. return needRegen, newHash
  46. }
  47. func main() {
  48. if len(os.Args) < 4 {
  49. log.Fatal("Insufficient number of arguments. Need: directory packageName filename")
  50. }
  51. dir, packageName, filename := os.Args[1], os.Args[2], os.Args[3]
  52. var useGlobalModTime bool
  53. if len(os.Args) == 5 {
  54. useGlobalModTime, _ = strconv.ParseBool(os.Args[4])
  55. }
  56. update, newHash := needsUpdate(dir, filename)
  57. if !update {
  58. fmt.Printf("bindata for %s already up-to-date\n", packageName)
  59. return
  60. }
  61. fmt.Printf("generating bindata for %s\n", packageName)
  62. var fsTemplates http.FileSystem = http.Dir(dir)
  63. err := vfsgen.Generate(fsTemplates, vfsgen.Options{
  64. PackageName: packageName,
  65. BuildTags: "bindata",
  66. VariableName: "Assets",
  67. Filename: filename,
  68. UseGlobalModTime: useGlobalModTime,
  69. })
  70. if err != nil {
  71. log.Fatalf("%v\n", err)
  72. }
  73. _ = os.WriteFile(filename+".hash", newHash, 0o666)
  74. }