[code] Backquote method patch as Mixin module

Ref topic: Error running Windows Command Line commands within Ruby plugin

Ref Ruby documentation for the Kernel#`() method.


Here is my old Kernel#backquote patch code, re-written as a mixin module:

backquote_patch_mixin_1_0_0.rb (2.3 KB)

# encoding: UTF-8

module Author

  # To test, call from Ruby Console:
  # Author::ThisPlugin::test()
  #
  module BackquotePatch # mixin module

    @@backquote_patch_version = '1.0.0'

    @@debug_backquote = false unless defined?(@@debug_backquote)

    def self::debug=(arg)
      @@debug_backquote = arg ? true : false
    end

    def self::debug?
      @@debug_backquote
    end

    def self::patch(obj)
      if obj.class == Module
        obj.module_eval {
          include(BackquotePatch) 
          extend(BackquotePatch)
        }
      elsif obj.class == Class
        obj.class_eval {
          include(BackquotePatch) 
          extend(BackquotePatch)
        }
      else # just a instance object
        obj.extend(BackquotePatch)
      end
    end

    ### MIXIN METHODS
    #
      def backquote(cmd)
        #
        puts "\n#{Module.nesting[0].name}: In method 'backquote'\n" if @@debug_backquote
        #
        tempfile = nil
        raise(TypeError,'string argument expected.',caller) unless cmd.is_a?(String)
        #
        tempfile = ENV['TMP'].encode('UTF-8').gsub('\\','/')
        tempfile << '/backquote.out'
        #
        ::Kernel::method('`').call("#{cmd} > #{tempfile}")
        return File.read(tempfile)
        #
      rescue
        raise
      ensure
        if tempfile && File::exist?(tempfile)
          File::delete(tempfile) rescue nil
        end
      end

      # Override the private instance method #`() that all objects inherit,
      #  and rewire %x delimited execute strings, to use our backquote() patch:
      alias_method("`",:backquote)
    #
    ###
  
  end # mixin module

  module ThisPlugin
    
    if RUBY_VERSION.to_f > 1.8 &&  # Test if running Ruby 1.9+,
    RUBY_PLATFORM !~ /darwin/i &&  # ... and not on Mac OSX,
    ( `ver`.empty? || # ... and the global backquote is bugged,
      BackquotePatch::debug? ) # ... or we are testing.
    
      include BackquotePatch # mixin as instance methods
      extend  BackquotePatch # mixin as singelton methods
    
    end # if global backquote method returns nothing.

    def self::test()
      UI.messagebox(`dir`,MB_MULTILINE,"Current Directory Listing")
    end

  end # this particular plugin sub-module

end # author's outer namespace module
1 Like

I know this is an old thread, but I get an invalid byte sequence in utf-8 (argumenterror) in Sketchup 2018.
It seems backquotes still evaluate to nil in SU2018.

What is awkward:
create file test.rb in SU2018 plugins folder containing:

require 'sketchup'
UI.messagebox(`echo hello`)

Launch SketchUp and an empty messagebox will be shown on my windows machine. However, if I Iaunch Sketchup from cmd, the messagebox will show the output.

Thus, double clicking a shortcut pointing to “C:\Program Files\SketchUp\SketchUp 2018\SketchUp.exe” behaves different than executing “C:\Program Files\SketchUp\SketchUp 2018\SketchUp.exe” in cmd…

I believe this is a known and logged regression issue, Ken.
(This original thread was meant to deal temporarily with either SU2015 or 2016.)

In that discussion I made metion of a statement Thomas made in these forums, but I cannot find it via search.

1 Like

The API’s UI::messagebox code has always been known to fail silently. Often the messagebox does not even show (as an empty window.)

We’ve always tried to steer coders away from using a messagebox for testing, and toward outputting to either console (if it’s not broken,) or a log file, etc. (On Windows you can use OutputDebugString and sends strings to MS DebugView.)

Yes, but in this case it is not the message box failing, which is used in this case as a way to provide fast feedback, it’s the backticks that fail. When launching sketchup from command line there seems to be no problem at all executing system commands and retrieving their output. When launching as a normal user would, executing system commands return nil.

Understood. We’ve had these problems before, but for some reason checking %x (backquote) execute strings and the console, (and sometimes unicode paths) seems to get broken every few versions.

@thomthom, checking these need to get added as a procedural item for each cycle.

But this is not even version related. If I boot Sketchup using Command Line, it al works fine. If I launch that very same version of Sketchup double clicking a shortcut it doesn’t work.

If you can show that this happens in EVERY version, then it isn’t version related. But I am saying we’ve had strange bugs happen in some past versions, based upon how SketchUp was started (ie, double-clicking an SKP file, or starting from a shortcut, and running as a normal user, or running SketchUp as an administrator. Etc.)

Hey Ken, feel free to open a specific issue in the GitHub tracker on what you feel is wrong or different than any other issue already logged. (“Squeaky wheel gets the grease”.)

So I did here.
I have also uploaded a youtube movie https://www.youtube.com/watch?v=G_nveWVCo18&feature=youtu.be

1 Like