Scripting with r2m

rest2mobile Android and Xcode plugins make it easy to generate native code directly inside your app project. However, they are not suited for automation or continuous integration. In this case, you should use r2m.

r2m is the command line interface for rest2mobile. You can find the source code on github. The README is a good primer to understand the r2m basics. What follows mostly focuses on how to use r2m scripts.

r2m contains the core library of the Magnet Mobile App Builder, which is our command line tool to build Magnet Mobile Backends. r2m inherited most of the Mobile App Builder’s command line features. It supports history, help, command and options auto-completion as well as some core utility commands.

In particular, the run command allows you to run scripts. A r2m script is just a text file containing r2m commands. You run scripts with the run command:


r2m run myscript


Since r2m is implemented in Groovy, you can also include groovy one-liner in your script, a bit like in a REPL. Here is an example:


// this is a comment
1 + 1
println "Hello World"


And here is how you can run it:

$ r2m run script1
// this is a comment
1 + 1
Result => 2
Hello World


Additionally, the exec command allows you to run actual shell command.


exec echo Hello World


Run it with:

$ r2m run script2
Hello World


Finally, you can include variables in your script. Variables are identified by ${} anchors. Here is another example that generates the OOTB GoogleDistance and YouTube APIs from our r2m-examples repo given an output directory dir for a specific platform target:


gen -d GoogleDistance -f -o ${dir}/googledistance -c DistanceController -p com.example ${target} 
gen -d YouTube -f -o ${dir}/youtube -c YouTubeController -n YT ${target}


To run it, specify the variables value with the -D option The –quiet option instructs r2m to skip the questionnaire and confirmation.

$ r2m run script3 -Ddir=gen -Dtarget=ios --quiet
gen -d GoogleDistance -f -o gen/googledistance -c DistanceController -p com.example ios 
Importing ReST example file 'google-distance.txt' to '/var/folders/rv/pss11rx53f3ckhq6hqrjr4l40000gn/T/google-distance.txt624826518699278105.tmp'
Generating ios Mobile API under gen/googledistance
Parsing ReST example: /private/var/folders/rv/pss11rx53f3ckhq6hqrjr4l40000gn/T/google-distance.txt624826518699278105.tmp
Success! Generated code is located in 'gen/googledistance' directory.
gen -d YouTube -f -o gen/youtube -c YouTubeController -n YT ios
Importing ReST example file 'YouTube.txt' to '/var/folders/rv/pss11rx53f3ckhq6hqrjr4l40000gn/T/YouTube.txt7136826791966787590.tmp'
Generating ios Mobile API under gen/youtube
Parsing ReST example: /private/var/folders/rv/pss11rx53f3ckhq6hqrjr4l40000gn/T/YouTube.txt7136826791966787590.tmp
Success! Generated code is located in 'gen/youtube' directory.


The generated directory will look like this:

├── googledistance
│   ├── Podfile
│   ├── Source
│   │   ├── Controllers
│   │   │   ├── DistanceController.h
│   │   │   └── DistanceController.m
│   │   └── Nodes
│   │       ├── Distance.h
│   │       ├── Distance.m
│   │       ├── Duration.h
│   │       ├── Duration.m
│   │       ├── Element.h
│   │       ├── Element.m
│   │       ├── GoogleDistanceResult.h
│   │       ├── GoogleDistanceResult.m
│   │       ├── Row.h
│   │       └── Row.m
│   └── Tests
│       └── DistanceControllerSpec.m
└── youtube
    ├── Podfile
    ├── Source
    │   ├── Controllers
    │   │   ├── YTYouTubeController.h
    │   │   └── YTYouTubeController.m
    │   └── Nodes
    │       ├── YTContentDetails.h
    │       ├── YTContentDetails.m
    │       ├── YTDefault.h
    │       ├── YTDefault.m
    │       ├── YTHigh.h
    │       ├── YTHigh.m
    │       ├── YTItem.h
    │       ├── YTItem.m
    │       ├── YTMedium.h
    │       ├── YTMedium.m
    │       ├── YTPageInfo.h
    │       ├── YTPageInfo.m
    │       ├── YTSnippet.h
    │       ├── YTSnippet.m
    │       ├── YTStatistics.h
    │       ├── YTStatistics.m
    │       ├── YTStatus.h
    │       ├── YTStatus.m
    │       ├── YTThumbnails.h
    │       ├── YTThumbnails.m
    │       ├── YTYouTubeResult.h
    │       └── YTYouTubeResult.m
    └── Tests
        └── YTYouTubeControllerSpec.m


Check out the run command help (r2m help -v run), if you want to get more information about r2m scripting. You can customize and automate quite a lot of code generation with r2m scripts. This should fit most of your automation and integration tests. Again, if you have any request or question, don’t hesitate to send your feedback on our developer website.