Skip to content

Commit

Permalink
JS dependencies auto-install in packaging scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Bernard committed Sep 18, 2024
1 parent bf4417f commit c1d9e72
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 16 deletions.
18 changes: 18 additions & 0 deletions cmake/FindNpm.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
######################################################
# - Try to find npm command
# Once done this will define
# NPM_FOUND - System has npm
# NPM_BINARY - Path to the npm binary file

######################################################
find_program(NPM_BINARY npm)

######################################################
include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set NPM_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(Npm "Cannot find NPM. This is required beause you took the git master branch. Download official tar.gz or disable webview."
NPM_BINARY)

######################################################
mark_as_advanced(NPM_BINARY)
11 changes: 10 additions & 1 deletion dev/gen-archive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,14 @@ fi
######################################################
echo "Generate ${prefix}.tar.gz..."
set -e
git archive --format=tar --prefix=${prefix}/ HEAD | bzip2 > ./${prefix}.tar.bz2
git archive --format=tar --prefix=${prefix}/ HEAD | bzip2 > /tmp/${prefix}.tar.bz2
pushd /tmp
tar -xf ${prefix}.tar.bz2
pushd ${prefix}/src/webview
./prepare.sh
popd
tar -cjf ${prefix}.tar.bz2 ${prefix}
popd
mv /tmp/${prefix}.tar.bz2 ./
rm -rfvd /tmp/${prefix}
echo "Finished"
38 changes: 23 additions & 15 deletions src/webview/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@
######################################################

######################################################
# Use npm to install project dependencies
message(STATUS "Installing project dependencies using npm...")
execute_process(
COMMAND npm ci
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/webview
RESULT_VARIABLE npm_cleaninstall_result
)

if(NOT npm_cleaninstall_result EQUAL 0)
message(FATAL_ERROR "Failed to install dependencies via npm.")
else()
message(STATUS "Dependencies installed successfully with npm.")
# Check for npm if needed
if (NOT IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/node_modules)
find_package(Npm REQUIRED)
endif()

######################################################
# Install the webview into share directory
INSTALL(DIRECTORY . DESTINATION share/malt/webview
PATTERN client_v2/node_modules EXCLUDE)
# Install nodejs dependencies if needed
# then install node_modules into share directory
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/node_modules)
message(STATUS "Copying package.json and package-lock.json to build directory")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/package.json ${CMAKE_CURRENT_BINARY_DIR}/package.json COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/package-lock.json ${CMAKE_CURRENT_BINARY_DIR}/package-lock.json COPYONLY)
add_custom_command (OUTPUT deps-loaded
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/prepare.sh ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Fetching webview javascript dependencies..."
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/prepare.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_custom_target(run ALL DEPENDS deps-loaded package.json package-lock.json)
INSTALL(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/node_modules DESTINATION share/malt/webview)
else (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/node_modules)
INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/node_modules DESTINATION share/malt/webview)
endif (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/node_modules)

#######################################################
## Install the webview into share directory
INSTALL(DIRECTORY . DESTINATION share/malt/webview)
54 changes: 54 additions & 0 deletions src/webview/prepare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash
######################################################
# PROJECT : MALT #
# VERSION : 1.2.2 #
# DATE : 06/2023 #
# AUTHOR : Valat Sébastien #
# LICENSE : CeCILL-C #
######################################################

######################################################
export PATH=./node_modules/.bin/:$PATH
SOURCE_DIR=$PWD

######################################################
if [ ! -z "$1" ]
then
cp bower.json "$1"
cd "$1"
fi

######################################################
if ! which npm
then
echo "Nodejs 'npm' command required to fetch the webview client dependencies, please install 'nodejs' before proceeding or download a release archive instead of using master branch"
exit 1
fi

######################################################
#Workaround on debian:jessie and ubuntu:16.X which rename
#node executable as nodejs which break bower
if ! which node
then
if which nodejs
then
echo "Create NodeJS -> Node fix for ubuntu/debian !"
mkdir -p ./node_modules/.bin/
ln -s $(which nodejs) ./node_modules/.bin/node
else
echo "You should install NodeJS to fetch web GUI components"
echo "Or download a release archive as it already contains all those files"
exit 1
fi
fi

######################################################
set -e
set -x
npm ci
rm -f ./node_modules/.bin/node
set +x
if [ ! -z "$1" ]; then
echo > deps-loaded
fi

0 comments on commit c1d9e72

Please sign in to comment.