How do I install Rubocop for SU 2020.1?

I’d like to use Rubocop to check the code of a plugin I’m writing. I’ve tried searching this forum, but haven’t found many references to Rubocop and nothing the clarifies for me where or how I should install it.

I’ve downloaded rubocop-sketchup-master.zip from GitHub following a link in one of the few references to Rubocop that I could find on this forum, and extracted it in my Downloads folder to rubocop-sketchup-master.

I expect it has to be copied somewhere (on MacOS Mojave 10.14.6) - but where?

The instructions say only:

gem install rubocop-sketchup

but don’t say where I should run that command.

Tried using that command in the Sketchup Ruby console (SU 2020.1), but it gives an error

> gem install rubocop
Error: #<NameError: undefined local variable or method `rubocop' for main:Object>
<main>:in `<main>'
SketchUp:1:in `eval'

Is there a simple guide to installation and use that I’m just not finding?

I’ve looked at the instructions at
https://rubocop-sketchup.readthedocs.io/en/latest/installation/

but they seem to assume a level of knowledge about this that I just don’t have.

They say I need to have Ruby 2.2+ installed, which I think I already do have on any recent MacOS installation, but I can’t find where it is. Nor am I clear if Rubocop should link to that, or to the SU specific Ruby installed by the SU installer.

Thanks for any link to more specific instructions, or for suggestions on how to get it to work.

Rubocop is not a SketchUp extension, it is a separate program that is used independently from SketchUp and is run from command line (or code editor integration) on a folder path containing ruby files.

You downloaded an archive of source code. Source code can be (compiled if necessary and) run from the directory where ever it is contained.

This is a command to install a release of Rubocop (alternatively to downloading the source code). This is a shell command, that means it needs to be run in a terminal. It looks different from valid Ruby language, that’s why the Ruby Console doesn’t understand it. If technical instructions don’t specifically mention this, one can assume a command is a shell command (often indicated by $ signs that are not part of the command). Shell commands are often shorter and easier to reproduce than describing locations of mouse clicks.

When you open the terminal program (and Ruby and Ruby Gems are installed on the system), you can copy-paste this command and press Enter.

In the terminal you navigate to the folder where you develop the SketchUp extension (edit the path):

cd /Users/john/my/cool/ruby/project

Then you run rubocop:

rubocop -r rubocop-sketchup
1 Like

Possibly another reason for SU to include Ruby executables…

Thank you.

I’ll try to follow that.

It’s the only link to a download that there is on GitHub, and when expanded, I can see it holds a range of directories and files.

I opened a Terminal window, changed directory to the downloaded and unzipped rubocop-sketchup-master folder, and ran

gem install rubocop.

That ran, but raised a permissions error:

Johns-iMac:rubocop-sketchup-master JohnWMcC$ gem install rubocop
Fetching: parallel-1.19.1.gem (100%)
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.

So I went down the chain of directories, and gave myself (user JohnWMcC) Read & Write permissions as far as the gem folder. just below 2.3.0.

Now when I cd to the rubocop... directory and run
gem install rubocop it no longer runs anything but responds with

Johns-iMac:rubocop-sketchup-master JohnWMcC$ Johns-iMac:rubocop-sketchup-master JohnWMcC$ gem install rubocop
-bash: Johns-iMac:rubocop-sketchup-master: command not found
Johns-iMac:rubocop-sketchup-master JohnWMcC$ 

I’m stuck at this point, but will leave it for now.

What am I doing wrong? Can’t see it myself for the moment.

The thing ending with the dollar sign is called a “prompt” and just tells you some more or less useful info (computer name, current user name and working directory). You accidentially copied it and executed it as if it was a valid shell command.

The instructions don’t say anything about downloading. The gem command (alternative to manual downloading) does the downloading for you. It is also often not a good idea to change permissions of system files. Instead, you can either run a command as root user, or tell this specific command to install into the user directory so that it does not attempt to touch files where it has no permissions to do so.
Try:

gem install rubocop-sketchup --user

Thanks again, @Aerilius.

I’ll reverse the permission changes, and try again from the command line.

I do know about the $ prompt and didn’t include it in any of the commands. But thanks for the reminder.

I did not know that gem install ... does the downloading for you.

That’s all becoming clearer.

I think the instructions could have explained more, for someone like me who doesn’t know these things.

Maybe:

gem install rubocop-sketchup --user-install

Still not quite there. Rubocop has dropped support for Ruby before v2.4, and I have 2.3,

cd'd to the 2.30.gems folder then ran the command (after the $ sign)

gem install rubocop --user

Output (including prompt, to show you where I was when running the command)

Johns-iMac:gems JohnWMcC$ gem install rubocop --user
WARNING:  You don't have /Users/JohnWMcC/.gem/ruby/2.3.0/bin in your PATH,
	  gem executables will not run.
Successfully installed parallel-1.19.1
Fetching: ast-2.4.0.gem (100%)
Successfully installed ast-2.4.0
Fetching: parser-2.7.1.2.gem (100%)
Successfully installed parser-2.7.1.2
Fetching: rainbow-3.0.0.gem (100%)
Successfully installed rainbow-3.0.0
Fetching: rexml-3.2.4.gem (100%)
Successfully installed rexml-3.2.4
Fetching: ruby-progressbar-1.10.1.gem (100%)
Successfully installed ruby-progressbar-1.10.1
Fetching: unicode-display_width-1.7.0.gem (100%)
Successfully installed unicode-display_width-1.7.0
Fetching: rubocop-0.83.0.gem (100%)
ERROR:  Error installing rubocop:
	rubocop requires Ruby version >= 2.4.0.

I think SU 2020.1 uses v2.5.x, but I can’t find where it puts it.

Not quite there yet.

If I CAN find it, where do I put my gem install... command`?

Or do I need to install a compatible native Ruby version separately? If so, which Ruby version?

I wouldn’t mess with SketchUp’s bundled Ruby. It’s inside the Application’s package:

/Applications/SketchUp 2020/SketchUp.app/Contents/Frameworks/Ruby.framework/Versions/2.5.5

You could use homebrew to install the latest Ruby if you want. Then install ruby with:
brew install ruby --with-doc

If you just use brew install ruby then ri won’t work. That’s what I did originally and had to uninstall ruby and start again.

You’ll have to add this new ruby to your shell’s path as the homebrew one goes here:
/usr/local/opt/ruby/bin/ruby
The Apple installed ruby will still be here:
/usr/bin/ruby

You could mess about with software that manages multiple versions of ruby, but I just use the latest version for trying things out but not using anything that’s not in 2.5.5 for SketchUp.

I’ve used homebrew before, and think I still have it installed.

Will try that. If THAT doesn’t work, I’ll give up - certainly for tonight, and possibly do without for longer.

Thanks.

[A couple of mins later. ]

Well, it is Updating homebrew, and hasn’t died yet!

Then thinking a long time with no output (yet).

Then soon after finishing updating Homebrew…

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

Don’t know what that means, nor have I time tonight to google for it.

Then when I thought it had finshed or died, yards more output starting:

==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and caskroom/cask).
==> New Formulae
abseil                                   libdazzle
adios2                                   libde265
ahoy                                     libdeflate

... and LOTS more...

==> Deleted Formulae
aap                                      libopkele
aiccu                                    libpng@1.2
ansible@1.9                              libpqxx@3
ansible@2.0                              librealsense@1

...

==> Updated Casks
010-editor
0xed
115browser
1clipboard
1password
1password-cli
...

==> Deleted Casks
0ad                                      makerbot-print
33-rpm                                   mal-updater
8bitshaper                               mammon
8tracksradiohelper                       management-tools
ending with

...
magican                                  zend-studio
mail-designer                            zendserver
mail-unread-menu                         zepheer
mailfollowup                             zeroinstall
mailpluginfix                            zest
majic                                    zipeg
makeiphoneringtone

...

Usage: brew install [options] formula

(then the options and forumlae you can use).

And then right at the end, easy to miss:

Error: invalid option: --with-doc

Enough for tonight got me. anyway. Will return to this tomorrow.

Command Line Tools are one of the requirements for homebrew:

xcode-select --install

Well, that bit worked quickly! (Yes, I know I said I’d stop for tonight, but this forum is amazing with its speed and knowledge.)

But the Ruby install is still complaining that the --with-docs argument is invalid.

Leaving that off, and trying again, I’ve now got Ruby 2.7.0.

I think perhaps I should have requested the specific Ruby 2.5.5 to match SU’s own, or even 2.5.0 for backward compatibility.

Or will rubocop work fine on 2.7.0?

If not, how do I (or do I even need to) uninstall 2.3.0 which came with MacOS (or should I not do that?) or 2.7.0, and install 2.5.0 or 2.5.6 instead?

There’s no ‘s’ on the end. It’s doc not docs.
brew install ruby --with-doc

Don’t delete the one that came with macOS. Check you have your paths set correct and which one you’re using with

ruby --version

and to see where it is:

which ruby

Yes, I think so.

Many thanks. Trying that I WILL leave to tomorrow.

I thought I’d copy/pasted the –with-docs but must have just typed it wrong.