add a feature which support the template

This commit is contained in:
Watson
2013-04-19 23:02:49 +09:00
parent dd7605d2fd
commit 990e352d62
15 changed files with 225 additions and 83 deletions

View File

@@ -48,12 +48,22 @@ class CreateCommand < Command
self.help = 'Create a new project'
def self.run(args)
if args.size != 1
die "Usage: motion create <appname>"
if args.size < 1 || args.size > 2
die "Usage: motion create <appname> [--template=<template_name>]"
end
app_name = args.shift
Motion::Project::App.create(app_name)
app_name = ''
template_name = 'ios'
args.each do |a|
case a
when /--template=(.+)/
template_name = $1.to_s
else
app_name = a
end
end
Motion::Project::App.create(app_name, template_name)
end
end

View File

@@ -21,6 +21,8 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require 'erb'
module Motion; module Project
class App
VERBOSE =
@@ -80,85 +82,8 @@ module Motion; module Project
builder.codesign(config, platform)
end
def create(app_name)
unless app_name.match(/^[\w\s-]+$/)
fail "Invalid app name"
end
if File.exist?(app_name)
fail "Directory `#{app_name}' already exists"
end
App.log 'Create', app_name
Dir.mkdir(app_name)
Dir.chdir(app_name) do
App.log 'Create', File.join(app_name, '.gitignore')
File.open('.gitignore', 'w') do |io|
io.puts ".repl_history"
io.puts "build"
io.puts "tags"
io.puts "app/pixate_code.rb"
io.puts "resources/*.nib"
io.puts "resources/*.momd"
io.puts "resources/*.storyboardc"
io.puts ".DS_Store"
io.puts "nbproject"
io.puts ".redcar"
io.puts "#*#"
io.puts "*~"
io.puts "*.sw[po]"
io.puts ".eprj"
io.puts ".sass-cache"
io.puts ".idea"
end
App.log 'Create', File.join(app_name, 'Rakefile')
File.open('Rakefile', 'w') do |io|
io.puts <<EOS
# -*- coding: utf-8 -*-
$:.unshift(\"#{$motion_libdir}\")
require 'motion/project'
Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = '#{app_name}'
end
EOS
end
App.log 'Create', File.join(app_name, 'app')
Dir.mkdir('app')
App.log 'Create', File.join(app_name, 'app/app_delegate.rb')
File.open('app/app_delegate.rb', 'w') do |io|
io.puts <<EOS
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
true
end
end
EOS
end
App.log 'Create', File.join(app_name, 'resources')
Dir.mkdir('resources')
Dir.glob("#{$motion_libdir}/../resources/**/*").each do |file|
App.log 'Create', File.join(app_name, 'resources', File.basename(file))
FileUtils.cp(file, 'resources')
end
App.log 'Create', File.join(app_name, 'spec')
Dir.mkdir('spec')
App.log 'Create', File.join(app_name, 'spec/main_spec.rb')
File.open('spec/main_spec.rb', 'w') do |io|
io.puts <<EOS
describe "Application '#{app_name}'" do
before do
@app = UIApplication.sharedApplication
end
it "has one window" do
@app.windows.size.should == 1
end
end
EOS
end
end
def create(app_name, template_name="ios")
Template.new(app_name, template_name).generate
end
def log(what, msg)
@@ -184,5 +109,75 @@ EOS
log what, msg unless VERBOSE
end
end
class Template
# for ERB
attr_reader :name
def initialize(app_name, template_name)
@name = @app_name = app_name
@template_name = template_name
@template_directory = File.expand_path(File.join(__FILE__, "../../template/#{@template_name}"))
unless app_name.match(/^[\w\s-]+$/)
fail "Invalid app name"
end
if File.exist?(app_name)
fail "Directory `#{app_name}' already exists"
end
unless File.exist?(@template_directory)
fail "Invalid template name"
end
end
def generate
App.log 'Create', @app_name
FileUtils.mkdir(@app_name)
Dir.chdir(@app_name) do
create_directories()
create_files()
end
end
private
def template_directory
@template_directory
end
def create_directories
Dir.glob(File.join(template_directory, "**/")).each do |dir|
dir.sub!("#{template_directory}/", '')
FileUtils.mkdir_p(dir) if dir.length > 0
end
end
def create_files
Dir.glob(File.join(template_directory, "**/*"), File::FNM_DOTMATCH).each do |src|
dest = src.sub("#{template_directory}/", '')
next if File.directory?(src)
next if dest.include?(".DS_Store")
dest = replace_file_name(dest)
if dest =~ /(.+)\.erb$/
App.log 'Create', "#{@app_name}/#{$1}"
File.open($1, "w") { |io|
io.print ERB.new(File.read(src)).result(binding)
}
else
App.log 'Create', "#{@app_name}/#{dest}"
FileUtils.cp(src, dest)
end
end
end
def replace_file_name(file_name)
file_name = file_name.sub("{name}", "#{@name}")
file_name
end
end
end
end; end

View File

@@ -0,0 +1,29 @@
# <%= name %>
TODO: Write a gem description
## Installation
Add this line to your application's Gemfile:
gem '<%= name %>'
And then execute:
$ bundle
Or install it yourself as:
$ gem install <%= name %>
## Usage
TODO: Write usage instructions here
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

View File

@@ -0,0 +1,9 @@
# -*- coding: utf-8 -*-
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project'
require './lib/<%= name %>'
Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = '<%= name %>'
end

View File

@@ -0,0 +1,5 @@
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
true
end
end

View File

@@ -0,0 +1,16 @@
<%=
indent = 0
string = ""
module_names = name.split(/[-_]/).map{ |x| x.capitalize }
module_names.each do |m|
string.concat(" " * indent + "class #{m}\n")
indent += 2
end
string = string + "\n"
module_names.size.times do
indent -= 2
string.concat(" " * indent + "end\n")
end
string
%>

View File

@@ -0,0 +1,8 @@
unless defined?(Motion::Project::Config)
raise "This file must be required within a RubyMotion project Rakefile."
end
lib_dir_path = File.dirname(File.expand_path(__FILE__))
Motion::Project::App.setup do |app|
app.files.unshift(Dir.glob(File.join(lib_dir_path, "project/**/*.rb")))
end

View File

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

@@ -0,0 +1,9 @@
describe "Application '<%= name %>'" do
before do
@app = UIApplication.sharedApplication
end
it "has one window" do
@app.windows.size.should == 1
end
end

View File

@@ -0,0 +1,23 @@
# -*- encoding: utf-8 -*-
VERSION = "1.0"
Gem::Specification.new do |spec|
spec.name = "<%= name %>"
spec.version = VERSION
spec.authors = ["<%= `git config --get user.name`.strip %>"]
spec.email = ["<%= `git config --get user.email`.strip %>"]
spec.description = %q{TODO: Write a gem description}
spec.summary = %q{TODO: Write a gem summary}
spec.homepage = ""
spec.license = ""
files = []
files << 'README.md'
files.concat(Dir.glob('lib/**/*.rb'))
spec.files = files
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.add_development_dependency "rake"
end

16
lib/motion/template/ios/.gitignore vendored Normal file
View File

@@ -0,0 +1,16 @@
.repl_history
build
tags
app/pixate_code.rb
resources/*.nib
resources/*.momd
resources/*.storyboardc
.DS_Store
nbproject
.redcar
#*#
*~
*.sw[po]
.eprj
.sass-cache
.idea

View File

@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project'
Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = '<%= name %>'
end

View File

@@ -0,0 +1,5 @@
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
true
end
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

@@ -0,0 +1,9 @@
describe "Application '<%= name %>'" do
before do
@app = UIApplication.sharedApplication
end
it "has one window" do
@app.windows.size.should == 1
end
end