Creating a New App

RubyMotion exists on the command line, so you should know the basics of navigating around a shell. The motion command is installed when you run the RubyMotion installer. It’s equivalent to the rails command in the Ruby realm. motion manages individual projects in addition to the core RubyMotion tools. For example, motion update automatically updates RubyMotion and motion support lets you file bug reports.

Let’s open a terminal and navigate to where you want to create your RubyMotion projects. We’re going to create a few projects over the course of this book, so your work area should preferably be somewhere without any other files or folders. Once you’ve picked that out, run motion create HelloMotion.

 $ ​​motion​​ ​​create​​ ​​HelloMotion
 Create HelloMotion
 Create HelloMotion/.gitignore
 Create HelloMotion/app/app_delegate.rb
 Create HelloMotion/Gemfile
 Create HelloMotion/Rakefile
 Create HelloMotion/resources/[email protected]
 Create HelloMotion/spec/main_spec.rb

motion create makes a HelloMotion folder in the current directory and adds some more files inside. These generated files and folders form the essential skeleton of a RubyMotion project. Go ahead and cd into it (cd ./HelloMotion) so that we can take a look. You’ll run all of our subsequent RubyMotion commands from within this folder, so definitely keep a terminal window or tab open pointing to the directory.

For now, we’ll just talk about two of the files it created: Rakefile and ./app/app_delegate.rb.

The Rakefile is created at the root of a project. It’s where we configure apps and include settings such as the app’s name, icon, resources, and source code locations. Before we get too far ahead of ourselves, let’s start with the basics.

The Rakefile gets its name from the rake command. rake is a command that allows you to execute arbitrary Ruby code via the command line. These chains (known as tasks) are loaded from the directory’s Rakefile and become executable in the form rake {task name}. For example, RubyMotion uses the rake device tasks to build an app to a device.

As of RubyMotion 2.28, the Rakefile will be generated to look like this:

 # -*- coding: utf-8 -*-
 $:.unshift(​"/Library/RubyMotion/lib"​)
 require ​'motion/project/template/ios'
 begin
  require ​'bundler'
  Bundler.require
 rescue​ LoadError
 end
 Motion::Project::App.setup ​do​ |app|
 # Use `rake config' to see complete project settings.
  app.name = ​'HelloMotion'
 end

The first line specifies the the file encoding; the next two lines import necessary RubyMotion libraries into our project. The code between the following begin and end block allows our project to use Bundler (http://bundler.io/) to manage Ruby dependencies. RubyMotion creates a Gemfile in our project by default to use with Bundler, but we won’t be using Bundler in this book. Next, we call Motion::Project::App.setup. This is what actually sets up the Rakefile to build a RubyMotion app. We pass it a block where we configure the app object. By default, RubyMotion sets the app’s name to correspond to what we passed in motion create.

Earlier we mentioned rake device, but that’s nowhere in the Rakefile. It and the other RubyMotion tasks are defined when we require motion/project. To list all the tasks, let’s run rake --tasks in a terminal.

 $ ​​rake​​ ​​--tasks
 rake archive ​# Create an .ipa archive
 rake archive:distribution ​# Create an .ipa archive for AppStore
 rake build ​# Build everything
 rake build:device ​# Build the device version
 rake build:simulator ​# Build the simulator version
 rake clean ​# Clear local build objects
 rake clean:all ​# Clean all build objects
 rake config ​# Show project config
 rake crashlog
 rake crashlog:device
 rake ctags ​# Generate ctags
 rake default ​# Build the project, then run the simulator
 rake device ​# Deploy on the device
 rake profile ​# Same as profile:simulator
 rake profile:device ​# Run a build on the device through Instruments
 rake profile:simulator ​# Run a build on the simulator through Instruments
 rake simulator ​# Run the simulator
 rake spec ​# Same as 'spec:simulator'
 rake spec:device ​# Run the test/spec suite on the device
 rake spec:simulator ​# Run the test/spec suite on the simulator
 rake static ​# Create a .a static library

There are lots of neat toys for us to play with. What isn’t evident is that the default, plain rake command is configured to be the same as building and running the app on the simulator.

Let’s take a look at the simulator by running rake in a terminal.

 $ ​​rake
 
  Build ./build/iPhoneSimulator-7.1-Development
  Compile ./app/app_delegate.rb
  Create ./build/iPhoneSimulator-7.1-Development/HelloMotion.app
  Link ./build/iPhoneSimulator-7.1-Development/HelloMotion.app/HelloMotion
  Create ./build/iPhoneSimulator-7.1-Development/HelloMotion.app/Info.plist
  Create ./build/iPhoneSimulator-7.1-Development/HelloMotion.app/PkgInfo
  Create ./build/iPhoneSimulator-7.1-Development/HelloMotion.dSYM
 Simulate ./build/iPhoneSimulator-7.1-Development/HelloMotion.app
 (main)> _

Give it a second to finish its business and...you’ll see an iPhone simulator pop up with our app running. Congratulations! You’ve just created your first iOS app in Ruby.

Additionally, your terminal should be displaying a new prompt. We can enter new Ruby code in the terminal and watch it execute on the fly; this is just like the rails console and irb commands in the Ruby and Rails world. Altogether, your screen should look like the one in Figure 1, Prompt with iPhone simulator.

images/first_app/0.png

Figure 1. Prompt with iPhone simulator

Hooray! But how did that really happen? How did we get from app.name = ’HelloMotion’ to an iPhone emulator popping up? To find the answer, we must leave rake and explore the app’s actual code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset