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# @parameter react_native_path: the path to the react native installation
9# @parameter relative_installation_root: the path to the relative installation root of the pods
10# @throws an error if it could not find the codegen folder.
11def build_codegen!(react_native_path, relative_installation_root)
12    codegen_repo_path = "#{relative_installation_root}/#{react_native_path}/packages/react-native-codegen";
13    codegen_npm_path = "#{relative_installation_root}/#{react_native_path}/../react-native-codegen";
14    codegen_cli_path = ""
15
16    if Dir.exist?(codegen_repo_path)
17      codegen_cli_path = codegen_repo_path
18    elsif Dir.exist?(codegen_npm_path)
19      codegen_cli_path = codegen_npm_path
20    else
21      raise "[codegen] Couldn't not find react-native-codegen."
22    end
23
24    if !Dir.exist?("#{codegen_cli_path}/lib")
25      Pod::UI.puts "[Codegen] building #{codegen_cli_path}."
26      system("#{codegen_cli_path}/scripts/oss/build.sh")
27    end
28  end
29
30# It generates an empty `ThirdPartyProvider`, required by Fabric to load the components
31#
32# @parameter react_native_path: path to the react native framework
33# @parameter new_arch_enabled: whether the New Architecture is enabled or not
34# @parameter codegen_output_dir: the output directory for the codegen
35def checkAndGenerateEmptyThirdPartyProvider!(react_native_path, new_arch_enabled, codegen_output_dir)
36    return if new_arch_enabled
37
38    relative_installation_root = Pod::Config.instance.installation_root.relative_path_from(Pathname.pwd)
39
40    output_dir = "#{relative_installation_root}/#{codegen_output_dir}"
41
42    provider_h_path = "#{output_dir}/RCTThirdPartyFabricComponentsProvider.h"
43    provider_cpp_path ="#{output_dir}/RCTThirdPartyFabricComponentsProvider.cpp"
44
45    if(!File.exist?(provider_h_path) || !File.exist?(provider_cpp_path))
46        # build codegen
47        build_codegen!(react_native_path, relative_installation_root)
48
49        # Just use a temp empty schema list.
50        temp_schema_list_path = "#{output_dir}/tmpSchemaList.txt"
51        File.open(temp_schema_list_path, 'w') do |f|
52            f.write('[]')
53            f.fsync
54        end
55
56        Pod::UI.puts '[Codegen] generating an empty RCTThirdPartyFabricComponentsProvider'
57        Pod::Executable.execute_command(
58        'node',
59        [
60            "#{relative_installation_root}/#{react_native_path}/scripts/generate-provider-cli.js",
61            "--platform", 'ios',
62            "--schemaListPath", temp_schema_list_path,
63            "--outputDir", "#{output_dir}"
64        ])
65        File.delete(temp_schema_list_path) if File.exist?(temp_schema_list_path)
66    end
67end
68
69def run_codegen!(
70  app_path,
71  config_file_dir,
72  new_arch_enabled: false,
73  disable_codegen: false,
74  react_native_path: "../node_modules/react-native",
75  fabric_enabled: false,
76  codegen_output_dir: 'build/generated/ios',
77  config_key: 'codegenConfig',
78  package_json_file: '~/app/package.json',
79  folly_version: '2021.07.22.00',
80  codegen_utils: CodegenUtils.new()
81  )
82  if new_arch_enabled
83    codegen_utils.use_react_native_codegen_discovery!(
84      disable_codegen,
85      app_path,
86      :react_native_path => react_native_path,
87      :fabric_enabled => fabric_enabled,
88      :config_file_dir => config_file_dir,
89      :codegen_output_dir => codegen_output_dir,
90      :config_key => config_key,
91      :folly_version => folly_version
92    )
93  else
94    # Generate a podspec file for generated files.
95    # This gets generated in use_react_native_codegen_discovery when codegen discovery is enabled.
96    react_codegen_spec = codegen_utils.get_react_codegen_spec(
97      package_json_file,
98      :fabric_enabled => fabric_enabled
99    )
100    codegen_utils.generate_react_codegen_podspec!(react_codegen_spec, codegen_output_dir)
101  end
102end
103