Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add app generator

  • Loading branch information...
commit fa3e88afe32d5c1fe574a942342f35b16e4ed270 1 parent a75526b
@jcrosby authored
View
2  bin/cloudkit
@@ -3,4 +3,4 @@ $:.unshift(File.dirname(__FILE__) + '/../lib')
require 'cloudkit'
args = ARGV.dup
-CloudKit::Command.run(args)
+CloudKit::Command.new.run(args)
View
1  lib/cloudkit.rb
@@ -14,6 +14,7 @@
require 'oauth/request_proxy/rack_request'
require 'oauth/server'
require 'oauth/signature'
+require 'cloudkit/generator'
require 'cloudkit/command'
require 'cloudkit/constants'
require 'cloudkit/exceptions'
View
22 lib/cloudkit/command.rb
@@ -1,11 +1,27 @@
module CloudKit
class Command
- def self.run(args)
+ include CloudKit::Generator
+
+ def run(args)
return help unless command = args.first
+ case command
+ when 'help'
+ help
+ else
+ @app_name = destination_root = command
+ template_root = File.join(File.expand_path(File.dirname(__FILE__)), 'templates', 'gen')
+ generate_app(template_root, destination_root)
+ end
+ end
+
+ def generate_app(template_root, destination_root)
+ FileUtils.mkdir(destination_root)
+ copy_contents(template_root, destination_root)
+ Formatador.display_line("[bold]#{@app_name} was created[/]")
end
- def self.help
- puts "CloudKit"
+ def help
+ Formatador.display_line("CloudKit")
end
end
end
View
36 lib/cloudkit/generator.rb
@@ -0,0 +1,36 @@
+module CloudKit
+ module Generator
+ def copy_contents(template_root, destination_root)
+ paths(template_root).each do |template_path|
+ if File.directory?(template_path)
+ generate_directory(template_path, destination_root)
+ copy_contents(template_path, File.join(destination_root, File.basename(template_path)))
+ else
+ generate_file(template_path, destination_root)
+ end
+ end
+ end
+
+ def generate_directory(template_path, destination_root)
+ FileUtils.mkdir_p(File.join(destination_root, File.basename(sub(template_path))))
+ end
+
+ def generate_file(template_path, destination_root)
+ content = File.read(template_path)
+ path_elements = File.split(sub(destination_root)).push(File.basename(sub(template_path)))
+ File.open(File.join(path_elements), 'w+') do |file|
+ file.puts(ERB.new(content).result(binding))
+ end
+ end
+
+ def paths(directory)
+ Dir.glob(File.join(directory, '*'), File::FNM_DOTMATCH).reject do |f|
+ f.match(/\/(\.){1,2}$/)
+ end
+ end
+
+ def sub(name)
+ name.sub('%app_name%', @app_name)
+ end
+ end
+end
View
10 lib/cloudkit/templates/gen/Gemfile
@@ -0,0 +1,10 @@
+source :gemcutter
+
+gem 'rack', '~> 1.1'
+gem 'sinatra', '~> 0.9'
+
+group :test do
+ gem 'rake'
+ gem 'rspec', '~> 1.3', :require => 'spec'
+ gem 'bundler', '~> 0.9.10'
+end
View
0  lib/cloudkit/templates/gen/README
No changes.
View
28 lib/cloudkit/templates/gen/Rakefile
@@ -0,0 +1,28 @@
+begin
+ require File.expand_path('../.bundle/environment', __FILE__)
+rescue LoadError
+ require 'rubygems'
+ require 'bundler'
+ Bundler.setup
+end
+
+require 'rake/clean'
+require 'spec/rake/spectask'
+
+Bundler.require(:default, :test)
+
+task :default => :spec
+
+desc "Run all examples (or a specific spec with TASK=xxxx)"
+Spec::Rake::SpecTask.new('spec') do |t|
+ t.spec_opts = ["-c"]
+ t.spec_files = begin
+ if ENV["TASK"]
+ ENV["TASK"].split(',').map { |task| "spec/**/#{task}_spec.rb" }
+ else
+ FileList['spec/**/*_spec.rb']
+ end
+ end
+end
+
+end
View
13 lib/cloudkit/templates/gen/config.ru
@@ -0,0 +1,13 @@
+begin
+ require File.expand_path('.bundle/environment', __FILE__)
+rescue LoadError
+ require 'rubygems'
+ require 'bundler'
+ Bundler.setup
+end
+
+Bundler.require
+require File.expand_path(File.join(File.dirname(__FILE__), 'lib', '<%= @app_name %>'))
+
+use Rack::Static, :urls => ["/css", "/img", "/js"], :root => "public"
+run <%= @app_name.capitalize %>.app
View
0  lib/cloudkit/templates/gen/config/.gitignore
No changes.
View
8 lib/cloudkit/templates/gen/lib/%app_name%.rb
@@ -0,0 +1,8 @@
+require 'sinatra/base'
+require File.dirname(__FILE__) + '/<%= @app_name %>/app'
+
+module <%= @app_name.capitalize %>
+ def self.app
+ <%= @app_name.capitalize %>::App
+ end
+end
View
9 lib/cloudkit/templates/gen/lib/%app_name%/app.rb
@@ -0,0 +1,9 @@
+module <%= @app_name.capitalize %>
+ class App < Sinatra::Base
+
+ get '/' do
+ erb :index
+ end
+
+ end
+end
View
0  lib/cloudkit/templates/gen/log/.gitignore
No changes.
View
0  lib/cloudkit/templates/gen/public/css/main.css
No changes.
View
0  lib/cloudkit/templates/gen/public/img/.gitignore
No changes.
View
0  lib/cloudkit/templates/gen/public/js/app.js
No changes.
View
6,240 lib/cloudkit/templates/gen/public/js/jquery-1.4.2.js
6,240 additions, 0 deletions not shown
View
1  lib/cloudkit/templates/gen/views/index.erb
@@ -0,0 +1 @@
+yeah!
View
12 lib/cloudkit/templates/gen/views/layout.erb
@@ -0,0 +1,12 @@
+<!doctype html>
+<html lang="en">
+<head>
+ <title><%= @app_name.capitalize %></title>
+ <link type="text/css" href="/css/main.css" rel="stylesheet"/>
+ <script type="text/javascript" src="/js/jquery-1.4.2.js"></script>
+</head>
+<body>
+<%%= yield %>
+</body>
+</html>
+<script type="text/javascript" src="/js/app.js"></script>
Please sign in to comment.
Something went wrong with that request. Please try again.