Intro to CocoaPods

Comment

Intro to CocoaPods

CocoaPods is cool. Due to my bad memory, I'm going to write here the steps I need to perform to profit from it.

What it does

Cocoapods simplifies the use of external source code in your project.

Installation

The first step is to set up the computer:

sudo gem update --system
sudo gem install cocoapods -V

Alternatively, instead of using sudo, you can install in you user directory as explained here.


Note: When recently updating I encountered the following error message:

ERROR:  While executing gem ... (Errno::EPERM)
    Operation not permitted - /usr/bin/xcodeproj

The solution (described here) was to run:

sudo gem install cocoapods -n /usr/local/bin

This can take some time. The optional -V prints additional stuff and gives confidence that something is going on. The same command can be used to udpate cocoapods. With that done, cocoapods should be installed in the computer. How can you check it works? Well, simply try:

pod

You should get something like this:

Setting up CocoaPods master repo

CocoaPods 0.35.0.rc2 is available. To update use: gem install cocoapods --pre [!] This is a test version we'd love you to try.

For more information see http://blog.cocoapods.org and the CHANGELOG for this version http://git.io/BaH8pQ.

Setup completed Usage:

$ pod COMMAND

  CocoaPods, the Objective-C library package manager.

Commands:

+ init                Generate a Podfile for the current directory.
+ install             Install project dependencies to Podfile.lock versions
+ ipc                 Inter-process communication
+ lib                 Develop pods
+ list                List pods
+ outdated            Show outdated project dependencies
+ plugins             Show available CocoaPods plugins
+ push                Temporary alias for the `pod repo push` command
+ repo                Manage spec-repositories
+ search              Searches for pods
+ setup               Setup the CocoaPods environment
+ spec                Manage pod specs
+ trunk               Interact with the CocoaPods API (e.g. publishing new
                      specs)
+ try                 Try a Pod!
+ update              Update outdated project dependencies and create new
                      Podfile.lock

Options:

--silent              Show nothing
--completion-script   Print the auto-completion script
--version             Show the version of the tool
--verbose             Show more debugging information
--no-ansi             Show output without ANSI codes
--help                Show help banner of specified command

Setup

To use cocoapods in a project you need to create a Podfile with information on which cocoapods you want to use. This file contains the dependencies of your project. An example:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
pod 'AFNetworking', '~> 2.0'
pod 'ARAnalytics', '~> 2.7'

This file must be located in the same directory as your xcodeproj file.

You can create a Podfile with:

pod init

The contents of this default file look like (created for a project called XXX):

Uncomment this line to define a global platform for your project
platform :ios, '6.0'

source 'https://github.com/CocoaPods/Specs.git'

target 'XXX' do
end

target 'XXXTests' do
end

The two targets that were created are inherited from the parent folder where the Podfile is created. These targets are default from Xcode.

Once you create the Podfile, you can download the code with:

pod install

For the generated Podfile above, the results is:

Analyzing dependencies

CocoaPods 0.35.0.rc2 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.

Downloading dependencies
Generating Pods project
Integrating client project

[!] From now on use `CocoapodsTest.xcworkspace`.

[!] [!] The Podfile does not contain any dependencies.

A new XCode workspace (extension xcworkspace) will be created. This workspace will include your original project. The dependencies you defined in the Podfile will be added to this workspace. In addition, the following is created:

  • Podfile.lock
  • Pods

The Pods directory contains an Xcode project (Pods.xcodeproj) used to build the pods you added.

The Podfile.lock file contains the versions of the pods that were donwloaded.

The podfile

See info on the podfile

Other articles

For details of how CocoaPods works under the hood, check this ObjC.io article.

Comment

Comment

Intel x64 assembly

Introduction

Intel has a nice document.

Registers

Integer

The initial 8 registers were expanded to 16 in x64. They were also widened to 64 bits.

Some have a legacy meaning:

  • rax: accumulator
  • rbx: base register
  • rcx: count register
  • rdx: data register
  • rdi: destination index
  • rsi: source index
  • rsp: stacp pointer
  • rbp: frame pointer

Special registers:

  • flags: Table from here:
Bit   Label    Desciption
---------------------------
0      CF      Carry flag
2      PF      Parity flag
4      AF      Auxiliary carry flag
6      ZF      Zero flag
7      SF      Sign flag
8      TF      Trap flag
9      IF      Interrupt enable flag
10     DF      Direction flag
11     OF      Overflow flag
12-13  IOPL    I/O Priviledge level
14     NT      Nested task flag
16     RF      Resume flag
17     VM      Virtual 8086 mode flag
18     AC      Alignment check flag (486+)
19     VIF     Virutal interrupt flag
20     VIP     Virtual interrupt pending flag
21     ID      ID flag

More info on register can be found here.

Floating point and MMX

There are 8 floating point 80-bit registers (FPR0-7) that overlap with the first of the 16 MMX 128-bit registers (XMM0-15).

Instruction types:

  • Move operations: mov/movsxd (sign extension)/movzxd (zero extension)
  • Subroutines: call/lock ret/push/pop
  • lea: load effective address
  • Arithmetic: add/sub/imul/inc/idiv
  • Shift: shr/sal/shl/sar
  • Rotate: rcr/rcl/rol/ror (with/out carry)
  • Logic: and/or/xor/neg
  • cmp
  • jmp
  • conditional jumps
    • signed
      • jge: Greater or equal
      • jg
      • je
      • jne
    • unsigned
      • ja: Jump above
      • jae: Jump above or equal
      • jns
      • jb: Jump below
  • test
  • cmov: Conditional move, based on a condition code below:
    • cmove
    • cmovb
  • bt?

Floating point:

  • cvttsd2si
  • movsd

Calling conventions:

  • Arguments in registers: rdi, rsi
  • Return value: rax

Syntax

There are two syntaxes.

Intel:

mov eax, 5
add esp, 4
mov eax, [ebx + ecx*4 + mem_location]

AT&T:

mov $5, %eax
addl $4, %esp
movl mem_location(%ebx,%ecx,4), %eax

Comment

1 Comment

Simple web app development with Flask

Flask is a very small framework for eveloping web apps.

Create the following directory structure:

  • routes.py
  • templates
  • static
    • css
    • img
    • js
  • Readme.md Flask relies on Jinja2 templates. These look like:
{% extends "layout.html" %}

{% block content %}
  <main class="hero-section">
  </main>
{% endblock %}

routes.py contains the main dispatcher:

brush:python
from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def index():
  return render_template("index.html")

@app.route("/about")
def about():
  return render_template("about.html")

if __name__ == "__main__":
  app.run(debug=True)

To install flask it is best to use a VirtualEnv. Download from the web and create a new virtual environment:

python virtualenv.py my_ve

To activate or deactivate th virtual environment:

source my_ve/bin/activate
source my_ve/bin/deactivate

To install flask in the VE:

pip install flask

To deploy to heroku:

pip install gunicorn
  • Create a requirements.txt

    pip freeze >requirements.txt

  • Create a Procfile to tell Heroku to run flask in gunicorn

    web:gunicorn routes:app

  • Create a heroku app

    heroku create

  • We can now push to Heroku git repo

    git push heroku master

  • Open in browser with

    heroku open

Database

Using the Flask SQLAlchemy extension:

pip install flask-sqlalchemy

and add this line to routes.py

app.config["SQLALCHEMY_DATABASE_URI"] = 'postgresql://localhost/db_name'

Create a models.py:

from flask.ext.sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class User(db.Model):
  __tablename__ = 'users'
  uid = db.Column(db.Integer, primary_key = True)
  firstname = db.Column(db.String(100))
  lastname = db.Column(db.String(100))
  email = db.Column(db.String(120), unique=True)
  pwdhash = db.Column(db.String(54))

  def __init__(self, firstname, lastname, email, password):
    self.firstname = firstname.title()
    self.lastname = lastname.title()
    self.email = email.lower()
    self.set_password(password)

The main routes.py needs to initialize the db object:

brush:python
from models import db

db.init_app(app)

Based on "Learning Flask" course at lynda.com, git repo

1 Comment