Sha256: 012df86cf87fdad183016ab4c7c499ab4466ec917bab98e5bf22b4a53a752b53

Contents?: true

Size: 1.78 KB

Versions: 12

Compression:

Stored size: 1.78 KB

Contents

# -*- coding: utf-8 -*-
# Copyright (C) 2010 Rocky Bernstein <rockyb@rubyforge.net>
require 'tempfile'
require 'linecache'
require_relative '../../base/subsubcmd'
require_relative '../substitute'

class Trepan::SubSubcommand::SetSubstituteString < Trepan::SubSubcommand
  unless defined?(HELP)
    HELP         = 
'set substitute string FROM-FILE STRING-VAR

Use the contents of string variable STRING-VAR as the source text for
FROM-FILE.  If a substitution rule was previously set for FROM-FILE,
the old rule is replaced by the new one.

If "." is given for FROM_FILE, the current instruction sequence name is used.'
    MIN_ABBREV   = 'st'.size  
    MAX_ARGS     = 2
    NAME         = File.basename(__FILE__, '.rb')
    SHORT_HELP   = 'Use STRING in place of an filename'
    PREFIX       = %w(set substitute string)
  end

  def run(args)
    if args.size != 3
      errmsg "This command needs 2 arguments, got #{args.size-1}."
      return
    end
    from_path = 
      if '.' == args[1]
        @proc.frame.iseq.source_container[1]
      else
        from_path = args[1]
      end
    
    to_str = args[2]
    val = @proc.debug_eval_no_errmsg(to_str)

    if val
      tempfile = Tempfile.new(["#{from_path}-#{to_str}-", '.rb'])
      tempfile.open.puts(val)
      @proc.remap_container[['string', from_path]] = ['file', tempfile.path]
      tempfile.close
      LineCache::cache(tempfile.path)
    else
      errmsg "Can't get value for #{to_str}"
    end
  end
end

if __FILE__ == $0
  # Demo it.
  require_relative '../../../mock'
  require_relative '../substitute'
  cmd = MockDebugger::subsub_setup(Trepan::SubSubcommand::SetSubstitute,
                                   Trepan::SubSubcommand::SetSubstituteString,
                                   false)
  cmd.run([cmd.name, '/tmp/foo.rb', 'a=1;b=2'])
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
trepanning-2.15.35 processor/command/set_subcmd/substitute_subcmd/string.rb
trepanning-1.93.35 processor/command/set_subcmd/substitute_subcmd/string.rb
trepanning-2.15.33 processor/command/set_subcmd/substitute_subcmd/string.rb
trepanning-1.93.32 processor/command/set_subcmd/substitute_subcmd/string.rb
trepanning-0.1.6 processor/command/set_subcmd/substitute_subcmd/string.rb
trepanning-0.1.4 processor/command/set_subcmd/substitute_subcmd/string.rb
trepanning-0.1.3 processor/command/set_subcmd/substitute_subcmd/string.rb
trepanning-0.1.2 processor/command/set_subcmd/substitute_subcmd/string.rb
trepanning-0.1.1 processor/command/set_subcmd/substitute_subcmd/string.rb
trepanning-0.1.0 processor/command/set_subcmd/substitute_subcmd/string.rb
trepanning-0.0.9 processor/command/set_subcmd/substitute_subcmd/string.rb
trepanning-0.0.8 processor/command/set_subcmd/substitute_subcmd/string.rb