require 'p4util/tasks' require 'rake/clean' P4Util::Tasks.new do |p4util| p4util.basename = 'api_' p4util.version = 'r14.2' p4util.p4_init_dir = 'p4_web_api/clients/ruby/p4_web_api_client/spec/init.base' end P4Util::Tasks.new do |p4util| p4util.basename = 'proj_' p4util.version = 'r14.2' p4util.p4_init_dir = 'p4_project_services/clients/ruby/p4_project_services_client/spec/init' end desc 'Create a new config.ru in the work/ directory for launching the web apps' task 'work/config.ru' do Dir.mkdir('work') unless Dir.exist?('work') File.open('work/config.ru', 'w') do |f| f.puts <<-END.gsub(/^[ ]{4}/, '') # Avoid any environmental status pollution from your dev environment ENV.keys.select { |k| k =~ /^P4/ }.each { |k| ENV.delete(k) } require 'sequel' DB = Sequel.connect('postgres://localhost:5432/p4_project_services') require 'p4_web_api' api = P4WebAPI::App.new api.settings.p4 = { 'port' => 'localhost:1666', 'charset' => 'auto' } api.settings.token_path = '#{File.absolute_path('work/tokens')}' api.settings.workspace_folder = '#{File.absolute_path('work/workspaces')}' require 'p4_project_services' proj = P4ProjectServices::App.new proj.settings.p4 = { 'port' => 'localhost:1666', 'charset' => 'auto' } proj.settings.token_path = '#{File.absolute_path('work/tokens')}' proj.settings.p4_web_api_url = 'http://localhost:4567' proj.settings.p4_web_api_prefix = '/p4_web_api/v1' require 'p4_phoenix_services' phx = P4PhoenixServices::App.new phx.settings.p4 = { 'port' => 'localhost:1666', 'charset' => 'auto' } phx.settings.token_path = '#{File.absolute_path('work/tokens')}' phx.settings.p4_project_services_url = 'http://localhost:4567' phx.settings.p4_project_services_prefix = '/p4_project_services/v1' run Rack::URLMap.new( '/p4_web_api' => api, '/p4_project_services' => proj, '/p4_phoenix_services' => phx ) END end end desc 'Create the Unicorn config for testing' task 'work/unicorn.rb' do Dir.mkdir('work') unless Dir.exist?('work') File.open('work/unicorn.rb', 'w') do |f| f.puts <<-END.gsub(/^[ ]{4}/, '') pid '#{File.absolute_path('work/unicorn.pid')}' listen 4567 worker_processes 6 END end end desc 'Start p4_web_services unicorn instance (in background)' task :services_start => ['work/unicorn.rb', 'work/config.ru'] do pid = Process.spawn('bundle exec unicorn -c work/unicorn.rb work/config.ru') Process.detach(pid) File.open('work/p4_web_services.pid', 'w') do |f| f.puts(pid) end retries = 10 while !system('curl http://localhost:4567/') do sleep(0.25) retries -= 1 end system('curl http://localhost:4567') or fail 'did not start services' end desc 'Kills the spawned p4_web_services process (via the PID file)' task :services_stop do if File.exist?('work/unicorn.pid') pid = IO.read('work/unicorn.pid').to_i Process.kill('TERM', pid) end end namespace :pg do desc 'Create a new PostgreSQL configuration for local development usage' task :init do unless Dir.exist?('work/pgdata') FileUtils.mkdir_p('work/pgdata') sh 'initdb -D work/pgdata' or fail 'initdb failed' end end desc 'Start postgres server with data hosted locally (only suitable for development)' task :start => :init do pgdata_dir = File.absolute_path('work/pgdata') sh "pg_ctl -D #{pgdata_dir} start -w" or fail "pg_ctl start failed" end desc 'Create local databases (p4_project_services)' task :create do sh 'createdb p4_project_services' or fail 'createdb failed' end desc 'Shutdown the postgres server using fast shutdown mode' task :stop do sh 'pg_ctl -D work/pgdata stop -m fast -w' end desc 'Stop and remove the temporary postgres data directory' task :clean => :stop do if Dir.exist?('work/pgdata') FileUtils.rmtree('work/pgdata') end end end namespace :db do desc 'Run migrations' task :migrate, [:version] do |t, args| require 'sequel' Sequel.extension :migration url = ENV['DATABASE_URL'] || 'postgres://localhost:5432/p4_project_services' db = Sequel.connect(url) if args[:version] puts "Migrating to version #{args[:version]}" Sequel::Migrator.run(db, 'p4_project_services/p4_project_services/migrations', target: args[:version].to_i) else puts 'Migrating to latest' Sequel::Migrator.run(db, 'p4_project_services/p4_project_services/migrations') end end end begin require 'ci/reporter/rake/rspec' require 'rspec/core/rake_task' ENV['CI_REPORTS'] = 'spec-output' CLEAN.include('spec-output', 'coverage') RSpec::Core::RakeTask.new(:api_spec) do |t| t.pattern = 'p4_web_api/clients/ruby/p4_web_api_client/spec/*.rb' t.ruby_opts = ["-I#{File.absolute_path('p4_web_api/clients/ruby/p4_web_api_client/spec/lib')}"] end task api_spec: 'ci:setup:rspec' RSpec::Core::RakeTask.new(:proj_spec) do |t| t.pattern = 'p4_project_services/clients/ruby/p4_project_services_client/spec/*.rb' t.ruby_opts = ["-I#{File.absolute_path('p4_project_services/clients/ruby/p4_project_services_client/spec/lib')}"] end task proj_spec: 'ci:setup:rspec' RSpec::Core::RakeTask.new(:proj_svc_spec) do |t| t.pattern = 'p4_project_services/p4_project_services/spec/*.rb' end task proj_svc_spec: 'ci:setup:rspec' rescue LoadError # no rspec available end desc 'Rebuild native tools in qt/work directory (please set CMAKE_PREFIX_PATH in environment)' task :qt_build do FileUtils.rmtree('qt/work') if Dir.exist?('qt/work') FileUtils.mkdir('qt/work') system('cd qt/work && cmake .. && make') or fail('qt build failed') end CLEAN.include('qt/work') desc 'Run all qt tests' task :qt_test do system('cd qt/work/p4_phoenix_services_client && ./PhoenixIntegrationTests') or fail('PhoenixIntegrationTests failed') end desc 'Build all gems and copy them to our top-level pkg/ directory' task :build do system('cd p4_web_services_auth && rake build') or fail 'rake gem failed for p4_web_services_auth' system('cd p4_web_api/p4_web_api && rake package') or fail 'rake package failed for p4_web_api' system('cd p4_web_api/clients/ruby/p4_web_api_client && rake build') or fail 'rake build failed for p4_web_api_client' system('cd p4_project_services/p4_project_services_data && rake build') or fail 'rake build failed for p4_project_services_data' system('cd p4_project_services/p4_project_services && rake build') or fail 'rake build failed for p4_project_services' system('cd p4_project_services/clients/ruby/p4_project_services_client && rake build') or fail 'rake build failed for p4_project_services_client' system('cd p4_phoenix_services/p4_phoenix_services && rake build') or fail 'rake build failed for p4_phoenix_services' if Dir.exist?('build') `chmod -R +w build` end Dir.mkdir('build') unless Dir.exist?('build') Dir.mkdir('build/gem') unless Dir.exist?('build/gem') Dir.glob('**/pkg/*.gem').each do |f| FileUtils.copy(f, "build/gem/#{File.basename(f)}") end ['qt/p4_project_services_client', 'qt/p4_phoenix_services_client'].each do |project| project_name = File.basename(project) puts "Preparing project #{project_name} from #{project}" Dir.glob("#{project}/**/*").each do |file| relative_path = file[(project.length+1)..-1] if File.file?(file) FileUtils.mkdir_p("build/#{project_name}/#{File.dirname(relative_path)}") changelist=`p4 changes -m1 '//web-services/p4ws-main/...#have' | awk '{print $2}'`.strip contents = nil File.open(file, 'rb') do |f| contents = f.read.gsub('DEV', changelist) end File.open("build/#{project_name}/#{relative_path}", 'wb') do |f| f.write(contents) end end end end Dir.glob('') end CLEAN.include('**/pkg') desc 'Create doc directory from docbook files (requires ant)' directory 'build/doc' => 'docbook' do FileUtils.rmtree('build/doc') if Dir.exist?('build/doc') sh 'cd docbook && ant publicsite -Ddoc.build.path=../tools/p4-doc/manuals/_build && cd ..' end CLEAN.include('build/doc')