kreegerator 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/kreegerator ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems' # ruby1.9 doesn't "require" it though
3
+ require 'thor'
4
+ require 'kreegerator/cli'
5
+
6
+ Kreegerator::CLI.start
data/kreegerator.gemspec CHANGED
@@ -4,10 +4,10 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'kreegerator/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
- gem.name = "kreegerator"
7
+ gem.name = 'kreegerator'
8
8
  gem.version = Kreegerator::VERSION
9
- gem.authors = ["Ben Kreeger"]
10
- gem.email = ["ben@kree.gr"]
9
+ gem.authors = ['Ben Kreeger']
10
+ gem.email = ['ben@kree.gr']
11
11
  gem.description = %q{A set of generators for various projects I work on.}
12
12
  gem.summary = %q{A set of generators for various projects I work on.}
13
13
  gem.homepage = 'http://github.com/kreeger/kreegerator'
@@ -15,10 +15,11 @@ Gem::Specification.new do |gem|
15
15
  gem.files = `git ls-files`.split($/)
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
- gem.require_paths = ["lib"]
18
+ gem.require_paths = ['lib']
19
19
 
20
20
  gem.add_runtime_dependency 'thor'
21
- gem.add_runtime_dependency 'xcodeproj'
21
+ gem.add_runtime_dependency 'tilt'
22
+ gem.add_runtime_dependency 'mini_magick'
22
23
 
23
24
  gem.add_development_dependency 'rake'
24
25
  end
@@ -0,0 +1,48 @@
1
+ require 'logger'
2
+ require 'thor'
3
+ require 'kreegerator'
4
+
5
+ class Kreegerator::CLI < Thor
6
+
7
+ map '-i' => :ios
8
+ map '-r' => :retina
9
+
10
+ desc 'ios TEMPLATE CLASS_NAME', 'fire off an iOS generator'
11
+ method_option :destination, desc: 'The destination where the file will end up.'
12
+ def ios(method_name, filename=nil)
13
+ if Kreegerator::IOS.respond_to?(method_name.to_sym)
14
+ if method_name == 'list'
15
+ Kreegerator::IOS.send(method_name.to_sym)
16
+ else
17
+ Kreegerator::IOS.send(method_name.to_sym, path_helper(options[:destination]), filename)
18
+ end
19
+ else
20
+ puts "The generator you asked for ('#{method_name}') does not exist."
21
+ end
22
+ end
23
+
24
+ desc 'retina ACTION GLOB', 'handles image adjustments for retina displays'
25
+ method_option :pngnq, desc: 'Runs the files through pngnq as well.', type: :integer
26
+ def retina(method_name, glob)
27
+ if Kreegerator::Retina.respond_to?(method_name.to_sym)
28
+ if method_name == 'list'
29
+ Kreegerator::Retina.send(method_name.to_sym)
30
+ else
31
+ Kreegerator::Retina.send(method_name.to_sym, glob, options)
32
+ end
33
+ else
34
+ puts "The action you asked for ('#{method_name}') does not exist."
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def path_helper(path)
41
+ if path.nil?
42
+ path = Dir.pwd
43
+ elsif !File.directory?(path)
44
+ path = File.basename(path)
45
+ end
46
+ path
47
+ end
48
+ end
@@ -0,0 +1,31 @@
1
+ require 'kreegerator/renderable'
2
+
3
+ class Kreegerator::IOS
4
+
5
+ class << self
6
+ include Kreegerator::Renderable
7
+
8
+ def list
9
+ puts methods(false).select { |m| m != :list }.join("\n")
10
+ end
11
+
12
+ def view_controller(path, name)
13
+ puts "Generating file for #{name} in #{File.expand_path path}."
14
+ render_headers_and_implementations 'view_controller', name, {}, path
15
+ end
16
+ alias_method :vc, :view_controller
17
+ alias_method :viewcontroller, :view_controller
18
+ alias_method :controller, :view_controller
19
+
20
+ private
21
+
22
+ def render_headers_and_implementations(template, class_name, data, destination_path)
23
+ data.merge!({ class_name: class_name })
24
+ renders = Hash[%w(h m).map { |t| [t.to_sym, render("ios/#{template}.#{t}.erb", data)] }]
25
+ renders.each do |type, contents|
26
+ filename = File.join File.expand_path(destination_path), "#{class_name}.#{type}"
27
+ File.open(filename, 'w') { |f| f.write contents }
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,38 @@
1
+ require 'tilt'
2
+ require 'erb'
3
+
4
+ # This module uses the amazing Tilt library to give the ability to render
5
+ # a template in the `lib/filemover/templates` directory to any including
6
+ # class.
7
+ module Kreegerator::Renderable
8
+
9
+ # Accepts path to a template relative to `lib/filemover/templates` along
10
+ # with a hash and renders a template using the amazing and magical Tilt.
11
+ #
12
+ # @param [String] template the relative path to the template, from
13
+ # `lib/filemover/templates`.
14
+ # @param [Hash] data the hash of values to render. The keys will be sent
15
+ # to the template as instance variables `@like_this`.
16
+ def render(template, data)
17
+
18
+ # META! Assign instance variables to self using the hash passed in.
19
+ data.each { |key, value| instance_variable_set "@#{key}", value }
20
+
21
+ # Get the absolute template location relative to our template basepath.
22
+ template_location = File.join(template_path, template)
23
+
24
+ # Let Tilt do the rest. Also pass in `self` as that's the reference to
25
+ # the object that holds our new, metaprogrammed `@instance_variables`.
26
+ Tilt.new(template_location).render(self)
27
+ end
28
+
29
+ protected
30
+
31
+ # A helper that provides the absolute path to `Filemover`'s master template
32
+ # directory at `lib/filemover/templates`.
33
+ #
34
+ # @return [String] the absolute path of the `templates` directory.
35
+ def template_path
36
+ File.expand_path('../../../templates', __FILE__)
37
+ end
38
+ end
@@ -0,0 +1,45 @@
1
+ require 'FileUtils'
2
+ require 'mini_magick'
3
+
4
+ class Kreegerator::Retina
5
+
6
+ class << self
7
+
8
+ def list
9
+ puts methods(false).select { |m| m != :list }.join("\n")
10
+ end
11
+
12
+ def downscale(glob, opts)
13
+ Dir[glob].select { |f| f =~ /\.png\Z/i }.each do |file|
14
+ puts "Downscaling #{file}."
15
+ scale_image file, 0.5, true
16
+ crunch_image(file, opts[:pngnq]) if opts[:pngnq]
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def scale_image(file, multiplier, is_retina)
23
+ file = adjust_name_if_needed file, is_retina
24
+ image = MiniMagick::Image.open file
25
+ new_dim = [(image[:width] * multiplier).to_i, (image[:height] * multiplier).to_i].join('x')
26
+ new_name = file.gsub '@2x.png', '.png'
27
+ image.resize new_dim
28
+ image.write new_name
29
+ end
30
+
31
+ def crunch_image(file, colors)
32
+ puts "Crunching image #{file}."
33
+ cmd = "pngnq #{file} -f -s 1"
34
+ cmd << " -n #{colors}" unless colors.nil?
35
+ system cmd
36
+ end
37
+
38
+ def adjust_name_if_needed(file, is_retina)
39
+ return file if is_retina && file =~ /@2x\./
40
+ new_file = file.gsub '.png', '@2x.png'
41
+ FileUtils.mv(file, new_file)
42
+ new_file
43
+ end
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
1
  module Kreegerator
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/lib/kreegerator.rb CHANGED
@@ -1,4 +1,6 @@
1
- require "kreegerator/version"
1
+ require 'kreegerator/ios'
2
+ require 'kreegerator/retina'
3
+ require 'kreegerator/version'
2
4
 
3
5
  module Kreegerator
4
6
  # Your code goes here...
@@ -0,0 +1,10 @@
1
+ //
2
+ // <%= @class_name %>.h
3
+ // Created on <%= Time.now.strftime('%y/%m/%d') %>.
4
+ //
5
+
6
+ #import <UIKit/UIKit.h>
7
+
8
+ @interface <%= @class_name %> : UIViewController
9
+
10
+ @end
@@ -0,0 +1,41 @@
1
+ //
2
+ // <%= @class_name %>.m
3
+ // Created on <%= Time.now.strftime('%y/%m/%d') %>.
4
+ //
5
+
6
+ #import "<%= @class_name %>.h"
7
+
8
+ @interface <%= @class_name %> ()
9
+ @end
10
+
11
+ @implementation <%= @class_name %>
12
+
13
+ #pragma mark - Lifecycle
14
+
15
+ - (id)init {
16
+ if ((self = [super init])) {
17
+ }
18
+ return self;
19
+ }
20
+
21
+ - (void)viewDidLoad {
22
+ [super viewDidLoad];
23
+ }
24
+
25
+ - (void)viewWillAppear:(BOOL)animated {
26
+ [super viewWillAppear:animated];
27
+ }
28
+
29
+ - (void)viewDidAppear:(BOOL)animated {
30
+ [super viewDidAppear:animated];
31
+ }
32
+
33
+ - (void)viewDidDisappear:(BOOL)animated {
34
+ [super viewDidDisappear:animated];
35
+ }
36
+
37
+ - (void)didReceiveMemoryWarning {
38
+ [super didReceiveMemoryWarning];
39
+ }
40
+
41
+ @end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kreegerator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-12 00:00:00.000000000 Z
12
+ date: 2012-12-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
@@ -28,7 +28,23 @@ dependencies:
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
- name: xcodeproj
31
+ name: tilt
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: mini_magick
32
48
  requirement: !ruby/object:Gem::Requirement
33
49
  none: false
34
50
  requirements:
@@ -62,7 +78,8 @@ dependencies:
62
78
  description: A set of generators for various projects I work on.
63
79
  email:
64
80
  - ben@kree.gr
65
- executables: []
81
+ executables:
82
+ - kreegerator
66
83
  extensions: []
67
84
  extra_rdoc_files: []
68
85
  files:
@@ -71,9 +88,16 @@ files:
71
88
  - LICENSE.txt
72
89
  - README.md
73
90
  - Rakefile
91
+ - bin/kreegerator
74
92
  - kreegerator.gemspec
75
93
  - lib/kreegerator.rb
94
+ - lib/kreegerator/cli.rb
95
+ - lib/kreegerator/ios.rb
96
+ - lib/kreegerator/renderable.rb
97
+ - lib/kreegerator/retina.rb
76
98
  - lib/kreegerator/version.rb
99
+ - templates/ios/view_controller.h.erb
100
+ - templates/ios/view_controller.m.erb
77
101
  homepage: http://github.com/kreeger/kreegerator
78
102
  licenses: []
79
103
  post_install_message:
@@ -88,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
88
112
  version: '0'
89
113
  segments:
90
114
  - 0
91
- hash: -4444663874704855524
115
+ hash: 721120211358296094
92
116
  required_rubygems_version: !ruby/object:Gem::Requirement
93
117
  none: false
94
118
  requirements:
@@ -97,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
121
  version: '0'
98
122
  segments:
99
123
  - 0
100
- hash: -4444663874704855524
124
+ hash: 721120211358296094
101
125
  requirements: []
102
126
  rubyforge_project:
103
127
  rubygems_version: 1.8.23