Sanitize identifier names before substituting them in C++ template files

main
amzn-sj 5 years ago
parent 39d207095a
commit 2ee4d8ff6b

@ -16,9 +16,9 @@
#include <AzCore/EBus/EBus.h>
namespace ${Name}
namespace ${SanitizedCppName}
{
class ${Name}Requests
class ${SanitizedCppName}Requests
: public AZ::EBusTraits
{
public:
@ -31,6 +31,6 @@ namespace ${Name}
// Put your public methods here
};
using ${Name}RequestBus = AZ::EBus<${Name}Requests>;
using ${SanitizedCppName}RequestBus = AZ::EBus<${SanitizedCppName}Requests>;
} // namespace ${Name}
} // namespace ${SanitizedCppName}

@ -15,24 +15,24 @@
#include <AzCore/Serialization/SerializeContext.h>
#include <${Name}EditorSystemComponent.h>
namespace ${Name}
namespace ${SanitizedCppName}
{
void ${Name}EditorSystemComponent::Reflect(AZ::ReflectContext* context)
void ${SanitizedCppName}EditorSystemComponent::Reflect(AZ::ReflectContext* context)
{
if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
{
serializeContext->Class<${Name}EditorSystemComponent, AZ::Component>()->Version(1);
serializeContext->Class<${SanitizedCppName}EditorSystemComponent, AZ::Component>()->Version(1);
}
}
void ${Name}EditorSystemComponent::Activate()
void ${SanitizedCppName}EditorSystemComponent::Activate()
{
AzToolsFramework::EditorEvents::Bus::Handler::BusConnect();
}
void ${Name}EditorSystemComponent::Deactivate()
void ${SanitizedCppName}EditorSystemComponent::Deactivate()
{
AzToolsFramework::EditorEvents::Bus::Handler::BusDisconnect();
}
} // namespace ${Name}
} // namespace ${SanitizedCppName}

@ -18,32 +18,32 @@
#include <AzToolsFramework/Entity/EditorEntityContextBus.h>
namespace ${Name}
namespace ${SanitizedCppName}
{
/// System component for ${Name} editor
class ${Name}EditorSystemComponent
/// System component for ${SanitizedCppName} editor
class ${SanitizedCppName}EditorSystemComponent
: public AZ::Component
, private AzToolsFramework::EditorEvents::Bus::Handler
{
public:
AZ_COMPONENT(${Name}EditorSystemComponent, "${EditorSysCompClassId}");
AZ_COMPONENT(${SanitizedCppName}EditorSystemComponent, "${EditorSysCompClassId}");
static void Reflect(AZ::ReflectContext* context);
${Name}EditorSystemComponent() = default;
${SanitizedCppName}EditorSystemComponent() = default;
private:
static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
{
provided.push_back(AZ_CRC("${Name}EditorService"));
provided.push_back(AZ_CRC("${SanitizedCppName}EditorService"));
}
static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
{
required.push_back(AZ_CRC("${Name}Service"));
required.push_back(AZ_CRC("${SanitizedCppName}Service"));
}
// AZ::Component
void Activate() override;
void Deactivate() override;
};
} // namespace ${Name}
} // namespace ${SanitizedCppName}

@ -17,21 +17,21 @@
#include <${Name}SystemComponent.h>
namespace ${Name}
namespace ${SanitizedCppName}
{
class ${Name}Module
class ${SanitizedCppName}Module
: public AZ::Module
{
public:
AZ_RTTI(${Name}Module, "${ModuleClassId}", AZ::Module);
AZ_CLASS_ALLOCATOR(${Name}Module, AZ::SystemAllocator, 0);
AZ_RTTI(${SanitizedCppName}Module, "${ModuleClassId}", AZ::Module);
AZ_CLASS_ALLOCATOR(${SanitizedCppName}Module, AZ::SystemAllocator, 0);
${Name}Module()
${SanitizedCppName}Module()
: AZ::Module()
{
// Push results of [MyComponent]::CreateDescriptor() into m_descriptors here.
m_descriptors.insert(m_descriptors.end(), {
${Name}SystemComponent::CreateDescriptor(),
${SanitizedCppName}SystemComponent::CreateDescriptor(),
});
}
@ -41,10 +41,10 @@ namespace ${Name}
AZ::ComponentTypeList GetRequiredSystemComponents() const override
{
return AZ::ComponentTypeList {
azrtti_typeid<${Name}SystemComponent>(),
azrtti_typeid<${SanitizedCppName}SystemComponent>(),
};
}
};
}// namespace ${Name}
}// namespace ${SanitizedCppName}
AZ_DECLARE_MODULE_CLASS(Gem_${Name}, ${Name}::${Name}Module)
AZ_DECLARE_MODULE_CLASS(Gem_${SanitizedCppName}, ${SanitizedCppName}::${SanitizedCppName}Module)

@ -18,19 +18,19 @@
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/Serialization/EditContextConstants.inl>
namespace ${Name}
namespace ${SanitizedCppName}
{
void ${Name}SystemComponent::Reflect(AZ::ReflectContext* context)
void ${SanitizedCppName}SystemComponent::Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
{
serialize->Class<${Name}SystemComponent, AZ::Component>()
serialize->Class<${SanitizedCppName}SystemComponent, AZ::Component>()
->Version(0)
;
if (AZ::EditContext* ec = serialize->GetEditContext())
{
ec->Class<${Name}SystemComponent>("${Name}", "[Description of functionality provided by this System Component]")
ec->Class<${SanitizedCppName}SystemComponent>("${SanitizedCppName}", "[Description of functionality provided by this System Component]")
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System"))
->Attribute(AZ::Edit::Attributes::AutoExpand, true)
@ -39,45 +39,45 @@ namespace ${Name}
}
}
void ${Name}SystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
void ${SanitizedCppName}SystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
{
provided.push_back(AZ_CRC("${Name}Service"));
provided.push_back(AZ_CRC("${SanitizedCppName}Service"));
}
void ${Name}SystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
void ${SanitizedCppName}SystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
{
incompatible.push_back(AZ_CRC("${Name}Service"));
incompatible.push_back(AZ_CRC("${SanitizedCppName}Service"));
}
void ${Name}SystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
void ${SanitizedCppName}SystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
{
AZ_UNUSED(required);
}
void ${Name}SystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
void ${SanitizedCppName}SystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
{
AZ_UNUSED(dependent);
}
void ${Name}SystemComponent::Init()
void ${SanitizedCppName}SystemComponent::Init()
{
}
void ${Name}SystemComponent::Activate()
void ${SanitizedCppName}SystemComponent::Activate()
{
${Name}RequestBus::Handler::BusConnect();
${SanitizedCppName}RequestBus::Handler::BusConnect();
AZ::TickBus::Handler::BusConnect();
}
void ${Name}SystemComponent::Deactivate()
void ${SanitizedCppName}SystemComponent::Deactivate()
{
AZ::TickBus::Handler::BusDisconnect();
${Name}RequestBus::Handler::BusDisconnect();
${SanitizedCppName}RequestBus::Handler::BusDisconnect();
}
void ${Name}SystemComponent::OnTick(float /*deltaTime*/, AZ::ScriptTimePoint /*time*/)
void ${SanitizedCppName}SystemComponent::OnTick(float /*deltaTime*/, AZ::ScriptTimePoint /*time*/)
{
}
} // namespace ${Name}
} // namespace ${SanitizedCppName}

@ -18,15 +18,15 @@
#include <AzCore/Component/TickBus.h>
#include <${Name}/${Name}Bus.h>
namespace ${Name}
namespace ${SanitizedCppName}
{
class ${Name}SystemComponent
class ${SanitizedCppName}SystemComponent
: public AZ::Component
, protected ${Name}RequestBus::Handler
, protected ${SanitizedCppName}RequestBus::Handler
, public AZ::TickBus::Handler
{
public:
AZ_COMPONENT(${Name}SystemComponent, "${SysCompClassId}");
AZ_COMPONENT(${SanitizedCppName}SystemComponent, "${SysCompClassId}");
static void Reflect(AZ::ReflectContext* context);
@ -37,7 +37,7 @@ namespace ${Name}
protected:
////////////////////////////////////////////////////////////////////////
// ${Name}RequestBus interface implementation
// ${SanitizedCppName}RequestBus interface implementation
////////////////////////////////////////////////////////////////////////
@ -54,4 +54,4 @@ namespace ${Name}
////////////////////////////////////////////////////////////////////////
};
} // namespace ${Name}
} // namespace ${SanitizedCppName}

@ -14,7 +14,7 @@
#include <AzTest/AzTest.h>
class ${Name}EditorTest
class ${SanitizedCppName}EditorTest
: public ::testing::Test
{
protected:
@ -29,7 +29,7 @@ protected:
}
};
TEST_F(${Name}EditorTest, SanityTest)
TEST_F(${SanitizedCppName}EditorTest, SanityTest)
{
ASSERT_TRUE(true);
}

@ -14,7 +14,7 @@
#include <AzTest/AzTest.h>
class ${Name}Test
class ${SanitizedCppName}Test
: public ::testing::Test
{
protected:
@ -29,7 +29,7 @@ protected:
}
};
TEST_F(${Name}Test, SanityTest)
TEST_F(${SanitizedCppName}Test, SanityTest)
{
ASSERT_TRUE(true);
}

@ -16,9 +16,9 @@
#include <AzCore/EBus/EBus.h>
namespace ${Name}
namespace ${SanitizedCppName}
{
class ${Name}Requests
class ${SanitizedCppName}Requests
: public AZ::EBusTraits
{
public:
@ -30,5 +30,5 @@ namespace ${Name}
// Put your public methods here
};
using ${Name}RequestBus = AZ::EBus<${Name}Requests>;
} // namespace ${Name}
using ${SanitizedCppName}RequestBus = AZ::EBus<${SanitizedCppName}Requests>;
} // namespace ${SanitizedCppName}

@ -17,21 +17,21 @@
#include "${Name}SystemComponent.h"
namespace ${Name}
namespace ${SanitizedCppName}
{
class ${Name}Module
class ${SanitizedCppName}Module
: public AZ::Module
{
public:
AZ_RTTI(${Name}Module, "${ModuleClassId}", AZ::Module);
AZ_CLASS_ALLOCATOR(${Name}Module, AZ::SystemAllocator, 0);
AZ_RTTI(${SanitizedCppName}Module, "${ModuleClassId}", AZ::Module);
AZ_CLASS_ALLOCATOR(${SanitizedCppName}Module, AZ::SystemAllocator, 0);
${Name}Module()
${SanitizedCppName}Module()
: AZ::Module()
{
// Push results of [MyComponent]::CreateDescriptor() into m_descriptors here.
m_descriptors.insert(m_descriptors.end(), {
${Name}SystemComponent::CreateDescriptor(),
${SanitizedCppName}SystemComponent::CreateDescriptor(),
});
}
@ -41,10 +41,10 @@ namespace ${Name}
AZ::ComponentTypeList GetRequiredSystemComponents() const override
{
return AZ::ComponentTypeList{
azrtti_typeid<${Name}SystemComponent>(),
azrtti_typeid<${SanitizedCppName}SystemComponent>(),
};
}
};
}// namespace ${Name}
}// namespace ${SanitizedCppName}
AZ_DECLARE_MODULE_CLASS(Gem_${Name}, ${Name}::${Name}Module)
AZ_DECLARE_MODULE_CLASS(Gem_${SanitizedCppName}, ${SanitizedCppName}::${SanitizedCppName}Module)

@ -18,19 +18,19 @@
#include "${Name}SystemComponent.h"
namespace ${Name}
namespace ${SanitizedCppName}
{
void ${Name}SystemComponent::Reflect(AZ::ReflectContext* context)
void ${SanitizedCppName}SystemComponent::Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
{
serialize->Class<${Name}SystemComponent, AZ::Component>()
serialize->Class<${SanitizedCppName}SystemComponent, AZ::Component>()
->Version(0)
;
if (AZ::EditContext* ec = serialize->GetEditContext())
{
ec->Class<${Name}SystemComponent>("${Name}", "[Description of functionality provided by this System Component]")
ec->Class<${SanitizedCppName}SystemComponent>("${SanitizedCppName}", "[Description of functionality provided by this System Component]")
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System"))
->Attribute(AZ::Edit::Attributes::AutoExpand, true)
@ -39,37 +39,37 @@ namespace ${Name}
}
}
void ${Name}SystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
void ${SanitizedCppName}SystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
{
provided.push_back(AZ_CRC("${Name}Service"));
provided.push_back(AZ_CRC("${SanitizedCppName}Service"));
}
void ${Name}SystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
void ${SanitizedCppName}SystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
{
incompatible.push_back(AZ_CRC("${Name}Service"));
incompatible.push_back(AZ_CRC("${SanitizedCppName}Service"));
}
void ${Name}SystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
void ${SanitizedCppName}SystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
{
AZ_UNUSED(required);
}
void ${Name}SystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
void ${SanitizedCppName}SystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
{
AZ_UNUSED(dependent);
}
void ${Name}SystemComponent::Init()
void ${SanitizedCppName}SystemComponent::Init()
{
}
void ${Name}SystemComponent::Activate()
void ${SanitizedCppName}SystemComponent::Activate()
{
${Name}RequestBus::Handler::BusConnect();
${SanitizedCppName}RequestBus::Handler::BusConnect();
}
void ${Name}SystemComponent::Deactivate()
void ${SanitizedCppName}SystemComponent::Deactivate()
{
${Name}RequestBus::Handler::BusDisconnect();
${SanitizedCppName}RequestBus::Handler::BusDisconnect();
}
}

@ -18,14 +18,14 @@
#include <${Name}/${Name}Bus.h>
namespace ${Name}
namespace ${SanitizedCppName}
{
class ${Name}SystemComponent
class ${SanitizedCppName}SystemComponent
: public AZ::Component
, protected ${Name}RequestBus::Handler
, protected ${SanitizedCppName}RequestBus::Handler
{
public:
AZ_COMPONENT(${Name}SystemComponent, "${SysCompClassId}");
AZ_COMPONENT(${SanitizedCppName}SystemComponent, "${SysCompClassId}");
static void Reflect(AZ::ReflectContext* context);
@ -36,7 +36,7 @@ namespace ${Name}
protected:
////////////////////////////////////////////////////////////////////////
// ${Name}RequestBus interface implementation
// ${SanitizedCppName}RequestBus interface implementation
////////////////////////////////////////////////////////////////////////

@ -1232,10 +1232,12 @@ def create_from_template(destination_path: str,
with_this = replace.pop(0)
replacements.append((replace_this, with_this))
sanitized_cpp_name = utils.sanitize_identifier_for_cpp(destination_name)
# dst name is Name
replacements.append(("${Name}", destination_name))
replacements.append(("${NameUpper}", destination_name.upper()))
replacements.append(("${NameLower}", destination_name.lower()))
replacements.append(("${SanitizedCppName}", sanitized_cpp_name))
if _instantiate_template(template_json_data,
destination_name,
@ -1536,10 +1538,12 @@ def create_project(project_path: str,
with_this = replace.pop(0)
replacements.append((replace_this, with_this))
sanitized_cpp_name = utils.sanitize_identifier_for_cpp(project_name)
# project name
replacements.append(("${Name}", project_name))
replacements.append(("${NameUpper}", project_name.upper()))
replacements.append(("${NameLower}", project_name.lower()))
replacements.append(("${SanitizedCppName}", sanitized_cpp_name))
# module id is a uuid with { and -
if module_id:
@ -1927,10 +1931,13 @@ def create_gem(gem_path: str,
with_this = replace.pop(0)
replacements.append((replace_this, with_this))
sanitized_cpp_name = utils.sanitize_identifier_for_cpp(gem_name)
# gem name
replacements.append(("${Name}", gem_name))
replacements.append(("${NameUpper}", gem_name.upper()))
replacements.append(("${NameLower}", gem_name.lower()))
replacements.append(("${SanitizedCppName}", sanitized_cpp_name))
# module id is a uuid with { and -
if module_id:

@ -36,6 +36,26 @@ def validate_identifier(identifier: str) -> bool:
return True
def sanitize_identifier_for_cpp(identifier: str) -> str:
"""
Convert the provided identifier to a valid C++ identifier
:param identifier: the name which needs to to sanitized
:return: str: sanitized identifier
"""
if not identifier:
return ''
sanitized_identifier = list(identifier)
if not (sanitized_identifier[0].isalpha() or sanitized_identifier[0] == '_'):
sanitized_identifier.insert(0, '_')
for index, character in enumerate(sanitized_identifier):
if not (character.isalnum() or character == '_'):
sanitized_identifier[index] = '_'
return "".join(sanitized_identifier)
def validate_uuid4(uuid_string: str) -> bool:
"""
Determine if the uuid supplied is valid.

Loading…
Cancel
Save