Skip to content

Commit

Permalink
Add image metadata #40
Browse files Browse the repository at this point in the history
  • Loading branch information
victorteokw committed May 9, 2015
1 parent 4eb48e0 commit 9a1b5ed
Show file tree
Hide file tree
Showing 25 changed files with 460 additions and 267 deletions.
1 change: 1 addition & 0 deletions cocoa_bean.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'sprockets-es6', '~> 0.6'
s.add_runtime_dependency 'jasmine', '~> 2.3'
s.add_runtime_dependency 'uglifier', '~> 2.7'
s.add_runtime_dependency 'fastimage', '~> 1.6.8'

s.add_development_dependency 'bundler', '~> 1.9'
s.add_development_dependency 'rake', '~> 10.4'
Expand Down
1 change: 1 addition & 0 deletions lib/cocoa_bean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ module CocoaBean
autoload :Generator, 'cocoa_bean/generator'
autoload :Task, 'cocoa_bean/task'
autoload :Preferences, 'cocoa_bean/preferences'
autoload :UserInterface, 'cocoa_bean/user_interface'
end
29 changes: 18 additions & 11 deletions lib/cocoa_bean/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,38 @@ def validate_platform!
attr_accessor :app
attr_accessor :platform

def self.options
[
['--more', 'Verbose output']
].concat(super)
end

def run
help!
end

def validate!
if self.class.beanfile_required?
warning_and_exit 'You should run this command inside a cocoa bean application directory.' unless beanfile_location
UserInterface.exit 'You should run this command inside a cocoa bean application directory.' unless beanfile_location
end
if self.class.validate_platform?
warning_and_exit 'Provide a platform is required.' unless @platform
warning_and_exit 'Platform is not supported by this app.' unless @app.supported_platforms.include? @platform
UserInterface.exit 'Provide a platform is required.' unless @platform
UserInterface.exit 'Platform is not supported by this app.' unless @app.supported_platforms.include? @platform
end
end

def warning_and_exit(message)
puts ('[!] ' + message).red
exit 1
end

def initialize(argv)
super
if self.class.beanfile_required?
load beanfile_location
@app = CocoaBean::Application.only_app
@app.root_directory = beanfile_directory
if beanfile_location
load beanfile_location
@app = CocoaBean::Application.only_app
@app.root_directory = beanfile_directory
end
end
unless argv.flag?('more')
require 'rake'
Rake::FileUtilsExt.verbose_flag = false
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/cocoa_bean/command/about.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def run
random_color_put " Platform specific code location: #{bold(p.code_location)}\n"
end
rescue CocoaBean::Application::ApplicationCountError => e
warning_and_exit("You should only declare one cocoa bean application.")
UserInterface.exit("You should only declare one cocoa bean application.")
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/cocoa_bean/command/open.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ def initialize(argv)
def run
begin
if @editor.nil? || @editor == ''
warning_and_exit("You haven't set your favorite editor.")
UserInterface.exit("You haven't set your favorite editor.")
end
if @root
system "#{@editor} #{beanfile_directory}"
else
system "#{@editor} #{Dir.pwd}"
end
rescue CocoaBean::Application::ApplicationCountError => e
warning_and_exit("You should only declare one cocoa bean application in the Beanfile.")
UserInterface.exit("You should only declare one cocoa bean application in the Beanfile.")
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/cocoa_bean/command/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def initialize(argv)
end

def run
temp = @app.full_path(@app.temporary_directory)
temp = File.expand_path("web", @app.full_path(@app.temporary_directory))
app_source = @app.full_path(@app.code_location)
web_source = @app.full_path(@app.get_platform(@platform).code_location)
ass_source = @app.full_path(@app.assets_location)
CocoaBean::Task.invoke("prev:#@platform:all",
CocoaBean::Task.invoke("preview:#@platform:all",
temp,
app_source,
web_source,
Expand Down
4 changes: 0 additions & 4 deletions lib/cocoa_bean/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,5 @@ def generate

attr_accessor :destination

def warning_and_exit(message)
puts ('[!] ' + message).red
exit 1
end
end
end
6 changes: 3 additions & 3 deletions lib/cocoa_bean/generator/template_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,23 @@ def process_file(from, to)
File.write(to, renderer.result(binding))
output_create_file(to)
rescue FileExistError => e
warning_and_exit("File #{to} exists! Cannot generate new application.")
UserInterface.exit("File #{to} exists! Cannot generate new application.")
end

def copy_file(from, to)
raise FileExistError if File.exist?(to)
FileUtils::cp(from, to)
output_create_file(to)
rescue FileExistError => e
warning_and_exit("File #{to} exists! Cannot generate new application.")
UserInterface.exit("File #{to} exists! Cannot generate new application.")
end

def create_directory(dir_path)
raise DirectoryExistError if Dir.exist?(dir_path)
FileUtils::mkdir_p(dir_path)
output_create_file(dir_path)
rescue DirectoryExistError => e
warning_and_exit("Directory #{dir_path} exists! Cannot generate new application.")
UserInterface.exit("Directory #{dir_path} exists! Cannot generate new application.")
end

def output_create_file(file_or_dir)
Expand Down
26 changes: 13 additions & 13 deletions lib/cocoa_bean/task.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
require 'rake'

UI = CocoaBean::UserInterface

def invoke(name, *args)
Rake::Task[name].invoke(*args)
end

module CocoaBean
class Task
require 'cocoa_bean/task/dist'
require 'cocoa_bean/task/genapp'
require 'cocoa_bean/task/preview'
require 'cocoa_bean/task/test'

def self.invoke(task_name, *args)
require 'rake'
require File.expand_path('helpers/build_js.rb', self.root_directory_of_tasks)
load_rake_file_for_task_named(task_name)
Rake::Task[task_name].invoke(*args)
end

def self.load_rake_file_for_task_named(task_name)
tokens = task_name.split(":")
tokens.pop
path = tokens.join('/') + '.rake'
load File.expand_path(path, root_directory_of_tasks)
end

def self.root_directory_of_cocoa_bean
File.expand_path('../../../', __FILE__)
end

def self.root_directory_of_tasks
File.expand_path('tasks', root_directory_of_cocoa_bean)
end
end
end
8 changes: 8 additions & 0 deletions lib/cocoa_bean/task/dist.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace "dist" do
namespace "web" do
task "all",
:app_source, :web_source, :ass_source, :dest do |t, args|
invoke "gen:app:web:all", *args
end
end
end
178 changes: 178 additions & 0 deletions lib/cocoa_bean/task/genapp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
def build_js(from_dir, build_file_name, to_dir, target_file_name)
require 'sprockets'
target = File.expand_path(target_file_name, to_dir)
environment = Sprockets::Environment.new
environment.append_path(from_dir)
js = environment[build_file_name].to_s
File.write(target, js)
end

namespace "gen" do
namespace "app" do
namespace "web" do

task "all",
:app_source, :web_source, :ass_source, :dest do |t, args|
app_source = args[:app_source]
web_source = args[:web_source]
ass_source = args[:ass_source]
dest = args[:dest]

invoke "gen:app:web:create dest dir", dest
invoke "gen:app:web:copy user plat spec code", web_source, dest
invoke "gen:app:web:gen user app js", app_source, dest
invoke "gen:app:web:dl jq", dest
invoke "gen:app:web:gen cb js", dest
invoke "gen:app:web:cp user assets", ass_source, dest
invoke "gen:app:web:create image metadata", ass_source, dest
end

task "create dest dir", :dest do |t, args|
dest = args[:dest]

directory dest do
UI.happy "destination directory created"
end.invoke
end

task "copy user plat spec code", :source, :dest do |t, args|
source = args[:source]
dest = args[:dest]

did_something = false

sources = Rake::FileList.new File.expand_path('**/*', source)
sources.map do |f|
target = f.gsub(source, dest)
file target => f do
if File.directory? f
sh "mkdir -p #{target}"
else
sh "cp #{f} #{target}"
end
did_something = true
end.invoke
end
end

task "gen user app js", :source_dir, :dest do |t, args|
source_dir = args[:source_dir]
dest = args[:dest]

sources = Rake::FileList.new File.expand_path('**/*', source_dir)
target = File.expand_path('application.js', dest)
file target => sources do
build_js source_dir, 'build.js', dest, 'application.js'
UI.happy "application.js generated"
end.invoke
end

task "dl jq", :dest do |t, args|
dest = args[:dest]

jquery_location = File.expand_path('jquery-2.1.3.js', args[:dest])

file jquery_location do
require 'open-uri'
content = open("http://code.jquery.com/jquery-2.1.3.js").read
File.open(jquery_location, 'w') {|f| f.write(content)}
UI.happy("#{jquery_location} downloaded")
end.invoke
end

task "gen cb js", :dest do |t, args|
dest = args[:dest]

cb_path = File.expand_path('src', CocoaBean::Task.root_directory_of_cocoa_bean)
sources = Rake::FileList.new(File.expand_path('**/*', cb_path))
target_name = 'cocoabean-' + CocoaBean::VERSION + '.js'
target = File.expand_path(target_name, dest)

file target => sources do
build_js(cb_path, 'build.js', dest, target_name)
UI.happy("#{target_name} generated")
end.invoke
end

task "cp user assets", :source, :dest do |t, args|
source = args[:source]
dest = args[:dest]

if File.directory? source
assets_dir = File.expand_path('assets', dest)
directory(assets_dir).invoke

did_something = false

sources = Rake::FileList.new File.expand_path('**/*', source)
sources.map do |f|
target = f.gsub(source, assets_dir)
file target => f do
if File.directory?(f)
sh "mkdir -p #{target}"
else
sh "cp #{f} #{target}"
end
did_something = true
end.invoke
end
UI.happy "assets copied" if did_something
end
end

task "create image metadata", :source, :dest do |t, args|
source = args[:source]
dest = args[:dest]

require 'fastimage'

if File.directory? source
metadata_target = File.expand_path('imagedata.js', dest)
all_assets = Rake::FileList.new File.expand_path('**/*', source)
images = all_assets.select {|f| f.match /\.(jpg|png|bmp|svg)$/ }
file metadata_target => images do
doublePixelRatios = images.select { |f| f.match /@2x/ }
triplePixelRatios = images.select { |f| f.match /@3x/ }
singlePixelRatios = images - doublePixelRatios - triplePixelRatios

jash = {}

images.each do |i|
if doublePixelRatios.include?(i)
pixel_ratio = 2
availability_flag = "@2x"
elsif triplePixelRatios.include?(i)
pixel_ratio = 3
availability_flag = "@3x"
elsif singlePixelRatios.include?(i)
pixel_ratio = 1
availability_flag = "@1x"
end

image_file_name = i
key_name = File.basename(i).sub('@2x', '').sub('@3x', '')
descriptor = jash[key_name]
if descriptor
jash[key_name][availability_flag] = true
# descriptor[availability_flag] = true
else
descriptor = {}
size = FastImage.size(i)
descriptor['width'] = (size[0] / pixel_ratio).floor
descriptor['height'] = (size[1] / pixel_ratio).floor
descriptor[availability_flag] = true
jash[key_name] = descriptor
end
end

content = "CB.__ImageMetadata = " + JSON.pretty_generate(jash) + ';'
File.write(metadata_target, content)

UI.happy "image metadata file generated"

end.invoke
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/cocoa_bean/task/preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace "preview" do
namespace "web" do
task "all",
:web_dir, :app_source, :web_source, :ass_source do |t, args|
web_dir = args[:web_dir]
app_source = args[:app_source]
web_source = args[:web_source]
ass_source = args[:ass_source]

invoke "gen:app:web:all", app_source, web_source, ass_source, web_dir
invoke "preview:web:launch", web_dir
end

task "launch", :web_dir do |t, args|
web_dir = args[:web_dir]

require 'webrick'
server = WEBrick::HTTPServer.new Port: 3000
server.mount "/", WEBrick::HTTPServlet::FileHandler, web_dir
trap('INT') { server.stop }
server.start
end
end
end

0 comments on commit 9a1b5ed

Please sign in to comment.