lib/ninjs/project.rb in ninjs-0.9.2 vs lib/ninjs/project.rb in ninjs-0.10.0

- old
+ new

@@ -1,37 +1,37 @@ module Ninjs class Project + attr_reader :app_filename, + :project_path, + :config + attr_writer :config, + :project_path, + :app_filename + def self.init_with_config(project_path) config = Ninjs::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 - attr_reader :app_filename, - :project_path, - :config - attr_writer :config, - :project_path, - :app_filename - def initialize(name = 'NinjsApplication', project_dir = '/') - name.gsub!(/\s|\-|\./) - proj_dir = clean_project_path project_dir - @modules = Array.new - - @color_start = "\e[32m" - @color_end = "\e[0m" - - @app_filename = name.downcase - @project_path = "#{Ninjs.root_directory}#{proj_dir}" - @config = Ninjs::Configuration.new @project_path, name + name.gsub!(/\s|\-|\./) + proj_dir = clean_project_path project_dir + @modules = Array.new + + @color_start = "\e[32m" + @color_end = "\e[0m" + + @app_filename = name.downcase + @project_path = "#{Ninjs.root_directory}#{proj_dir}" + @config = Ninjs::Configuration.new @project_path, name end def clean_project_path(dir) dir += '/' unless dir.match(/\/$/) dir = '/' << dir unless dir.match(/^\//) @@ -95,54 +95,23 @@ def import_test_files File.copy "#{Ninjs.base_directory}/repository/ninjs/tests/index.html", "#{@project_path}tests" File.copy "#{Ninjs.base_directory}/repository/ninjs/tests/ninjs.test.js", "#{@project_path}tests" File.copy "#{Ninjs.base_directory}/repository/ninjs/tests/ninjs.utilities.test.js", "#{@project_path}tests" + File.copy "#{Ninjs.base_directory}/repository/ninjs/tests/qunit/qunit.js", "#{@project_path}tests/qunit" + File.copy "#{Ninjs.base_directory}/repository/ninjs/tests/qunit/qunit.css", "#{@project_path}tests/qunit" end def update - #read_cache get_updated_modules compile_modules update_application_file compress_application if @config.output == 'compressed' puts "#{@color_start}>>>#{@color_end} application updated" unless @errors @errors = false - #write_cache end - def read_cache - if File.exists? "#{@project_path}.cache" - parse_cache File.open("#{@project_path}.cache").readlines - else - create_cache - read_cache - end - end - - def parse_cache(cache_lines) - @cache_files = Hash.new - cache_lines.each do |line| - cache_file = line.split('|') - @cache_files[cache_file.first] = cache_file.last - end - end - - def create_cache - File.open("#{@project_path}.cache", 'w+') - end - - def write_cache - directory_files = get_directory_script_files - timestamped_files = get_files_timestamps directory_files - File.open("#{@project_path}.cache", 'w+') do |cache_file| - timestamped_files.each do |file, mtime| - cache_file << file + '|' + mtime + "\n" - end - end - end - def get_directory_script_files script_files = Array.new Dir["#{@project_path}**/*.js"].each do |file| script_files << file unless file.match(/application\/|tests\//) end @@ -171,66 +140,78 @@ create_module_file module_file, module_filename end end def create_module_file(module_file, module_name) - begin - module_src = "#{@project_path}modules/#{module_file}" + begin + module_src = "#{@project_path}modules/#{module_file}" - ninjs_lib_secretary = Sprockets::Secretary.new( - :root => "#{Ninjs.base_directory}", - :asset_root => "#{@config.asset_root}", - :load_path => ["repository"], - :source_files => ["#{module_src}"] - ) + ninjs_lib_secretary = Sprockets::Secretary.new( + :root => "#{Ninjs.base_directory}", + :asset_root => "#{@config.asset_root}", + :load_path => ["repository"], + :source_files => ["#{module_src}"] + ) - module_file = ninjs_lib_secretary.concatenation - message = File.exists?("#{@project_path}application/#{module_name}.js") ? "\e[32m>>>\e[0m application/#{module_name}.js updated" : "\e[32m>>>\e[0m application/#{module_name}.js created" - module_file.save_to "#{@project_path}application/#{module_name}.js" - ninjs_lib_secretary.install_assets + module_file = ninjs_lib_secretary.concatenation + message = File.exists?("#{@project_path}application/#{module_name}.js") ? "\e[32m>>>\e[0m application/#{module_name}.js updated" : "\e[32m>>>\e[0m application/#{module_name}.js created" + module_file.save_to "#{@project_path}application/#{module_name}.js" + ninjs_lib_secretary.install_assets - #puts message - rescue Exception => error - @errors = true - puts "Sprockets error: #{error.message}" - end - + #puts message + rescue Exception => error + @errors = true + puts "Sprockets error: #{error.message}" + end end def update_application_file - message = File.exists?("#{@project_path}application/#{@app_filename}.js") ? "application/#{@app_filename}.js updated" : "application/#{@app_filename}.js created" - filename = "#{@project_path}application/#{@app_filename}.js" + application_file = "#{@project_path}application/#{@app_filename}.js" - File.open(filename, "w+") do |file| - @config.dependencies.each do |dependency| - file << "/*---------- #{dependency} ----------*/" - file << "\n//= require #{dependency}\n\n" if dependency.match(/^\<.+\>$/) - file << "\n//= require \"#{dependency}\"\n\n" if dependency.match(/^[^\<].+|[^\>]$/) - end - - file << "/* Ninjs #{Time.now.to_s} */\n" - file << "//= require \"../lib/nin.js\"\n\n" - file << "\nvar #{@config.name} = new NinjsApplication();\n\n" - - @config.autoload.each do |auto_file| - file << "/*---------- Ninjs autoload #{auto_file} ----------*/" - file << "\n//= require #{auto_file}\n\n" if auto_file.match(/^\<.+\>$/) - file << "\n//= require \"#{auto_file}\"\n\n" if auto_file.match(/^[^\<].+|[^\>]$/) - end + File.open(application_file, "w+") do |file| + write_dependencies(file) + write_core(file) + write_autoload(file) end + compile_application_file application_file + end + + def write_dependencies(file) + @config.dependencies.each do |dependency| + file << "/*---------- #{dependency} ----------*/" + file << "\n//= require #{dependency}\n\n" if dependency.match(/^\<.+\>$/) + file << "\n//= require \"#{dependency}\"\n\n" if dependency.match(/^[^\<].+|[^\>]$/) + end + end + + def write_core(file) + file << "/*---------- Ninjs core ../lib/nin.js ----------*/\n" + file << "//= require \"../lib/nin.js\"\n\n" + file << "\nvar #{@config.name} = new NinjsApplication();\n\n" + end + + def write_autoload(file) + @config.autoload.each do |auto_file| + file << "/*---------- Ninjs autoload #{auto_file} ----------*/" + file << "\n//= require #{auto_file}\n\n" if auto_file.match(/^\<.+\>$/) + file << "\n//= require \"#{auto_file}\"\n\n" if auto_file.match(/^[^\<].+|[^\>]$/) + end + end + + def compile_application_file(file) begin ninjs_lib_secretary = Sprockets::Secretary.new( :root => "#{Ninjs.base_directory}", :asset_root => "#{@config.asset_root}", :load_path => ["repository"], - :source_files => ["#{filename}"] + :source_files => ["#{file}"] ) application_file = ninjs_lib_secretary.concatenation ninjs_lib_secretary.install_assets - application_file.save_to "#{filename}" + application_file.save_to "#{file}" rescue Exception => error @errors = true puts "\e[0;31m!!!\e[0m Sprockets error: #{error.message}" end end @@ -246,8 +227,10 @@ File.open(full_path, "w+") do |module_file| module_file << JSMin.minify(uncompressed) end end end - + end -end + # class Project +end +#module Ninjs \ No newline at end of file