Class: Steep::Drivers::Watch

Inherits:
Object
  • Object
show all
Includes:
Utils::DriverHelper
Defined in:
lib/steep/drivers/watch.rb

Instance Attribute Summary collapse

Attributes included from Utils::DriverHelper

#steepfile

Instance Method Summary collapse

Methods included from Utils::DriverHelper

#load_config, #type_check

Constructor Details

#initialize(stdout:, stderr:) ⇒ Watch

Returns a new instance of Watch.



11
12
13
14
15
16
# File 'lib/steep/drivers/watch.rb', line 11

def initialize(stdout:, stderr:)
  @dirs = []
  @stdout = stdout
  @stderr = stderr
  @queue = Thread::Queue.new
end

Instance Attribute Details

#dirsObject (readonly)

Returns the value of attribute dirs.



4
5
6
# File 'lib/steep/drivers/watch.rb', line 4

def dirs
  @dirs
end

#queueObject (readonly)

Returns the value of attribute queue.



7
8
9
# File 'lib/steep/drivers/watch.rb', line 7

def queue
  @queue
end

#stderrObject (readonly)

Returns the value of attribute stderr.



6
7
8
# File 'lib/steep/drivers/watch.rb', line 6

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



5
6
7
# File 'lib/steep/drivers/watch.rb', line 5

def stdout
  @stdout
end

Instance Method Details

#listenerObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/steep/drivers/watch.rb', line 18

def listener
  @listener ||= begin
    Steep.logger.info "Watching #{dirs.join(", ")}..."
    Listen.to(*dirs.map(&:to_s)) do |modified, added, removed|
      Steep.logger.tagged "watch" do
        Steep.logger.info "Received file system updates: modified=[#{modified.join(",")}], added=[#{added.join(",")}], removed=[#{removed.join(",")}]"
      end
      queue << [modified, added, removed]
    end
  end
end


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/steep/drivers/watch.rb', line 77

def print_project_result(project)
  project.targets.each do |target|
    Steep.logger.tagged "target=#{target.name}" do
      case (status = target.status)
      when Project::Target::SignatureSyntaxErrorStatus
        printer = SignatureErrorPrinter.new(stdout: stdout, stderr: stderr)
        printer.print_syntax_errors(status.errors)
      when Project::Target::SignatureValidationErrorStatus
        printer = SignatureErrorPrinter.new(stdout: stdout, stderr: stderr)
        printer.print_semantic_errors(status.errors)
      when Project::Target::TypeCheckStatus
        status.type_check_sources.each do |source_file|
          source_file.errors.each do |error|
            error.print_to stdout
          end
        end
      end
    end
  end
end

#runObject



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
# File 'lib/steep/drivers/watch.rb', line 98

def run()
  if dirs.empty?
    stdout.puts "Specify directories to watch"
    return 1
  end

  project = load_config()

  loader = Project::FileLoader.new(project: project)
  loader.load_sources([])
  loader.load_signatures()

  type_check project
  print_project_result project

  listener.start

  stdout.puts "👀 Watching directories, Ctrl-C to stop."
  begin
    type_check_loop project
  rescue Interrupt
    # bye
  end

  0
end

#type_check_loop(project) ⇒ Object



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
# File 'lib/steep/drivers/watch.rb', line 30

def type_check_loop(project)
  until queue.closed?
    stdout.puts "🚥 Waiting for updates..."

    events = []
    events << queue.deq
    until queue.empty?
        events << queue.deq(nonblock: true)
    end

    events.compact.each do |modified, added, removed|
      modified.each do |name|
        path = Pathname(name).relative_path_from(Pathname.pwd)

        project.targets.each do |target|
          target.update_source path, path.read if target.source_file?(path)
          target.update_signature path, path.read if target.signature_file?(path)
        end
      end

      added.each do |name|
        path = Pathname(name).relative_path_from(Pathname.pwd)

        project.targets.each do |target|
          target.add_source path, path.read if target.possible_source_file?(path)
          target.add_signature path, path.read if target.possible_signature_file?(path)
        end
      end

      removed.each do |name|
        path = Pathname(name).relative_path_from(Pathname.pwd)

        project.targets.each do |target|
          target.remove_source path if target.source_file?(path)
          target.remove_signature path if target.signature_file?(path)
        end
      end
    end

    stdout.puts "🔬 Type checking..."
    type_check project
    print_project_result project
  end
rescue ClosedQueueError
  # nop
end