Skip to content
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

Ign pkg_create #1582

Closed
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/cmd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,33 @@ configure_file(

install(FILES ${model_configured} DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/ignition/)

#===============================================================================
# Used for the installed model command version.
# Generate the ruby script that gets installed.
# Note that the major version of the library is included in the name.
set(cmd_model_script_generated "${CMAKE_CURRENT_BINARY_DIR}/cmdpkg_create${PROJECT_VERSION_MAJOR}.rb")
set(cmd_model_script_configured "${cmd_model_script_generated}.configured")

configure_file(
"cmdpkg_create.rb.in"
"${cmd_model_script_configured}"
@ONLY)
file(GENERATE
OUTPUT "${cmd_model_script_generated}"
INPUT "${cmd_model_script_configured}")

install(FILES ${cmd_model_script_generated} DESTINATION lib/ruby/ignition)
install(DIRECTORY resources
DESTINATION lib/ruby/ignition)
# Used for the installed version.
set(ign_model_ruby_path "${CMAKE_INSTALL_PREFIX}/lib/ruby/ignition/cmdpkg_create${PROJECT_VERSION_MAJOR}")

set(model_configured "${CMAKE_CURRENT_BINARY_DIR}/pkg_create{PROJECT_VERSION_MAJOR}.yaml")
configure_file(
"pkg_create.yaml.in"
${model_configured})

install(FILES ${model_configured} DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/ignition/)
#===============================================================================
# Generate the ruby script for internal testing.
# Note that the major version of the library is included in the name.
Expand Down
248 changes: 248 additions & 0 deletions src/cmd/cmdpkg_create.rb.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
#!/usr/bin/ruby
require 'erb'
require 'open-uri'
require 'yaml'
require 'ostruct'

# Copyright (C) 2022 Open Source Robotics Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# We use 'dl' for Ruby <= 1.9.x and 'fiddle' for Ruby >= 2.0.x
if RUBY_VERSION.split('.')[0] < '2'
require 'dl'
require 'dl/import'
include DL
else
require 'fiddle'
require 'fiddle/import'
include Fiddle
end

require 'optparse'

# Constants.
LIBRARY_NAME = '@library_location@'
LIBRARY_VERSION = '@PROJECT_VERSION_FULL@'

COMMON_OPTIONS =
" -h [--help] Print this help message.\n"\
" \n" +
" --force-version <VERSION> Use a specific library version.\n"\
" \n" +
' --versions Show the available versions.'

COMMANDS = { 'pkg_create' =>
"Create a new ignition package.\n"+
" \n"\
" ign pkg_create [options] \n\n"\
" \n"\
"Available Options: \n\n"\
" -n [--name] arg Name of new package [Required] \n\n"\
" -a [--advance_pkg] Package with various example resources \n\n"+
" -b [--built_type] arg Build type between ament_cmake or cmake \n\n"+
" -e [--export_type] arg Export type between colcon or plain-camke] \n\n"+
" -d [--standard_dependecies] Package depends on standard ign packages \n\n"+
" -g [--gazebo_version] arg Gazebo version name for dependencies \n\n"+

COMMON_OPTIONS
}

#
# Class for the Gazebo pkg_create command line tools.
#
class Cmd

#
# Return a structure describing the options.
#
def parse(args)
options = {
}
usage = COMMANDS[args[0]]

opt_parser = OptionParser.new do |opts|
opts.banner = usage

opts.on('-h', '--help') do
puts usage
exit
end
opts.on('-n [arg]', '--name [arg]', String, 'Name of new package') do |n|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include author and license as options as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, that information will be used when using ament cmake. Will that be ok?

options['name'] = n
end
opts.on('-a','--advance_pkg', String, 'Package with various example resources') do
harshmahesheka marked this conversation as resolved.
Show resolved Hide resolved
options['advance_pkg'] = 1
end
opts.on('-b [arg]', '--built_type [arg]', String, 'Build type between ament_cmake or cmake') do |b|
options['built_type'] = b
end
opts.on('-e [arg]', '--export_type [arg]', String, 'Export type between colcon or plain-camke') do |e|
options['export_type'] = e
end
opts.on('-d','--standard_dependecies', String, 'Package depends on standard ign packages') do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are standard dependencies? Perhaps it'd be better to name dependencies individually.
You could have a --all-gz-libs to have it depend on all dependencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will change the name to all-ign-libs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed it to --ign_dependencies because I am not including all ign packages as some of them are unnecessary

options['standard_dependecies'] = 1
end
opts.on('-g [arg]', '--gazebo_version [arg]',String, 'Gazebo version name for dependencies') do |g|
options['gazebo_version'] = g
end

end # opt_parser do
begin
opt_parser.parse!(args)
rescue
puts usage
exit(-1)
end

# Check if all options are correct

if !(options.key?('name') && options['name'].class()==String)
puts "ERROR: Please provide proper package name.\n"
puts usage
exit(-1)
end
if (options.key?('built_type') && !(['ament_cmake','cmake'].include? options["built_type"]))
puts "WARNING: Invalid Built-type,defaulting to cmake\n"
options['built_type'] = "cmake"
end
if (options.key?('export_type') && !(['plain_cmake','colcon'].include? options["export_type"]))
puts "WARNING: Invalid Export-type,defaulting to colcon\n"
options['export_type'] = "colcon"
end
if (options.key?('gazebo_version') && !(['acropolis','blueprint','citadel','dome','edifice','fortress','garden'].include? options["gazebo_version"]))
puts "WARNING: Invalid Gazebo Version,defaulting to fortress\n"
options['gazebo_version'] = "fortress"
end
#Giving default values

if !options.key?('advance_pkg')
options['advance_pkg'] = 0
end
if !options.key?('built_type')
options['built_type'] = "cmake"
end
if !options.key?('export_type')
options['export_type'] = "colcon"
end
if !options.key?('standard_dependecies')
options['standard_dependecies'] = 0
end
if !options.key?('gazebo_version')
options['gazebo_version'] = "fortress"
end

options['command'] = args[0]

options
end # parse()

def execute(args)
options = parse(args)

# Read the plugin that handles the command.
if LIBRARY_NAME[0] == '/'
# If the first character is a slash, we'll assume that we've been given an
# absolute path to the library. This is only used during test mode.
plugin = LIBRARY_NAME
else
# We're assuming that the library path is relative to the current
# location of this script.
plugin = File.expand_path(File.join(File.dirname(__FILE__), LIBRARY_NAME))
end
conf_version = LIBRARY_VERSION

begin
Importer.dlload plugin
rescue DLError => e
puts "Library error for [#{plugin}]: #{e.to_s}"
exit(-1)
end

##Code for package generation
if Dir.exists?(options['name'])
puts "ERROR:Folder with this name already exists."
exit(-1)
end

#Creates Package folder
Dir.mkdir(options['name'])

#Fetching dependencies versions for standard dependencies
if options['standard_dependecies']==1
dependencies = URI.open('https://raw.githubusercontent.com/ignition-tooling/gazebodistro/master/collection-%s.yaml'% options['gazebo_version']){|f| f.read}
dependencies_yaml = YAML.load(dependencies)
dependencies_dict={}
for dependency in dependencies_yaml.fetch('repositories').keys
dependencies_dict[dependency]=dependencies_yaml['repositories'][dependency]['version'].scan( /\d+$/ ).first
end
end

resources_dir=__dir__+"/resources"

#Creates CMakeLists.txt using erb
cmakelists_render = ERB.new(File.read(resources_dir+"/CMakeLists.erb"),trim_mode: ">")
cmakelists_file = File.new(options['name']+"/CMakeLists.txt", "w")
cmakelists_file.puts(cmakelists_render.result(OpenStruct.new(options).instance_eval { binding }))
cmakelists_file.close

#Creates package.xml for ament dependent
if options['built_type']=='ament_cmake'
package_xml_render = ERB.new(File.read(resources_dir+"/package_xml.erb"),trim_mode: ">")
package_xml_file = File.new(options['name']+"/package.xml", "w")
package_xml_file.puts(package_xml_render.result(OpenStruct.new(options).instance_eval { binding }))
package_xml_file.close
end

#Creates example resources for advance package
if options['advance_pkg']==1
Dir.mkdir(options['name']+"/worlds")
Dir.mkdir(options['name']+"/models")
Dir.mkdir(options['name']+"/models/elevator")
Dir.mkdir(options['name']+"/plugins")
Dir.mkdir(options['name']+"/src")

executable_txt=File.read(resources_dir+"/executables/random_points.cc")
executable_file = File.new(options['name']+"/src/random_points.cc", "w")
executable_file.puts(executable_txt)
executable_file.close

plugin_txt=File.read(resources_dir+"/plugins/HelloWorld.cc")
plugin_file = File.new(options['name']+"/plugins/HelloWorld.cc", "w")
plugin_file.puts(plugin_txt)
plugin_file.close

plugin_header_txt=File.read(resources_dir+"/plugins/HelloWorld.hh")
plugin_header_file = File.new(options['name']+"/plugins/HelloWorld.hh", "w")
plugin_header_file.puts(plugin_header_txt)
plugin_header_file.close

world_txt=File.read(resources_dir+"/worlds/empty_platform_with_elevator.sdf")
world_file = File.new(options['name']+"/worlds/empty_platform_with_elevator.sdf", "w")
world_file.puts(world_txt)
world_file.close

model_config_txt=File.read(resources_dir+"/models/elevator/model.config")
model_config_file = File.new(options['name']+"/models/elevator/model.config", "w")
model_config_file.puts(model_config_txt)
model_config_file.close

model_sdf_txt=File.read(resources_dir+"/models/elevator/model.sdf")
model_sdf_file = File.new(options['name']+"/models/elevator/model.sdf", "w")
model_sdf_file.puts(model_sdf_txt)
model_sdf_file.close
end
# # execute
end
# class
end
8 changes: 8 additions & 0 deletions src/cmd/pkg_create.yaml.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--- # Model subcommand available inside ignition gazebo.
format: 1.0.0
library_name: ignition-gazebo-ign
library_version: @PROJECT_VERSION_FULL@
library_path: @ign_model_ruby_path@
commands:
- pkg_create : Create a new ignition package
---
80 changes: 80 additions & 0 deletions src/cmd/resources/CMakeLists.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#These lines will set minimum required version of cmake and declare the name of the project.
cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR)
project(<%= options['name'] %>)
<% if options['standard_dependecies']==1 %>

#This is a list of general packages on which your ignition package will depend,find_package function will find and source theses packages.
#You can add or delete dependencies based on your requirement.
<% if options['built_type']=='ament_cmake' %>
#Since,the package is amnet dependent we will find and call ament_cmake.

find_package(ament_cmake REQUIRED)
<% end %>

find_package(ignition-cmake<%= dependencies_dict["ign-cmake"]%> REQUIRED)

find_package(ignition-gazebo<%= dependencies_dict["ign-gazebo"]%> REQUIRED COMPONENTS gui)
set(IGN_GAZEBO_VER ${ignition-gazebo<%= dependencies_dict["ign-gazebo"]%>_VERSION_MAJOR})

find_package(ignition-gui<%= dependencies_dict["ign-gui"]%> REQUIRED)
set(IGN_GUI_VER ${ignition-gui<%= dependencies_dict["ign-gui"]%>_VERSION_MAJOR})

find_package(ignition-rendering<%= dependencies_dict["ign-rendering"]%> REQUIRED)
set(IGN_RENDERING_VER ${ignition-rendering<%= dependencies_dict["ign-rendering"]%>_VERSION_MAJOR})

find_package(ignition-sensors<%= dependencies_dict["ign-sensors"]%> REQUIRED)
set(IGN_SENSORS_VER ${ignition-sensors<%= dependencies_dict["ign-sensors"]%>_VERSION_MAJOR})

find_package(ignition-msgs<%= dependencies_dict["ign-msgs"]%> REQUIRED)
set(IGN_MSGS_VER ${ignition-msgs<%= dependencies_dict["ign-msgs"]%>_VERSION_MAJOR})

find_package(ignition-plugin<%= dependencies_dict["ign-plugin"]%> REQUIRED COMPONENTS register)
set(IGN_PLUGIN_VER ${ignition-plugin<%= dependencies_dict["ign-plugin"]%>_VERSION_MAJOR})

find_package(ignition-utils<%= dependencies_dict["ign-utils"]%> REQUIRED)
set(IGN_UTILS_VER ${ignition-utils<%= dependencies_dict["ign-utils"]%>_VERSION_MAJOR})

find_package(ignition-common<%= dependencies_dict["ign-common"]%> REQUIRED COMPONENTS profiler)
set(IGN_COMMON_VER ${ignition-common<%= dependencies_dict["ign-common"]%>_VERSION_MAJOR})
<% end %>
<% if options['advance_pkg']==1 %>

#These will install directories with various resources like worlds,plugins etc.
ign_add_resources(worlds)
ign_add_resources(models)
ign_export_variable(LD_LIBRARY_PATH @CMAKE_INSTALL_PREFIX@/lib)
ign_add_plugins(plugins COMMON_PUBLIC_LIBRARIES
ignition-gazebo${IGN_GAZEBO_VER}::ignition-gazebo${IGN_GAZEBO_VER}
ignition-common${IGN_COMMON_VER}::ignition-common${IGN_COMMON_VER}
ignition-plugin${IGN_PLUGIN_VER}::ignition-plugin${IGN_PLUGIN_VER})
ign_add_executables(src COMMON_PUBLIC_LIBRARIES
ignition-msgs${IGN_MSGS_VER}::ignition-msgs${IGN_MSGS_VER})

#This line will export paths of all the above mentioned resources.
<% if options['export_type']=="colcon"%>
ign_environment_hook(colcon)
<% end %>
<% if options['export_type']=="plain_cmake"%>
ign_environment_hook(plain-cmake)
<% end %>
<% end %>

#These line will run tests for the package, if build testing is set to be TRUE
#You can add your tests here

if(BUILD_TESTING)

<% if options['built_type']=='ament_cmake' %>
find_package(ament_cmake_gtest REQUIRED)
<% end %>
set("PROJECT_BINARY_PATH" ${CMAKE_CURRENT_BINARY_DIR})
set("PROJECT_SOURCE_PATH" ${CMAKE_CURRENT_SOURCE_DIR})

endif()
<% if options['built_type']=='ament_cmake' %>

#The project setup is done by ament_package().
#It installs the package.xml, registers the package with the ament index, and installs config (and possibly target) files
#for CMake so that it can be found by other packages using find_package.
ament_package()
<% end %>
Loading