Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
NPMInstall.cmake 4.82 KiB
# - Download and install locally the library using the npm package tool
#  npm_install(project.json PUBLIC public_dir SOURCE source_dir DESTINATION install_dir)
# execute npm build in and install the files in the install_dir
# public_dir is the directory where the static files are stored
# source_dir is the directory where the javascript/css files are stored

# Copyright (C) 2008-2009
# ASTRON (Netherlands Foundation for Research in Astronomy)
# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, softwaresupport@astron.nl
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# $Id: PythonInstall.cmake 32905 2015-11-17 15:31:54Z schaap $

find_package(NPM)

#
# function npm_install
#
function(npm_install NPM_PACKAGE_SPECIFICATION)
	# Precondition check.
    if(NOT NPM_EXECUTABLE)
        message(FATAL_ERROR "npm_install: cannot find npm")
    endif(NOT NPM_EXECUTABLE)
    
    # Parsing arguments
    set(one_value_arguments PUBLIC SOURCE DESTINATION)
    ## syntax is cmake_parse_arguments(prefix options one_value_arguments multi_value_arguments arguments_to_be_parsed)
	cmake_parse_arguments(NPM_INSTALL "" "${one_value_arguments}" "" ${ARGN} ) 
	
	
	# Checks if all the required arguments have been specified
	if(NOT NPM_INSTALL_PUBLIC)
		message(FATAL_ERROR "specify public directory.\n ex npm_install(project.json PUBLIC public_dir SOURCE source_dir DESTINATION install_dir)")
	endif(NOT NPM_INSTALL_PUBLIC)
	
	if(NOT NPM_INSTALL_SOURCE)
		message(FATAL_ERROR "specify source directory.\n ex npm_install(project.json PUBLIC public_dir SOURCE source_dir DESTINATION install_dir)")
	endif(NOT NPM_INSTALL_SOURCE)
	
	
	# Compute the full path to the source, and the public directory, and the json spec
	
	get_filename_component(WEBSITE_PUBLIC_DIR "${NPM_INSTALL_PUBLIC}" REALPATH)
	get_filename_component(WEBSITE_SOURCE_DIR "${NPM_INSTALL_SOURCE}" REALPATH)
	get_filename_component(JSON_PACKAGE_SPECIFICATION "${NPM_PACKAGE_SPECIFICATION}" REALPATH)
	
	# Checks if the directories public and source are actually present in the disk
	if(EXISTS WEBSITE_PUBLIC_DIR)
		message(FATAL_ERROR "public directory \"${NPM_INSTALL_PUBLIC}\" cannot be found.")
	endif(EXISTS WEBSITE_PUBLIC_DIR)
	
	if(EXISTS WEBSITE_SOURCE_DIR)
		message(FATAL_ERROR "source directory \"${NPM_INSTALL_SOURCE}\" cannot be found.")
	endif(EXISTS WEBSITE_SOURCE_DIR)
	
	get_filename_component(NPM_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}" REALPATH)
	
	add_custom_target(copy_src_public ALL SOURCES "public" "src" COMMENT "Copying src and public directory into build directory")
	add_custom_target(download_npm_dependencies ALL SOURCES "node_modules" "package-lock.json" DEPENDS copy_src_public package_json COMMENT "Attempt downloading of required npm dependencies")
	add_custom_target(packing_javascript_files ALL SOURCES build COMMENT "Attempt packing of javascript files")
	add_custom_target(start_development_server_${PACKAGE_NAME} SOURCES "node_modules" "package-lock.json" COMMENT "Start start_development_server for ${PACKAGE_NAME}")
	
	add_custom_command(
	OUTPUT package_json
 	COMMAND ${CMAKE_COMMAND} -E copy_if_different
      ${JSON_PACKAGE_SPECIFICATION} ${NPM_BINARY_DIR}/package.json
	COMMENT "Copying package.json")
	
	add_custom_command(
	OUTPUT "src"
	COMMAND ${CMAKE_COMMAND} -E copy_directory
      ${WEBSITE_SOURCE_DIR} ${NPM_BINARY_DIR}/src
	COMMENT "Copying javascript src directory")
	
	add_custom_command(
	OUTPUT "public"
	COMMAND ${CMAKE_COMMAND} -E copy_directory
      ${WEBSITE_PUBLIC_DIR} ${NPM_BINARY_DIR}/public
	COMMENT "Copying public directory")
	
	add_custom_command(
	OUTPUT "node_modules" "package-lock.json"
	COMMAND npm install
	DEPENDS "package.json"
	WORKING_DIRECTORY ${NPM_BINARY_DIR}
	COMMENT "Downloading npm dependencies for ${NPM_BINARY_DIR}/package.json")
	
	add_custom_command(
	TARGET start_development_server_${PACKAGE_NAME}
	COMMAND npm start
	WORKING_DIRECTORY ${NPM_BINARY_DIR}
	COMMENT "Starting development server")
	
	add_custom_command(
	OUTPUT "build"
	COMMAND npm run build
	DEPENDS copy_src_public download_npm_dependencies
	COMMENT "Packing javascript files for deploy")
	
	message(${NPM_INSTALL_DESTINATION})
	
	install(DIRECTORY ${NPM_BINARY_DIR}/build/ DESTINATION ${CMAKE_INSTALL_PREFIX}/${NPM_INSTALL_DESTINATION})

endfunction(npm_install)