-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Pkg and CI Template Creation #10
Open
Joschi3
wants to merge
3
commits into
master
Choose a base branch
from
feature/create
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
#!/usr/bin/env python3 | ||
from tuda_workspace_scripts.workspace import get_workspace_root, PackageChoicesCompleter | ||
|
||
import argcomplete | ||
import argparse | ||
import os | ||
from ament_index_python.packages import get_package_share_directory | ||
|
||
try: | ||
import git | ||
except ImportError: | ||
print("GitPython is required! Install using 'apt install python3-git'") | ||
raise | ||
|
||
try: | ||
import copier | ||
except ImportError: | ||
print( | ||
"Copier is required! Install using 'pip3 install copier --break-system-packages'" | ||
) | ||
raise | ||
|
||
|
||
def parseArguments() -> argparse.Namespace: | ||
|
||
parser = argparse.ArgumentParser( | ||
description="Creates a ROS 2 package from templates" | ||
) | ||
|
||
parser.add_argument( | ||
"--template", | ||
type=str, | ||
default=None, | ||
choices=["cpp_pkg", "msgs_pkg", "python_pkg", "ci"], | ||
help="Template", | ||
) | ||
parser.add_argument( | ||
"--destination", | ||
"-d", | ||
type=str, | ||
default=None, | ||
help="Destination directory (default: <workspace_root>/src/)", | ||
) | ||
parser.add_argument( | ||
"--defaults", action="store_true", help="Use defaults for all options" | ||
) | ||
|
||
parser.add_argument("--package-name", type=str, default=None, help="Package name") | ||
parser.add_argument("--description", type=str, default=None, help="Description") | ||
parser.add_argument("--maintainer", type=str, default=None, help="Maintainer") | ||
parser.add_argument( | ||
"--maintainer-email", type=str, default=None, help="Maintainer email" | ||
) | ||
parser.add_argument("--author", type=str, default=None, help="Author") | ||
parser.add_argument("--author-email", type=str, default=None, help="Author email") | ||
parser.add_argument( | ||
"--license", | ||
type=str, | ||
default=None, | ||
choices=[ | ||
"Apache-2.0", | ||
"BSL-1.0", | ||
"BSD-2.0", | ||
"BSD-2-Clause", | ||
"BSD-3-Clause", | ||
"GPL-3.0-only", | ||
"LGPL-2.1-only", | ||
"LGPL-3.0-only", | ||
"MIT", | ||
"MIT-0", | ||
], | ||
help="License", | ||
) | ||
parser.add_argument("--node-name", type=str, default=None, help="Node name") | ||
parser.add_argument( | ||
"--node-class-name", type=str, default=None, help="Class name of node" | ||
) | ||
parser.add_argument( | ||
"--is-component", action="store_true", default=None, help="Make it a component?" | ||
) | ||
parser.add_argument( | ||
"--is-lifecycle", | ||
action="store_true", | ||
default=None, | ||
help="Make it a lifecycle node?", | ||
) | ||
parser.add_argument( | ||
"--has-launch-file", | ||
action="store_true", | ||
default=None, | ||
help="Add a launch file?", | ||
) | ||
parser.add_argument( | ||
"--launch-file-type", | ||
type=str, | ||
choices=["xml", "py", "yml"], | ||
help="Type of launch file", | ||
) | ||
parser.add_argument( | ||
"--has-params", action="store_true", default=None, help="Add parameter loading" | ||
) | ||
parser.add_argument( | ||
"--has-subscriber", action="store_true", default=None, help="Add a subscriber?" | ||
) | ||
parser.add_argument( | ||
"--has-publisher", action="store_true", default=None, help="Add a publisher?" | ||
) | ||
parser.add_argument( | ||
"--has-service-server", | ||
action="store_true", | ||
default=None, | ||
help="Add a service server?", | ||
) | ||
parser.add_argument( | ||
"--has-action-server", | ||
action="store_true", | ||
default=None, | ||
help="Add an action server?", | ||
) | ||
parser.add_argument( | ||
"--has-timer", action="store_true", default=None, help="Add a timer callback?" | ||
) | ||
parser.add_argument( | ||
"--auto-shutdown", | ||
action="store_true", | ||
default=None, | ||
help="Automatically shutdown the node after launch (useful in CI/CD)?", | ||
) | ||
parser.add_argument( | ||
"--interface-types", | ||
type=str, | ||
default=None, | ||
choices=["Message", "Service", "Action"], | ||
help="Interfaces types", | ||
) | ||
parser.add_argument("--msg-name", type=str, default=None, help="Message name") | ||
parser.add_argument("--srv-name", type=str, default=None, help="Service name") | ||
parser.add_argument("--action-name", type=str, default=None, help="Action name") | ||
parser.add_argument( | ||
"--ci-type", | ||
type=str, | ||
choices=["github", "gitlab"], | ||
default=None, | ||
help="CI type", | ||
) | ||
parser.add_argument( | ||
"--add_pre_commit", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only argument using _ in its name. |
||
action="store_true", | ||
default=None, | ||
help="Add pre-commit hook?", | ||
) | ||
|
||
argcomplete.autocomplete(parser) | ||
return parser.parse_args() | ||
|
||
|
||
def add_git_config_info(answers): | ||
# add author and maintainer info from git config if not yet set | ||
git_config = git.GitConfigParser() | ||
git_config.read() | ||
if not answers.get("author") or not answers.get("maintainer"): | ||
answers["user_name_git"] = git_config.get_value("user", "name") | ||
if not answers.get("author_email") or not answers.get("maintainer_email"): | ||
answers["user_email_git"] = git_config.get_value("user", "email") | ||
|
||
|
||
def add_ros_distro(answers): | ||
if os.environ.get("ROS_DISTRO"): | ||
answers["ros_distro"] = os.environ.get("ROS_DISTRO") | ||
|
||
|
||
def create(template_pkg_name: str, template_url: str): | ||
|
||
# pass specified arguments as data to copier | ||
args = parseArguments() | ||
workspace_src = os.path.join(get_workspace_root(), "src") | ||
|
||
# Adapt destination path | ||
if args.destination is None: | ||
args.destination = workspace_src | ||
if not os.path.isabs(args.destination): | ||
args.destination = os.path.join(workspace_src, args.destination) | ||
print(f"Destination: {args.destination}") | ||
|
||
answers = {k: v for k, v in vars(args).items() if v is not None} | ||
|
||
# add author and maintainer info from git config if not yet set | ||
add_git_config_info(answers) | ||
|
||
add_ros_distro(answers) | ||
|
||
# get pkg template location if installed as ros pkg | ||
try: | ||
template_location = get_package_share_directory(template_pkg_name) | ||
except KeyError: | ||
print( | ||
f"Package '{template_pkg_name}' not found locally. Using remote template." | ||
) | ||
template_location = template_url | ||
|
||
# run copier | ||
try: | ||
copier.run_copy( | ||
template_location, | ||
args.destination, | ||
data=answers, | ||
defaults=args.defaults, | ||
unsafe=True, | ||
vcs_ref="HEAD", | ||
) | ||
|
||
except copier.CopierAnswersInterrupt: | ||
print("Aborted") | ||
return | ||
|
||
|
||
if __name__ == "__main__": | ||
template_pkg_name = "ros2_pkg_create" | ||
template_url = "https://github.com/Joschi3/ros2-pkg-create" | ||
create(template_pkg_name, template_url) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you drop the
--user
?