Module: Lydown::CLI::Proofing

Defined in:
lib/lydown/cli/proofing.rb

Class Method Summary collapse

Class Method Details

.handle_changed_file(event, opts) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lydown/cli/proofing.rb', line 40

def handle_changed_file(event, opts)
  path = File.expand_path(event.path)
  if path =~ /^#{opts[:source_filename]}\/(.+)/
    path = $1
  end
  @last_proof_path = event.path unless File.basename(event.path) == 'movement.ld'
  if @last_proof_path
    file_opts = opts.deep_merge(opts_for_path(@last_proof_path, opts))
    file_opts[:base_path] = path
    process(file_opts)
  end
rescue => e
  puts e.class
  puts e.message
  puts e.backtrace.join("\n")
end

.opts_for_path(path, opts) ⇒ Object



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
# File 'lib/lydown/cli/proofing.rb', line 57

def opts_for_path(path, opts)
  path = path.gsub('/./', '/')
  part = File.basename(path, '.*')
  base = opts[:source_filename] || '.'
  if base == '.'
    base = File.expand_path(base)
  end
  dir = File.dirname(path)
  if dir =~ /^\.\/(.+)$/
    dir = $1
  end
  if File.expand_path(dir) =~ /#{base}\/([^\/]+)/
    mvt = $1
  else
    mvt = nil
  end
  
  opts = {}.deep!
  opts[:movements] = [mvt]
  if opts[:score_only]
    opts[:mode] = :score
  else
    opts[:parts] = [part]
    opts[:mode] = :proof
    opts[:line_range] = Lydown::CLI::Diff.diff_line_range(path)
  end
  
  opts
end

.process(opts) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/lydown/cli/proofing.rb', line 87

def process(opts)
  if opts[:line_range] != [nil, nil]
    t = Time.now.strftime("%H:%M:%S")
    unless opts[:silent]
      $stderr.puts "[#{t}] Changed: #{opts[:base_path]} \
        (lines #{opts[:line_range][0]}..#{opts[:line_range][1]})"
    end
    Lydown::CLI::Compiler.process(opts)
  end
end

.start_proofing(opts) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/lydown/cli/proofing.rb', line 5

def start_proofing(opts)
  $stderr.puts "Proof mode: #{opts[:source_filename]} -> #{opts[:output_filename]}"

  trap("INT") {return}
  watch(opts)
  loop {sleep 1000}
ensure
  unwatch
end

.unwatchObject



33
34
35
36
37
38
# File 'lib/lydown/cli/proofing.rb', line 33

def unwatch
  return unless @watcher

  @watcher.stop
  @watcher = nil
end

.watch(opts) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lydown/cli/proofing.rb', line 15

def watch(opts)
  unwatch # teardown previous watcher
  
  source = opts[:source_filename]
  Lydown::CLI::Diff.fill_cache(source)
  @last_proof_path = nil
  
  @watcher = DirectoryWatcher.new(source, glob: ["**/*.ld"], pre_load: true)
  
  @watcher.interval = 0.25
  @watcher.add_observer do |*events|
    events.each do |e|
      handle_changed_file(e, opts) if e.type == :modified
    end
  end
  @watcher.start
end