forked from skyrim-multiplayer/skymp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yarn.cmake
73 lines (64 loc) · 2.26 KB
/
yarn.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function(yarn_execute_command)
cmake_parse_arguments(A "" "WORKING_DIRECTORY;OUTPUT_VARIABLE;RESULT_VARIABLE" "COMMAND" ${ARGN})
foreach(arg WORKING_DIRECTORY COMMAND)
if("${A_${arg}}" STREQUAL "")
message(FATAL_ERROR "Missing ${arg} argument")
endif()
endforeach()
# https://github.com/skyrim-multiplayer/skymp/issues/55
if(NOT IS_ABSOLUTE "${A_WORKING_DIRECTORY}")
message(FATAL_ERROR "Expected WORKING_DIRECTORY to be an absolute path, but got: ${A_WORKING_DIRECTORY}")
endif()
if(WIN32)
set(temp_bat "${CMAKE_CURRENT_BINARY_DIR}/temp.bat")
set(yarn_cmd ${temp_bat})
set(yarn_arg "")
set(str "yarn")
foreach(arg ${A_COMMAND})
string(APPEND str " ${arg}")
endforeach()
file(WRITE ${temp_bat} ${str})
else()
set(yarn_cmd "yarn")
set(yarn_arg ${A_COMMAND})
endif()
execute_process(COMMAND ${yarn_cmd} ${yarn_arg}
WORKING_DIRECTORY ${A_WORKING_DIRECTORY}
RESULT_VARIABLE yarn_result
OUTPUT_VARIABLE yarn_output
# TODO: ERROR_VARIABLE
)
if("${A_RESULT_VARIABLE}" STREQUAL "")
if(NOT "${yarn_result}" STREQUAL "0")
message(FATAL_ERROR "yarn ${A_COMMAND} exited with ${yarn_result}:\n${yarn_output}")
endif()
else()
set("${A_RESULT_VARIABLE}" "${yarn_result}" PARENT_SCOPE)
endif()
set("${A_OUTPUT_VARIABLE}" "${yarn_output}" PARENT_SCOPE)
endfunction()
function(yarn_set_script)
cmake_parse_arguments(A "" "WORKING_DIRECTORY;NAME;RESULT_VARIABLE;OUTPUT_VARIABLE" "SCRIPT" ${ARGN})
foreach(arg WORKING_DIRECTORY NAME SCRIPT RESULT_VARIABLE OUTPUT_VARIABLE)
if ("${A_${arg}}" STREQUAL "")
message(FATAL_ERROR "Missing ${arg} argument")
endif()
endforeach()
foreach(arg ${A_SCRIPT})
string(APPEND script "${arg} ")
endforeach()
set(js_src "
var fs = require('fs');
var packageJson = JSON.parse(fs.readFileSync('package.json'));
packageJson.scripts['${A_NAME}'] = '${script}';
fs.writeFileSync('package.json', JSON.stringify(packageJson, null, 2));
")
execute_process(COMMAND node -p "${js_src}"
WORKING_DIRECTORY ${A_WORKING_DIRECTORY}
RESULT_VARIABLE res
OUTPUT_VARIABLE out
# TODO: ERROR_VARIABLE
)
set("${A_RESULT_VARIABLE}" "${res}" PARENT_SCOPE)
set("${A_OUTPUT_VARIABLE}" "${out}" PARENT_SCOPE)
endfunction()