Skip to content

Commit

Permalink
cli: add the --build or -b option to acd and autoproj locate
Browse files Browse the repository at this point in the history
This resolves the package's build directory instead of its source,
really useful when building within a separate build area.
  • Loading branch information
doudou committed Jul 23, 2015
1 parent 0caab29 commit a3fd70a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
39 changes: 29 additions & 10 deletions lib/autoproj/cli/locate.rb
Expand Up @@ -11,6 +11,7 @@ class AmbiguousSelection < RuntimeError; end

def initialize(ws = nil)
super
self.ws.load_config

@installation_manifest = Autoproj::InstallationManifest.new(self.ws.root_dir)
if !File.file?(installation_manifest.default_manifest_path)
Expand All @@ -24,22 +25,41 @@ def validate_options(selected, options)
return selected.first, options
end

def result_value(pkg, options)
if options[:build]
if pkg.builddir
pkg.builddir
else
raise ConfigError, "#{pkg.name} does not have a build directory"
end
else
pkg.srcdir
end
end

def run(selection, options = Hash.new)
if !selection
puts ws.root_dir
if options[:build]
puts ws.prefix_dir
else
puts ws.root_dir
end
return
end

if File.directory?(selection)
selection = File.expand_path(selection)
end

selection_rx = Regexp.new(Regexp.quote(selection))
candidates = []
installation_manifest.each do |pkg|
name = pkg.name
srcdir = pkg.srcdir
if name == selection
puts srcdir
if name == selection || pkg.srcdir == selection
puts result_value(pkg, options)
return
elsif name =~ selection_rx
candidates << srcdir
candidates << pkg
end
end

Expand All @@ -59,12 +79,11 @@ def run(selection, options = Hash.new)
candidates_strict = []
installation_manifest.each do |pkg|
name = pkg.name
srcdir = pkg.srcdir
if name =~ rx
candidates << srcdir
candidates << pkg
end
if name =~ rx_strict
candidates_strict << srcdir
candidates_strict << pkg
end
end

Expand All @@ -76,7 +95,7 @@ def run(selection, options = Hash.new)
if candidates.size > 1
# If there is more than one candidate, check if there are some that are not
# present on disk
present = candidates.find_all { |dir| File.directory?(dir) }
present = candidates.find_all { |pkg| File.directory?(pkg.srcdir) }
if present.size == 1
candidates = present
end
Expand All @@ -87,7 +106,7 @@ def run(selection, options = Hash.new)
elsif candidates.size > 1
raise ArgumentError, "multiple packages match #{selection} in the current autoproj installation: #{candidates.join(", ")}"
else
puts candidates.first
puts result_value(candidates.first, options)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/autoproj/cli/main.rb
Expand Up @@ -130,6 +130,8 @@ def clean(*packages)
end

desc 'locate [PACKAGE]', 'return the path to the given package, or the path to the root if no packages are given on the command line'
option :build, aliases: :b, type: :boolean,
desc: "outputs the package's build directory instead of its source directory"
def locate(package = nil)
run_autoproj_cli(:locate, :Locate, Hash[], *Array(package))
end
Expand Down
8 changes: 6 additions & 2 deletions lib/autoproj/installation_manifest.rb
@@ -1,7 +1,7 @@
module Autoproj
# Manifest of installed packages imported from another autoproj installation
class InstallationManifest
Package = Struct.new :name, :srcdir, :prefix
Package = Struct.new :name, :srcdir, :prefix, :builddir

DEFAULT_MANIFEST_NAME = ".autoproj-installation-manifest"

Expand All @@ -17,7 +17,11 @@ def default_manifest_path

def load(path = default_manifest_path)
@packages = CSV.read(path).map do |row|
Package.new(*row)
pkg = Package.new(*row)
if pkg.builddir && pkg.builddir.empty?
pkg.builddir = nil
end
pkg
end
end

Expand Down
23 changes: 14 additions & 9 deletions lib/autoproj/workspace.rb
Expand Up @@ -446,19 +446,23 @@ def setup_package_directories(pkg)
pkg = manifest.find_autobuild_package(pkg_name)
pkg.srcdir = File.join(root_dir, srcdir)
if pkg.respond_to?(:builddir)
# If we're given an absolute build dir, we have to append the
# package name to it to make it unique
if Pathname.new(build_dir).absolute?
pkg.builddir = File.join(build_dir, pkg_name)
else
pkg.builddir = build_dir
end
pkg.builddir = compute_builddir(pkg)
end

pkg.prefix = File.join(prefix_dir, prefixdir)
pkg.doc_target_dir = File.join(prefix_dir, 'doc', pkg_name)
pkg.logdir = File.join(pkg.prefix, "log")
end

def compute_builddir(pkg)
# If we're given an absolute build dir, we have to append the
# package name to it to make it unique
if Pathname.new(build_dir).absolute?
File.join(build_dir, pkg.name)
else
build_dir
end
end

# Finalizes the configuration loading
#
Expand Down Expand Up @@ -525,10 +529,11 @@ def setup_environment_from_packages
end

def export_installation_manifest
File.open(File.join(root_dir, ".autoproj-installation-manifest"), 'w') do |io|
File.open(File.join(root_dir, InstallationManifest::DEFAULT_MANIFEST_NAME), 'w') do |io|
manifest.all_selected_packages(false).each do |pkg_name|
if pkg = manifest.find_autobuild_package(pkg_name)
io.puts "#{pkg_name},#{pkg.srcdir},#{pkg.prefix}"
builddir = (pkg.builddir if pkg.respond_to?(:builddir))
io.puts "#{pkg_name},#{pkg.srcdir},#{pkg.prefix},#{builddir}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion shell/autoproj_sh
@@ -1,6 +1,6 @@
acd() {
local pkg_path
pkg_path=$(autoproj locate $1)
pkg_path=$(autoproj locate "$@")

if [ $? != 0 ]; then
return 1
Expand Down

0 comments on commit a3fd70a

Please sign in to comment.