1# Copyright (c) Meta Platforms, Inc. and affiliates.
2#
3# This source code is licensed under the MIT license found in the
4# LICENSE file in the root directory of this source tree.
5
6# It builds the codegen CLI if it is not present
7#
8# Parameters:
9# - react_native_path: the path to the react native installation
10# - relative_installation_root: the path to the relative installation root of the pods
11# - dir_manager: a class that implements the `Dir` interface. Defaults to `Dir`, the Dependency can be injected for testing purposes.
12# @throws an error if it could not find the codegen folder.
13def build_codegen!(react_native_path, relative_installation_root, dir_manager: Dir)
14    codegen_repo_path = "#{relative_installation_root}/#{react_native_path}/../react-native-codegen";
15    codegen_npm_path = "#{relative_installation_root}/#{react_native_path}/../@react-native/codegen";
16    codegen_cli_path = ""
17
18    if dir_manager.exist?(codegen_repo_path)
19      codegen_cli_path = codegen_repo_path
20    elsif dir_manager.exist?(codegen_npm_path)
21      codegen_cli_path = codegen_npm_path
22    else
23      raise "[codegen] Could not find react-native-codegen."
24    end
25
26    if !dir_manager.exist?("#{codegen_cli_path}/lib")
27      Pod::UI.puts "[Codegen] building #{codegen_cli_path}."
28      system("#{codegen_cli_path}/scripts/oss/build.sh")
29    end
30  end
31
32# It generates an empty `ThirdPartyProvider`, required by Fabric to load the components
33#
34# Parameters:
35# - react_native_path: path to the react native framework
36# - new_arch_enabled: whether the New Architecture is enabled or not
37# - dir_manager: a class that implements the `Dir` interface. Defaults to `Dir`, the Dependency can be injected for testing purposes.
38# - file_manager: a class that implements the `File` interface. Defaults to `File`, the Dependency can be injected for testing purposes.
39def checkAndGenerateEmptyThirdPartyProvider!(react_native_path, new_arch_enabled, dir_manager: Dir, file_manager: File)
40    return if new_arch_enabled
41
42    relative_installation_root = Pod::Config.instance.installation_root.relative_path_from(Pathname.pwd)
43
44    output_dir = "#{react_native_path}/React/Fabric"
45
46    provider_h_path = "#{output_dir}/RCTThirdPartyFabricComponentsProvider.h"
47    provider_cpp_path ="#{output_dir}/RCTThirdPartyFabricComponentsProvider.mm"
48
49    if(!file_manager.exist?(provider_h_path) || !file_manager.exist?(provider_cpp_path))
50        # build codegen
51
52
53        # Just use a temp empty schema list.
54        temp_schema_list_path = "#{output_dir}/tmpSchemaList.txt"
55        file_manager.open(temp_schema_list_path, 'w') do |f|
56            f.write('[]')
57            f.fsync
58        end
59
60        Pod::UI.puts '[Codegen] generating an empty RCTThirdPartyFabricComponentsProvider'
61        Pod::Executable.execute_command(
62        'node',
63        [
64            "#{relative_installation_root}/#{react_native_path}/scripts/generate-provider-cli.js",
65            "--platform", 'ios',
66            "--schemaListPath", temp_schema_list_path,
67            "--outputDir", "#{output_dir}"
68        ])
69        file_manager.delete(temp_schema_list_path) if file_manager.exist?(temp_schema_list_path)
70    end
71end
72
73def run_codegen_ABI49_0_0!(
74  app_path,
75  config_file_dir,
76  new_arch_enabled: false,
77  disable_codegen: false,
78  react_native_path: "../node_modules/react-native",
79  fabric_enabled: false,
80  hermes_enabled: true,
81  codegen_output_dir: 'build/generated/ios',
82  config_key: 'codegenConfig',
83  package_json_file: '~/app/package.json',
84  folly_version: '2021.07.22.00',
85  codegen_utils: CodegenUtils.new()
86  )
87
88  if new_arch_enabled
89    codegen_utils.use_react_native_codegen_discovery!(
90      disable_codegen,
91      app_path,
92      :react_native_path => react_native_path,
93      :fabric_enabled => fabric_enabled,
94      :hermes_enabled => hermes_enabled,
95      :config_file_dir => config_file_dir,
96      :codegen_output_dir => codegen_output_dir,
97      :config_key => config_key,
98      :folly_version => folly_version
99    )
100  else
101    # Generate a podspec file for generated files.
102    # This gets generated in use_react_native_codegen_discovery when codegen discovery is enabled.
103    react_codegen_spec = codegen_utils.get_react_codegen_spec(
104      package_json_file,
105      :fabric_enabled => fabric_enabled,
106      :hermes_enabled => hermes_enabled
107    )
108    codegen_utils.generate_react_codegen_podspec!(react_codegen_spec, codegen_output_dir)
109  end
110end
111