/**
* File: codeCreator.cpp
*
* Author: ross
* Created: Jun 24 2018
*
* Description:   Creates a new file (replacing if it exists)
*
* Copyright: Anki, Inc. 2018
*/

#include "codeGen/codeCreator.h"
#include <google/protobuf/io/zero_copy_stream.h>
#include <memory>
#include <string>

using namespace google::protobuf;

namespace Anki {

CodeCreator::CodeCreator( const std::string& protoFile ) 
  : _protoFile(protoFile) 
{ 
}

bool CodeCreator::Write( const std::string& filename, compiler::GeneratorContext* context )
{
  std::unique_ptr<io::ZeroCopyOutputStream> output( context->Open(filename) );
  io::Printer printer( output.get(), '$' );

  printer.Print( "// Autogenerated from proto file " );
  printer.Print( _protoFile.c_str() );
  printer.Print( " using Anki protoc plugin\n\n\n" );
  
  if( !WriteCode( filename, context, &printer ) ) {
    return false;
  }
  return !printer.failed();
}

} // namespace
