Permalink
Browse files

Initial implementation

  • Loading branch information...
0 parents commit de792889453805f9feece40642da627acac2b61c @yury yury committed Jul 11, 2012
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--format progress
@@ -0,0 +1,7 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in ib.gemspec
+gemspec
+
+gem 'rake'
+gem 'bundler'
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Yury Korolev
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,72 @@
+# IB
+
+rubymotion interface builder support (yes, with outlets)
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'ib'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install ib
+
+In your Rake file:
+
+```ruby
+# -*- coding: utf-8 -*-
+$:.unshift("/Library/RubyMotion/lib")
+require 'motion/project'
+
+# if you use bundler
+require 'bundler/setup'
+Bundler.setup
+
+# require 'ib tasks'
+require 'ib/tasks'
+
+
+Motion::Project::App.setup do |app|
+ # ...
+ app.files.unshift IB::PATH # add ib module
+end
+
+```
+
+## Usage
+
+Extend your controllers with IB module:
+
+```ruby
+class SuperController < UIViewController
+
+ # define attribute accessor
+ attr_accessor title
+
+ # define ib outlet
+ ib_outlet :title, UILabel
+
+ # define action method
+ def onclick UIButton * button
+
+ end
+
+ # define ib action
+ ib_action :onclick
+end
+```
+
+Run `rake design` create Storyboard or nibs (put them in resources folder) and you will be able to bind outlets and actions to your ruby code.
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/ib/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["Yury Korolev"]
+ gem.email = ["yury.korolev@gmail.com"]
+ gem.description = %q{Stupid rubymotion ib outlets support}
+ gem.summary = %q{Small portion of love to interface builder with rubymotion}
+ gem.homepage = ""
+
+ gem.files = `git ls-files`.split($\)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.name = "ib"
+ gem.require_paths = ["lib"]
+ gem.version = IB::VERSION
+
+ gem.add_dependency "xcodeproj", ">= 0.2.3"
+
+ gem.add_development_dependency "rspec", ">= 2.0"
+end
@@ -0,0 +1,10 @@
+module IB
+
+ def ib_outlet name, type
+ # maybe call attr_accessor here?
+ end
+
+ def ib_action name
+ end
+
+end
@@ -0,0 +1,38 @@
+require 'fileutils'
+require 'ib/version'
+require 'ib/parser'
+require 'xcodeproj'
+
+class IB::Generator
+ def write src, dest
+ files = IB::Parser.new.find_all(src)
+
+ FileUtils.mkpath dest
+
+ File.open("#{dest}/stubs.h", 'w') do |f|
+ f.write <<-OBJC
+// Generated by IB v#{IB::VERSION} gem. Do not edit it manually
+// Run `rake design` to refresh
+
+#import <UIKit/UIKit.h>
+
+#{generate_objc(files)}
+OBJC
+ end
+
+ end
+
+ def generate_objc files
+ src = files.map do |path, info|
+<<-OBJC
+@interface #{info[:class][0][0]} : #{info[:class][0][1]}
+
+#{info[:outlets].map {|name, type| "@property IBOutlet #{type} * #{name};" }.join("\n")}
+
+#{info[:actions].map {|action| "-(IBAction) #{action[0]}:(id) sender;" }.join("\n")}
+
+@end
+OBJC
+ end.join("\n" * 2)
+ end
+end
@@ -0,0 +1,38 @@
+class IB::Parser
+ def find_all(dir)
+ all = {}
+ Dir.glob("#{dir}/**/*.rb") do |file|
+ if info = find(file)
+ all[file] = info
+ end
+ end
+ all
+ end
+
+ def find(path)
+ src = File.read(path)
+ info = {class: find_class(src)}
+
+ return false if info[:class].length == 0
+
+ info[:outlets] = find_outlets(src)
+
+ info[:actions] = find_actions(src)
+
+ info[:path] = path
+
+ info
+ end
+
+ def find_class src
+ src.scan /^\s*class\s+([a-zA-Z][_a-zA-Z0-9]+)\s*<\s*([a-zA-Z][_a-zA-Z0-9]+)/
+ end
+
+ def find_outlets src
+ src.scan /^\s+ib_outlet\s+:([a-zA-Z][_a-zA-Z0-9]*)\s*?,\s*['"]?([a-zA-Z][_a-zA-Z0-9]+)/
+ end
+
+ def find_actions src
+ src.scan /^\s+ib_action\s+:([a-zA-Z][_a-zA-Z0-9]*)/
+ end
+end
@@ -0,0 +1,23 @@
+require 'fileutils'
+require 'ib/parser'
+require 'ib/generator'
+require 'xcodeproj'
+
+class IB::Project
+ def write app_path = "./app", resources_path = "./resources"
+ project = Xcodeproj::Project.new
+ target = project.targets.new_static_library(:ios, 'ui')
+ stubs = IB::Generator.new.write(app_path, "ui.xcodeproj")
+ stubs_path = Pathname.new("ui.xcodeproj/stubs.h")
+ files = [Xcodeproj::Project::PBXNativeTarget::SourceFileDescription.new(stubs_path, nil, nil)]
+
+
+ Dir.glob("#{resources_path}/**/*.{png,jpg,jpeg,storyboard,xib}") do |file|
+ files << Xcodeproj::Project::PBXNativeTarget::SourceFileDescription.new(Pathname.new(file), nil, nil)
+ end
+
+ target.add_source_files(files)
+
+ project.save_as("ui.xcodeproj")
+ end
+end
@@ -0,0 +1,13 @@
+require "ib"
+require "ib/project"
+require "rake"
+
+desc "Generates ui.xcodeproj and open it"
+task "design" do
+ IB::Project.new.write
+ system "open ui.xcodeproj"
+end
+
+module IB
+ PATH = File.expand_path("../ib.rb", File.dirname(__FILE__))
+end
@@ -0,0 +1,3 @@
+module IB
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,9 @@
+class CustomView < UIView
+ extend IB
+
+ ib_outlet :greenLabel, UIGreenLabel
+ ib_outlet :redLabel, "UILabel"
+
+ ib_action :someAction
+
+end
@@ -0,0 +1,2 @@
+class EmptyView < UIView
+end
@@ -0,0 +1,2 @@
+class SimpleClass
+end
@@ -0,0 +1,29 @@
+require "spec_helper"
+
+require "ib/generator"
+
+describe IB::Generator do
+ it "generates stubs" do
+ files = IB::Parser.new.find_all("spec/fixtures")
+ stubs = IB::Generator.new.generate_objc(files)
+ stubs.should == <<-OBJC
+@interface CustomView : UIView
+
+@property IBOutlet UIGreenLabel * greenLabel;
+@property IBOutlet UILabel * redLabel;
+
+-(IBAction) someAction:(id) sender;
+
+@end
+
+
+@interface EmptyView : UIView
+
+
+
+
+
+@end
+OBJC
+ end
+end
@@ -0,0 +1,20 @@
+require "spec_helper"
+
+require "ib/parser"
+
+describe IB::Parser do
+ it "finds outlets and actions" do
+ info = IB::Parser.new.find("spec/fixtures/custom_view.rb")
+ info[:class].should == [["CustomView", "UIView"]]
+ info[:outlets].should == [["greenLabel", "UIGreenLabel"], ["redLabel", "UILabel"]]
+ info[:actions].should == [["someAction"]]
+ end
+
+ it "detects simple classes" do
+ IB::Parser.new.find("spec/fixtures/simple_class.rb").should == false
+ end
+
+ it "finds all infos" do
+ puts IB::Parser.new.find_all("spec/fixtures").inspect
+ end
+end
@@ -0,0 +1,9 @@
+require "spec_helper"
+
+require "ib/project"
+
+describe IB::Project do
+ it "generates xcode project" do
+ IB::Project.new.write("spec/fixtures")
+ end
+end
@@ -0,0 +1,11 @@
+# This file was generated by the `rspec --init` command. Conventionally, all
+# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
+# Require this file using `require "spec_helper.rb"` to ensure that it is only
+# loaded once.
+#
+# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
+RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+ config.run_all_when_everything_filtered = true
+ config.filter_run :focus
+end

0 comments on commit de79288

Please sign in to comment.