Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 220 lines (196 sloc) 6.929 kB
e1e4fa5 @jlong Added an export command which allows you to convert a Serve site to s…
authored
1 require 'fileutils'
e4ed21c @jlong more verbose output for create and convert commands
authored
2 require 'serve/out'
e1e4fa5 @jlong Added an export command which allows you to convert a Serve site to s…
authored
3 require 'serve/path'
918834e @jlong tweaked the way javascripts were downloaded
authored
4 require 'serve/javascripts'
e4ed21c @jlong more verbose output for create and convert commands
authored
5
9a8fd04 @jlong don't doc Project class
authored
6 module Serve #:nodoc:
7 #
8 # Serve::Project.new(options).create
9 # Serve::Project.new(options).convert
10 #
11 class Project #:nodoc:
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
12 attr_reader :location, :framework, :template
cfb4b34 @revans Stubbed out the new project folder for creating new server projects a…
revans authored
13
ab34580 @revans Create and Covert options implemented.
revans authored
14 def initialize(options)
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
15 @location = normalize_path(options[:directory])
ab34580 @revans Create and Covert options implemented.
revans authored
16 @framework = options[:framework]
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
17 @template = options[:template] || 'default'
cfb4b34 @revans Stubbed out the new project folder for creating new server projects a…
revans authored
18 end
19
ad05ac8 @jlong removed verbose comments
authored
20 # Create a new Serve project
cfb4b34 @revans Stubbed out the new project folder for creating new server projects a…
revans authored
21 def create
22 setup_base
e4ed21c @jlong more verbose output for create and convert commands
authored
23 %w(
24 public/images
25 public/javascripts
26 public/stylesheets
e0963aa @jlong * Updated project README and other files to be more friendly to new u…
authored
27 stylesheets
e4ed21c @jlong more verbose output for create and convert commands
authored
28 ).each { |path| make_path path }
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
29 copy_project_template @template
918834e @jlong tweaked the way javascripts were downloaded
authored
30 install_javascript_framework @framework
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
31 copy_readme
aa60e65 @jlong make the serve convert command a bit smarter about directory names; p…
authored
32 post_create_message
cfb4b34 @revans Stubbed out the new project folder for creating new server projects a…
revans authored
33 end
34
e1e4fa5 @jlong Added an export command which allows you to convert a Serve site to s…
authored
35 def self.create(options={})
36 new(options).create
37 end
38
ad05ac8 @jlong removed verbose comments
authored
39 # Convert an existing Compass project to a Serve project
cfb4b34 @revans Stubbed out the new project folder for creating new server projects a…
revans authored
40 def convert
41 setup_base
e4ed21c @jlong more verbose output for create and convert commands
authored
42 move_file 'images', 'public/'
43 move_file 'stylesheets', 'public/'
aa60e65 @jlong make the serve convert command a bit smarter about directory names; p…
authored
44 if File.directory? "#{@location}/javascripts"
918834e @jlong tweaked the way javascripts were downloaded
authored
45 move_file 'javascripts', 'public/'
46 else
47 make_path 'public/javascripts'
48 end
aa60e65 @jlong make the serve convert command a bit smarter about directory names; p…
authored
49 if File.directory? "#{@location}/src"
50 move_file 'src', 'stylesheets'
51 elsif File.directory? "#{@location}/sass"
52 move_file 'sass', 'styleheets'
53 end
918834e @jlong tweaked the way javascripts were downloaded
authored
54 install_javascript_framework @framework
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
55 copy_readme
aa60e65 @jlong make the serve convert command a bit smarter about directory names; p…
authored
56 post_convert_message
cfb4b34 @revans Stubbed out the new project folder for creating new server projects a…
revans authored
57 end
58
e1e4fa5 @jlong Added an export command which allows you to convert a Serve site to s…
authored
59 def self.convert(options={})
60 new(options).convert
61 end
62
cfb4b34 @revans Stubbed out the new project folder for creating new server projects a…
revans authored
63 private
64
e4ed21c @jlong more verbose output for create and convert commands
authored
65 include Serve::Out
e1e4fa5 @jlong Added an export command which allows you to convert a Serve site to s…
authored
66 include Serve::Path
918834e @jlong tweaked the way javascripts were downloaded
authored
67 include Serve::JavaScripts
68
ad05ac8 @jlong removed verbose comments
authored
69 # Files required for both a new server project and for an existing compass project.
cfb4b34 @revans Stubbed out the new project folder for creating new server projects a…
revans authored
70 def setup_base
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
71 make_path
e4ed21c @jlong more verbose output for create and convert commands
authored
72 %w(
73 public
74 tmp
75 views
76 ).each { |path| make_path path }
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
77 create_file 'Gemfile', read_bootstrap_file('Gemfile', true)
78 create_file 'config.ru', read_bootstrap_file('config.ru')
79 create_file '.gitignore', read_bootstrap_file('gitignore')
80 create_file 'compass.config', read_bootstrap_file('compass.config')
e4ed21c @jlong more verbose output for create and convert commands
authored
81 create_empty_file 'tmp/restart.txt'
cfb4b34 @revans Stubbed out the new project folder for creating new server projects a…
revans authored
82 end
83
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
84 # Copy files from project template
85 def copy_project_template(name)
86 source = lookup_template_directory(name)
87 raise 'invalid template' unless source
88
17f3e31 @jlong fix failing tests
authored
89 files = glob_path(source, true)
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
90
91 files.each do |filename|
92 from_path = "#{source}/#{filename}"
93 to_path = "#{@location}/#{filename}"
94 if File.directory? from_path
95 make_path filename
96 else
97 if File.file? to_path
56276d8 @jlong fix problem with output of create command
authored
98 log_action "exists", to_path
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
99 else
56276d8 @jlong fix problem with output of create command
authored
100 log_action "create", to_path
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
101 FileUtils.cp from_path, to_path
102 end
103 end
104 end
105 end
106
ad05ac8 @jlong removed verbose comments
authored
107 # Install a JavaScript framework if one was specified
918834e @jlong tweaked the way javascripts were downloaded
authored
108 def install_javascript_framework(framework)
109 if framework
110 if valid_javascript_framework?(framework)
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
111 path = "#{@location}/public/javascripts"
918834e @jlong tweaked the way javascripts were downloaded
authored
112 filename = javascript_filename(framework, path)
113 if File.exists? filename
114 log_action 'exists', filename
115 else
116 log_action 'install', filename
117 fetch_javascript framework, path
118 end
119 else
120 puts "*** #{framework} javascript framework not supported. ***"
121 puts "Supported frameworks: #{ supported_javascript_frameworks.join(', ') }"
122 end
e4ed21c @jlong more verbose output for create and convert commands
authored
123 end
a4bf091 @revans Added additional specs for the new create
revans authored
124 end
125
aa60e65 @jlong make the serve convert command a bit smarter about directory names; p…
authored
126 # Copy readme file if not included in template
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
127 def copy_readme
128 create_file 'README.md', read_bootstrap_file('README.md'), :silent
129 end
130
aa60e65 @jlong make the serve convert command a bit smarter about directory names; p…
authored
131 # Display post create message
132 def post_create_message(action_message = "You created a new Serve project.")
133 puts ""
134 puts "Woohoo! #{action_message}"
135 puts ""
136 puts "A couple of basic files are in place ready for you to edit."
137 puts "Remember to edit the project Gemfile and run:"
138 puts ""
139 puts " bundle install"
140 puts ""
141 puts "To start serving your project, run:"
142 puts ""
143 puts " cd \"#{@location}\""
144 puts " serve"
145 puts ""
146 puts "Then go to http://localhost:4000 in your web browser."
147 puts ""
148 puts "Have fun!"
149 puts ""
150 end
151
152 # Display post convert message
153 def post_convert_message
154 post_create_message "You converted your Compass project to a Serve project."
155 if File.exists? "#{@location}/config.rb"
1aeaa6d @jlong notate old compass config in convert command
authored
156 puts "============================================================================"
aa60e65 @jlong make the serve convert command a bit smarter about directory names; p…
authored
157 puts "Please Note: You still need to copy your unique settings from config.rb to "
158 puts "compass.config. Remove config.rb when you are finished."
1aeaa6d @jlong notate old compass config in convert command
authored
159 puts "============================================================================"
160 puts ""
161 end
162 end
163
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
164 # Read and optionally eval a bootstrap template by name
165 def read_bootstrap_file(name, eval = false)
166 contents = IO.read(normalize_path(File.dirname(__FILE__), "bootstrap", name))
167 eval ? instance_eval("%{#{contents}}") : contents
e1e4fa5 @jlong Added an export command which allows you to convert a Serve site to s…
authored
168 end
169
ad05ac8 @jlong removed verbose comments
authored
170 # Create a file with contents
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
171 def create_file(file, contents, exists=:noisy)
172 path = "#{@location}/#{file}"
e4ed21c @jlong more verbose output for create and convert commands
authored
173 unless File.exists? path
d75ba5f @jlong renamed Out#action to Out#log_action
authored
174 log_action "create", path
e4ed21c @jlong more verbose output for create and convert commands
authored
175 File.open(path, 'w+') { |f| f.puts contents }
176 else
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
177 log_action "exists", path if exists == :noisy
e4ed21c @jlong more verbose output for create and convert commands
authored
178 end
cfb4b34 @revans Stubbed out the new project folder for creating new server projects a…
revans authored
179 end
180
ad05ac8 @jlong removed verbose comments
authored
181 # Create an empty file
e4ed21c @jlong more verbose output for create and convert commands
authored
182 def create_empty_file(file)
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
183 path = "#{@location}/#{file}"
e4ed21c @jlong more verbose output for create and convert commands
authored
184 FileUtils.touch(path)
cfb4b34 @revans Stubbed out the new project folder for creating new server projects a…
revans authored
185 end
186
ad05ac8 @jlong removed verbose comments
authored
187 # Make every directory in a given path
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
188 def make_path(path=nil)
189 path = File.join(*[@location, path].compact)
e4ed21c @jlong more verbose output for create and convert commands
authored
190 unless File.exists? path
d75ba5f @jlong renamed Out#action to Out#log_action
authored
191 log_action "create", path
e4ed21c @jlong more verbose output for create and convert commands
authored
192 FileUtils.mkdir_p(path)
193 else
d75ba5f @jlong renamed Out#action to Out#log_action
authored
194 log_action "exists", path
e4ed21c @jlong more verbose output for create and convert commands
authored
195 end
cfb4b34 @revans Stubbed out the new project folder for creating new server projects a…
revans authored
196 end
197
ad05ac8 @jlong removed verbose comments
authored
198 # Move a file from => to (relative to the project location)
e4ed21c @jlong more verbose output for create and convert commands
authored
199 def move_file(from, to)
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
200 from_path = "#{@location}/#{from}"
201 to_path = "#{@location}/#{to}"
e4ed21c @jlong more verbose output for create and convert commands
authored
202 if File.exists? from_path
1aeaa6d @jlong notate old compass config in convert command
authored
203 to = to + from if to[-1..-1] == "/"
d75ba5f @jlong renamed Out#action to Out#log_action
authored
204 log_action "move", "#{@location}/{#{from} => #{to}}"
e4ed21c @jlong more verbose output for create and convert commands
authored
205 FileUtils.mv from_path, to_path
206 end
cfb4b34 @revans Stubbed out the new project folder for creating new server projects a…
revans authored
207 end
208
3be4cf7 @jlong Added new --template option which allows you to specify a project tem…
authored
209 def default_templates_directory
210 "#{File.dirname(__FILE__)}/templates"
211 end
212
213 def lookup_template_directory(name)
214 path = "#{default_templates_directory}/#{name}"
215 path = normalize_path(name) unless File.directory?(path)
216 path if File.directory?(path)
217 end
cfb4b34 @revans Stubbed out the new project folder for creating new server projects a…
revans authored
218 end
b568440 @eval bugfix: create can't find file LICENSE
eval authored
219 end
Something went wrong with that request. Please try again.