Skip to content

Commit

Permalink
Allow Lilypond version selection in exec command (close #60)
Browse files Browse the repository at this point in the history
Pass verbose flag to lilypond when compiling (see #57, #58)
  • Loading branch information
noteflakes committed Feb 17, 2018
1 parent 1dff6dc commit 3e54a7d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source "http://rubygems.org"

gem "httpclient", "~>2.7.1"
gem "ruby-progressbar", "~>1.7.5"
gem "thor", "~>0.19.1"
gem "thor", "~>0.20.0"
gem "directory_watcher", "1.5.1"
gem "rugged", ">=0.23.0", platforms: [:ruby]

Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ GEM
rspec-support (3.2.2)
ruby-progressbar (1.7.5)
rugged (0.23.0)
thor (0.19.1)
thor (0.20.0)

PLATFORMS
ruby
Expand All @@ -30,7 +30,7 @@ DEPENDENCIES
rspec (= 3.2.0)
ruby-progressbar (~> 1.7.5)
rugged (>= 0.23.0)
thor (~> 0.19.1)
thor (~> 0.20.0)

BUNDLED WITH
1.11.2
39 changes: 35 additions & 4 deletions docs/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,18 @@ Compiles Lilypond source files. This command is synonymous to running `lilypond`

- `--force-version`/`-F`: use Lilypond version specified in user file
- `--install`/`-n`: install version of Lilypond if not found (if the required version was overriden using any of `--env`, `--force-version` or `--use`)
- `--open`/`-O`: open output file after compilation
- `--music=<MUSIC>`/`-M<MUSIC>`: compile the given Lilypond code:

```bash
$ lyp c -M"c'4 e' g' e' c'1" -o test
```

- `--music-relative=<MUSIC>`/`-m<MUSIC>`: compile the given Lilypond code in relative pitch:

```bash
$ lyp c -m"c4 e g e c1" -o test
```

- `--raw`/`-R`: run Lilypond "raw" (no pre-processing of dependencies)
- `--require=PACKAGE`/`-rPACKAGE`: preload the specified package

Expand All @@ -489,11 +500,26 @@ Shows dependencies for a given source file.

### lyp exec

Synopsis: `lyp exec SCRIPT ...`
Synopsis: `lyp exec ... SCRIPT ...`

Shorthand: `lyp x`

Runs a Lilypond script (using the currently selected version of Lilypond).
Runs a Lilypond script (using the currently selected version of Lilypond). The `exec` command accepts the following options:

- `--env`/`-E`: use the Lilypond version specified in the `LILYPOND_VERSION` environment variable:

```bash
$ LILYPOND_VERSION=2.19.50 lyp x midi2ly ...
```

- `--install`/`-n`: install version of Lilypond if not found (if the required version was overriden using any of `--env`, `--force-version` or `--use`)
- `--use=VERSION`/`-uVERSION`: use the given version of Lilypond:

```bash
$ lyp x -u2.19.53 midi2ly...
# automatically install given version of Lilypond
$ lyp c -nu2.19.53 midi2ly ...
```

### lyp flatten

Expand All @@ -507,7 +533,12 @@ Synopsis: `lyp install PACKAGE|lilypond@VERSION`

Shorthand: `lyp i`

Installs a package or a Lilypond. See <a href="#" jump-to-id="installing-packages">installing packages</a> and <a href="#" jump-to-id="installing-and-using-lilypond">installing versions of Lilypond</a> above.
Installs a package or a Lilypond. See <a href="#" jump-to-id="installing-packages">installing packages</a> and <a href="#" jump-to-id="installing-and-using-lilypond">installing versions of Lilypond</a> above. The `install` command accepts the following options:

- `--default`/`-d`: make the version to install the default version (when installing Lilypond)
- `--dev`: install the current working directory as a development package
- `--test`/`-t`: run package tests after installation (when installing a package)
- `--update`/`-u`: remove any old versions (when installing a package)

### lyp list

Expand Down
15 changes: 12 additions & 3 deletions lib/lyp/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Lyp::CLI < Thor
"w" => :watch,
"x" => :exec

check_unknown_options! :except => [:compile, :watch, :benchmark]
check_unknown_options! :except => [:compile, :exec, :watch, :benchmark]
class_option :verbose, aliases: '-V', :type => :boolean, desc: 'show verbose output'

no_commands do
Expand Down Expand Up @@ -129,9 +129,10 @@ def cleanup
def compile(*argv)
opts, argv = Lyp::Lilypond.preprocess_argv(argv)
opts[:verbose] ||= options[:verbose]
argv.unshift('-V') if options[:verbose]
$cmd_options = opts

lilypond_path = Lyp::Lilypond.select_lilypond_version(opts, argv.last)
Lyp::Lilypond.select_lilypond_version(opts, argv.last)

$stderr.puts "Lyp #{Lyp::VERSION}" unless opts[:mode] == :quiet
Lyp::System.test_installed_status!
Expand All @@ -156,7 +157,15 @@ def deps(fn)

desc "exec <CMD> [<options>...]", "Execute a lilypond script"
def exec(*argv)
$stderr.puts "Lyp #{Lyp::VERSION}"
opts, argv = Lyp::Lilypond.preprocess_bin_script_argv(argv)
opts[:verbose] ||= options[:verbose]
$cmd_options = opts

$cmd_options = opts

Lyp::Lilypond.select_lilypond_version(opts, argv.last)

$stderr.puts "Lyp #{Lyp::VERSION}" unless opts[:mode] == :quiet
Lyp::System.test_installed_status!
Lyp::Lilypond.invoke_script(argv, {})
end
Expand Down
47 changes: 45 additions & 2 deletions lib/lyp/lilypond.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def parse_lilypond_arg(arg, argv, argv_clean, options)
when '-S', '--snippet'
argv_clean.concat ['-dbackend=eps', '-daux-files=#f', '--png', '-dresolution=600']
options[:snippet_paper_preamble] = true
when '--svg'
argv_clean.concat ['-dbackend=svg']
when '-u', '--use'
options[:use_version] = argv.shift
when '-V', '--verbose'
Expand All @@ -75,13 +77,50 @@ def parse_lilypond_arg(arg, argv, argv_clean, options)
options[:mode] = :system
when '--invoke-quiet'
options[:mode] = :quiet
when '--svg'
argv_clean.concat ['-dbackend=svg']
else
argv_clean << arg
end
end

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

[options, argv_clean]
end

def parse_lilypond_bin_script_arg(arg, argv, argv_clean, options)
case arg
when ARGUMENT_EQUAL_REGEXP
argv.insert(0, "--#{$1}", $2)
when ARGUMENT_SHORTHAND_REGEXP
argv.insert(0, "-#{$1}", $2)
when NO_ARGUMENT_SHORTHAND_REGEXP
tmp_args = []
$1.each_char {|c| tmp_args << "-#{c}"}
tmp_args << "-#{$2}"
argv.insert(0, *tmp_args)
when '-E', '--env'
unless ENV['LILYPOND_VERSION']
raise "$LILYPOND_VERSION not set"
end
options[:use_version] = ENV['LILYPOND_VERSION']
when '-n', '--install'
options[:install] = true
when '-u', '--use'
options[:use_version] = argv.shift
when '--invoke-system'
options[:mode] = :system
else
argv_clean << arg
argv_clean += argv
end
end

def prepare_inline_music_file(music, opts = {})
filename = Lyp.tmp_filename('.ly')
File.open(filename, 'w+') do |f|
Expand Down Expand Up @@ -149,6 +188,10 @@ def compile(argv, opts = {})
def invoke(argv, opts = {})
lilypond = current_lilypond

if opts[:verbose]
puts "#{lilypond} #{argv.join(" ")}"
end

case opts[:mode]
when :system
system("#{lilypond} #{argv.join(" ")}")
Expand Down

0 comments on commit 3e54a7d

Please sign in to comment.