1
0

.fast-make-targets 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  2. # Almost all code borrowed from Zshell's _make function
  3. #
  4. # Copyright (c) 2018 Sebastian Gniazdowski
  5. local -a TARGETS
  6. .make-expandVars() {
  7. local open close var val front='' rest=$1
  8. while [[ $rest == (#b)[^$]#($)* ]]; do
  9. front=$front${rest[1,$mbegin[1]-1]}
  10. rest=${rest[$mbegin[1],-1]}
  11. case $rest[2] in
  12. ($) # '$$'. may not appear in target and variable's value
  13. front=$front\$\$
  14. rest=${rest[3,-1]}
  15. continue
  16. ;;
  17. (\() # Variable of the form $(foobar)
  18. open='('
  19. close=')'
  20. ;;
  21. ({) # ${foobar}
  22. open='{'
  23. close='}'
  24. ;;
  25. ([[:alpha:]]) # $foobar. This is exactly $(f)oobar.
  26. open=''
  27. close=''
  28. var=$rest[2]
  29. ;;
  30. (*) # bad parameter name
  31. print -- $front$rest
  32. return 1
  33. ;;
  34. esac
  35. if [[ -n $open ]]; then
  36. if [[ $rest == \$$open(#b)([[:alnum:]_]##)(#B)$close* ]]; then
  37. var=$match
  38. else # unmatched () or {}, or bad parameter name
  39. print -- $front$rest
  40. return 1
  41. fi
  42. fi
  43. val=''
  44. if [[ -n ${VAR_ARGS[(i)$var]} ]]; then
  45. val=${VAR_ARGS[$var]}
  46. else
  47. if [[ -n $opt_args[(I)(-e|--environment-overrides)] ]]; then
  48. if [[ $parameters[$var] == scalar-export* ]]; then
  49. val=${(P)var}
  50. elif [[ -n ${VARIABLES[(i)$var]} ]]; then
  51. val=${VARIABLES[$var]}
  52. fi
  53. else
  54. if [[ -n ${VARIABLES[(i)$var]} ]]; then
  55. val=${VARIABLES[$var]}
  56. elif [[ $parameters[$var] == scalar-export* ]]; then
  57. val=${(P)var}
  58. fi
  59. fi
  60. fi
  61. rest=${rest//\$$open$var$close/$val}
  62. done
  63. print -- ${front}${rest}
  64. }
  65. .make-parseMakefile () {
  66. local input var val target dep TAB=$'\t' tmp IFS=
  67. while read input
  68. do
  69. case "$input " in
  70. # TARGET: dependencies
  71. # TARGET1 TARGET2 TARGET3: dependencies
  72. ([[*?[:alnum:]$][^$TAB:=%]#:[^=]*)
  73. target=$(.make-expandVars ${input%%:*})
  74. TARGETS+=( ${(z)target} )
  75. ;;
  76. esac
  77. done
  78. }
  79. # Cache generated parsing for 1sec per session or globally if configured, per Makefile path.
  80. if [[ -n "${FAST_HIGHLIGHT[chroma-make-cache-global]}" ]]; then
  81. # Determine Makefile path.
  82. # TODO: find a way to expand path and resolve alias - this cause deduplicated cache file.
  83. local makefile_path
  84. makefile_path=${FAST_HIGHLIGHT[chroma-make-custom-dir]%/}/${FAST_HIGHLIGHT[chroma-make-custom-file]}
  85. # If not absolute, resolve to absolute path
  86. [[ $makefile_path != /* ]] && makefile_path="$PWD/$makefile_path"
  87. # Generate a safe hash for the cache file name using sha1sum
  88. local makefile_hash
  89. makefile_hash=$(print -n -- "$makefile_path" | sha1sum | awk '{print $1}')
  90. # Generate a sha1sum hash from the input variable
  91. local input var val target dep TAB=$'\t' tmp IFS=
  92. local input_hash
  93. input_hash=$(print -n -- "$input" | sha1sum | awk '{print $1}')
  94. # Generate the cache file path.
  95. local cache_file
  96. cache_file="/tmp/fast-highlight-make-cache-${makefile_hash}-${input_hash}"
  97. if [[ ! -f $cache_file ]]; then
  98. # Clean up old cache files.
  99. rm -rf /tmp/fast-highlight-make-cache-${makefile_hash}-*
  100. # Generate new cache file.
  101. .make-parseMakefile "$input"
  102. print -r -- "${(j:;:)TARGETS}" >| "$cache_file"
  103. fi
  104. FAST_HIGHLIGHT[chroma-make-cache]="$(<$cache_file)"
  105. FAST_HIGHLIGHT[chroma-make-cache-born-at]="0"
  106. else
  107. if [[ -z "${FAST_HIGHLIGHT[chroma-make-cache]}" || $(( EPOCHSECONDS - FAST_HIGHLIGHT[chroma-make-cache-born-at] )) -gt 7 ]]; then
  108. .make-parseMakefile
  109. FAST_HIGHLIGHT[chroma-make-cache-born-at]="$EPOCHSECONDS"
  110. FAST_HIGHLIGHT[chroma-make-cache]="${(j:;:)TARGETS}"
  111. fi
  112. fi
  113. reply2=( "${(s:;:)FAST_HIGHLIGHT[chroma-make-cache]}" )
  114. # vim:ft=zsh:et