mirror of
https://github.com/zhigang1992/RubyMotion.git
synced 2026-04-01 09:31:20 +08:00
124 lines
2.7 KiB
Ruby
124 lines
2.7 KiB
Ruby
#!/usr/bin/ruby
|
|
|
|
$motion_libdir = File.expand_path(File.join(File.dirname(File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__), '../lib'))
|
|
$:.unshift($motion_libdir)
|
|
require 'motion/version'
|
|
|
|
class Command
|
|
class << self
|
|
attr_accessor :name
|
|
attr_accessor :help
|
|
end
|
|
end
|
|
|
|
class CreateCommand < Command
|
|
self.name = 'create'
|
|
self.help = 'Create a new project'
|
|
|
|
def self.run(args)
|
|
if args.size != 1
|
|
$stderr.puts "Please specify an app name (e.g. motion create Hello)"
|
|
return false
|
|
end
|
|
|
|
app_name = args.shift
|
|
|
|
unless app_name.match(/^[a-zA-Z\d\s]+$/)
|
|
$stderr.puts "Invalid app name"
|
|
return false
|
|
end
|
|
|
|
if File.exist?(app_name)
|
|
$stderr.puts "Directory `#{app_name}' already exists"
|
|
return false
|
|
end
|
|
|
|
Dir.mkdir(app_name)
|
|
Dir.chdir(app_name) do
|
|
File.open('Rakefile', 'w') do |io|
|
|
io.puts <<EOS
|
|
$:.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
|
|
Dir.mkdir('app')
|
|
File.open('app/main.rb', 'w') do |io|
|
|
io.puts <<EOS
|
|
class AppDelegate
|
|
def application(application, didFinishLaunchingWithOptions:launchOptions)
|
|
puts "Hello World!"
|
|
return true
|
|
end
|
|
end
|
|
EOS
|
|
end
|
|
Dir.mkdir('resources')
|
|
Dir.mkdir('spec')
|
|
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
|
|
end
|
|
end
|
|
|
|
class UpdateCommand < Command
|
|
self.name = 'update'
|
|
self.help = 'Update current distribution'
|
|
|
|
def self.run(args)
|
|
$stderr.puts "update command not implemented yet"
|
|
return false
|
|
end
|
|
end
|
|
|
|
class MotionMainCommand
|
|
Commands = [CreateCommand, UpdateCommand]
|
|
|
|
def initialize(args)
|
|
arg = args.shift
|
|
case arg
|
|
when '-h', '--help'
|
|
usage
|
|
when '-v', '--version'
|
|
$stdout.puts Motion::Version
|
|
exit 1
|
|
when /^-/
|
|
$stderr.puts "Unknown option: #{arg}"
|
|
exit 1
|
|
end
|
|
command = Commands.find { |command| command.name == arg }
|
|
usage unless command
|
|
exit command.run(args) ? 0 : 1
|
|
end
|
|
|
|
def usage
|
|
$stderr.puts 'Usage:'
|
|
$stderr.puts " #{File.basename(__FILE__)} [-h, --help]"
|
|
$stderr.puts " #{File.basename(__FILE__)} [-v, --version]"
|
|
$stderr.puts " #{File.basename(__FILE__)} <command> [<args...>]"
|
|
$stderr.puts ''
|
|
$stderr.puts 'Commands:'
|
|
Commands.each do |command|
|
|
$stderr.puts " #{command.name}".ljust(15) + command.help
|
|
end
|
|
exit 1
|
|
end
|
|
end
|
|
|
|
MotionMainCommand.new(ARGV)
|