[Code] !autoload.rb loads "!_autoload" folders - v3.0.0
-
[ Code ] !autoload.rb loads "!_autoload" folders
If you find this useful, please donate:
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 # ----------------------------------------------------------------------------
-
Updated : v1.1.0
-
Updated : v2.0.0
See file header for changes.
-
REVISION PLANNED - Done (bumped to v2.0.0)
- 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".- John (driven) suggests to replace whitespace in Mac paths with "_" to make running shell scripts easier.
-
Updated : v3.0.0
Added a
begin
..rescue
block to prevent script loading
loop from short-circuiting when an error occurs. -
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
-
@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()
andSketchup.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 globalKernel
methods. (return values and filenames inException
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 theSketchupExtension
class uses these overides.This is really an issue for it own topic thread (and perhaps there already is one?)
Advertisement