2018-07-27 12:33:14 +08:00
/**
* Tencent is pleased to support the open source community by making Tars available .
*
* Copyright ( C ) 2016 THL A29 Limited , a Tencent company . All rights reserved .
*
* Licensed under the BSD 3 - Clause License ( the " License " ) ; you may not use this file except
* in compliance with the License . You may obtain a copy of the License at
*
* https : //opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing , software distributed
* under the License is distributed on an " AS IS " BASIS , WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND , either express or implied . See the License for the
* specific language governing permissions and limitations under the License .
*/
# include "code_generator.h"
string CodeGenerator : : printHeaderRemark ( const string & sTypeName )
{
ostringstream s ;
s < < " // ********************************************************************** " < < endl ;
s < < " // Parsed By " < < IDL_NAMESPACE_STR < < " Parser( " < < PARSER_VERSION < < " ), Generated By " < < EXECUTE_FILENAME < < " ( " < < GENERATOR_VERSION < < " ) " < < endl ;
s < < " // " < < IDL_NAMESPACE_STR < < " Parser Maintained By < " < < TC_Common : : upper ( IDL_NAMESPACE_STR ) < < " > and " < < EXECUTE_FILENAME < < " Maintained By <superzheng> " < < endl ;
2019-03-20 19:23:14 +08:00
s < < " // Generated from \" " < < TC_File : : extractFileName ( _sIdlFile ) < < " \" by " < <
2018-07-27 12:33:14 +08:00
( _bEntry ? sTypeName : ( _bMinimalMembers ? " Minimal " : " Relation " ) ) < < " Mode " < < endl ;
s < < " // ********************************************************************** " < < endl ;
s < < endl ;
2019-01-09 11:45:21 +08:00
return s . str ( ) ;
}
string CodeGenerator : : printHeaderRemark ( const string & sTypeName , const string & sSuffix )
{
ostringstream s ;
s < < printHeaderRemark ( sTypeName ) ;
s < < sSuffix < < endl ;
2018-07-27 12:33:14 +08:00
s < < endl ;
2019-01-09 11:45:21 +08:00
2018-07-27 12:33:14 +08:00
return s . str ( ) ;
}
void CodeGenerator : : createFile ( const string & file , const bool bEntry )
{
_sIdlFile = getRealFileInfo ( file ) ;
_bEntry = bEntry ;
g_parse - > parse ( _sIdlFile ) ;
vector < ContextPtr > contexts = g_parse - > getContexts ( ) ;
for ( size_t i = 0 ; i < contexts . size ( ) ; i + + )
{
if ( _sIdlFile = = contexts [ i ] - > getFileName ( ) )
{
2019-03-20 19:23:14 +08:00
scan ( _sIdlFile , true ) ; // collect idl symbols
2018-07-27 12:33:14 +08:00
2019-03-20 19:23:14 +08:00
if ( ! _bClient & & ! _bServer )
2018-07-27 12:33:14 +08:00
{
2019-03-20 19:23:14 +08:00
if ( _bTS )
{
generateTS ( contexts [ i ] ) ; // generate .ts
}
else
{
generateJS ( contexts [ i ] ) ; // generate .js
if ( _bDTS ) generateDTS ( contexts [ i ] ) ; // generate .d.ts
}
2018-07-27 12:33:14 +08:00
}
2019-03-20 19:23:14 +08:00
if ( _bClient )
2018-07-27 12:33:14 +08:00
{
2019-03-20 19:23:14 +08:00
if ( _bTS )
{
if ( ! generateTSProxy ( contexts [ i ] ) ) return ; // generate .ts for proxy classes
}
else
{
if ( ! generateJSProxy ( contexts [ i ] ) ) return ; // generate .js for proxy classes
if ( _bDTS ) generateDTSProxy ( contexts [ i ] ) ; // generate .d.ts for proxy classes
}
2018-07-27 12:33:14 +08:00
}
if ( _bServer )
{
2019-03-20 19:23:14 +08:00
if ( _bTS )
{
if ( ! generateTSServer ( contexts [ i ] ) ) return ; // generate .ts for server classes
generateTSServerImp ( contexts [ i ] ) ; // generate .ts for server implementations
}
else
{
if ( ! generateJSServer ( contexts [ i ] ) ) return ; // generate .js for server classes
if ( _bDTS ) generateDTSServer ( contexts [ i ] ) ; // generate .d.ts for server classes
generateJSServerImp ( contexts [ i ] ) ; // generate .js for server implementations
}
2018-07-27 12:33:14 +08:00
}
vector < string > files = contexts [ i ] - > getIncludes ( ) ;
for ( size_t ii = 0 ; _bRecursive & & ii < files . size ( ) ; ii + + )
{
CodeGenerator node ;
node . setRpcPath ( _sRpcPath ) ;
node . setStreamPath ( _sStreamPath ) ;
node . setTargetPath ( _sToPath ) ;
node . setRecursive ( _bRecursive ) ;
node . setUseSpecialPath ( _bUseSpecialPath ) ;
node . setUseStringRepresent ( _bUseStringRepresent ) ;
node . setStringBinaryEncoding ( _bStringBinaryEncoding ) ;
node . setMinimalMembers ( _bMinimalMembers ) ;
node . setDependent ( _depMembers ) ;
2019-03-20 19:23:14 +08:00
node . setEnableTS ( _bTS ) ;
2018-07-27 12:33:14 +08:00
node . setEnableDTS ( _bDTS ) ;
node . createFile ( files [ ii ] , false ) ;
}
}
}
2019-03-20 19:23:14 +08:00
}