Class: Judojs::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/judojs/project.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = 'JudoApplication', project_dir = '/') ⇒ Project

Returns a new instance of Project.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/judojs/project.rb', line 23

def initialize(name = 'JudoApplication', project_dir = '/')
   name.gsub!(/\s|\-|\./)
   proj_dir = project_dir  || '/'
   proj_dir += '/' unless proj_dir.match(/\/$/)
   proj_dir = '/' << proj_dir unless proj_dir.match(/^\//)
   
   @color_start = "\e[32m"
   @color_end = "\e[0m"
   
   @app_filename = name.downcase
   @project_path = "#{Judojs.root_directory}#{proj_dir}"
   @config = Judojs::Configuration.new @project_path, name
   @manifest = Array['application',
                     'elements',
                     'lib',
                     'models',
                     'modules',
                     'plugins',
                     'tests']
end

Instance Attribute Details

#app_filenameObject

Returns the value of attribute app_filename.



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

def app_filename
  @app_filename
end

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#manifestObject (readonly)

Returns the value of attribute manifest.



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

def manifest
  @manifest
end

#project_pathObject

Returns the value of attribute project_path.



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

def project_path
  @project_path
end

Class Method Details

.init_with_config(project_path) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/judojs/project.rb', line 4

def self.init_with_config(project_path)
  config = Judojs::Configuration.new project_path
  config.read
  
  project = Project.new
  project.config = config
  project.project_path = config.project_path
  project.app_filename = config.name.downcase
  project
end

Instance Method Details

#compile_modulesObject



122
123
124
125
126
127
# File 'lib/judojs/project.rb', line 122

def compile_modules
  @modules.each do |module_file|
    module_filename = Judojs::Helpers.create_module_filename module_file
    create_module_file module_file, module_filename
  end
end

#compress_applicationObject



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/judojs/project.rb', line 188

def compress_application
  application = @project_path + 'application'
  modules = Dir.entries(application)
  modules.reject! { |file| file =~ /^\./ }

  modules.each do |module_file|
    full_path = application + "/#{module_file}"
    uncompressed = File.open(full_path, "r").readlines.join('')
    File.open(full_path, "w+") do |module_file|
      module_file << JSMin.minify(uncompressed)
    end
  end
end

#createObject



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

def create
  puts "#{@color_start}>>>#{@color_end} Creating the #{@config.name} project in #{@project_path}" 
  create_project_structure
  @config.create
  create_judo_lib_file
  create_utility_lib_file
  create_judo_application_file
  import_javascripts
end

#create_judo_application_fileObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/judojs/project.rb', line 100

def create_judo_application_file      
  filename = "#{@project_path}application/#{@app_filename}.js"
  
  #puts File.exists?("#{@project_path}application/#{@app_filename}.js") ? "application/#{@app_filename}.js updated" : "application/#{@app_filename}.js created"
  File.open(filename, "w+") do |file|
    file << "//-- Judojs #{Time.now.to_s}  --//\n"
    file << File.open("#{@project_path}lib/judo.js", 'r').readlines.join('')
    file << "\nvar #{@config.name} = new JudoApplication();"
  end
end

#create_judo_lib_fileObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/judojs/project.rb', line 72

def create_judo_lib_file
  judo_lib_secretary = Sprockets::Secretary.new(
    :root         => "#{Judojs.base_directory}",
    :asset_root   => "#{@config.asset_root}",
    :load_path    => ["repository"],
    :source_files => ["repository/judojs/core/judo.js"]
  )

  judo_lib = judo_lib_secretary.concatenation
  judo_lib.save_to "#{@project_path}lib/judo.js"

  puts "lib/judo.js created"
end

#create_module_file(module_file, module_name) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/judojs/project.rb', line 129

def create_module_file(module_file, module_name)
    begin
      module_src = "#{@project_path}modules/#{module_file}"

      judo_lib_secretary = Sprockets::Secretary.new(
        :root         => "#{Judojs.base_directory}",
        :asset_root   => "#{@config.asset_root}",
        :load_path    => ["repository"],
        :source_files => ["#{module_src}"]
      )

      module_file = judo_lib_secretary.concatenation
      message = File.exists?("#{@project_path}application/#{module_name}.js") ? "application/#{module_name}.js updated" : "application/#{module_name}.js created"
      module_file.save_to "#{@project_path}application/#{module_name}.js"
      judo_lib_secretary.install_assets

      #puts message
    rescue Exception => error
      @errors = true
      puts "Sprockets error: #{error.message}"
    end
    
end

#create_project_structureObject



63
64
65
66
67
68
69
70
# File 'lib/judojs/project.rb', line 63

def create_project_structure
  Dir.mkdir "#{@project_path}" unless File.exists? "#{@project_path}"
  
  @manifest.each do |folder|
    puts "#{folder}/ created" unless File.exists? "#{@project_path}#{folder}"
    Dir.mkdir "#{@project_path}#{folder}" unless File.exists? "#{@project_path}#{folder}"
  end
end

#create_utility_lib_fileObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/judojs/project.rb', line 86

def create_utility_lib_file
  utility_lib_secretary = Sprockets::Secretary.new(
    :root         => "#{Judojs.base_directory}",
    :asset_root   => "#{@config.asset_root}",
    :load_path    => ["repository"],
    :source_files => ["repository/judojs/utilities/all.js"]
  )
  
  utility_lib = utility_lib_secretary.concatenation
  utility_lib.save_to "#{@project_path}lib/utilities.js"
  
  puts "lib/utilities.js created"
end

#get_modulesObject



117
118
119
120
# File 'lib/judojs/project.rb', line 117

def get_modules      
  entries = Dir.entries "#{@project_path}modules"
  @modules = entries.reject { |file| file.match(/^\./) }
end

#import_javascriptsObject



111
112
113
114
115
# File 'lib/judojs/project.rb', line 111

def import_javascripts
  File.copy "#{Judojs.base_directory}/repository/judojs/tests/index.html", "#{@project_path}tests"
  File.copy "#{Judojs.base_directory}/repository/judojs/tests/judojs.test.js", "#{@project_path}tests"
  File.copy "#{Judojs.base_directory}/repository/judojs/tests/judojs.utilities.test.js", "#{@project_path}tests"
end

#updateObject



54
55
56
57
58
59
60
61
# File 'lib/judojs/project.rb', line 54

def update
  get_modules
  compile_modules
  update_application_file
  compress_application if @config.output == 'compressed'
  puts "#{@color_start}>>>#{@color_end} application updated" unless @errors
  @errors = false
end

#update_application_fileObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/judojs/project.rb', line 153

def update_application_file
  message = File.exists?("#{@project_path}application/#{@app_filename}.js") ? "application/#{@app_filename}.js updated" : "application/#{@app_filename}.js created"      
  
  content = String.new
  content << "/* Judojs #{Time.now.to_s} */\n"
  content << "//= require \"../lib/judo.js\"\n\n"
  content << "\nvar #{@config.name} = new JudoApplication();"
  
  filename = "#{@project_path}application/#{@app_filename}.js"
  File.open(filename, "w+") do |file|
    file << content
    @config.autoload.each do |auto_file|
      file << "\n\n/*---------- Judojs autoload #{auto_file} ----------*/"
      file << "\n//= require #{auto_file}\n" if auto_file.match(/^\<.+\>$/)
      file << "\n//= require \"#{auto_file}\"\n" if auto_file.match(/^[^\<].+|[^\>]$/)
    end
  end
  
  begin
    judo_lib_secretary = Sprockets::Secretary.new(
      :root         => "#{Judojs.base_directory}",
      :asset_root   => "#{@config.asset_root}",
      :load_path    => ["repository"],
      :source_files => ["#{filename}"]
    )

    application_file = judo_lib_secretary.concatenation
    judo_lib_secretary.install_assets
    application_file.save_to "#{filename}"
  rescue Exception => error
    @errors = true
    puts "\e[0;31m!!!\e[0m Sprockets error: #{error.message}"
  end
end