5 # Find stylua executable.
10 # This module defines the following variables:
14 # ``STYLUA_EXECUTABLE``
21 # .. command:: add_stylua
25 # add_stylua(target [VERBOSE]
26 # [WORKING_DIRECTORY dir] [COMMENT comment] [CONFIG cfg]
27 # [OPTIONS opt ...] [SOURCES src ...] [src ...]
30 include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
31 find_program(STYLUA_EXECUTABLE NAMES stylua)
32 mark_as_advanced(STYLUA_EXECUTABLE)
34 if(EXISTS "${STYLUA_EXECUTABLE}")
35 execute_process(COMMAND ${STYLUA_EXECUTABLE} --version OUTPUT_VARIABLE STYLUA_VERSION)
36 string(REGEX REPLACE "stylua ([^\n ]+).*" "\\1" STYLUA_VERSION "${STYLUA_VERSION}")
39 find_package_handle_standard_args(StyLua
49 function(add_stylua target)
50 cmake_parse_arguments(STYLUA "VERBOSE" "WORKING_DIRECTORY;COMMENT;CONFIG" "OPTIONS;SOURCES" ${ARGN})
51 list(APPEND STYLUA_SOURCES ${STYLUA_UNPARSED_ARGUMENTS})
53 list(INSERT STYLUA_OPTIONS 0 --verbose)
55 if(EXISTS "${STYLUA_CONFIG}")
56 list(APPEND STYLUA_OPTIONS --config-path ${STYLUA_CONFIG})
58 if(NOT STYLUA_WORKING_DIRECTORY)
59 set(STYLUA_WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
61 if(NOT STYLUA_COMMENT)
62 set(STYLUA_COMMENT "Formatting ${target} using stylua")
64 add_custom_target(${target} # https://github.com/JohnnyMorganz/StyLua
65 ${STYLUA_EXECUTABLE} ${STYLUA_OPTIONS} -- ${STYLUA_SOURCES}
66 WORKING_DIRECTORY ${STYLUA_WORKING_DIRECTORY}
67 COMMENT "${STYLUA_COMMENT}"