Running with Ruby

Tag: Ruby 1.9 (page 1 of 18)

Installing Rhodes on Ubuntu and making it run on Ruby 2.1

People who want to use Rhodes are recommended to use it under Ruby 1.9.3. But to me it seems kinda old. This is a way to install and run it on 2.1:

Oracle Java

Oracle Java is a recommended one to use with Rhodes, so just:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

SDK and NDK

Download both SDK and NDK. After that extract them in the same directory.

Rhodes gem

ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
gem install rhodes --no-ri --no-rdoc
# This will take some time...
Fetching: extlib-0.9.16.gem (100%)
Successfully installed extlib-0.9.16
Fetching: templater-1.0.0.gem (100%)
Successfully installed templater-1.0.0
# Bla bla bla...
7 gems installed

and now the harder part…

Rhodes setup

rhodes-setup
We will ask you a few questions below about your dev environment.
JDK path (required) (): /usr/lib/jvm/java-8-oracle
Android SDK path (blank to skip) (): /home/user/Android/android-sdk-linux
Android NDK path (blank to skip) (): /home/user/Android/android-ndk-r10
Windows Mobile 6 SDK CabWiz (blank to skip) (): 
BlackBerry JDE 4.6 (blank to skip) (): 
BlackBerry JDE 4.6 MDS (blank to skip) (): 
BlackBerry JDE 4.2 (blank to skip) (): 
BlackBerry JDE 4.2 MDS (blank to skip) (): 

Rhodes test app

Now we need a test app that will tell us what errors do we have related to our Ruby version:

rhodes app testapp
cd ./testapp

If you try to run rake -T you won’t see any tasks. Instead you’ll see path to your Rhodes gem sources:

rake -T
/home/user/.rvm/gems/ruby-2.1.2/gems/rhodes-3.5.1.12
# go there
cd /home/user/.rvm/gems/ruby-2.1.2/gems/rhodes-3.5.1.12

Rakefile changes

We need to make some changes to few files. To see rake tasks, we need to change one thing in Rhodes rakefile:

# I assume you're in Rhodes gem dir
# cd /home/user/.rvm/gems/ruby-2.1.2/gems/rhodes-3.5.1.12
# vim Rakefile

# Around line 33 - remove all of this:
module Rake
  class Task
    attr_accessor :comment
  end
end

Now in our app dir we can:

rake -T
rake build:android:eclipsebundle        # Build RhoBundle for Eclipse project
rake build:android:extensions           # Building native extensioons
rake build:android:rhobundle            # Build RhoBundle for android
rake build:symbian:rhobundle            # Build symbian rhobundle
rake build:wp8:rhobundle                # Build WP8 rhobundle
rake build:wp:rhobundle                 # Build WP7 rhobundle
rake clean:android                      # Clean Android
rake clean:bb                           # Clean bb
rake clean:bbsim                        # Clean bb and simulator
rake clean:iphone                       # Clean iphone
rake clean:win32                        # Clean win32
rake clean:wince                        # Clean windows ce
rake clean:wm                           # Clean wm
rake clean:wp                           # Clean wp
rake clean:wp8                          # Clean wp
rake config:checkbb                     # Check local blackberry configuration
rake device:android:debug               # Build debug self signed for device
rake device:android:production          # Build production signed for device
rake device:bb:debug                    # Build debug for device
rake device:bb:production               # Build production for device
rake device:iphone:production           # Builds and signs iphone for production
rake device:symbian:production          # Build production for device
rake device:win32:production            # Build installer for Windows
rake device:wince:production            # Build production for device or emulator
rake device:wm:production               # Build production for device or emulator
rake device:wp8:production              # Build production for device or emulator
rake device:wp:production               # Build production for device or emulator
rake run:android                        # build and launch emulator
rake run:android:device                 # build and install on device
rake run:android:rhosimulator           # Run application on RhoSimulator
# a lot of tasks...
rake uninstall:android                  # uninstall from emulator
rake uninstall:android:device           # uninstall from device

Android SDK setup

Go to your Android SDK dir and:

chmod +x ./tools/android
./tools/android

And use the Android 4.2.2 (API 17). Below you can see a printscreen of all the elements you need to have installed. If you have a Android L or any higher API than API 17, please remove them. Rhodes seems to have some issues with them.

android

androidcommon.rb changes

If you try to run your testapp, you will end up with following error:

Can't detect NDK toolchain path (corrupted NDK installation?)
/home/user/.rvm/gems/ruby-2.1.2/gems/rhodes-3.5.1.12/platform/android/build/androidcommon.rb:88:in `setup_ndk'
/home/user/.rvm/gems/ruby-2.1.2/gems/rhodes-3.5.1.12/platform/android/build/android.rake:388:in `block (2 levels) in <top (required)>'
/home/user/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
/home/user/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => run:android => run:android:emulator => config:android:emulator => config:android
(See full trace by running task with --trace)

edit androidcommon.rb and change:

@@ -71,8 +71,9 @@ def setup_ndk(ndkpath,apilevel)
   $ndktools = nil
   $ndkabi = "unknown"
   $ndkgccver = "unknown"
-  ["arm-linux-androideabi-4.4.3", "arm-eabi-4.4.0", "arm-eabi-4.2.1"].each do |abi|
+  ["arm-linux-androideabi-4.6", "arm-eabi-4.4.0", "arm-eabi-4.2.1"].each do |abi|
     variants = []
     variants << File.join(ndkpath, "toolchains", abi, "prebuilt", $ndkhost)
     variants << File.join(ndkpath, "build/prebuilt", $ndkhost, abi)
     variants.each do |variant|
@@ -118,6 +119,7 @@ def setup_ndk(ndkpath,apilevel)
   end
 
   variants.each do |variant|
     sysroot = File.join(ndkpath, variant, "android-#{last_api_level}/arch-arm")
     next unless File.directory? sysroot
     $ndksysroot = sysroot
@@ -244,7 +246,8 @@ def cc_run(command, args, chdir = nil)
   cmdstr = argv.map! { |x| x.to_s }.map! { |x| x =~ / / ? '' + x + '' : x }.join(' ')
   puts cmdstr
   $stdout.flush
-  argv = cmdstr if RUBY_VERSION =~ /^1\.[89]/
+  argv = cmdstr if RUBY_VERSION =~ /^(1\.[89]|2\.[0-9])/
   IO.popen(argv) do |f|
     while data = f.gets
       puts data

android_tools.rb changes

If you get this error:

NoMethodError: undefined method `rm_f' for AndroidTools:Module
/home/user/.rvm/gems/ruby-2.1.2/gems/rhodes-3.5.1.12/platform/android/build/android_tools.rb:211:in `run_emulator'
/home/user/.rvm/gems/ruby-2.1.2/gems/rhodes-3.5.1.12/platform/android/build/android.rake:1971:in `block (3 levels) in <top (required)>'
/home/user/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
/home/user/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => run:android => run:android:emulator
(See full trace by running task with --trace)

Here’s fix for that (in android_tools.rb):

@@ -208,7 +208,7 @@ module_function :is_device_running
 def  run_emulator(options = {})
   system("\"#{$adb}\" start-server")
 
-  rm_f $applog_path if !$applog_path.nil?
+  FileUtils.rm_f $applog_path if !$applog_path.nil?
   logcat_process()
   
   unless is_emulator_running

Android SDK tools symlinks and changes

Apkbuilder

In your Android SDK dir run:

cat android | sed -e 's/com.android.sdkmanager.Main/com.android.sdklib.build.ApkBuilderMain/g' > apkbuilder
chmod a+x apkbuilder

Zipalign

cp ./build-tools/19.1.0/zipalign ./tools

aapt and lib

ln -s /home/mencio/Software/Android/android-sdk-linux/build-tools/19.1.0/aapt ./platform-tools/
ln -s /home/mencio/Software/Android/android-sdk-linux/build-tools/19.1.0/lib ./platform-tools/

error: device not found

Last thing to change. If you get this error:

* daemon not running. starting it now on port 5037 *
* daemon started successfully *
error: device not found
- waiting for device -
rake aborted!
Unable to create AVD image. No appropriate target API for SDK version: 2.1
/home/mencio/.rvm/gems/ruby-2.1.2/gems/rhodes-3.5.1.12/platform/android/build/android_tools.rb:231:in `run_emulator'
/home/mencio/.rvm/gems/ruby-2.1.2/gems/rhodes-3.5.1.12/platform/android/build/android.rake:1971:in `block (3 levels) in <top (required)>'
/home/mencio/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
/home/mencio/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => run:android => run:android:emulator
(See full trace by running task with --trace)

you need to edit your build.yml file and change:

android:
  version: 2.1

to 4.2.2 (or any other that you use)

android:
  version: 4.2.2

After all of that, you should be able to run:

rake run:android

NameError: uninitialized constant Gem::Builder

If you have this issue and you still want to build Rhodes from sources (Github), please downgrade:

gem update --system 1.8.25

Then just:

rake gem
gem install ./rhodes-5.0.2.gem

and get back to new gem version:

gem update --system

Ruby programmers/project managers/CEOs Y U NO enforce code quality?

Introduction

This post is a result of recent events with one of the companies that I work with. Sometimes, I help people because I want, sometimes because they pay me, mostly both ;). This time I’ve been helping one company decide on how to outsource their software development. They have many applications, some of them are old, some of them are younger, they constantly build something new as well – a never-ending story. I didn’t want to get into the sole “picking the right company” part of this deal. I was not responsible for financial or any other non-programming part of this process, so instead of recommending companies I focused on something else: How to ensure the same (or better) quality, without having a dedicated team working close to all other people related to a given project.

I’ve spent some time studying their software, what they do, how they work and what their goals are. Also I’ve talked with their developers about their feelings and thoughts about all of this. I’ve spoken to other employees as well, and one thing struck me:

(Obrazek JPEG, 480×480 pikseli)

No one really cares! As long as it works, why would they? Programmers didn’t have enough time (and sometimes knowledge), project managers didn’t care as long as it worked (or also didn’t know how to measure things like that), CEO thought that since there are so many apps, it is impossible to do this without a big overhead (and that this would cost a lot of money).

Low quality is just a way to postpone costs that later will be much higher

I can understand why CEOs can think like that. What I don’t understand is why project managers and CTOs don’t explain this to them. If CEO doesn’t care, no one will be able to. And this will turn against everyone one day. Many poorly written projects are hard to give away. The more complicated they are, the more price grows.

Quality always costs. There’s no doubt about that. But if you ensure it from the beginning, the further you go with your project the more you will gain. And I’m not talking only about money. Keep in mind, that people don’t like other peoples code. And working with poorly written code is what they hate. Low quality equals a much higher rotation rate in teams that have to work with such code. Also keep in mind that everything changes. Programmers change their jobs, some companies are being closed, some are being created, and if you want to outsource stuff, you need to always be aware of that. The only thing that should not change from your perspective is your code quality.

It’s always expensive to transfer knowledge and know-how

Outsourcing is not easy. Your team needs to transfer all the know-how to some other places and people. The more code there is, the harder it gets. But what could you do to make it go faster, easier and cheaper?

Ensure code quality!

You could ensure some code and documentation standards across all the firms that will work on your software. That way you can diversify across few that will be able to work with others code. If all of them share same principals, their work quality should be similar, their code should be readable for all of them and you could rotate across those companies according to your business needs. Further more you could even specify in the contract what you expect and what they need to obtain in terms of quality. Programmers will still make mistakes, things will still not work the way you wanted but even then standards will allow other people to fix those issues without any unexpected impact on the whole project. If you set the standards, programmers will have a bit less freedom in how they work and what they do. It’s not always good but it is one of those nonfinancial costs you need to take.

Few tools you can use in order to move forward

Some “code related” tools that can help you out with Ruby based software:

  • Rspec (or any other test framework),
  • Simplecov,
  • YARD,
  • Rubocop.

Rspec (or any other test framework)

This is quite obvious. Tests are must be for any application that we want to maintain. Working with not tested software can be a nightmare. But how to measure tests/specs quality?

Simplecov to the rescue

We could (and we should!) use Simplecov. SimpleCov is a code coverage analysis tool for Ruby.

687474703a2f2f636f6c737a6f776b612e6769746875622e636f6d2f73696d706c65636f762f6465766973655f726573756c742d302e352e332e706e67

Picture taken from official Simplecov Github page

It will show you any flaws of your test suites. Not tested classes, methods, cases. It will also generate some useful statistics. If you require usage of this library from your contractors, you can include coverage level as one of parts of your contract. That way they will be obligated to keep tests up and running all the time. Of course they will have to agree on that, because if you force them to use it, they may just write tests that will “pass” Simplecov statistics, delivering you green, low quality tests/specs.

YARD – Yay! A Ruby Documentation Tool

I like self-explaining code. Ruby code can be written both in good and bad way. Unfortunately self-explaining code won’t answer all the questions. For example, it won’t answer why something is done that way, where there might be a (theoretically) better solution. Also an outsourced code often changes maintainers. None of them will pass all the knowledge to a next one. In cases like that I recommend full YARD documentation. At least on a module/class and public methods level.YARD is a documentation generation tool for the Ruby programming language. It enables the user to generate consistent, usable documentation that can be exported to a number of formats very easily

Some programmers write comments, some don’t. But even if they do, without standardization, it’s hard to read and maintain. If you use YARD, none of this will happen. You will end-up with a consistent, standardized documentation that can be viewed both online and offline. You can also easily monitor YARD doc coverage level by running:

yard stats --list-undoc

For example, you may get such result:

Files:          21
Modules:         5 (    0 undocumented)
Classes:        25 (    0 undocumented)
Constants:       8 (    1 undocumented)
Methods:        51 (    0 undocumented)
 98.88% documented

Undocumented Objects:

(in file: app/models/credit_card.rb)

Then it is quite clear that in file credit_card.rb some elements are undocumented and that developers should update docs. As you can see, YARD allows us to easily measure and monitor comments quality.

RuboCop – A Ruby static code analyzer, based on the community Ruby style guide

RuboCop is a Ruby static code analyzer. Out of the box it will enforce many of the guidelines outlined in the community Ruby Style Guide. So, instead of having code written in many ways, you can define a file called .rubocop that will contain all the rules about Ruby code style. That way, you can ensure same standards across many applications/dev teams/companies. It will notify you if anything is not as it should be.

.....C.................................................C......................C......

Offenses:

app/controllers/portal/base_controller.rb:40:3: 
C: Cyclomatic complexity for respond_with is too high. [8/6]
  def respond_with(*resources, &block)
  ^^^
app/controllers/portal/base_controller.rb:40:3: 
C: Method has too many lines. [18/15]
  def respond_with(*resources, &block)
  ^^^
app/controllers/application_controller.rb:52:3: 
C: Cyclomatic complexity for respond_with is too high. [8/6]
  def respond_with(*resources, &block)
  ^^^

Summary

Code quality is really important. It’s also a topic for more than a blog post, but I hope you will consider this article useful. Tools that I’ve presented won’t replace good developers but they can help you out omit troubles. If you use them wisely, you can ensure similar code quality in many dev teams.

Olderposts

Copyright © 2017 Running with Ruby

Theme by Anders NorenUp ↑