Method: Roby::Application#find_dirs

Defined in:
lib/roby/app.rb

#find_files_in_dirs(*path, options) ⇒ Array<String>

Enumerates the subdirectories of paths in #search_path matching the given path. The subdirectories are resolved using File.join(*path) If one of the elements of the path is the string ‘ROBOT’, it gets replaced by the robot name and type.

Given a search dir of [app1, app2]

app1/models/tasks/goto.rb
app1/models/tasks/v3/goto.rb
app2/models/tasks/asguard/goto.rb

Examples:

find_dirs('tasks', 'ROBOT', all: true, order: :specific_first)
# returns [app1/models/tasks/v3,
#          app2/models/tasks/asguard,
#          app1/models/tasks/]
find_dirs('tasks', 'ROBOT', all: true, order: :specific_first,
                            prioritize_root_paths: true)
# returns [app1/models/tasks/v3,
#          app1/models/tasks/,
#          app2/models/tasks/asguard]
find_dirs('tasks', 'ROBOT', all: true, order: :specific_last,
                            prioritize_root_paths: true)
# returns [app2/models/tasks/asguard,
#          app1/models/tasks/,
#          app1/models/tasks/v3]
find_dirs('tasks', 'ROBOT', all: false, order: :specific_first)
# returns [app1/models/tasks/v3/goto.rb]

Parameters:

  • options (Hash)

    a customizable set of options

Returns:

  • (Array<String>)


2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
# File 'lib/roby/app.rb', line 2523

def find_dirs(*dir_path)
    return [] if search_path.empty?

    Application.debug { "find_dirs(#{dir_path.map(&:inspect).join(', ')})" }
    if dir_path.last.kind_of?(Hash)
        options = dir_path.pop
    end
    options =
        Kernel.validate_options(options || {}, :all, :order, :path,
                                prioritize_root_paths: false)

    if dir_path.empty?
        raise ArgumentError, "no path given"
    end

    search_path = options[:path] || self.search_path
    if !options.has_key?(:all)
        raise ArgumentError, "no :all argument given"
    elsif !options.has_key?(:order)
        raise ArgumentError, "no :order argument given"
    elsif !i[specific_first specific_last].include?(options[:order])
        raise ArgumentError, "expected either :specific_first or :specific_last for the :order argument, but got #{options[:order]}"
    end

    relative_paths = []

    base_dir_path = dir_path.dup
    base_dir_path.delete_if { |p| p =~ /ROBOT/ }
    relative_paths = [base_dir_path]
    if dir_path.any? { |p| p =~ /ROBOT/ } && robot_name && robot_type
        robot_configuration_names.each do |replacement|
            robot_dir_path = dir_path.map do |s|
                s.gsub("ROBOT", replacement)
            end
            relative_paths << robot_dir_path
        end
    end

    root_paths = search_path.dup
    if options[:order] == :specific_first
        relative_paths = relative_paths.reverse
    else
        root_paths = root_paths.reverse
    end

    result = []
    Application.debug { "  relative paths: #{relative_paths.inspect}" }
    prioritize_root_paths = options[:prioritize_root_paths]
    absolute_paths =
        compute_absolute_paths(relative_paths, root_paths,
                               prioritize_root_paths: prioritize_root_paths)
    absolute_paths.each do |abs_path|
        Application.debug { "  absolute path: #{abs_path}" }
        if File.directory?(abs_path)
            Application.debug { "    selected" }
            result << abs_path
        end
    end

    if result.empty?
        result
    elsif !options[:all]
        [result.first]
    else
        result
    end
end