Skip to content

Commit

Permalink
Refactor Analytics swig logic (#1108)
Browse files Browse the repository at this point in the history
* Refactor Analytics swig logic

* Update CMakeLists.txt

* Add a deprecated ParameterGroupId

* Add generated headers to the doc only logic

* Update generate_constants.py
  • Loading branch information
a-maurice authored Oct 17, 2024
1 parent 1f80a27 commit 1fc6b99
Show file tree
Hide file tree
Showing 9 changed files with 642 additions and 299 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ endif()
if (FIREBASE_INCLUDE_ANALYTICS)
add_subdirectory(analytics)
list(APPEND TARGET_LINK_LIB_NAMES "firebase_analytics" "firebase_analytics_swig")
list(APPEND DOCUMENTATION_ONLY_LIB_NAMES "firebase_analytics_swig")
list(APPEND DOCUMENTATION_ONLY_LIB_NAMES "firebase_analytics_swig" FIREBASE_UNITY_ANALYTICS_GENERATED_FILES)
list(APPEND PROJECT_LIST_HEADER " X(Analytics)")
endif()
if (FIREBASE_INCLUDE_APP_CHECK)
Expand Down
47 changes: 47 additions & 0 deletions analytics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,54 @@ set(firebase_analytics_swig
src/swig/analytics.i
)

# Generate CSharp files for the various Analytics constants
set(analytics_cpp_generated_headers_dir
"${CMAKE_BINARY_DIR}/generated/analytics/src/include/firebase/analytics")
set(analytics_csharp_generated_dir
"${CMAKE_BINARY_DIR}/analytics/generated")
file(MAKE_DIRECTORY ${analytics_csharp_generated_dir})

# Generate the header file by invoking the generate_constants python script.
function(generate_analytics_unity_file CPP_FILE CSHARP_FILE)
add_custom_command(
OUTPUT ${CSHARP_FILE}
COMMAND ${FIREBASE_PYTHON_EXECUTABLE} "${CMAKE_CURRENT_LIST_DIR}/generate_constants.py"
"--cpp_header=${CPP_FILE}"
"--csharp_file=${CSHARP_FILE}"
DEPENDS FIREBASE_ANALYTICS_GENERATED_HEADERS
${CPP_FILE}
COMMENT "Generating ${CSHARP_FILE}"
)
endfunction()

# Call the above function for all of the files to generate.
generate_analytics_unity_file(
"${analytics_cpp_generated_headers_dir}/event_names.h"
"${analytics_csharp_generated_dir}/EventNames.cs"
)
generate_analytics_unity_file(
"${analytics_cpp_generated_headers_dir}/parameter_names.h"
"${analytics_csharp_generated_dir}/ParameterNames.cs"
)
generate_analytics_unity_file(
"${analytics_cpp_generated_headers_dir}/user_property_names.h"
"${analytics_csharp_generated_dir}/UserPropertyNames.cs"
)
add_custom_target(FIREBASE_UNITY_ANALYTICS_GENERATED_FILES
DEPENDS
${analytics_csharp_generated_dir}/EventNames.cs
${analytics_csharp_generated_dir}/ParameterNames.cs
${analytics_csharp_generated_dir}/UserPropertyNames.cs
)

# Firebase Analytics CSharp files
set(firebase_analytics_src
src/Consent.cs
src/FirebaseAnalytics.cs
src/Parameter.cs
${analytics_csharp_generated_dir}/EventNames.cs
${analytics_csharp_generated_dir}/ParameterNames.cs
${analytics_csharp_generated_dir}/UserPropertyNames.cs
)

firebase_swig_add_library(firebase_analytics_swig
Expand Down Expand Up @@ -59,6 +105,7 @@ mono_add_library(firebase_analytics_cs
${FIREBASE_PLATFORM_REF}
DEPENDS
firebase_analytics_swig
FIREBASE_UNITY_ANALYTICS_GENERATED_FILES
)

if(FIREBASE_IOS_BUILD)
Expand Down
83 changes: 83 additions & 0 deletions analytics/generate_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.

"""Convert C++ headers of Analytics constants to C# files."""

import datetime
import os
import re
import subprocess

from absl import app
from absl import flags

FLAGS = flags.FLAGS

# Required args
flags.DEFINE_string('cpp_header', '', 'C++ header file containing a '
'set of constants to convert to C#.')
flags.register_validator(
'cpp_header',
lambda x: x and os.path.exists(x),
message=('Must reference an existing C++ header file'))
flags.DEFINE_string('csharp_file', '', 'Full path of the C# file to write '
'out.')
flags.register_validator(
'csharp_file', lambda x: x, message='Output C# file must be specified')

CPP_NAMESPACE = 'namespace analytics'

DOC_REPLACEMENTS = [
('static const char\*const k', 'public static string ')
]


def main(unused_argv):
"""Convert the C++ file into C#, going line-by-line to edit it."""
with open(FLAGS.cpp_header) as input_file:
with open(FLAGS.csharp_file, 'w') as output_file:
# Write the initial lines at the top
output_file.write('// Copyright %s Google LLC\n\n' %
str(datetime.date.today().year))
output_file.write('namespace Firebase.Analytics {\n\n')
output_file.write('public static partial class FirebaseAnalytics {\n\n')

found_namespace = False
for line in input_file:
line = line.rstrip()

# Ignore everything in the C++ file until inside the namespaces
if not found_namespace:
if re.search(CPP_NAMESPACE, line):
found_namespace = True
continue
# Stop copying when finding the next namespace (we assume it is closing)
if re.search(CPP_NAMESPACE, line):
break

for replace_from, replace_to in DOC_REPLACEMENTS:
if (re.search(replace_from, line)):
line = re.sub(replace_from, replace_to, line)
output_file.write(line + '\n')

# Write the lines at the end
# Close the class
output_file.write('}\n\n')
# close the namespace
output_file.write('}\n')

return 0

if __name__ == '__main__':
app.run(main)
38 changes: 38 additions & 0 deletions analytics/src/Consent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/

namespace Firebase.Analytics {

/// @brief The type of consent to set.
///
/// Supported consent types are mapped to corresponding constants in the Android
/// and iOS SDKs. Omitting a type retains its previous status.
public enum ConsentType {
AdStorage = 0,
AnalyticsStorage,
AdUserData,
AdPersonalization
}

/// @brief The status value of the consent type.
///
/// Supported statuses are ConsentStatus.Granted and ConsentStatus.Denied.
public enum ConsentStatus {
Granted = 0,
Denied
}

}
Loading

0 comments on commit 1fc6b99

Please sign in to comment.