provision.awk 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. ###
  2. # Modifying Yii2's files for initialize Vagrant VM
  3. #
  4. # @author HA3IK <golubha3ik@gmail.com>
  5. # @version 1.0.0
  6. BEGIN {
  7. print "AWK BEGINs its work:"
  8. IGNORECASE = 1
  9. # Correct IP - wildcard last octet
  10. match(ip, /(([0-9]+\.)+)/, arr)
  11. ip = arr[1] "*"
  12. }
  13. BEGINFILE {
  14. msg = "- Work with: " FILENAME
  15. # Define array index for the file
  16. switch (FILENAME) {
  17. case /environments\/dev\/(back|front)end\/config\/main\-local\.php$/:
  18. isFile["IsMainLocConf"] = 1
  19. msg = msg " - allow VM IP for Gii and debug toolbar"
  20. break
  21. }
  22. # Print the final message
  23. print msg
  24. }
  25. # BODY
  26. {
  27. # IF environments/dev/(back|front)end/config/main-local.php
  28. if (isFile["IsMainLocConf"]) {
  29. # IF the line[s] after yii\(debug|gii)\Module
  30. if (FNR == nextLine["nubmer"]) {
  31. # Prepare for next line
  32. ++nextLine["nubmer"]
  33. # IF line has "allowedIPs"
  34. if (index($0, "allowedIPs")) {
  35. # IF our IP is not there
  36. if (!index($0, ip)) {
  37. # Add it
  38. match($0, /([^\]]+)(.+)/, arr)
  39. $0 = sprintf("%s, '%s'%s", arr[1], ip, arr[2])
  40. }
  41. # Delete next line
  42. delete nextLine
  43. # IF "allowedIPs" are not set - search for the end of an array structure
  44. } else if ($0 ~ /\];$/) {
  45. # Rewrite line
  46. $0 = nextLine["indent"] "'allowedIPs' => ['127.0.0.1', '::1', '" ip "'],\n" $0
  47. delete nextLine
  48. }
  49. # IF line is done
  50. if (!length(nextLine)) {
  51. printf " Line %d: Allowed IP: %s\n", FNR, ip
  52. }
  53. # Search for yii\(debug|gii)\Module
  54. } else if (match($0, /^(\s+).+yii\\(debug|gii)\\Module/, arr)) {
  55. # Save next line and indent
  56. nextLine["nubmer"] = FNR + 1
  57. nextLine["indent"] = arr[1]
  58. }
  59. # Rewrite the file
  60. print $0 > FILENAME
  61. }
  62. }
  63. ENDFILE {
  64. delete isFile
  65. close(FILENAME)
  66. }
  67. END {
  68. print "AWK ENDs its work."
  69. }