Class: Autoproj::CLI::Base

Inherits:
Object
  • Object
show all
Includes:
Ops::Tools
Defined in:
lib/autoproj/cli/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ops::Tools

#common_options, #create_autobuild_package, #load_autoprojrc, #load_main_initrb

Constructor Details

#initialize(ws = Workspace.default) ⇒ Base

Returns a new instance of Base.



16
17
18
19
# File 'lib/autoproj/cli/base.rb', line 16

def initialize(ws = Workspace.default)
    @ws = ws
    @env_sh_updated = nil
end

Instance Attribute Details

#wsWorkspace (readonly)

The underlying workspace

Returns:



14
15
16
# File 'lib/autoproj/cli/base.rb', line 14

def ws
  @ws
end

Class Method Details

.validate_options(args, options) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/autoproj/cli/base.rb', line 185

def self.validate_options(args, options)
    options, remaining = filter_options options,
                                        silent: false,
                                        verbose: false,
                                        debug: false,
                                        color: TTY::Color.color?,
                                        progress: TTY::Color.color?,
                                        parallel: nil

    Autoproj.silent = options[:silent]
    Autobuild.color = options[:color]
    Autobuild.progress_display_enabled = options[:progress]

    if options[:verbose]
        Autoproj.verbose  = true
        Autobuild.verbose = true
        Rake.application.options.trace = false
        Autobuild.debug = false
    end

    if options[:debug]
        Autoproj.verbose  = true
        Autobuild.verbose = true
        Rake.application.options.trace = true
        Autobuild.debug = true
    end

    if (level = options[:parallel])
        Autobuild.parallel_build_level = Integer(level)
        remaining[:parallel] = Integer(level)
    end

    [args, remaining.to_sym_keys]
end

Instance Method Details

#export_env_sh(shell_helpers: ws.config.shell_helpers?) ⇒ Object



220
221
222
223
224
225
226
227
228
229
# File 'lib/autoproj/cli/base.rb', line 220

def export_env_sh(shell_helpers: ws.config.shell_helpers?)
    # @env_sh_updated == nil means "did not even export".
    # make sure that it is set to 'true' or 'false'
    @env_sh_updated =
        if ws.export_env_sh(shell_helpers: shell_helpers)
            true
        else
            false
        end
end

#normalize_command_line_package_selection(selection) ⇒ (Array<String>,Boolean)

Normalizes the arguments given by the user on the command line

This converts relative paths to full paths, and removes mentions of the configuration directory (as it is handled separately in autoproj)

Returns:

  • ((Array<String>,Boolean))

    the normalized arguments that could e.g. be passed to #resolve_selection, as well as whether the config directory was selected or not



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/autoproj/cli/base.rb', line 30

def normalize_command_line_package_selection(selection)
    selection = selection.map do |name|
        if File.directory?(name)
            "#{File.expand_path(name)}/"
        else
            name
        end
    end

    config_selected = false
    selection.delete_if do |name|
        if name =~ /^#{Regexp.quote(ws.config_dir)}(?:#{File::SEPARATOR}|$)/ ||
           name =~ /^#{Regexp.quote(ws.remotes_dir)}(?:#{File::SEPARATOR}|$)/
            config_selected = true
        elsif (ws.config_dir + File::SEPARATOR) =~ /^#{Regexp.quote(name)}/
            config_selected = true
            false
        end
    end

    [selection, config_selected]
end

#notify_env_sh_updatedObject



231
232
233
234
235
236
237
238
239
# File 'lib/autoproj/cli/base.rb', line 231

def notify_env_sh_updated
    return if @env_sh_updated.nil?

    if @env_sh_updated
        Autoproj.message "  updated environment", :green
    else
        Autoproj.message "  environment already up-to-date", :green
    end
end

#resolve_selection(user_selection, checkout_only: true, only_local: false, recursive: true, non_imported_packages: :ignore, auto_exclude: false) ⇒ (Array<String>,Array<String>,PackageSelection)

Resolves the user-provided selection into the set of packages that should be processed further

While #resolve_user_selection really only considers packages and strings, this methods takes care of doing recursive resolution of dependencies, as well as splitting the packages into source and osdep packages.

It loads the packages in sequence (that’s the only way the full selection can be computed), and is therefore responsible for updating the packages if needed (disabled by default)

Parameters:

  • user_selection (Array<String>)

    the selection provided by the user

  • checkout_only (Boolean) (defaults to: true)

    if packages should be updated (false) or only missing packages should be checked out (the default)

  • only_local (Boolean) (defaults to: false)

    if the update/checkout operation is allowed to access the network. If only_local is true but some packages should be checked out, the update will fail

  • recursive (Boolean) (defaults to: true)

    whether the resolution should be done recursively (i.e. dependencies of directly selected packages should be added) or not

  • non_imported_packages (Symbol) (defaults to: :ignore)

    whether packages that are not imported should simply be ignored (:ignore), returned (:return) or should be checked out (:checkout). Setting checkout_only to true and this to anything but nil guarantees in effect that no import operation will take place, only loading

Returns:

  • ((Array<String>,Array<String>,PackageSelection))

    the list of selected source packages, the list of selected OS packages and the package selection resolution object

See Also:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/autoproj/cli/base.rb', line 142

def resolve_selection(user_selection, checkout_only: true, only_local: false, recursive: true, non_imported_packages: :ignore, auto_exclude: false)
    resolved_selection, = resolve_user_selection(user_selection, filter: false)

    ops = Ops::Import.new(ws)
    source_packages, osdep_packages = ops.import_packages(
        resolved_selection,
        checkout_only: checkout_only,
        only_local: only_local,
        recursive: recursive,
        warn_about_ignored_packages: false,
        non_imported_packages: non_imported_packages,
        auto_exclude: auto_exclude
    )

    [source_packages, osdep_packages, resolved_selection]
rescue ExcludedSelection => e
    raise CLIInvalidSelection, e.message, e.backtrace
end

#resolve_user_selection(selected_packages, **options) ⇒ (PackageSelection,Array<String>)

Resolve a user-provided selection

Returns:

  • ((PackageSelection,Array<String>))

    the resolved selections and the list of entries in selected_packages that have not been resolved



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/autoproj/cli/base.rb', line 59

def resolve_user_selection(selected_packages, **options)
    if selected_packages.empty?
        selection =
            begin ws.manifest.default_packages
            rescue ExcludedSelection => e
                raise CLIInvalidSelection, e.message, e.backtrace
            end
        if Autoproj.verbose
            Autoproj.message "selected packages: #{selection.each_package_name.to_a.sort.join(', ')}"
        end
        return selection, []
    end
    selected_packages = selected_packages.to_set

    selected_packages, nonresolved =
        begin ws.manifest.expand_package_selection(selected_packages, **options)
        rescue ExcludedSelection => e
            raise CLIInvalidSelection, e.message, e.backtrace
        end

    # Try to auto-add stuff if nonresolved
    nonresolved.delete_if do |sel|
        sel = File.expand_path(sel)
        next unless File.directory?(sel)

        while sel != "/"
            handler, srcdir = Autoproj.package_handler_for(sel)
            if handler
                Autoproj.message "  auto-adding #{srcdir} using the #{handler.gsub(/_package/, '')} package handler"
                srcdir = File.expand_path(srcdir)
                relative_to_root = Pathname.new(srcdir).relative_path_from(Pathname.new(ws.source_dir))
                pkg = ws.in_package_set(ws.manifest.main_package_set, ws.manifest.file) do
                    send(handler, relative_to_root.to_s, workspace: ws)
                end
                ws.setup_package_directories(pkg)
                selected_packages.select(sel, pkg.name, weak: true)
                break(true)
            end

            sel = File.dirname(sel)
        end
    end

    if Autoproj.verbose
        Autoproj.message "selected packages: #{selected_packages.each_package_name.to_a.sort.join(', ')}"
    end
    [selected_packages, nonresolved]
end

#validate_options(args, options) ⇒ Object



179
180
181
182
183
# File 'lib/autoproj/cli/base.rb', line 179

def validate_options(args, options)
    interactive = options.delete(:interactive)
    ws.config.interactive = interactive unless interactive.nil?
    self.class.validate_options(args, options)
end

#validate_user_selection(user_selection, resolved_selection) ⇒ Object

Check whether the user selection refers to non-existant/unknown packages

Parameters:

  • user_selection (Array<String>)

    List of selected packages

  • resolved_selection (Autoproj::PackageSelection)

    The selection of known packages



168
169
170
171
172
173
174
175
176
177
# File 'lib/autoproj/cli/base.rb', line 168

def validate_user_selection(user_selection, resolved_selection)
    not_matched = user_selection.find_all do |pkg_name|
        !resolved_selection.has_match_for?(pkg_name) &&
            !(resolved_selection.ignored?(pkg_name) ||
              resolved_selection.excluded?(pkg_name))
    end
    unless not_matched.empty?
        raise CLIInvalidArguments, "autoproj: wrong package selection on command line, cannot find a match for #{not_matched.to_a.sort.join(', ')}"
    end
end