Skip to content

Commit

Permalink
Move outside the scripts for package managers in metacallcli, trying …
Browse files Browse the repository at this point in the history
…to make more portable the cli.
  • Loading branch information
viferga committed Jan 22, 2020
1 parent c334a73 commit 35e05e0
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 226 deletions.
44 changes: 40 additions & 4 deletions source/examples/metacallcli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ add_executable(${target}
# Create namespaced alias
add_executable(${META_PROJECT_NAME}::${target} ALIAS ${target})

# Configure package manager path
if(MSVC)
set(PROJECT_METACALL_PACK_MAN_DIR "${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}")
else()
set(PROJECT_METACALL_PACK_MAN_DIR "${CMAKE_BINARY_DIR}")
endif()

# Copy package manager scripts
add_custom_target(${target}-scripts ALL
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/package_manager/pip.py ${PROJECT_METACALL_PACK_MAN_DIR}
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/package_manager/npm.js ${PROJECT_METACALL_PACK_MAN_DIR}
)

# Set dependency to metacallcli
add_dependencies(${target} ${target}-scripts)

#
# Project options
#
Expand Down Expand Up @@ -121,15 +138,22 @@ install(TARGETS ${target}
BUNDLE DESTINATION ${INSTALL_BIN} COMPONENT examples
)

# Scripts (Package Managers)
install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/package_manager/pip.py
${CMAKE_CURRENT_SOURCE_DIR}/package_manager/npm.js
DESTINATION ${INSTALL_LIB}
COMPONENT runtime
)

#
# Define test
#

set(TEST_COMMAND_LINE_ARGS "help") # TODO: Forward all commands, create an adaptor to existing system
#set(TEST_COMMAND_LINE_ARGS "help") # TODO: Forward all commands, create an adaptor to existing system
set(TEST_COMMAND_LINE_ARGS "load mock test.mock")

# TODO: NODE_PATH=`npm root --quiet -g`
#set(TEST_COMMAND_LINE_ARGS "install node rambda")
#set(TEST_COMMAND_LINE_ARGS "install py numpy")
#set(TEST_COMMAND_LINE_ARGS "install node rambda") # TODO: This segfaults during ctest but not when running it normally

if(WIN32)
set(TEST_COMMAND cmd /c)
Expand All @@ -152,7 +176,19 @@ set_property(TEST ${target}

include(TestEnvironmentVariables)

if(NOT OPTION_BUILD_GUIX)
# Get node path for npm
execute_process(
COMMAND npm root --quiet -g
OUTPUT_VARIABLE NODE_PATH
)
set(NODE_PATH "NODE_PATH=${NODE_PATH}")
else()
set(NODE_PATH "NODE_PATH=$ENV{NODE_PATH}")
endif()

test_environment_variables(${target}
${TEST_COMMAND_LINE_ARGS}
${TESTS_ENVIRONMENT_VARIABLES}
${NODE_PATH}
)
31 changes: 25 additions & 6 deletions source/examples/metacallcli/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@

#include <clocale>

/* Package Managers */
#include "package_manager/pip.hpp"
#include "package_manager/npm.hpp"

/* -- Namespace Declarations -- */

using namespace metacallcli;
Expand Down Expand Up @@ -342,8 +338,31 @@ void application::parameter_iterator::evaluate()
/* List of scripts that run pip/npm/gem */
static std::unordered_map<std::string, std::string> install_scripts =
{
{ "py", package_manager::pip },
{ "node", package_manager::npm }
{
"py",

"#!/usr/bin/env python3\n"
"\n"
"try:\n"
" from pip import main as pipmain\n"
"except ImportError:\n"
" from pip._internal import main as pipmain\n"
"\n"
"def package_manager(args):\n"
" return pipmain(args);\n"
},
{
"node",

"const path = require('path');\n"
"let npm = { package_manager: function (args) { console.log('NPM could not be found, please set up LOADER_LIBRARY_PATH enviroment variable,'); } };\n"
"try {\n"
" npm = require(path.join(process.env['LOADER_LIBRARY_PATH'], 'npm.js'));\n"
"} catch (e) {\n"
" console.log(e);\n"
"}\n"
"module.exports = npm;\n"
}
};

/* List of available commands when installing */
Expand Down
179 changes: 0 additions & 179 deletions source/examples/metacallcli/package_manager/npm.hpp

This file was deleted.

10 changes: 7 additions & 3 deletions source/examples/metacallcli/package_manager/npm.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* #!/usr/bin/env node */ // Shebang fails when inlining the source in a C++ header and installing with Guix (the shebang cannot be patched properly)
#!/usr/bin/env node

/* This has been ripped off from NPM and adapted to be callable instead of invoked by exec */

function package_manager(args) {
// windows: running "npm blah" in this folder will invoke WSH, not node.
/* global WScript */
// global WScript
if (typeof WScript !== 'undefined') {
WScript.echo(
'npm does not work when run\n' +
Expand Down Expand Up @@ -37,7 +37,11 @@ function package_manager(args) {
var nopt = require('npm/node_modules/nopt')

// Overwrite process args
process.argv = [ 'node', 'npm', ...args ];
process.argv = [
process.env['NODE_EXE_PATH'] || 'node',
process.env['NODE_PATH'] ? path.join(process.env['NODE_PATH'], 'npm', 'bin', 'npm-cli.js') : 'npm',
...args,
];

log.verbose('cli', process.argv)

Expand Down
33 changes: 0 additions & 33 deletions source/examples/metacallcli/package_manager/pip.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion source/ports/node_port/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 35e05e0

Please sign in to comment.