Skip to content

Commit

Permalink
add --no-init & --rclnodejs-version cli flags (#14)
Browse files Browse the repository at this point in the history
* add --no-init & --rclnodejs-version cli flags

* improved ros version specific tests

* set 775 permission on run_ros_version.sh

* Updated docs to with new create-package cli flags.
  • Loading branch information
wayneparrott authored Apr 16, 2021
1 parent ffb7f08 commit 931fbeb
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 26 deletions.
10 changes: 6 additions & 4 deletions package-creation-tool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Alternatively using npx:
npx rclnodejs-cli create-package <package_name>
npx rclnodejs-cli create-package <package_name> --typescript
```
Be aware that when using `npx` to run `rclnodejs-cli`, this package includes a dependency on the rclnodejs package which has a lengthy install postinstall step. Thus if you frequently use `rclnodejs-cli` you may benefit from more responsiveness by installing this package globally.

View the `create-package` options:
```
Expand All @@ -34,18 +35,19 @@ or
| | | (__| | | | | (_) | (_| | __/| \__ \
|_| \___|_|_| |_|\___/ \__,_|\___|/ |___/
|__/
Usage: rclnodejs-cli create-package <package_name> [options...]
Usage: rclnodejs create-package <package_name> [options...]
Create a ROS2 package for Nodejs development.
Options:
--description <description> The description given in the package.xml
--destination-directory <directory_path> Directory where to create the package directory
--license <license> The license attached to this package
--maintainer-email <email> email address of the maintainer of this package
--maintainer-name <name> name of the maintainer of this package
--typescript Configure as a TypeScript Node.js project
--maintainer-email <email> Email address of the maintainer of this package
--maintainer-name <name> Name of the maintainer of this package
--no-init Do not run "npm init"
--rclnodejs-version <x.y.z> The version of rclnodejs to use
--typescript Configure as a TypeScript Node.js project
--dependencies <ros_packages...> list of ROS dependencies
-h, --help display help for command
```
Expand Down
18 changes: 11 additions & 7 deletions package-creation-tool/lib/package_creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ class PkgCreator {
createPkgCmd
.usage('<package_name> [options...]')
.description('Create a ROS2 package for Nodejs development.')
// .option('--rclnodejs-version <x.y.z>', 'The version of rclnodejs to use')
.option('--description <description>', 'The description given in the package.xml')
.option('--destination-directory <directory_path>', 'Directory where to create the package directory')
.option('--license <license>', 'The license attached to this package')
.option('--maintainer-email <email>', 'email address of the maintainer of this package')
.option('--maintainer-name <name>', 'name of the maintainer of this package')
.option('--typescript', 'Configure as a TypeScript Node.js project')
.option('--maintainer-email <email>', 'Email address of the maintainer of this package')
.option('--maintainer-name <name>', 'Name of the maintainer of this package')
.option('--no-init', 'Do not run "npm init"')
.option('--rclnodejs-version <x.y.z>', 'The version of rclnodejs to use')
.option('--typescript', 'Configure as a TypeScript Node.js project')
.option('--dependencies <ros_packages...>', 'list of ROS dependencies')
.action((packageName, options) => {
this.createPackage(packageName, options);
Expand All @@ -35,9 +35,9 @@ class PkgCreator {
let cmd = path.join(__dirname, '..', 'scripts', script);
cmd += ` ${packageName}`;

// if (options.rclnodejsVersion) {
// cmd += ` --rclnodejs-version "${options.rclnodejsVersion}"`;
// }
if (options.rclnodejsVersion) {
cmd += ` --rclnodejs-version "${options.rclnodejsVersion}"`;
}

if (options.description) {
cmd += ` --description "${options.description}"`;
Expand All @@ -59,6 +59,10 @@ class PkgCreator {
cmd += ` --maintainer-name "${options.maintainerName}"`;
}

if (options.noInit) {
cmd += ' --no-init';
}

if (options.typescript) {
cmd += ' --typescript';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ def add_arguments(self, parser, cli_name):
parser.add_argument(
'package_name',
help='The package name')
# parser.add_argument(
# '--rclnodejs-version',
# default='latest',
# help='The version of the rclnodejs client library to include')
parser.add_argument(
'--description',
default='TODO: Package description',
Expand All @@ -65,19 +61,27 @@ def add_arguments(self, parser, cli_name):
help='list of dependencies')
parser.add_argument(
'--maintainer-email',
help='email address of the maintainer of this package'),
help='Email address of the maintainer of this package'),
parser.add_argument(
'--maintainer-name',
default=getpass.getuser(),
help='name of the maintainer of this package'),
help='Name of the maintainer of this package'),
parser.add_argument(
'--template_location',
'--no-init',
action='store_true',
default=False,
help='Do not run \'npm init\' on newly created package')
parser.add_argument(
'--template-location',
help='The path to templates directory')
parser.add_argument(
'--typescript',
action='store_true',
default=False,
help='Configure as a TypeScript Node.js project')
parser.add_argument(
'--rclnodejs-version',
help='Version of rclnodejs client library to use, x.y.z format')

def main(self, *, args):
# run 'ros2 pkg create <package_name>'
Expand Down Expand Up @@ -169,8 +173,9 @@ def main(self, *, args):
os.path.join(template_dir_path, 'CMakeLists.install.em'),
os.path.join(cwd, CMAKELISTS_FILENAME))

# install all node.js dependencies in package.json
_run_npm_install()
if not args.no_init:
# install all node.js dependencies in package.json
_run_npm_install(args)

print('Node.js setup complete.')
return SUCCESS_RETURN
Expand Down Expand Up @@ -264,8 +269,8 @@ def _expand_template(template_file, data, output_file):
with open(output_file, 'w') as h:
h.write(content)

def _run_npm_install():
npm = shutil.which('npm');
def _run_npm_install(args):
npm = shutil.which('npm')
if not npm:
npm = shutil.which('yarn')
if not npm:
Expand All @@ -279,5 +284,9 @@ def _run_npm_install():

print(f' Running \'{os.path.basename(npm)} install\' command.')
subprocess.run([npm, 'install'])
return subprocess.run([npm, 'install', 'rclnodejs'])
rclnodejs_pkg = f"rclnodejs{'@' + args.rclnodejs_version if args.rclnodejs_version else ''}"
return subprocess.run([npm, 'install', rclnodejs_pkg])




5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "rclnodejs-cli",
"version": "0.1.1",
"version": "0.1.2",
"description": "Commandline tools for the ROS2 rclnodejs client library",
"main": "index.js",
"bin": "./index.js",
"directories": {
"test": "test"
},
"scripts": {
"postinstall": "colcon build --base-paths package-creation-tool && node scripts/set_script_permissions.js",
"postinstall": "npm run build-package-creation-tool && node scripts/set_script_permissions.js",
"build-package-creation-tool": "colcon build --base-paths package-creation-tool",
"test": "mocha",
"lint": "eslint --max-warnings=0 --ext js index.js src message-generator-tool package-creation-tool test"
},
Expand Down
2 changes: 2 additions & 0 deletions test/run_ros_version.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo off
install\local_setup.bat && echo on && echo %ROS_DISTRO%
3 changes: 3 additions & 0 deletions test/run_ros_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
source install/local_setup.sh && \
echo $ROS_DISTRO
100 changes: 99 additions & 1 deletion test/test-create-package-cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function rmdir(dir) {
}
}

function verifyPackage(pkgPath, typescript) {
function verifyPackage(pkgPath, typescript = false, noInit = false, rclnodejsVersion = undefined) {
assert.ok(fs.existsSync(pkgPath));
assert.ok(fs.existsSync(path.join(pkgPath, 'package.xml')));
assert.ok(fs.existsSync(path.join(pkgPath, 'package.json')));
Expand All @@ -29,12 +29,52 @@ function verifyPackage(pkgPath, typescript) {
} else {
assert.ok(fs.existsSync(path.join(pkgPath, 'src', 'index.js')));
}

const nodeModulesDirExists = fs.existsSync(path.join(pkgPath, 'node_modules'));
assert.ok(noInit ? !nodeModulesDirExists : nodeModulesDirExists);

if (!noInit && rclnodejsVersion) {
// load package.json and find the version number of the rclnodejs dependency
// eslint-disable-next-line global-require, import/no-dynamic-require
const pkg = require(path.join(pkgPath, 'package.json'));
assert.ok(
pkg
&& pkg.dependencies
&& pkg.dependencies.rclnodejs
&& pkg.dependencies.rclnodejs.endsWith(rclnodejsVersion),
);
}
}

function scriptExt() {
return process.platform === 'win32' ? '.bat' : '.sh';
}

function rosDistroName() {
const cwd = process.cwd();

let script = `run_ros_version${scriptExt()}`;
script = path.join(cwd, 'test', script);
const stdoutBuf = childProcess.execSync(script, [], {
cwd,
});

const lines = stdoutBuf.toString().match(/[^\r\n]+/g);
if (lines && lines.length > 0) {
return lines[lines.length - 1];
}

return undefined;
}

function rclnodejsVersionForRosDistro() {
const distroName = rosDistroName();
if (distroName === 'foxy') return '0.18.1';
if (distroName === 'eloquent') return '0.18.1';
if (distroName === 'dashing') return '0.10.3';
return undefined;
}

describe('rclnodejs-cli package-creation-tool', function () {
this.timeout(0);

Expand Down Expand Up @@ -101,6 +141,38 @@ describe('rclnodejs-cli package-creation-tool', function () {
done();
});

it('ros2cli-extension create javascript package --rclnodejs-version', (done) => {
const version = rclnodejsVersionForRosDistro();
if (!version) {
console.warn('Unable to run test \'ros2cli-extension create javascript package --rclnodejs-version\' Could not recognized ROS distro name.');
done();
}

const cwd = process.cwd();

let script = `run_ros2cli_pkg_create_nodejs${scriptExt()}`;
script = path.join(cwd, 'test', script);
childProcess.execSync(`${script} ${pkgName} --rclnodejs-version ${version}`);

const pkgPath = path.join(cwd, pkgName);
verifyPackage(pkgPath, false, false, version);

done();
});

it('ros2cli-extension create javascript package --no-init', (done) => {
const cwd = process.cwd();

let script = `run_ros2cli_pkg_create_nodejs${scriptExt()}`;
script = path.join(cwd, 'test', script);
childProcess.execSync(`${script} ${pkgName} --no-init`);

const pkgPath = path.join(cwd, pkgName);
verifyPackage(pkgPath, false, true);

done();
});

it('rclnodejs-cli create-package javascript', (done) => {
const cli = path.join(__dirname, '..', '..', 'rclnodejs-cli');
childProcess.execSync(`npx ${cli} create-package ${pkgName}`).toString();
Expand All @@ -120,4 +192,30 @@ describe('rclnodejs-cli package-creation-tool', function () {

done();
});

it('rclnodejs-cli create-package javascript --no-init', (done) => {
const cli = path.join(__dirname, '..', '..', 'rclnodejs-cli');
childProcess.execSync(`npx ${cli} create-package ${pkgName} --no-init`).toString();

const pkgPath = path.join(cli, pkgName);
verifyPackage(pkgPath, false, false);

done();
});

it('rclnodejs-cli create-package javascript --rclnodejs-version', (done) => {
const version = rclnodejsVersionForRosDistro();
if (!version) {
console.warn('Unable to run test \'rclnodejs-cli create-package javascript --rclnodejs-version\'. Could not recognized ROS distro name.');
done();
}

const cli = path.join(__dirname, '..', '..', 'rclnodejs-cli');
childProcess.execSync(`npx ${cli} create-package ${pkgName} --rclnodejs-version ${version}`).toString();

const pkgPath = path.join(cli, pkgName);
verifyPackage(pkgPath, false, false, version);

done();
});
});

0 comments on commit 931fbeb

Please sign in to comment.