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?


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

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

#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.



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

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/qedproject/project.rb', line 127

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

#collect_librariesObject



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

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



190
191
192
# File 'lib/qedproject/project.rb', line 190

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

#create_gemfileObject



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

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

#create_guardfileObject



194
195
196
# File 'lib/qedproject/project.rb', line 194

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

#create_indexObject



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

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


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/qedproject/project.rb', line 157

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



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

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



115
116
117
118
119
120
121
122
123
124
# File 'lib/qedproject/project.rb', line 115

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
end

#get_librariesObject

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



182
183
184
185
186
187
188
# File 'lib/qedproject/project.rb', line 182

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)


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

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)


100
101
102
# File 'lib/qedproject/project.rb', line 100

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.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/qedproject/project.rb', line 76

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
end

#render_template(source, dest) ⇒ Object



211
212
213
214
215
216
217
218
# File 'lib/qedproject/project.rb', line 211

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



93
94
95
96
97
# File 'lib/qedproject/project.rb', line 93

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



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

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

#uses_jquery?Boolean

Returns:

  • (Boolean)


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

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

#vendor_rootObject



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

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