forked from scantailor/scantailor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatchFile.cmake
38 lines (32 loc) · 1.49 KB
/
PatchFile.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Applies a list of search / replace expressions to a file.
# If the file is modified, saves a backup with .orig name
# appended to the original file name. If a backup already
# exists, it's not overwritten.
# Example usage:
# PATCH_FILE("C:/test.txt" "/apple/orange/" "/@([A-Z_]+)@/@\\1_OLD@")
FUNCTION(PATCH_FILE file_)
MESSAGE(STATUS "Patching file ${file_}")
FILE(READ "${file_}" contents_)
SET(orig_contents "${contents_}")
FOREACH(search_replace_ ${ARGN})
IF(search_replace_ STREQUAL "")
MESSAGE(FATAL_ERROR "Empty string passed as a search/replace expression to PATCH_FILE()")
ENDIF()
# Parse the string of type /search/replace/ into search_ and _replace variables.
# Note that any symbol missing from both search and replace can be used instead of /.
STRING(SUBSTRING "${search_replace_}" 0 1 sep_)
STRING(REGEX REPLACE "^${sep_}([^${sep_}]+)${sep_}.*" "\\1" search_ "${search_replace_}")
STRING(REGEX REPLACE "^${sep_}[^${sep_}]+${sep_}([^${sep_}]*)${sep_}\$" "\\1" replace_ "${search_replace_}")
IF(search_replace_ STREQUAL "${replace_}")
MESSAGE(FATAL_ERROR "Invalid search/replace expression passed to PATCH_FILE()")
ENDIF()
STRING(REGEX REPLACE "${search_}" "${replace_}" contents_ "${contents_}")
ENDFOREACH()
IF(NOT "${contents_}" STREQUAL "${orig_contents_}")
# Make a backup copy, if not already there.
IF(NOT EXISTS "${file_}.orig")
CONFIGURE_FILE("${file_}" "${file_}.orig" COPYONLY)
ENDIF()
FILE(WRITE "${file_}" "${contents_}")
ENDIF()
ENDFUNCTION()