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.

86 lines
1.7 KiB

  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. // +build ignore
  5. package main
  6. import (
  7. "bytes"
  8. "crypto/sha1"
  9. "fmt"
  10. "io/ioutil"
  11. "log"
  12. "net/http"
  13. "os"
  14. "path/filepath"
  15. "strconv"
  16. "github.com/shurcooL/vfsgen"
  17. )
  18. func needsUpdate(dir string, filename string) (bool, []byte) {
  19. needRegen := false
  20. _, err := os.Stat(filename)
  21. if err != nil {
  22. needRegen = true
  23. }
  24. oldHash, err := ioutil.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. update, newHash := needsUpdate(dir, filename)
  53. if !update {
  54. fmt.Printf("bindata for %s already up-to-date\n", packageName)
  55. return
  56. }
  57. fmt.Printf("generating bindata for %s\n", packageName)
  58. var fsTemplates http.FileSystem = http.Dir(dir)
  59. err := vfsgen.Generate(fsTemplates, vfsgen.Options{
  60. PackageName: packageName,
  61. BuildTags: "bindata",
  62. VariableName: "Assets",
  63. Filename: filename,
  64. })
  65. if err != nil {
  66. log.Fatalf("%v\n", err)
  67. }
  68. _ = ioutil.WriteFile(filename+".hash", newHash, 0666)
  69. }