require 'rake' require 'rake/clean' require 'json' include Rake::DSL require_relative './environment.rb' require_relative './commands.rb' require_relative './dependencies.rb' require_relative './console.rb' require_relative './timer.rb' require_relative './settings.rb' require_relative './text.rb' require_relative './svnexports.rb' CLEAN.include('log','bin/**/*.pdb') CLOBBER.include('bin','obj','TestResults','*.gem','bin/**/*.exe') class DevTasks < Hash attr_accessor :timer def initialize @timer = Timer.new self[:name]=pwd.split('/').last#Rake.application.original_dir.split('/').last self[:svn_exports]=SvnExports.new self[:scm]=Environment.scm self[:scm_origin]=Environment.scm_origin self[:branch]=Environment.branch self[:command_order]=['pull','upgrade','setup','add','build','test','commit','push','verify','publish'] self[:relative_directory]=Environment.relative_directory self[:working_directory]=Environment.working_directory self[:context]=Environment.context self[:machine]=Environment.machine self[:platform]=RUBY_PLATFORM self[:dev_root]=Environment.dev_root self[:settings]=Settings.new self[:files]=Hash.new self[:files][:source]=Rake::FileList.new("**/{*.{rb,feature,spec,cs,c,m,cpp,cxx,h,hpp,i,jam,csproj,vcproj,snk,vcxproj,xcodeproj,plist,xib,sln,filters,xaml,resx,settings,config},Jamfile,.semver,Gemfile,README,LICENSE}") self[:files][:build]=Rake::FileList.new('**/*.{gemspec,sln}') self[:files][:artifact]=Rake::FileList.new("**/*.{gem,lib,dll,exe,h,hpp}") self[:commands]=Commands.new update end def add hash hash.each do |key,value| self[key]=value if !has_key?(key) end end def update self[:dependencies]=Dependencies.new if !has_key?(:dependencies) allartifactsexist = true self[:files][:artifact].each{|f| allartifactsexist=false if !File.exists?(f)} self[:newest_source_file]=self[:files][:source].max_by {|f| File.mtime(f)} self[:newest_artifact_file]=self[:files][:artifact].max_by { |f| File.mtime(f) } if allartifactsexist self[:commands]=Commands.new if !has_key?(:commands) #self[:commands].update self[:up_to_date] = false if(allartifactsexist && !self[:newest_artifact].nil? && !self[:newest_src_file].nil? && File.exists?(self[:newest_artifact]) && File.exists?(self[:newest_src_file])) if(File.mtime(self[:newest_artifact]) > File.mtime(self[:newest_src_file])) self[:command_order]=['upgrade'] if(File.mtime(self[:newest_artifact]) > File.mtime(self[:newest_src_file])) self[:up_to_date]=true end self[:newest_artifact_file_mtime]=File.mtime(self[:newest_artifact_file]).to_s self[:newest_source_file_mtime]=File.mtime(self[:newest_source_file]).to_s end update_tasks end def execute cmd self[:commands].execute_command cmd end def execute_task task sym_task = task.to_sym timer=Timer.new Console.announce_task_start task #self[:commands][sym_task].update if self[:commands][sym_task].respond_to? update if(!self[:commands].has_key?(sym_task)) puts "no commands discovered for task " + task else self[:commands][sym_task].update self[:commands][sym_task].each {|c| self[:commands].execute_command(c) } end elapsed = timer.elapsed if elapsed > 30 elapsed_str="[" + "%.0f" %(elapsed) + "s]" Console.announce_task_end task, elapsed_str end end def define_task task_name if(!Rake::Task.task_defined?(task_name)) ruby="desc '#{task_name} task';task :#{task_name} do;DEV_TASKS.execute_task '#{task_name}';end" eval(ruby) end end def update_tasks dev_task_defaults=Array.new self[:commands].each do |key,array| #if(array.length > 0) define_task key dev_task_defaults << key #end end update_task_dependencies end def update_task_dependencies eval('task :build=>[:setup]') if Rake::Task.task_defined?(:setup) && Rake::Task.task_defined?(:build) eval('task :test=>[:build]') if Rake::Task.task_defined?(:build) && Rake::Task.task_defined?(:test) end def setup_svn_export(uri,dir) DEV_TASKS[:commands][:setup] << "svn export #{uri} #{dir}" if(!File.exists?(dir)) end def setup_copy(src,dest) if(File.exists?(src)) FileUtils.mkdir_p(File.dirname(dest)) if(!File.exists?(File.dirname(dest))) if(!File.exists?(dest)) DEV_TASKS[:commands][:setup] << "<%FileUtils.cp('#{src}','#{dest}')%>" end else if(src.kind_of?(Array)) src.each{|s|setup_copy(s,dest)} end end end def svn_export(dep_dir,uri) if(!File.exists?("#{Environment.dev_root}/dep/#{dep_dir}")) dest="#{Environment.dev_root}/dep/#{dep_dir}" Environment.execute "svn export #{v} #{Environment.dev_root}/dep/#{dep_dir}" if !dest.include?("@") Environment.execute "svn export #{v} #{Environment.dev_root}/dep/#{dep_dir}@" if dest.include?("@") end end end DEV_TASKS=DevTasks.new DEV=DEV_TASKS desc 'executes tasks in order of command_order, if they are defined' task :dev_tasks_default do DEV_TASKS.update DEV_TASKS[:command_order].each{|c| Rake::Task[c].invoke if(Rake::Task.task_defined?(c)) } puts "Total Time " + DEV_TASKS.timer.elapsed_str end desc 'dev_tasks default' task :default do Rake::Task[:dev_tasks_default].invoke end desc 'displays information about current rake directory' task :info do DEV_TASKS.update Console.print_hash("",DEV_TASKS) end desc 'displays help for dev_task' task :help do puts "use rake -T to see available tasks" end desc 'rake projects defined in DEV_TASKS[:dependencies][:projects]' task :rake_dependencies_projects do Console.announce_task_start "rake_dependencies_projects" DEV_TASKS[:dependencies][:projects].each{|project| dir=Environment.dev_root + "/wrk/" + project Environment.rake(dir,"default",true) if File.exists?(dir) } end desc 'clobber projects defined in DEV_TASKS[:dependencies][:projects]' task :clobber_dependencies_projects do Console.announce_task_start "clobber_dependencies_projects" DEV_TASKS[:dependencies][:projects].each{|project| dir=Environment.dev_root + "/wrk/" + project Environment.rake(dir,"clobber",true) if File.exists?(dir) } end desc 'clean projects defined in DEV_TASKS[:dependencies][:projects]' task :clean_dependencies_projects do Console.announce_task_start "clean_dependencies_projects" DEV_TASKS[:dependencies][:projects].each{|project| dir=Environment.dev_root + "/wrk/" + project Environment.rake(dir,"clean",true) if File.exists?(dir) } end