Module: Lydown::CLI::Compiler

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

Class Method Summary collapse

Class Method Details

.compile(ly_code, opts) ⇒ Object



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

def compile(ly_code, opts)
  opts = opts.deep_clone
  begin
    STDERR.puts "Compiling => #{opts[:output_target]}"
    t1 = Time.now
    Lydown::Lilypond.compile(ly_code, opts)
    t2 = Time.now
    STDERR.puts "Elapsed: #{t2-t1}s"
  rescue LydownError => e
    STDERR.puts e.message
    STDERR.puts e.backtrace.join("\n")
  rescue => e
    STDERR.puts "#{e.class}: #{e.message}"
    STDERR.puts e.backtrace.join("\n")
  end

  if opts[:open_target]
    filename = "#{opts[:output_target]}.#{opts[:format]}"
    
    unless File.file?(filename)
      filename = "#{opts[:output_target]}-page1.#{opts[:format]}"
    end

    # Mac OSX specific probably
    system("open #{filename}")
  end
end

.output_filename(opts) ⇒ Object



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

def output_filename(opts)
  fn = opts[:output_filename]
  if opts[:movements] && opts[:movements].size == 1
    fn << "-#{opts[:movements].first}"
  end
  if opts[:parts] && opts[:parts].size == 1
    fn << "-#{opts[:parts].first}"
  end
  fn
end

.process(opts) ⇒ Object



16
17
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
# File 'lib/lydown/cli/compiler.rb', line 16

def process(opts)
  opts = opts.deep_clone
  # translate lydown code to lilypond code
  ly_code = ''
  begin
    opts[:path] = opts[:source_filename]
    opts[:nice_error] = true
    work = Lydown::Work.new(opts)
    ly_code = work.to_lilypond(opts)
  rescue LydownError => e
    STDERR.puts e.message
    STDERR.puts e.backtrace.join("\n")
    exit 1
  rescue => e
    STDERR.puts "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
    exit 1
  end
  
  opts[:output_target] = output_filename(opts)

  if opts[:format] == 'ly'
    unless opts[:output_target]
      STDOUT << ly_code
    else
      begin
        File.open(opts[:output_target] + '.ly', 'w+') do |f|
          f.write ly_code
        end
      rescue => e
        STDERR.puts "#{e.class}: #{e.message}"
      end
    end
  else
    compile(ly_code, opts)
  end
end