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
# 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
  parse_options
end

Instance Method Details

#runObject



18
19
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
# File 'lib/rwm/commands/run.rb', line 18

def run
  task = @argv.shift

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

  package_name = @argv.shift

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

  packages = if package_name
               pkg = workspace.find_package(package_name)
               unless pkg
                 $stderr.puts "Unknown package: #{package_name}"
                 return 1
               end
               [pkg]
             elsif @affected_only
               detector = AffectedDetector.new(workspace, graph, committed_only: @committed_only)
               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 that have the requested rake task
  runnable = packages.select { |pkg| pkg.has_rake_task?(task) }
  if runnable.empty?
    puts "No packages with a `#{task}` rake task found."
    return 0
  end

  # Auto-detect cacheable tasks unless --no-cache
  cache = TaskCache.new(workspace, graph) unless @no_cache
  if cache
    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

  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.success

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

  puts
  if runner.success?
    puts "All packages passed."
    0
  else
    failed = runner.failed_results
    $stderr.puts "#{failed.size} package(s) failed:"
    failed.each { |r| $stderr.puts "  - #{r.package_name}" }
    1
  end
end