Class: Rwm::Commands::Run

Inherits:
Object
  • Object
show all
Defined in:
lib/rwm/commands/run.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Run

Returns a new instance of Run.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rwm/commands/run.rb', line 8

def initialize(argv)
  @argv = argv
  @affected_only = false
  @committed_only = false
  @no_cache = false
  @buffered = false
  @concurrency = nil
  @dry_run = false
  @base_branch = nil
  parse_options
end

Instance Method Details

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rwm/commands/run.rb', line 20

def run
  task = @argv.shift

  unless task
    $stderr.puts "Usage: rwm run <task> [<package>] [--affected] [--base REF] [--dry-run] [--no-cache] [--buffered] [--concurrency N]"
    return 1
  end

  package_name = @argv.shift

  workspace = Workspace.find
  graph = DependencyGraph.load(workspace)

  packages = if package_name
               [workspace.find_package(package_name)]
             elsif @affected_only
               detector = AffectedDetector.new(workspace, graph, committed_only: @committed_only, base_branch: @base_branch)
               affected = detector.affected_packages
               if affected.empty?
                 puts "No affected packages. Nothing to run."
                 return 0
               end
               puts "Running on #{affected.size} affected package(s)..."
               affected
             else
               workspace.packages
             end

  if packages.empty?
    puts "No packages found."
    return 0
  end

  # Filter to packages with a Rakefile (skip those without one entirely)
  runnable = packages.select(&:has_rakefile?)
  if runnable.empty?
    puts "No packages with a Rakefile found."
    return 0
  end

  Rwm.debug("run: #{runnable.size} package(s) with Rakefiles")

  # Auto-detect cacheable tasks unless --no-cache
  cache = TaskCache.new(workspace, graph) unless @no_cache
  if cache
    cache.preload_declarations(runnable)
    cacheable, not_cacheable = runnable.partition { |pkg| cache.cacheable?(pkg, task) }
    cached, uncached = cacheable.partition { |pkg| cache.cached?(pkg, task) }
    cached.each { |pkg| puts "[#{pkg.name}] cached" }
    runnable = uncached + not_cacheable
  end

  if runnable.empty?
    puts "All packages cached. Nothing to run."
    return 0
  end

  if @dry_run
    puts "Dry run: would run `rake #{task}` on #{runnable.size} package(s):"
    runnable.each { |pkg| puts "  #{pkg.name}" }
    return 0
  end

  puts "Running `rake #{task}` across #{runnable.size} package(s)..."
  puts

  runner_opts = { packages: runnable, buffered: @buffered }
  runner_opts[:concurrency] = @concurrency if @concurrency
  runner = TaskRunner.new(graph, **runner_opts)
  runner.run_task(task)

  # Store cache for successful cacheable packages
  if cache
    runner.results.each do |result|
      next unless result.passed?

      pkg = workspace.find_package(result.package_name)
      cache.store(pkg, task) if cache.cacheable?(pkg, task)
    end
  end

  passed = runner.results.count(&:passed?)
  failed_results = runner.results.select { |r| r.failed? || r.errored? }
  no_task = runner.results.count(&:skipped?)
  dep_failed = runner.results.count(&:dep_skipped?)

  total = runner.results.size
  parts = []
  parts << "#{passed} passed" unless passed.zero?
  parts << "#{failed_results.size} failed" unless failed_results.empty?
  parts << "#{dep_failed} skipped (dep failed)" unless dep_failed.zero?
  parts << "#{no_task} skipped (no task)" unless no_task.zero?

  puts
  puts "#{total} package(s): #{parts.join(", ")}."

  passed_results = runner.results.select(&:passed?)
  no_task_results = runner.results.select(&:skipped?)
  dep_skipped_results = runner.results.select(&:dep_skipped?)
  Rwm.debug("passed: #{passed_results.map(&:package_name).join(", ")}") unless passed_results.empty?
  Rwm.debug("skipped (no matching task): #{no_task_results.map(&:package_name).join(", ")}") unless no_task_results.empty?
  Rwm.debug("skipped (dep failed): #{dep_skipped_results.map(&:package_name).join(", ")}") unless dep_skipped_results.empty?

  if failed_results.empty?
    0
  else
    $stderr.puts "Failed:"
    failed_results.each { |r| $stderr.puts "  - #{r.package_name}" }
    1
  end
end