def run_or_die(cmd)
  #puts "*** #{cmd}"
  result = `#{cmd}`
  raise "ERROR: #{cmd} failed" if $?.exitstatus != 0
  result
end

puts "*** Clean-building mogenerator"
build_settings = run_or_die 'xcodebuild -project ../mogenerator.xcodeproj -configuration Debug -showBuildSettings'
built_products_dir = build_settings.lines.select{|line|line[/\sBUILT_PRODUCTS_DIR\s/]}[0].strip.sub('BUILT_PRODUCTS_DIR = ','')
run_or_die 'xcodebuild -project ../mogenerator.xcodeproj -scheme mogenerator clean' # need this to pick up template changes
run_or_die 'xcodebuild -project ../mogenerator.xcodeproj -scheme mogenerator'
mogenPath = "#{built_products_dir}/mogenerator"

def gen_and_compile(desc, mogenPath, extra_mogen_args, extra_gcc_args)
  puts "*** Testing #{desc}"
  ENV['MOMC_NO_INVERSE_RELATIONSHIP_WARNINGS'] = '1'
  run_or_die "#{mogenPath.gsub(/ /, '\\ ')} --model test.xcdatamodel --output MOs --baseClass MyBaseClass #{extra_mogen_args}"
  run_or_die 'cp HumanMO.h HumanMO.m MyProtocol.h TestProtocol.m MOs'
  run_or_die "clang -o testbin test.m MyBaseClass.m Gender.m MOs/*.m -I#{Dir.pwd} -framework Foundation -framework Cocoa -framework CoreData -fmodules #{extra_gcc_args}"
  run_or_die "xcrun momc -MOMC_NO_INVERSE_RELATIONSHIP_WARNINGS test.xcdatamodel #{Dir.pwd}/test.mom"
  puts run_or_die './testbin'
end

desc 'Generate, Compile and Run MRC Code (with objc literals)'
task :mrc do
  gen_and_compile('MRC (with objc literals)', mogenPath, '', '')
end

desc 'Generate, Compile and Run MRC Code (without objc literals)'
task :mrc_noliterals do
  gen_and_compile('MRC (without objc literals)', mogenPath, '--template-var noliterals=true', '')
end

desc 'Generate, Compile and Run ARC Code (with objc literals)'
task :arc do
  gen_and_compile('ARC (with objc literals)', mogenPath, '--template-var arc=true', '-fobjc-arc')
end

desc 'Generate, Compile and Run ARC Code (without objc literals)'
task :arc_noliterals do
  gen_and_compile('ARC (without objc literals)', mogenPath, '--template-var arc=true --template-var noliterals=true', '-fobjc-arc')
end

desc 'Generate, Compile and Run ARC Code (without objc literals)'
task :v2 do
  Rake::Task[:clean].execute
  gen_and_compile('v2 (ARC + objc literals)', mogenPath, '--v2', '-fobjc-arc')
  Rake::Task[:clean].execute
end

desc 'Clean output'
task :clean do
  run_or_die 'rm -rf MOs testbin test.mom'
end

task :default do
  Rake::Task[:clean].execute
  
  Rake::Task[:mrc].execute
  Rake::Task[:clean].execute
  
  Rake::Task[:mrc_noliterals].execute
  Rake::Task[:clean].execute
  
  Rake::Task[:arc].execute
  Rake::Task[:clean].execute
  
  Rake::Task[:arc_noliterals].execute
  Rake::Task[:clean].execute
end