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.

71 lines
2.4 KiB

  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. )
  7. func hookContext(hook *Hook, scannerTxt string) {
  8. if scannerTxt == "" {
  9. hook.Context = "PostCommit"
  10. } else if strings.HasPrefix(scannerTxt, "sandpoints-ext") {
  11. revs := strings.Fields(scannerTxt)
  12. if revs[1] == "publish" {
  13. hook.Publish = true
  14. }
  15. if revs[2] == "offline" {
  16. hook.Offline = true
  17. }
  18. if revs[3] == "file" {
  19. hook.Context = "SandpointsFileProtocol"
  20. }
  21. if revs[3] == "http" {
  22. hook.Context = "SandpointsHTTPProtocol"
  23. }
  24. } else {
  25. // it only handles gitea use case if not Local or sandpoints-ext
  26. hook.Context = "PostReceive"
  27. hook.Stdinput = scannerTxt
  28. }
  29. }
  30. func cleanUpPublicHTML(hugo *Hugo, hook *Hook) {
  31. if hook.Publish {
  32. if _, err := os.Stat(filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName)); err == nil {
  33. err := os.RemoveAll(filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName))
  34. check(err)
  35. err = os.MkdirAll(hook.PublicHTMLPath, 0755)
  36. check(err)
  37. }
  38. } else if _, err := os.Lstat(filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview")); err == nil {
  39. err := os.RemoveAll(filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview"))
  40. check(err)
  41. } else if _, err := os.Stat(filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview")); err == nil {
  42. err := os.RemoveAll(filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview"))
  43. check(err)
  44. }
  45. }
  46. func copyToPublicHTML(hugo *Hugo, hook *Hook) {
  47. if hook.Publish {
  48. err := os.Rename(hugo.DestinationDir, filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName))
  49. check(err)
  50. err = os.Symlink(filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName), filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview"))
  51. check(err)
  52. if hugo.CatalogPrefix != "" && hugo.CatalogName != "" {
  53. err = os.Symlink(filepath.Join(hook.PublicHTMLPath, "libraries", hugo.CatalogName), filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, hugo.CatalogPrefix))
  54. check(err)
  55. }
  56. } else {
  57. err := os.MkdirAll(filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName), 0755)
  58. check(err)
  59. err = os.Rename(hugo.DestinationDir, filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview"))
  60. check(err)
  61. if hugo.CatalogPrefix != "" && hugo.CatalogName != "" {
  62. err = os.Symlink(filepath.Join(hook.PublicHTMLPath, "libraries", hugo.CatalogName), filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview", hugo.CatalogPrefix))
  63. check(err)
  64. }
  65. }
  66. }