<commit_before>require 'uri'
require 'open3'
require 'ruby-progressbar'

module Lyp::Lilypond
  class << self
    NO_ARGUMENT_OPTIONS_REGEXP = /^\-([REnFOcSA]+)(.+)/

    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

    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

    VERSION_STATEMENT_REGEX = /\\version "([^"]+)"/

    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!
      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

    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

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

      invoke(argv, opts)
    end

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

      case opts[:mode]
      when :system
        system("#{lilypond} #{argv.join(" ")}")
      else
        Kernel.exec(lilypond, *argv)
      end
    end

    def default_lilypond<commit_msg>Add support for spawn mode when invoking lilypond<commit_after>require 'uri'
require 'open3'
require 'ruby-progressbar'

module Lyp::Lilypond
  class << self
    NO_ARGUMENT_OPTIONS_REGEXP = /^\-([REnFOcSA]+)(.+)/

    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

    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

    VERSION_STATEMENT_REGEX = /\\version "([^"]+)"/

    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!
      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

    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

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

      invoke(argv, opts)
    end

    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

    def default_lilypond
{'fraction_non_alphanumeric': 0.14620141342756185, 'fraction_numerical': 0.0035335689045936395, 'mean_word_length': 2.169855342977135, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1734295', 'n_tokens_mistral': 2304, 'n_tokens_neox': 2287, 'n_words': 497}