sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    [Code] !autoload.rb loads "!_autoload" folders - v3.0.0

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 2 Posters 1.7k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by Dan Rathbun


      [ Code ] !autoload.rb loads "!_autoload" folders


      If you find this useful, please donate:PayPalButton


      This is about cleaning up the clutter in the Plugins folder.


      This script is meant to be personally modified, especially the paths, for individual tastes.
      (The Mac paths are only a poor example on my part.)

      You can also see that there are empty sections, where you can add code.
      Consider this an Example. Tweak it to your own hearts desire.

      #  ----------------------------------------------------------------------------
      #   File; !autoload.rb 
      #{ ----------------------------------------------------------------------------
      #   Prepared by;  Dan Rathbun
      #  ----------------------------------------------------------------------------
      #{  Versions;
      #
      #    1.0.0  ;  2011-08-09
      #
      #    1.1.0  ;  2011-08-09
      #
      #    2.0.0  ;  2012-02-19
      #    |
      #    ~ Changed order of load for user documents plugin paths.
      #        Version specific path now takes precedence over Common path.
      #    ~ Made sure the $LOAD_PATH array is uniqued.
      #    ~ Show Mac paths with whitespace filled with '_' character, for making
      #        running shell scripts easier.
      #    ~ Updated Notes section.
      #    ~ All document header sections now expand and collapse (in Notepad++.)
      #
      #    3.0.0  ;  2012-09-28
      #    |
      #    ~ Added begin .. rescue block to prevent script loading loop from
      #       short-circuiting when an error occurs in a file.
      #
      #} ============================================================================
      #{  WARRANTY / DISCLAIMER;
      #
      #   THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
      #   IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
      #   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
      #  ----------------------------------------------------------------------------
      #   License / Terms of Use;
      #
      #   Public Domain
      #}
      # ----------------------------------------------------------------------------
      #{  NOTES;  
      #
      #   (1) Place this configuration script in the Sketchup "Plugins" folder.
      #
      #       Create the verdir and comdir directories manually, if you wish to use
      #       these paths, then edit the verdir and comdir paths to suit your setup.
      #
      #   * The directories must PRE-EXIST, or they will NOT be added to $LOAD_PATH.
      #
      #   *** Be sure to save this file as "UTF-8 without BOM" / "ANSI as UTF-8" if
      #         your directory paths conatain any UTF-8 characters. Also recommend
      #         that you set end of Line characters to UNIX.
      #
      #   (2) You can now move any scriptlets out of the Plugins folder, and into
      #       a new subfolder (of any of the $LOAD_PATH paths,) named "!_autoload".
      #
      #       Extension registration scripts can go in "!_autoload" as long as they
      #       use relative paths, and NOT absolute paths.
      #
      #   (3) True Plugins should go in a subfolder of their own, and even better if
      #       they are in a subfolder, of the "Author's" folder.
      #}
      #} ============================================================================
      
      
      require("langhandler.rb")
      require("sketchup.rb")
      require("extensions.rb")
      require("dynamiccomponents.rb")
      
      #show_ruby_panel() # from "sketchup.rb"
      
      
      # ----------------------------------------------------------------------------
      #  Push any additional Search paths (that may have a "!_autoload" subfolder,)
      #    into the $LOAD_PATH array below. 
      # ----------------------------------------------------------------------------
      
      ### The directories must PRE-EXIST, or they will NOT be added to $LOAD_PATH. 
      
      if RUBY_PLATFORM =~ /(mswin|mingw)/ # MS Windows
      
        # Create HOME environment var if not defined.
        # Allows ~ (tilde) path expansion using File.expand_path("~/some/dir/path").
        ENV['HOME']=ENV['USERPROFILE'] unless ENV['HOME']
      
        # Example; Windows User Documents Current Version Plugins path;
        verdir = File.expand_path("~/My Documents/Google Sketchup/Sketchup #{Sketchup.version.to_i}/Plugins")
        $LOAD_PATH << verdir if Kernel.test(?d,verdir)
      
        # Example; Windows User Documents Common (all versions) Plugins path;
        comdir = File.expand_path("~/My Documents/Google Sketchup/Common/Plugins")
        $LOAD_PATH << comdir if Kernel.test(?d,comdir)
      
        #
        # Add additional paths here.
        #
      
        $LOAD_PATH.uniq!
      
      end
      
      if RUBY_PLATFORM =~ /(darwin)/ # Mac OSX
      
        # Example; Mac User Documents Current Version Plugins path;
        verdir = File.expand_path("~/Documents/Google_Sketchup/Sketchup_#{Sketchup.version.to_i}/Plugins")
        $LOAD_PATH << verdir if Kernel.test(?d,verdir)
      
        # Example; Mac User Documents Common (all versions) Plugins path;
        comdir = File.expand_path("~/Documents/Google_Sketchup/Common/Plugins")
        $LOAD_PATH << comdir if Kernel.test(?d,comdir)
      
        #
        # Add additional paths here.
        #
      
        $LOAD_PATH.uniq!
      
      end
      
      # ----------------------------------------------------------------------------
      #  Load all "!_autoload" folders, that reside in any of the $LOAD_PATH
      #    array folders.
      # ----------------------------------------------------------------------------
      
      $LOAD_PATH.each do |abspath|
        #
        autodir = "!_autoload"
        #
        autopath = File.join(abspath,autodir)
        #
        if Kernel.test(?d,autopath)
          #
          Dir.new( autopath ).each { |file|
            ext = File.extname(file)
            next if ext.empty? || ext[-1,1]=="!" # skip if ends in '!'
            begin
              if ext == ".rbs"
                Sketchup.require( "#{autodir}/#{file}" )
              elsif [".rb",".rbw",".o",".so",".dll",".dylib",".bundle"].include?(ext)
                require( "#{autodir}/#{file}" )
              end
            rescue Exception => e
              puts("File; '#{autodir}/#{file}' did not load;")
              puts("Error; #<#{e.class.name}; #{e.message}>")
              puts e.backtrace unless $VERBOSE.nil?
            else
              puts("File; '#{autodir}/#{file}' loaded successfully.")
            end
          }
        end #if autopath
      end
      
      # ----------------------------------------------------------------------------
      # Any other require() calls to load scripts in subfolders,
      #  or in the Ruby lib path subfolders... etc.
      # ----------------------------------------------------------------------------
      
      
      
      # ----------------------------------------------------------------------------
      #  Any additional configuration calls
      # ----------------------------------------------------------------------------
      
      
      
      

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by


        Updated : v1.1.0


        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by


          Updated : v2.0.0


          See file header for changes.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by


            REVISION PLANNED - Done (bumped to v2.0.0)


            1. First off... I booboo'd. The order of the paths are reversed.

            The version path should take precedence, over the common path.

            Consider a plugin "Widget" with features that work on all releases of SU 6 and 7.
            It goes in the "Common/Plugins"
            Then the author updates it to add nifty features for SU 8.
            What do you do with it?
            You put it the "Version 8/Plugins" so WHEN you run SU8, it loads instead of the older common version.
            But when you run 7 or 6, to deal with old files, etc., they will not see the newer version, and not having a specific version of their own, they'll load the older version from "Common/Plugins".

            1. John (driven) suggests to replace whitespace in Mac paths with "_" to make running shell scripts easier.

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by


              Updated : v3.0.0


              Added a begin .. rescue block to prevent script loading
              loop from short-circuiting when an error occurs.

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • renderizaR Offline
                renderiza
                last edited by

                This is very useful information…Thanks!

                Question…

                Is the “.rb” extension really necessary when using for example “require (“sketchup.rb”)?

                Can it just be “require (“sketchup”)?

                Again Thanks

                [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  @unknownuser said:

                  Is the “.rb” extension really necessary when using for example “require (“sketchup.rb”)?

                  The answer is not very simple.

                  You should read the method dictionary for Kernel.require(), Kernel.load(), Sketchup.require() and Sketchup.load().

                  These methods act differently:

                  • depending on the arguments (absolute or relative paths)
                  • depending upon the Ruby version
                  • depending whether a file extension is given

                  The require() methods push successful paths into the $LOADED_FEATURES array, but may not add the actual extension of the file that was loaded. (This could cause confusion later.)

                  The overridden methods in the Sketchup module do not act exactly the same as the global Kernel methods. (return values and filenames in Exception messages.)

                  We almost need a complicated decision tree with all the permutations... to decide when to use a extension and when not to.

                  It is recommended NOT to use a file extension with the Sketchup module overrides. (This also helps in development, as the same loader script can load both rb and rbs files, after scrambling.)
                  Also be aware that the SketchupExtension class uses these overides.

                  This is really an issue for it own topic thread (and perhaps there already is one?)

                  ❓

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  Buy SketchPlus
                  Buy SUbD
                  Buy WrapR
                  Buy eBook
                  Buy Modelur
                  Buy Vertex Tools
                  Buy SketchCuisine
                  Buy FormFonts

                  Advertisement