Merge pull request #37561 from tchaikov/wip-cmake-node-mirror

cmake: support Node.js and npm registry mirror

Reviewed-by: Brad Hubbard <bhubbard@redhat.com>
Reviewed-by: Tiago Melo <tmelo@suse.com>
This commit is contained in:
Kefu Chai 2020-10-08 14:50:30 +08:00 committed by GitHub
commit 718d9c5997
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,6 +17,25 @@ function(add_npm_command)
COMMENT ${NC_COMMENT})
endfunction(add_npm_command)
function(add_npm_options)
set(commands)
cmake_parse_arguments(NC "" "NODEENV_DIR;TARGET" "OPTION" ${ARGN})
foreach(opt ${NC_OPTION})
string(REPLACE "=" ";" opt ${opt})
list(GET opt 0 key)
list(GET opt 1 value)
list(APPEND commands
COMMAND
. ${NC_NODEENV_DIR}/bin/activate &&
npm config set ${key} ${value} --userconfig ${NC_NODEENV_DIR}/.npmrc &&
deactivate)
endforeach()
add_custom_target(${NC_TARGET}
${commands}
DEPENDS ${NC_NODEENV_DIR}/bin/npm
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
endfunction(add_npm_options)
if(WITH_SYSTEM_NPM)
set(mgr-dashboard-nodeenv-dir )
set(nodeenv "")
@ -24,32 +43,36 @@ if(WITH_SYSTEM_NPM)
DEPENDS frontend/node_modules
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/pybind/mgr/dashboard/frontend
)
else()
else(WITH_SYSTEM_NPM)
set(mgr-dashboard-nodeenv-dir ${CMAKE_CURRENT_BINARY_DIR}/node-env)
set(nodeenv NODEENV)
set(mgr-dashboard-userconfig --userconfig ${mgr-dashboard-nodeenv-dir}/.npmrc)
set(virt-activate . ${mgr-dashboard-nodeenv-dir}/bin/activate)
if(DEFINED ENV{NODE_MIRROR})
set(node_mirror_opt "--mirror=$ENV{NODE_MIRROR}")
endif()
add_custom_command(
OUTPUT "${mgr-dashboard-nodeenv-dir}/bin/npm"
COMMAND ${CMAKE_SOURCE_DIR}/src/tools/setup-virtualenv.sh --python=${MGR_PYTHON_EXECUTABLE} ${mgr-dashboard-nodeenv-dir}
COMMAND ${mgr-dashboard-nodeenv-dir}/bin/pip install nodeenv
COMMAND ${mgr-dashboard-nodeenv-dir}/bin/nodeenv -p --node=12.18.2
COMMAND ${mgr-dashboard-nodeenv-dir}/bin/nodeenv ${node_mirror_opt} -p --node=12.18.2
COMMAND mkdir ${mgr-dashboard-nodeenv-dir}/.npm
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "dashboard nodeenv is being installed"
)
add_custom_target(mgr-dashboard-nodeenv
COMMAND ${virt-activate} && npm config set python ${MGR_PYTHON_EXECUTABLE} ${mgr-dashboard-userconfig} && deactivate
COMMAND ${virt-activate} && npm config set cache ${mgr-dashboard-nodeenv-dir}/.npm ${mgr-dashboard-userconfig} && deactivate
DEPENDS ${mgr-dashboard-nodeenv-dir}/bin/npm
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
)
if(DEFINED ENV{NPM_REGISTRY})
set(npm_registry_opts "OPTION" "registry=$ENV{NPM_REGISTRY}")
endif()
add_npm_options(
NODEENV_DIR ${mgr-dashboard-nodeenv-dir}
TARGET mgr-dashboard-nodeenv
OPTION python=${MGR_PYTHON_EXECUTABLE}
OPTION cache=${mgr-dashboard-nodeenv-dir}/.npm
${npm_registry_opts})
add_custom_target(mgr-dashboard-frontend-deps
DEPENDS frontend/node_modules mgr-dashboard-nodeenv
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/pybind/mgr/dashboard/frontend
)
endif()
endif(WITH_SYSTEM_NPM)
add_npm_command(
OUTPUT "${CMAKE_SOURCE_DIR}/src/pybind/mgr/dashboard/frontend/node_modules"