forked from weatheredwatcher/Sow
/
sing
executable file
·171 lines (144 loc) · 5.28 KB
/
sing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env ruby
# 1.9 adds realpath to resolve symlinks; 1.8 doesn't
# have this method, so we add it so we get resolved symlinks
# and compatibility
unless File.respond_to? :realpath
class File #:nodoc:
def self.realpath path
return realpath(File.readlink(path)) if symlink?(path)
path
end
end
end
$: << File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../lib')
require 'rubygems'
require 'gli'
require 'sing_version'
require 'fileutils'
require 'net/http'
include GLI
program_desc 'Sing will create the basic directory structure and base files for a Sinatra application.'
version Sing::VERSION
# desc 'display the version'
# switch [:v,:version]
# desc 'runs with output (default)'
# switch [:V,:verbose]
# desc 'runs with minimal output'
# switch [:q,:quiet]
# desc 'the project name'
# default_value 'my_app'
# arg_name 'project_name'
# flag [:p,:project]
desc 'Creates new project'
arg_name 'project_name'
command :new do |c|
# c.desc 'Describe a switch to new'
# c.switch :s
# c.desc 'Describe a flag to new'
# c.default_value 'default'
# c.flag :f
c.action do |global_options,options,args|
# Define the directory structure that you want
dir_structure = ['public/stylesheets','public/javascripts','public/images', 'views']
javascript_dir = 'public/javascripts'
css_dir = 'public/stylesheets'
# This makes sure that a project name is present
if args.length == 0
puts "Please enter the name of your project"
exit 1
end
# Sets the project name and creates the main directory
project = args[0]
FileUtils.mkdir_p project
puts "created - /#{project}"
# Move into the main directory
FileUtils.cd project
# Creates app.rb, config.ru, gemfile, README.md
File.open("./app.rb", 'w') do |file|
file.puts "require 'rubygems'\nrequire 'sinatra'\nrequire 'haml'\n\nget '/' do\n\t@title = 'Hello World'\n\thaml :index\nend"
end
puts "\ncreated - app.rb"
File.open("./config.ru", 'w') do |file|
file.puts "require './app'\nrun Sinatra::Application"
end
puts "created - config.ru"
File.open("./Gemfile", 'w') do |file|
file.puts "source 'http://rubygems.org'\n\ngem 'sinatra'\ngem 'haml'\ngem 'activesupport'"
end
puts "created - Gemfile"
File.open("./README.md", 'w') do |file|
file.puts "This is your README. Use markdown to create it."
end
puts "created - README.md"
puts "\n"
# Creates the directory structure
dir_structure.each do |dir|
FileUtils.mkdir_p dir
puts "created - /#{dir}"
end
puts "\n"
#creates layout.haml, and index.haml (includes jquery, and bootstrap css, responsive css, and all plugins)
File.open("./views/layout.haml", 'w') do |file|
file.puts "!!!"
file.puts "%html"
file.puts " %head"
file.puts " %title= @title"
file.puts ""
file.puts " %meta{:charset => 'utf-8'}"
file.puts ""
file.puts " %script{:src => 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'}"
file.puts " %script{:src => 'http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.0.2/bootstrap.min.js'}"
file.puts " %script{:src => 'javascripts/application.js'}"
file.puts ""
file.puts " %link{:type => 'text/css', :rel => 'stylesheet', :media => 'screen', :href => 'http://twitter.github.com/bootstrap/assets/css/bootstrap.css'}"
file.puts " %link{:type => 'text/css', :rel => 'stylesheet', :media => 'screen', :href => 'http://twitter.github.com/bootstrap/assets/css/bootstrap-responsive.css'}"
file.puts " %link{:type => 'text/css', :rel => 'stylesheet', :media => 'screen', :href => 'stylesheets/application.css'}"
file.puts " %body{:style => 'padding-top: 60px'}"
file.puts ""
file.puts " = yield"
file.puts ""
file.puts " .container"
file.puts " %hr"
end
puts "created - layout.haml"
File.open("./views/index.haml", 'w') do |file|
file.puts ".navbar.navbar-fixed-top"
file.puts " .navbar-inner"
file.puts " .container"
file.puts " %a.brand{:href => '/'}"
file.puts " Hello World!"
file.puts ".container"
file.puts " %h2.center Hello World"
end
puts "created - index.haml"
# Create the application.css file
File.open("./public/stylesheets/application.css", 'w') do |file|
file.puts ".center{\n\ttext-align: center;\n}\n\n.right{\n\tfloat: right;\n}\n\n.left{\n\tfloat: left;\n}\n\n.clear{\n\tclear: both;\n}"
end
puts "created - application.css"
# Creates application.js file
File.open("./public/javascripts/application.js", 'w') do |file|
file.puts "// Application javascript goes here"
end
puts "created - application.js"
end
end
pre do |global,command,options,args|
# Pre logic here
# Return true to proceed; false to abort and not call the
# chosen command
# Use skips_pre before a command to skip this block
# on that command only
true
end
post do |global,command,options,args|
# Post logic here
# Use skips_post before a command to skip this
# block on that command only
end
on_error do |exception|
# Error logic here
# return false to skip default error handling
true
end
exit GLI.run(ARGV)