Skip to content

Instantly share code, notes, and snippets.

@roy
Created April 20, 2012 13:12
Show Gist options
  • Save roy/2428397 to your computer and use it in GitHub Desktop.
Save roy/2428397 to your computer and use it in GitHub Desktop.
Local setup javascript based applications for ios
require 'sprockets'
require 'rack/rewrite'
require "erb"
project_root = File.expand_path(File.dirname(__FILE__))
assets = Sprockets::Environment.new(project_root) do |env|
env.logger = Logger.new(STDOUT)
end
assets.append_path(File.join(project_root, 'assets'))
assets.append_path(File.join(project_root, 'assets', 'javascripts'))
assets.append_path(File.join(project_root, 'assets', 'stylesheets'))
assets.append_path(File.join(project_root, 'assets', 'images'))
map "/build" do
run assets
end
status = 200
view = ERB.new(File.read("index.html.erb")).result
use Rack::ContentLength
use Rack::ContentType
run lambda {[status, {}, [ERB.new(File.read("index.html.erb")).result]]}
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta charset="utf-8">
<!-- iPad/iPhone specific css below, add after your main css >
<link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" />
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />
-->
<script type="text/javascript" src="build/application.js" charset="utf-8"></script>
<link rel="stylesheet" href="build/application.css" media="screen" type="text/css">
<script>
function BlockMove(event) {
// Tell Safari not to move the window.
event.preventDefault() ;
}
</script>
</head>
<body ontouchmove="BlockMove(event);">
<% Dir.glob("assets/javascripts/app/templates/**/*.handlebars").each do |file| %>
<% regex = /assets\/javascripts\/app\/templates\/(.*).handlebars/ %>
<script type="text/x-handlebars" data-template-name="<%= file.match(regex)[1] %>">
<%= File.read(file) %>
</script>
<% end %>
</body>
</html>
require 'rubygems'
require 'bundler'
require 'pathname'
require 'logger'
require 'fileutils'
Bundler.require
module Sprockets
class StaticCompiler
attr_accessor :env, :target, :paths
def initialize(env, target, paths, options = {})
@env = env
@target = target
@paths = paths
@digest = options.key?(:digest) ? options.delete(:digest) : true
@manifest = options.key?(:manifest) ? options.delete(:manifest) : true
@manifest_path = options.delete(:manifest_path) || target
@zip_files = options.delete(:zip_files) || /\.(?:css|html|js|svg|txt|xml)$/
end
def compile
manifest = {}
env.each_logical_path do |logical_path|
next unless compile_path?(logical_path)
if asset = env.find_asset(logical_path)
manifest[logical_path] = write_asset(asset)
end
end
write_manifest(manifest) if @manifest
end
def write_manifest(manifest)
FileUtils.mkdir_p(@manifest_path)
File.open("#{@manifest_path}/manifest.yml", 'wb') do |f|
YAML.dump(manifest, f)
end
end
def write_asset(asset)
path_for(asset).tap do |path|
filename = File.join(target, path)
FileUtils.mkdir_p File.dirname(filename)
asset.write_to(filename)
end
end
def compile_path?(logical_path)
paths.each do |path|
case path
when Regexp
return true if path.match(logical_path)
when Proc
return true if path.call(logical_path)
else
return true if File.fnmatch(path.to_s, logical_path)
end
end
false
end
def path_for(asset)
@digest ? asset.digest_path : asset.logical_path
end
end
end
ROOT = Pathname(File.dirname(__FILE__))
LOGGER = Logger.new(STDOUT)
BUNDLES = %w( application.css application.js )
BUILD_DIR = ROOT.join("build")
SOURCE_DIR = ROOT.join("assets")
task :compile do
enviorment = Sprockets::Environment.new(ROOT) do |env|
env.logger = LOGGER
end
enviorment.append_path(SOURCE_DIR.join('javascripts').to_s)
enviorment.append_path(SOURCE_DIR.join('stylesheets').to_s)
enviorment.append_path(SOURCE_DIR.join('images').to_s)
comp = Sprockets::StaticCompiler.new enviorment, BUILD_DIR, [/\.(png|jpg)$/, /^(application|ie)\.(css|js)$/], {:digest => false, :zip_files => false, :manifest => false }
comp.compile
# compile index.html.erb
File.open("index.html", 'w') {|f| f.write(ERB.new(File.read("index.html.erb")).result) }
end
task :build do
`cd .. && xcodebuild -configuration Debug -target rodiapp -sdk iphonesimulator5.1`
`iphonesim launch ~/Code/iPhone/rodiapp/build/Debug-iphonesimulator/rodiapp.app`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment