Skip to content

Commit 1fc6b99

Browse files
authored
Refactor Analytics swig logic (#1108)
* Refactor Analytics swig logic * Update CMakeLists.txt * Add a deprecated ParameterGroupId * Add generated headers to the doc only logic * Update generate_constants.py
1 parent 1f80a27 commit 1fc6b99

File tree

9 files changed

+642
-299
lines changed

9 files changed

+642
-299
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ endif()
273273
if (FIREBASE_INCLUDE_ANALYTICS)
274274
add_subdirectory(analytics)
275275
list(APPEND TARGET_LINK_LIB_NAMES "firebase_analytics" "firebase_analytics_swig")
276-
list(APPEND DOCUMENTATION_ONLY_LIB_NAMES "firebase_analytics_swig")
276+
list(APPEND DOCUMENTATION_ONLY_LIB_NAMES "firebase_analytics_swig" FIREBASE_UNITY_ANALYTICS_GENERATED_FILES)
277277
list(APPEND PROJECT_LIST_HEADER " X(Analytics)")
278278
endif()
279279
if (FIREBASE_INCLUDE_APP_CHECK)

analytics/CMakeLists.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,54 @@ set(firebase_analytics_swig
2121
src/swig/analytics.i
2222
)
2323

24+
# Generate CSharp files for the various Analytics constants
25+
set(analytics_cpp_generated_headers_dir
26+
"${CMAKE_BINARY_DIR}/generated/analytics/src/include/firebase/analytics")
27+
set(analytics_csharp_generated_dir
28+
"${CMAKE_BINARY_DIR}/analytics/generated")
29+
file(MAKE_DIRECTORY ${analytics_csharp_generated_dir})
30+
31+
# Generate the header file by invoking the generate_constants python script.
32+
function(generate_analytics_unity_file CPP_FILE CSHARP_FILE)
33+
add_custom_command(
34+
OUTPUT ${CSHARP_FILE}
35+
COMMAND ${FIREBASE_PYTHON_EXECUTABLE} "${CMAKE_CURRENT_LIST_DIR}/generate_constants.py"
36+
"--cpp_header=${CPP_FILE}"
37+
"--csharp_file=${CSHARP_FILE}"
38+
DEPENDS FIREBASE_ANALYTICS_GENERATED_HEADERS
39+
${CPP_FILE}
40+
COMMENT "Generating ${CSHARP_FILE}"
41+
)
42+
endfunction()
43+
44+
# Call the above function for all of the files to generate.
45+
generate_analytics_unity_file(
46+
"${analytics_cpp_generated_headers_dir}/event_names.h"
47+
"${analytics_csharp_generated_dir}/EventNames.cs"
48+
)
49+
generate_analytics_unity_file(
50+
"${analytics_cpp_generated_headers_dir}/parameter_names.h"
51+
"${analytics_csharp_generated_dir}/ParameterNames.cs"
52+
)
53+
generate_analytics_unity_file(
54+
"${analytics_cpp_generated_headers_dir}/user_property_names.h"
55+
"${analytics_csharp_generated_dir}/UserPropertyNames.cs"
56+
)
57+
add_custom_target(FIREBASE_UNITY_ANALYTICS_GENERATED_FILES
58+
DEPENDS
59+
${analytics_csharp_generated_dir}/EventNames.cs
60+
${analytics_csharp_generated_dir}/ParameterNames.cs
61+
${analytics_csharp_generated_dir}/UserPropertyNames.cs
62+
)
63+
2464
# Firebase Analytics CSharp files
2565
set(firebase_analytics_src
66+
src/Consent.cs
67+
src/FirebaseAnalytics.cs
68+
src/Parameter.cs
69+
${analytics_csharp_generated_dir}/EventNames.cs
70+
${analytics_csharp_generated_dir}/ParameterNames.cs
71+
${analytics_csharp_generated_dir}/UserPropertyNames.cs
2672
)
2773

2874
firebase_swig_add_library(firebase_analytics_swig
@@ -59,6 +105,7 @@ mono_add_library(firebase_analytics_cs
59105
${FIREBASE_PLATFORM_REF}
60106
DEPENDS
61107
firebase_analytics_swig
108+
FIREBASE_UNITY_ANALYTICS_GENERATED_FILES
62109
)
63110

64111
if(FIREBASE_IOS_BUILD)

analytics/generate_constants.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Convert C++ headers of Analytics constants to C# files."""
16+
17+
import datetime
18+
import os
19+
import re
20+
import subprocess
21+
22+
from absl import app
23+
from absl import flags
24+
25+
FLAGS = flags.FLAGS
26+
27+
# Required args
28+
flags.DEFINE_string('cpp_header', '', 'C++ header file containing a '
29+
'set of constants to convert to C#.')
30+
flags.register_validator(
31+
'cpp_header',
32+
lambda x: x and os.path.exists(x),
33+
message=('Must reference an existing C++ header file'))
34+
flags.DEFINE_string('csharp_file', '', 'Full path of the C# file to write '
35+
'out.')
36+
flags.register_validator(
37+
'csharp_file', lambda x: x, message='Output C# file must be specified')
38+
39+
CPP_NAMESPACE = 'namespace analytics'
40+
41+
DOC_REPLACEMENTS = [
42+
('static const char\*const k', 'public static string ')
43+
]
44+
45+
46+
def main(unused_argv):
47+
"""Convert the C++ file into C#, going line-by-line to edit it."""
48+
with open(FLAGS.cpp_header) as input_file:
49+
with open(FLAGS.csharp_file, 'w') as output_file:
50+
# Write the initial lines at the top
51+
output_file.write('// Copyright %s Google LLC\n\n' %
52+
str(datetime.date.today().year))
53+
output_file.write('namespace Firebase.Analytics {\n\n')
54+
output_file.write('public static partial class FirebaseAnalytics {\n\n')
55+
56+
found_namespace = False
57+
for line in input_file:
58+
line = line.rstrip()
59+
60+
# Ignore everything in the C++ file until inside the namespaces
61+
if not found_namespace:
62+
if re.search(CPP_NAMESPACE, line):
63+
found_namespace = True
64+
continue
65+
# Stop copying when finding the next namespace (we assume it is closing)
66+
if re.search(CPP_NAMESPACE, line):
67+
break
68+
69+
for replace_from, replace_to in DOC_REPLACEMENTS:
70+
if (re.search(replace_from, line)):
71+
line = re.sub(replace_from, replace_to, line)
72+
output_file.write(line + '\n')
73+
74+
# Write the lines at the end
75+
# Close the class
76+
output_file.write('}\n\n')
77+
# close the namespace
78+
output_file.write('}\n')
79+
80+
return 0
81+
82+
if __name__ == '__main__':
83+
app.run(main)

analytics/src/Consent.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
namespace Firebase.Analytics {
18+
19+
/// @brief The type of consent to set.
20+
///
21+
/// Supported consent types are mapped to corresponding constants in the Android
22+
/// and iOS SDKs. Omitting a type retains its previous status.
23+
public enum ConsentType {
24+
AdStorage = 0,
25+
AnalyticsStorage,
26+
AdUserData,
27+
AdPersonalization
28+
}
29+
30+
/// @brief The status value of the consent type.
31+
///
32+
/// Supported statuses are ConsentStatus.Granted and ConsentStatus.Denied.
33+
public enum ConsentStatus {
34+
Granted = 0,
35+
Denied
36+
}
37+
38+
}

0 commit comments

Comments
 (0)