Module: Lyp::Lilypond

Defined in:
lib/lyp/windows.rb,
lib/lyp/lilypond.rb

Constant Summary collapse

NO_ARGUMENT_OPTIONS_REGEXP =
/^\-([REnFOcSA]+)(.+)/
VERSION_STATEMENT_REGEX =
/\\version "([^"]+)"/
CMP_VERSION =
proc do |x, y|
  Lyp.version(x[:version]) <=> Lyp.version(y[:version])
end
BASE_URL =
"http://download.linuxaudio.org/lilypond/binaries"
SYSTEM_LILYPOND_PATCH_WARNING =
<<-EOF.gsub(/^\s{6}/, '').chomp
  The system-installed lilypond version %s needs to be patched in
  order to support custom music fonts. This operation will replace the file

    %s

  Would you like to overwrite this file? (y/n):
EOF
CHECK_UPDATE_INTERVAL =
7 * 86400
CHECK_UPDATE_STAMP_KEY =
'lilypond/last_update_stamp'
UNSTABLE_UPDATE_MESSAGE =
<<EOF
Lilypond version %s is now available. Install it by typing:

  lyp install lilypond@unstable

EOF

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.forced_versionObject (readonly)

Returns the value of attribute forced_version.



185
186
187
# File 'lib/lyp/lilypond.rb', line 185

def forced_version
  @forced_version
end

Class Method Details

.check_lilypond!Object

Make sure there’s a default and current lilypond set



188
189
190
191
192
193
194
# File 'lib/lyp/lilypond.rb', line 188

def check_lilypond!
  path = default_lilypond
  select_default_lilypond! unless path && path =~ /lilypond$/

  path = current_lilypond
  set_current_lilypond(default_lilypond) unless path && path =~ /lilypond$/
end

.check_updateObject



732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'lib/lyp/lilypond.rb', line 732

def check_update
  last_check = Lyp::Settings.get_value(
    CHECK_UPDATE_STAMP_KEY, Time.now - CHECK_UPDATE_INTERVAL)

  return unless last_check < Time.now - CHECK_UPDATE_INTERVAL

  Lyp::Settings.set_value(CHECK_UPDATE_STAMP_KEY, Time.now)

  # check unstable
  installed = latest_installed_unstable_version
  return unless installed

  available = {version: latest_unstable_version}
  installed = {version: installed}

  if CMP_VERSION[available, installed] > 0
    puts UNSTABLE_UPDATE_MESSAGE % available[:version]
  end
end

.compile(argv, opts = {}) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/lyp/lilypond.rb', line 100

def compile(argv, opts = {})
  unless argv.last == '-'
    fn = Lyp.wrap(argv.pop, opts)
    argv << fn
  end

  invoke(argv, opts)
end

.copy_fonts_from_all_packages(version, opts) ⇒ Object



612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'lib/lyp/lilypond.rb', line 612

def copy_fonts_from_all_packages(version, opts)
  return unless Lyp::FONT_COPY_REQ =~ Lyp.version(version)

  ly_fonts_dir = File.join(lyp_lilypond_share_dir(version), 'lilypond/current/fonts')

  Dir["#{Lyp.packages_dir}/**/fonts"].each do |package_fonts_dir|
    Dir["#{package_fonts_dir}/**/*"].each do |fn|
      next unless File.file?(fn)
      target_fn = case File.extname(fn)
      when '.otf'
        File.join(ly_fonts_dir, 'otf', File.basename(fn))
      when '.svg', '.woff'
        File.join(ly_fonts_dir, 'svg', File.basename(fn))
      else
        next
      end

      FileUtils.cp(fn, target_fn)
    end
  end
end

.copy_lilypond_files(base_path, version, opts) ⇒ Object



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/lyp/lilypond.rb', line 553

def copy_lilypond_files(base_path, version, opts)
  target_dir = File.join(Lyp.lilyponds_dir, version)

  FileUtils.rm_rf(target_dir) if File.exists?(target_dir)

  # create directory for lilypond files
  FileUtils.mkdir_p(target_dir)

  # copy files
  STDERR.puts "Copying..." unless opts[:silent]
  %w{bin etc lib lib64 share var}.each do |entry|
    dir = File.join(base_path, entry)
    FileUtils.cp_r(dir, target_dir, remove_destination: true) if File.directory?(dir)
  end

  # Show lilypond versions
  STDERR.puts `#{target_dir}/bin/lilypond -v` unless opts[:silent] || opts[:no_version_test]
rescue => e
  puts e.message
end

.current_lilypondObject

The current lilypond path is stored in a temporary file named by the session id. Thus we can persist the version selected by the user



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/lyp/lilypond.rb', line 133

def current_lilypond
  return forced_lilypond if @forced_version

  settings = get_session_settings

  if !settings[:current]
    settings[:current] = default_lilypond
    set_session_settings(settings)
  end

  settings[:current]
end

.current_lilypond_versionObject



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/lyp/lilypond.rb', line 146

def current_lilypond_version
  path = current_lilypond
  version = File.basename(File.expand_path("#{File.dirname(path)}/../.."))

  unless (Lyp.version(version) rescue nil)
    resp = `#{path} -v`
    if resp.lines.first =~ /LilyPond ([0-9\.]+)/i
      version = $1
    end
  end
  version
end

.default_lilypondObject



123
124
125
# File 'lib/lyp/lilypond.rb', line 123

def default_lilypond
  Lyp::Settings['lilypond/default']
end

.detect_lilypond_platformObject



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/lyp/lilypond.rb', line 411

def detect_lilypond_platform
  case RUBY_PLATFORM
  when /x86_64-darwin/
    "darwin-x86"
  when /ppc-darwin/
    "darwin-ppc"
  when "i686-linux"
    "linux-x86"
  when "x86_64-linux"
    "linux-64"
  when "ppc-linux"
    "linux-ppc"
  when "x64-mingw32"
    "mingw"
  end
end

.detect_version_from_specifier(version_specifier) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/lyp/lilypond.rb', line 390

def detect_version_from_specifier(version_specifier)
  case version_specifier
  when /^\d/
    version_specifier
  when nil, 'stable'
    latest_stable_version
  when 'unstable'
    latest_unstable_version
  when 'latest'
    latest_version
  else
    req = Lyp.version_req(version_specifier)
    lilypond = search.reverse.find {|l| req =~ Lyp.version(l[:version])}
    if lilypond
      lilypond[:version]
    else
      raise "Could not find version matching #{version_specifier}"
    end
  end
end

.download_lilypond(url, fn, opts) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/lyp/windows.rb', line 44

def download_lilypond(url, fn, opts)
  STDERR.puts "Downloading #{url}" unless opts[:silent]

  if opts[:silent]
    `curl -s -o "#{fn}" "#{url}"`
  else
    `curl -o "#{fn}" "#{url}"`
  end
end

.exec(cmd, raise_on_failure = true) ⇒ Object



702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
# File 'lib/lyp/lilypond.rb', line 702

def exec(cmd, raise_on_failure = true)
  $_out = ""
  $_err = ""
  success = nil
  Open3.popen3(cmd) do |_in, _out, _err, wait_thr|
    exit_value = wait_thr.value
    $_out = _out.read
    $_err = _err.read
    success = exit_value == 0
  end
  if !success && raise_on_failure
    raise "Error executing cmd #{cmd}:\n#{parse_error_msg($_err)}"
  end
  success
end

.filter_installed_list(version_specifier) ⇒ Object



228
229
230
231
# File 'lib/lyp/lilypond.rb', line 228

def filter_installed_list(version_specifier)
  list = (system_lilyponds + lyp_lilyponds).sort!(&CMP_VERSION)
  list.select {|l| version_match(l[:version], version_specifier, list)}
end

.force_env_version!Object



174
175
176
177
178
179
# File 'lib/lyp/lilypond.rb', line 174

def force_env_version!
  @forced_version = ENV['LILYPOND_VERSION']
  unless @forced_version
    raise "LILYPOND_VERSION not set"
  end
end

.force_version!(version) ⇒ Object



181
182
183
# File 'lib/lyp/lilypond.rb', line 181

def force_version!(version)
  @forced_version = version
end

.forced_lilypondObject



165
166
167
168
169
170
171
172
# File 'lib/lyp/lilypond.rb', line 165

def forced_lilypond
  lilypond = filter_installed_list(@forced_version)[0]
  if lilypond
    lilypond[:path]
  else
    raise "No installed version found matching '#{@forced_version}'"
  end
end

.get_file_expected_version(file_path) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/lyp/lilypond.rb', line 90

def get_file_expected_version(file_path)
  if IO.read(file_path) =~ VERSION_STATEMENT_REGEX
    $1
  else
    raise "Could not find version statement in #{file_path}"
  end
rescue => e
  raise "Failed to read #{file_path}"
end

.get_session_settingsObject



210
211
212
# File 'lib/lyp/lilypond.rb', line 210

def get_session_settings
  YAML.load(IO.read(session_settings_filename)) rescue {}
end

.get_system_lilyponds_pathsObject



13
14
15
# File 'lib/lyp/windows.rb', line 13

def get_system_lilyponds_paths
  []
end

.install(version_specifier, opts = {}) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
# File 'lib/lyp/lilypond.rb', line 374

def install(version_specifier, opts = {})
  version = detect_version_from_specifier(version_specifier)
  raise "No version found matching specifier #{version_specifier}" unless version

  STDERR.puts "Installing version #{version}" unless opts[:silent]
  install_version(version, opts)

  lilypond_path = lyp_lilypond_path(version)
  set_current_lilypond(lilypond_path)
  set_default_lilypond(lilypond_path) if opts[:default]
end

.install_if_missing(version_specifier, opts = {}) ⇒ Object



368
369
370
371
372
# File 'lib/lyp/lilypond.rb', line 368

def install_if_missing(version_specifier, opts = {})
  if filter_installed_list(version_specifier).empty?
    install(version_specifier, opts)
  end
end

.install_lilypond_files(fn, platform, version, opts) ⇒ Object



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/lyp/lilypond.rb', line 486

def install_lilypond_files(fn, platform, version, opts)
  tmp_target = "#{Lyp::TMP_ROOT}/lilypond-#{version}"
  FileUtils.mkdir_p(tmp_target)

  case platform
  when /darwin/
    install_lilypond_files_osx(fn, tmp_target, platform, version, opts)
  when /linux/
    install_lilypond_files_linux(fn, tmp_target, platform, version, opts)
  when /mingw/
    install_lilypond_files_windows(fn, tmp_target, platform, version, opts)
  end

ensure
  FileUtils.rm_rf(tmp_target)
end

.install_lilypond_files_linux(fn, target, platform, version, opts) ⇒ Object

Since linux versions are distributed as sh archives, we need first to extract the sh archive, then extract the resulting tar file



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/lyp/lilypond.rb', line 512

def install_lilypond_files_linux(fn, target, platform, version, opts)
  STDERR.puts "Extracting..." unless opts[:silent]

  # create temp directory in which to extract .sh file
  tmp_dir = "#{Lyp::TMP_ROOT}/#{Time.now.to_f}"
  FileUtils.mkdir_p(tmp_dir)

  FileUtils.cd(tmp_dir) do
    exec "sh #{fn} --tarball >/dev/null"
  end

  tmp_fn = "#{tmp_dir}/lilypond-#{version}-1.#{platform}.tar.bz2"

  exec "tar -xjf #{tmp_fn} -C #{target}"

  copy_lilypond_files("#{target}/usr", version, opts)
ensure
  FileUtils.rm_rf(tmp_dir)
end

.install_lilypond_files_osx(fn, target, platform, version, opts) ⇒ Object



503
504
505
506
507
508
# File 'lib/lyp/lilypond.rb', line 503

def install_lilypond_files_osx(fn, target, platform, version, opts)
  STDERR.puts "Extracting..." unless opts[:silent]
  exec "tar -xjf #{fn} -C #{target}"

  copy_lilypond_files("#{target}/LilyPond.app/Contents/Resources", version, opts)
end

.install_lilypond_files_windows(fn, target, platform, version, opts) ⇒ Object



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/lyp/lilypond.rb', line 532

def install_lilypond_files_windows(fn, target, platform, version, opts)
  STDERR.puts "Running NSIS Installer..." unless opts[:silent]

  target_dir = File.join(Lyp.lilyponds_dir, version)
  FileUtils.mkdir_p(target_dir)

  # run installer
  cmd = "#{fn} /S /D=#{target_dir.gsub('/', '\\')}"
  `#{cmd}`

  # wait for installer to finish
  t1 = Time.now
  while !File.file?("#{target_dir}/usr/bin/lilypond.exe")
    sleep 0.5
    raise "Windows installation failed" if Time.now - t1 >= 60
  end

  # Show lilypond versions
  STDERR.puts `#{target_dir}/usr/bin/lilypond -v` unless opts[:silent] || opts[:no_version_test]
end

.install_version(version, opts) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
# File 'lib/lyp/lilypond.rb', line 428

def install_version(version, opts)
  platform = detect_lilypond_platform
  url = lilypond_install_url(platform, version, opts)
  fn = temp_install_filename(url)

  download_lilypond(url, fn, opts) unless File.file?(fn)
  install_lilypond_files(fn, platform, version, opts)

  patch_font_scm(version)
  copy_fonts_from_all_packages(version, opts)
end

.invoke(argv, opts = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/lyp/lilypond.rb', line 109

def invoke(argv, opts = {})
  lilypond = current_lilypond

  case opts[:mode]
  when :system
    system("#{lilypond} #{argv.join(" ")}")
  when :spawn
    pid = spawn(lilypond, *argv, opts[:spawn_opts] || {})
    Process.detach(pid)
  else
    Kernel.exec(lilypond, *argv)
  end
end

.latest_installed_unstable_versionObject



359
360
361
362
# File 'lib/lyp/lilypond.rb', line 359

def latest_installed_unstable_version
  latest = list.reverse.find {|l| Lyp.version(l[:version]).segments[1].odd?}
  latest ? latest[:version] : nil
end

.latest_stable_versionObject



351
352
353
# File 'lib/lyp/lilypond.rb', line 351

def latest_stable_version
  search.reverse.find {|l| Lyp.version(l[:version]).segments[1].even?}[:version]
end

.latest_unstable_versionObject



355
356
357
# File 'lib/lyp/lilypond.rb', line 355

def latest_unstable_version
  search.reverse.find {|l| Lyp.version(l[:version]).segments[1].odd?}[:version]
end

.latest_versionObject



364
365
366
# File 'lib/lyp/lilypond.rb', line 364

def latest_version
  search.last[:version]
end

.lilypond_install_url(platform, version, opts) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/lyp/lilypond.rb', line 440

def lilypond_install_url(platform, version, opts)
  ext = case platform
  when /darwin/
    ".tar.bz2"
  when /linux/
    ".sh"
  when /mingw/
    ".exe"
  end
  filename = "lilypond-#{version}-1.#{platform}"

  "#{BASE_URL}/#{platform}/#{filename}#{ext}"
end

.list(opts = {}) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/lyp/lilypond.rb', line 233

def list(opts = {})
  system_list = opts[:lyp_only] ? [] : system_lilyponds
  lyp_list = opts[:system_only] ? [] : lyp_lilyponds

  default = default_lilypond
  unless default
    latest = system_list.sort(&CMP_VERSION).last || lyp_list.sort(&CMP_VERSION).last
    if latest
      default = latest[:path]
      set_default_lilypond(default)
    end
  end
  current = current_lilypond

  lilyponds = system_list + lyp_list

  lilyponds.each do |l|
    l[:default] = l[:path] == default
    l[:current] = l[:path] == current
  end

  # sort by version
  lilyponds.sort!(&CMP_VERSION)
end

.lyp_lilypond_path(version) ⇒ Object



17
18
19
# File 'lib/lyp/windows.rb', line 17

def lyp_lilypond_path(version)
  "#{Lyp.lilyponds_dir}/#{version}/usr/bin/lilypond.exe"
end

.lyp_lilypond_share_dir(version) ⇒ Object



21
22
23
# File 'lib/lyp/windows.rb', line 21

def lyp_lilypond_share_dir(version)
  File.join(Lyp.lilyponds_dir, version, 'usr/share')
end

.lyp_lilypondsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lyp/windows.rb', line 25

def lyp_lilyponds
  list = []

  Dir["#{Lyp.lilyponds_dir}/*"].each do |path|
    next unless File.directory?(path) && File.basename(path) =~ /^[\d\.]+$/
  
    root_path = path
    version = File.basename(path)
    path = File.join(path, "usr/bin/lilypond.exe")
    list << {
      root_path: root_path,
      path: path,
      version: version
    }
  end

  list
end

.parse_error_msg(msg) ⇒ Object



718
719
720
# File 'lib/lyp/lilypond.rb', line 718

def parse_error_msg(msg)
  (msg =~ /[^\n]+: error.+failed files: ".+"/m) ? $& : msg
end

.parse_lilypond_arg(arg, argv, argv_clean, options) ⇒ Object



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
# File 'lib/lyp/lilypond.rb', line 20

def parse_lilypond_arg(arg, argv, argv_clean, options)
  case arg
  when NO_ARGUMENT_OPTIONS_REGEXP
    # handle multiple options in shorthand form, e.g. -FnO
    tmp_args = []
    $1.each_char {|c| tmp_args << "-#{c}"}
    tmp_args << "-#{$2}"
    argv = tmp_args + argv
  when '-A', '--auto-install-deps'
    options[:resolve] = true
  when '-c', '--cropped'
    argv_clean += ['-dbackend=eps', '-daux-files=#f']
  when '-E', '--env'
    unless ENV['LILYPOND_VERSION']
      STDERR.puts "$LILYPOND_VERSION not set"
      exit 1
    end
    options[:use_version] = ENV['LILYPOND_VERSION']
  when '-F', '--force-version'
    options[:force_version] = true
  when '-n', '--install'
    options[:install] = true
  when '-O', '--open'
    options[:open] = true
  when '-r', '--require'
    options[:ext_require] ||= []
    options[:ext_require] << argv.shift
  when /^(?:\-r|\-\-require\=)"?([^\s]+)"?/
    options[:ext_require] ||= []
    options[:ext_require] << $1
  when '-R', '--raw'
    options[:raw] = true
  when '-S', '--snippet'
    argv_clean += ['-dbackend=eps', '-daux-files=#f', '--png', '-dresolution=600']
    options[:snippet_paper_preamble] = true
  when '-u', '--use'
    options[:use_version] = argv.shift
  when /^(?:\-u|\-\-use\=)"?([^\s]+)"?/
    options[:use_version] = $1
  else
    argv_clean << arg
  end
end

.patch_font_scm(version) ⇒ Object



574
575
576
577
578
579
# File 'lib/lyp/lilypond.rb', line 574

def patch_font_scm(version)
  return unless Lyp::FONT_PATCH_REQ =~ Lyp.version(version)

  target_fn = File.join(lyp_lilypond_share_dir(version), 'lilypond/current/scm/font.scm')
  FileUtils.cp(Lyp::FONT_PATCH_FILENAME, target_fn)
end

.patch_system_lilypond_font_scm(lilypond, opts) ⇒ Object



590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/lyp/lilypond.rb', line 590

def patch_system_lilypond_font_scm(lilypond, opts)
  return false unless Lyp::FONT_PATCH_REQ =~ Lyp.version(lilypond[:version])

  target_fn = File.join(lilypond[:data_path], '/scm/font.scm')
  # do nothing if alredy patched
  if IO.read(target_fn) == IO.read(Lyp::FONT_PATCH_FILENAME)
    return true
  end

  prompt = SYSTEM_LILYPOND_PATCH_WARNING % [lilypond[:version], target_fn]
  return unless Lyp.confirm_action(prompt)

  puts "Patching #{target_fn}:" unless opts[:silent]
  if File.writeable?(target_fn)
    FileUtils.cp(target_fn, "#{target_fn}.old")
    FileUtils.cp(Lyp::FONT_PATCH_FILENAME, target_fn)
  else
    Lyp.sudo_cp(target_fn, "#{target_fn}.old")
    Lyp.sudo_cp(Lyp::FONT_PATCH_FILENAME, target_fn)
  end
end

.preprocess_argv(argv) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/lyp/lilypond.rb', line 9

def preprocess_argv(argv)
  options = {}
  argv = argv.dup # copy for iterating
  argv_clean = []
  while arg = argv.shift
    parse_lilypond_arg(arg, argv, argv_clean, options)
  end

  [options, argv_clean]
end

.search(version_specifier = nil) ⇒ Object

Returns a list of versions of lilyponds available for download



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/lyp/lilypond.rb', line 316

def search(version_specifier = nil)
  require 'open-uri'

  platform = detect_lilypond_platform
  url = "#{BASE_URL}/#{platform}/"

  versions = []

  open(url).read.scan(/a href=\"lilypond-([0-9\.]+)[^>]+\"/) do |m|
    versions << $1
  end

  installed_versions = list.map {|l| l[:version]}
  versions.select! {|v| version_match(v, version_specifier, versions)}
  versions.map do |v|
    {
      version: v,
      installed: installed_versions.include?(v)
    }
  end
end

.select_default_lilypond!Object



200
201
202
203
204
205
206
207
208
# File 'lib/lyp/lilypond.rb', line 200

def select_default_lilypond!
  latest = system_lilyponds.sort(&CMP_VERSION).last || lyp_lilyponds.sort(&CMP_VERSION).last
  if latest
    default = latest[:path]
    set_default_lilypond(default)
  else
    raise Lyp::LILYPOND_NOT_FOUND_MSG
  end
end

.select_lilypond_version(opts, file_path) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lyp/lilypond.rb', line 66

def select_lilypond_version(opts, file_path)
  forced_version = opts[:force_version] ?
    get_file_expected_version(file_path) : opts[:use_version]

  if forced_version
    Lyp::Lilypond.install_if_missing(forced_version) if opts[:install]
    Lyp::Lilypond.force_version!(forced_version)
  end

  Lyp::Lilypond.check_lilypond!

  opts[:lilypond_version] = current_lilypond_version

  Lyp::Lilypond.current_lilypond.tap do |path|
    unless path && File.file?(path)
      STDERR.puts "No version of lilypond found. To install lilypond run 'lyp install lilypond'."
      exit 1
    end
  end
rescue => e
  STDERR.puts e.message
  exit 1
end

.session_settings_filenameObject



220
221
222
# File 'lib/lyp/lilypond.rb', line 220

def session_settings_filename
  "#{Lyp::TMP_ROOT}/session.#{Process.getsid}.yml"
end

.set_current_lilypond(path) ⇒ Object



159
160
161
162
163
# File 'lib/lyp/lilypond.rb', line 159

def set_current_lilypond(path)
  settings = get_session_settings
  settings[:current] = path
  set_session_settings(settings)
end

.set_default_lilypond(path) ⇒ Object



127
128
129
# File 'lib/lyp/lilypond.rb', line 127

def set_default_lilypond(path)
  Lyp::Settings['lilypond/default'] = path
end

.set_session_settings(settings) ⇒ Object



214
215
216
217
218
# File 'lib/lyp/lilypond.rb', line 214

def set_session_settings(settings)
  File.open(session_settings_filename, 'w+') do |f|
    f << YAML.dump(settings)
  end
end

.system_lilypondsObject



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/lyp/lilypond.rb', line 278

def system_lilyponds
  list = get_system_lilyponds_paths
  return list if list.empty?

  list.inject([]) do |m, path|
    begin
      resp = `#{path} #{Lyp::DETECT_SYSTEM_LILYPOND_FILENAME} 2>/dev/null`

      if resp =~ /(.+)\n(.+)/
        version, data_path = $1, $2
        m << {
          root_path: File.expand_path(File.join(File.dirname(path), '..')),
          data_path: data_path,
          path: path,
          version: $1,
          system: true
        }
      end
    rescue
      # ignore error, don't include this version in the list of lilyponds
    end
    m
  end
end

.temp_install_filename(url) ⇒ Object



454
455
456
457
# File 'lib/lyp/lilypond.rb', line 454

def temp_install_filename(url)
  u = URI(url)
  "#{Lyp::TMP_ROOT}/#{File.basename(u.path)}"
end

.uninstall(version_specifier, opts = {}) ⇒ Object



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# File 'lib/lyp/lilypond.rb', line 673

def uninstall(version_specifier, opts = {})
  list = list(lyp_only: true)
  if version_specifier
    list.select! {|l| version_match(l[:version], version_specifier, list)}
  elsif !opts[:all]
    # if no version is specified
    raise "No version specifier given.\nTo uninstall all versions run 'lyp uninstall lilypond -a'.\n"
  end

  if list.empty?
    if version_specifier
      raise "No lilypond found matching #{version_specifier}"
    else
      raise "No lilypond found"
    end
  end

  list.each do |l|
    puts "Uninstalling lilypond #{l[:version]}" unless opts[:silent]
    set_current_lilypond(nil) if l[:current]
    set_default_lilypond(nil) if l[:default]
    uninstall_lilypond_version(l[:root_path])
  end
end

.uninstall_lilypond_version(path) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lyp/windows.rb', line 54

def uninstall_lilypond_version(path)
  # run installer
  uninstaller_path = File.join(path, 'uninstall.exe')
  if File.file?(uninstaller_path)
    cmd = "#{uninstaller_path} /S _?=#{path.gsub('/', '\\')}"
    `#{cmd}`
  
    # wait for installer to finish
    t1 = Time.now
    while !File.directory?("#{target_dir}/usr")
      sleep 0.5
      raise "Uninstallation failed" if Time.now - t1 >= 60
    end
  end

  FileUtils.rm_rf(path)
end

.use(version, opts) ⇒ Object



638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
# File 'lib/lyp/lilypond.rb', line 638

def use(version, opts)
  lilypond_list = list.reverse

  case version
  when 'system'
    lilypond = lilypond_list.find {|v| v[:system] }
    unless lilypond
      raise "Could not find a system installed version of lilypond"
    end
  when 'latest'
    lilypond = lilypond_list.first
  when 'stable'
    lilypond = lilypond_list.find do |v|
      Lyp.version(v[:version]).segments[1].even?
    end
  when 'unstable'
    lilypond = lilypond_list.find do |v|
      Lyp.version(v[:version]).segments[1].odd?
    end
  else
    version = "~>#{version}.0" if version =~ /^\d+\.\d+$/
    req = Lyp.version_req(version)
    lilypond = lilypond_list.find {|v| req =~ Lyp.version(v[:version])}
  end

  unless lilypond
    raise "Could not find a lilypond matching \"#{version}\""
  end

  set_current_lilypond(lilypond[:path])
  set_default_lilypond(lilypond[:path]) if opts[:default]

  lilypond
end

.valid_lilypond?(path) ⇒ Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/lyp/lilypond.rb', line 196

def valid_lilypond?(path)
  (File.file?(path) rescue nil) && (`#{path} -v` =~ /^GNU LilyPond/)
end

.version_match(version, specifier, all_versions) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/lyp/lilypond.rb', line 338

def version_match(version, specifier, all_versions)
  case specifier
  when 'latest'
    version == all_versions.last
  when 'stable'
    Lyp.version(version).segments[1].even?
  when 'unstable'
    Lyp.version(version).segments[1].odd?
  else
    Lyp.version_req(specifier) =~ Lyp.version(version)
  end
end