Class: QEDProject::Project

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/qedproject/project.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#cp, #cp_r, #create_file, #mkdir_p, #render_template_to_file

Constructor Details

#initialize(project_path, options = {}) ⇒ Project

Creates a new Project instance. Options:

:libs : A list of js libraries to include, Can be :backbone, :jquery, :knockout
:jammit: boolean, should Jammit be included?
:coffeescript : Should CoffeeScript support be included?
:sass           Should sass support be included?


38
39
40
41
42
# File 'lib/qedproject/project.rb', line 38

def initialize(project_path, options = {})
  self.path = project_path
  self.process_options(options)
  self.set_paths
end

Instance Attribute Details

#coffeescriptObject

Returns the value of attribute coffeescript.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def coffeescript
  @coffeescript
end

#css_assetsObject

Returns the value of attribute css_assets.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def css_assets
  @css_assets
end

#css_pathObject

Returns the value of attribute css_path.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def css_path
  @css_path
end

#images_pathObject

Returns the value of attribute images_path.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def images_path
  @images_path
end

#jammitObject

Returns the value of attribute jammit.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def jammit
  @jammit
end

#js_assetsObject

Returns the value of attribute js_assets.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def js_assets
  @js_assets
end

#js_pathObject

Returns the value of attribute js_path.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def js_path
  @js_path
end

#libsObject

Returns the value of attribute libs.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def libs
  @libs
end

#livereloadObject

Returns the value of attribute livereload.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def livereload
  @livereload
end

#no_overwriteObject

Returns the value of attribute no_overwrite.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def no_overwrite
  @no_overwrite
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def path
  @path
end

#public_dirObject

Returns the value of attribute public_dir.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def public_dir
  @public_dir
end

#run_bundleObject

Returns the value of attribute run_bundle.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def run_bundle
  @run_bundle
end

#sassObject

Returns the value of attribute sass.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def sass
  @sass
end

#skip_indexObject

Returns the value of attribute skip_index.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def skip_index
  @skip_index
end

#sorted_libsObject

Returns the value of attribute sorted_libs.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def sorted_libs
  @sorted_libs
end

#testingObject

Returns the value of attribute testing.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def testing
  @testing
end

#verboseObject

Returns the value of attribute verbose.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def verbose
  @verbose
end

Class Method Details

.create(project_path, options = {}) ⇒ Object

convenience method to create a new project. Simply call create with the project path and the options.



16
17
18
# File 'lib/qedproject/project.rb', line 16

def self.create(project_path, options= {})
   self.new(project_path, options).generate
end

Instance Method Details

#add_testingObject

includes the Jasmine BDD framework for javascript testing



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/qedproject/project.rb', line 140

def add_testing
  mkdir_p File.join(self.path, "spec"), :verbose => self.verbose
  cp_r File.join(self.vendor_root, "jasmine", "lib"), 
                 File.join(self.path, "spec", "lib"), :verbose => self.verbose
  if self.uses_jquery?  
    cp_r File.join(self.vendor_root, "jasmine-jquery", "jasmine-jquery.js"), File.join(self.path, "spec", "lib"), :verbose => self.verbose
  end

  render_template_to_file "suite.html", File.join(self.path, "spec", "SpecRunner.html"), binding
  if self.coffeescript
    render_template_to_file "sampleSpec.coffee", File.join(self.path, "spec", "sampleSpec.coffee"), binding
  else
    render_template_to_file "sampleSpec.js", File.join(self.path, "spec", "sampleSpec.js"), binding
  end

end

#bundleObject



44
45
46
47
48
49
50
# File 'lib/qedproject/project.rb', line 44

def bundle
  Dir.chdir(self.path) do
    puts "Bundling assets"
    `bundle install`
  end
    
end

#collect_librariesObject



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
# File 'lib/qedproject/project.rb', line 52

def collect_libraries
  
  self.css_assets = []
  self.js_assets = []
  self.sorted_libs = []
  
  self.libs.each do |requested_library|
    raise QEDProject::BadLibraryError, "#{requested_library} is not a valid library" unless QEDProject::Libraries::Base.libs.include? requested_library
    library = QEDProject::Libraries::Base.libs[requested_library]   
    
    if library.respond_to? :dependencies
      dependencies = library.dependencies
      dependencies.each do |d|
        unless self.sorted_libs.include?(d)
          self.sorted_libs << d
        end
      end
    end
    
    unless self.sorted_libs.include?(requested_library)
      self.sorted_libs << requested_library
    end
  end
  
  self.sorted_libs.each do |lib|
    library = QEDProject::Libraries::Base.libs[lib]   
    self.js_assets += library.js_files if library.respond_to?(:js_files)
    self.css_assets += library.css_files if library.respond_to?(:css_files)
  end
  
end

#create_asset_fileObject



203
204
205
# File 'lib/qedproject/project.rb', line 203

def create_asset_file
  render_template("assets.yml", File.join(self.path, "config", "assets.yml"))
end

#create_gemfileObject



220
221
222
# File 'lib/qedproject/project.rb', line 220

def create_gemfile
  render_template "Gemfile", File.join(self.path, "Gemfile")
end

#create_guardfileObject



207
208
209
# File 'lib/qedproject/project.rb', line 207

def create_guardfile
  render_template "Guardfile", File.join(self.path, "Guardfile")
end

#create_indexObject



211
212
213
214
# File 'lib/qedproject/project.rb', line 211

def create_index
  @project = self
  render_template "index.html", File.join(self.path, self.public_dir, "index.html") unless self.skip_index
end

#create_project_skeletonObject

Set up the main project skeleton project

public/
  assets/           * only with assets
  images/
  javascripts/      * only without assets
  stylesheets/      * only without assets
config/
  assets.yml        * optional
coffeescripts/      * optional
sass/               * optional
spec/               * optional
Guardfile           * optional


170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/qedproject/project.rb', line 170

def create_project_skeleton
  mkdir_p( self.path, :verbose => self.verbose)
  mkdir_p( File.join(self.path, self.public_dir), :verbose => self.verbose)
  mkdir_p( File.join(self.path, self.images_path), :verbose => self.verbose)
  mkdir_p( File.join(self.path, "_qedtmp"), :verbose => self.verbose)
  mkdir_p( File.join(self.path, self.js_path), :verbose => self.verbose)
  mkdir_p( File.join(self.path, self.css_path), :verbose => self.verbose)
  mkdir_p( File.join(self.path, "config"), :verbose => self.verbose) if self.needs_config_folder?
  if self.coffeescript
    mkdir_p( File.join(self.path, "coffeescripts"), :verbose => self.verbose) 
    create_file(File.join(self.path, "coffeescripts", "app.coffee"), :verbose => self.verbose, :no_overwrite => self.no_overwrite) 
  else
    create_file(File.join(self.path, self.js_path, "app.js" ), :verbose => self.verbose, :no_overwrite => self.no_overwrite)
  end
  if self.sass
    mkdir_p( File.join(self.path, "sass"), :verbose => self.verbose) 
    create_file( File.join(self.path, "sass", "app.sass"), :verbose => self.verbose, :no_overwrite => self.no_overwrite)
  else
    create_file(File.join(self.path, self.css_path, "app.css" ), :verbose => self.verbose, :no_overwrite => self.no_overwrite)
  end
end

#create_rakefileObject



216
217
218
# File 'lib/qedproject/project.rb', line 216

def create_rakefile
    render_template "Rakefile", File.join(self.path, "Rakefile")
end

#generateObject

Start the project generation. Create the folder Get all of the libraries create an assets file if needed add the testing folder if needed create a guardfile if needed



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/qedproject/project.rb', line 126

def generate
  self.create_project_skeleton
  self.create_index unless self.skip_index                         
  self.get_libraries if self.libs
  self.create_asset_file if self.jammit
  self.add_testing if self.testing
  self.create_guardfile if self.needs_guardfile?
  self.create_rakefile
  self.create_gemfile
  self.bundle if self.run_bundle
  
end

#get_librariesObject

Loop through the libraries the user added and run their generate methods which pulls in the additional optional files.



195
196
197
198
199
200
201
# File 'lib/qedproject/project.rb', line 195

def get_libraries
  self.sorted_libs.each do |lib|
    library = QEDProject::Libraries::Base.libs[lib]
    l = library.new(self)
    l.generate!
  end
end

#needs_config_folder?Boolean

Only jammit needs a config folder for now.

Returns:

  • (Boolean)


116
117
118
# File 'lib/qedproject/project.rb', line 116

def needs_config_folder?
  self.jammit
end

#needs_guardfile?Boolean

We need a guardfile if the user is using jammit, sass, or coffeescript.

Returns:

  • (Boolean)


111
112
113
# File 'lib/qedproject/project.rb', line 111

def needs_guardfile?
  self.jammit || self.sass || self.coffeescript || self.livereload
end

#process_options(options) ⇒ Object

Sets instance methods with values from the options hash.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/qedproject/project.rb', line 85

def process_options(options)

  self.libs = options[:libs] || []
  
  collect_libraries
  self.public_dir = options[:public_dir] || "public"
  self.jammit = options[:jammit]
  self.sass = options[:sass]
  self.coffeescript = options[:coffeescript]
  self.verbose = options[:verbose]
  self.testing = options[:testing]
  self.skip_index = options[:skip_index]
  self.livereload = options[:livereload]
  self.no_overwrite = options[:no_overwrite] ? true : false
  self.run_bundle = options[:bundle]
  
end

#render_template(source, dest) ⇒ Object



224
225
226
227
228
229
230
231
# File 'lib/qedproject/project.rb', line 224

def render_template(source, dest)
  if self.no_overwrite && File.exist?(dest)
    puts "Skipping #{dest}" if self.verbose
  else
    render_template_to_file source, dest, binding
    puts "Created #{dest}" if self.verbose
  end
end

#set_pathsObject

Set up the basic paths we’ll use throughout



104
105
106
107
108
# File 'lib/qedproject/project.rb', line 104

def set_paths
   self.images_path = File.join(self.public_dir, "images")
   self.js_path = self.jammit ? "javascripts" : File.join(self.public_dir, "javascripts")
   self.css_path = self.jammit ? "stylesheets" : File.join(self.public_dir, "stylesheets")
end

#template_rootObject



20
21
22
# File 'lib/qedproject/project.rb', line 20

def template_root
  File.expand_path("../../../templates", __FILE__)
end

#uses_jquery?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/qedproject/project.rb', line 28

def uses_jquery?
  self.libs.include?(:jquery)
end

#vendor_rootObject



24
25
26
# File 'lib/qedproject/project.rb', line 24

def vendor_root
  File.expand_path("../../../vendor", __FILE__)
end