Guest User

Untitled

a guest
Aug 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bytes"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. "path/filepath"
  9. "regexp"
  10. "strings"
  11. )
  12.  
  13. var checker = regexp.MustCompile(`^// Fill out[^\r\n]*`)
  14. var checkerForNew = regexp.MustCompile(`^// Copyright 2014[^\r\n]*`)
  15. var newCopyright = []byte(`// Copyright 2014-2018 9M Interactive, Inc. All Rights Reserved.`) // modify this line to what you desire
  16. var bom = "\xef\xbb\xbf"
  17.  
  18. func main() {
  19. dir := os.Args[1]
  20. log.Println("path", dir)
  21.  
  22. err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  23. if info.IsDir() {
  24. return nil
  25. }
  26.  
  27. ext := filepath.Ext(info.Name())
  28. if ext != ".cpp" && ext != ".h" && ext != ".cs" {
  29. return nil
  30. }
  31.  
  32. fileContent, err := ioutil.ReadFile(path)
  33. if err != nil {
  34. log.Println(err)
  35. return nil
  36. }
  37.  
  38. // trim bom
  39. fileContent = bytes.TrimLeft(fileContent, bom)
  40.  
  41. var newContent []byte
  42. if idx := checker.FindIndex(fileContent); idx != nil {
  43. // has copyright notice
  44. log.Println("has copyright")
  45. newContent = checker.ReplaceAll(fileContent, newCopyright)
  46. } else if !checkerForNew.Match(fileContent) {
  47. // no copyright notice
  48. log.Println("no copyright")
  49. var builder strings.Builder
  50. builder.Write(newCopyright)
  51. builder.Write([]byte("\r\n\r\n"))
  52. builder.Write(fileContent)
  53.  
  54. newContent = []byte(builder.String())
  55. }
  56.  
  57. if newContent != nil {
  58. if err := ioutil.WriteFile(path, newContent, 0644); err != nil {
  59. log.Println("overwrite error:", err)
  60. }
  61.  
  62. log.Println("fixed", path)
  63. } else {
  64. log.Println("skipping", path)
  65. }
  66.  
  67. return nil
  68. })
  69.  
  70. log.Println("done", err)
  71. }
Add Comment
Please, Sign In to add comment