Sha256: 62855be25f6a7a3350abedcc72ec3a6631859c644429711674baafdbe73035bf

Contents?: true

Size: 1.33 KB

Versions: 8

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module Conductor
  # Script runner
  class Script
    attr_reader :args, :path

    def initialize(script)
      parts = Shellwords.split(script)
      self.path = parts[0]
      self.args = parts[1..].join(' ')
    end

    def path=(path)
      @path = if path =~ %r{^[%/]}
                File.expand_path(path)
              else
                script_dir = File.expand_path('~/.config/conductor/scripts')
                if File.exist?(File.join(script_dir, path))
                  File.join(script_dir, path)
                elsif TTY::Which.exist?(path)
                  TTY::Which.which(path)
                else
                  raise "Path to #{path} not found"

                end
              end
    end

    def args=(array)
      @args = if array.is_a?(Array)
                array.join(' ')
              else
                array
              end
    end

    def run
      stdin = Conductor.stdin

      raise 'Script path not defined' unless @path

      use_stdin = true
      if args =~ /\$\{?file\}?/
        use_stdin = false
        args.sub!(/\$\{?file\}?/, Env.env[:filepath])
      else
        raise 'No input' unless stdin

      end

      if use_stdin
        `echo #{Shellwords.escape(stdin)} | #{Env} #{path} #{args}`
      else
        `#{Env} #{path} #{args}`
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
marked-conductor-1.0.7 lib/conductor/script.rb
marked-conductor-1.0.6 lib/conductor/script.rb
marked-conductor-1.0.5 lib/conductor/script.rb
marked-conductor-1.0.4 lib/conductor/script.rb
marked-conductor-1.0.3 lib/conductor/script.rb
marked-conductor-1.0.2 lib/conductor/script.rb
marked-conductor-1.0.1 lib/conductor/script.rb
marked-conductor-1.0.0 lib/conductor/script.rb