From e7b7ba382a97851e2a3e09927d4e9848d9720d83 Mon Sep 17 00:00:00 2001 From: Harris Borawski Date: Mon, 8 Nov 2021 13:51:11 -0800 Subject: [PATCH 1/2] add config option to generate dependency labels for an external repository --- lib/cocoapods/bazel.rb | 8 +++++--- lib/cocoapods/bazel/config.rb | 6 +++++- lib/cocoapods/bazel/target.rb | 19 ++++++++++++------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/cocoapods/bazel.rb b/lib/cocoapods/bazel.rb index 256c9f0..6e42e50 100644 --- a/lib/cocoapods/bazel.rb +++ b/lib/cocoapods/bazel.rb @@ -26,7 +26,7 @@ def self.post_install(installer:) build_files = Hash.new { |h, k| h[k] = StarlarkCompiler::BuildFile.new(workspace: workspace, package: k) } installer.pod_targets.each do |pod_target| package = sandbox.pod_dir(pod_target.pod_name).relative_path_from(workspace).to_s - if package.start_with?('..') + if package.start_with?('..') and not config.external_repository raise Informative, <<~MSG Bazel does not support Pod located outside of current workspace: \"#{package}\". To fix this, you can move the Pod into workspace, @@ -44,7 +44,8 @@ def self.post_install(installer:) pod_target, fa.spec, default_xcconfigs, - config.experimental_deps_debug_and_release + config.experimental_deps_debug_and_release, + config.external_repository ) end @@ -53,7 +54,8 @@ def self.post_install(installer:) pod_target, nil, default_xcconfigs, - config.experimental_deps_debug_and_release + config.experimental_deps_debug_and_release, + config.external_repository ) bazel_targets = [default_target] + targets_without_library_specification diff --git a/lib/cocoapods/bazel/config.rb b/lib/cocoapods/bazel/config.rb index 3e2a570..229603d 100644 --- a/lib/cocoapods/bazel/config.rb +++ b/lib/cocoapods/bazel/config.rb @@ -54,7 +54,8 @@ class Config default_xcconfigs: {}.freeze, features: { experimental_deps_debug_and_release: false - } + }, + external_repository: false }.with_indifferent_access.freeze private_constant :DEFAULTS @@ -110,6 +111,9 @@ def default_xcconfigs def experimental_deps_debug_and_release to_h[:features][:experimental_deps_debug_and_release] end + def external_repository + to_h[:external_repository] + end end end end diff --git a/lib/cocoapods/bazel/target.rb b/lib/cocoapods/bazel/target.rb index 1692eee..71e2b4f 100644 --- a/lib/cocoapods/bazel/target.rb +++ b/lib/cocoapods/bazel/target.rb @@ -27,10 +27,10 @@ def add(name, value, defaults: nil) include XCConfigResolver - attr_reader :installer, :pod_target, :file_accessors, :non_library_spec, :label, :package, :default_xcconfigs, :resolved_xconfig_by_config - private :installer, :pod_target, :file_accessors, :non_library_spec, :label, :package, :default_xcconfigs, :resolved_xconfig_by_config + attr_reader :installer, :pod_target, :file_accessors, :non_library_spec, :label, :package, :default_xcconfigs, :resolved_xconfig_by_config, :external_repository + private :installer, :pod_target, :file_accessors, :non_library_spec, :label, :package, :default_xcconfigs, :resolved_xconfig_by_config, :external_repository - def initialize(installer, pod_target, non_library_spec = nil, default_xcconfigs = {}, experimental_deps_debug_and_release = false) + def initialize(installer, pod_target, non_library_spec = nil, default_xcconfigs = {}, experimental_deps_debug_and_release = false, external_repository = false) @installer = installer @pod_target = pod_target @file_accessors = non_library_spec ? pod_target.file_accessors.select { |fa| fa.spec == non_library_spec } : pod_target.file_accessors.select { |fa| fa.spec.library_specification? } @@ -41,16 +41,21 @@ def initialize(installer, pod_target, non_library_spec = nil, default_xcconfigs @default_xcconfigs = default_xcconfigs @resolved_xconfig_by_config = {} @experimental_deps_debug_and_release = experimental_deps_debug_and_release + @external_repository = external_repository end def bazel_label(relative_to: nil) package_basename = File.basename(package) + packageName = package + if external_repository + packageName = package.gsub('Pods/', '@Pods//') + end if package == relative_to ":#{label}" elsif package_basename == label - "//#{package}" + "#{packageName}" else - "//#{package}:#{label}" + "#{packageName}:#{label}" end end @@ -67,7 +72,7 @@ def test_host end app_spec, app_target = *app_host_info - Target.new(installer, app_target, app_spec, {}, @experimental_deps_debug_and_release) + Target.new(installer, app_target, app_spec, {}, @experimental_deps_debug_and_release, @external_repository) end def type @@ -97,7 +102,7 @@ def dependent_targets_by_config raise "Unhandled: #{non_library_spec.spec_type}" end - targets.transform_values { |v| v.uniq.map { |target| self.class.new(installer, target) } } + targets.transform_values { |v| v.uniq.map { |target| self.class.new(installer, target, nil, {}, false, @external_repository) } } end def product_module_name From 1fdcdcef44de94b836e08cebb4061e32f8db1010 Mon Sep 17 00:00:00 2001 From: Harris Borawski Date: Mon, 13 Dec 2021 10:49:56 -0800 Subject: [PATCH 2/2] move external_repository into experimental features --- lib/cocoapods/bazel/config.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/cocoapods/bazel/config.rb b/lib/cocoapods/bazel/config.rb index 229603d..1ab55eb 100644 --- a/lib/cocoapods/bazel/config.rb +++ b/lib/cocoapods/bazel/config.rb @@ -8,7 +8,7 @@ class Config # When enabled cocoapods-bazel will add one additional config_setting for the 'deps' attribute only # containing both 'debug' and 'release' dependencies. # - # In other works when this flag is active cocoapods-bazel will continue to create these: + # In other words when this flag is active cocoapods-bazel will continue to create these: # # - //Pods/cocoapods-bazel:debug # - //Pods/cocoapods-bazel:release @@ -40,7 +40,16 @@ class Config # This might be ok for some teams but it prevents others that are interested in using cocoapods-bazel to migrate to Bazel and eventually stop # depending on cocoapods. If the generated BUILD files don't contain "all" states and a 'pod install' is always required it's not trivial how to eventually treat the # BUILD files as source of truth. - :experimental_deps_debug_and_release + :experimental_deps_debug_and_release, + # When enabled, cocoapods-bazel will generate pod dependencies with labels pointing to an external "Pods" repository + # For Example: + # + # //Pods/SomePod + # + # Becomes + # + # @Pods//SomePod + :external_repository ].freeze private_constant :PLUGIN_KEY DEFAULTS = { @@ -53,9 +62,9 @@ class Config buildifier: true, default_xcconfigs: {}.freeze, features: { - experimental_deps_debug_and_release: false + experimental_deps_debug_and_release: false, + external_repository: false }, - external_repository: false }.with_indifferent_access.freeze private_constant :DEFAULTS @@ -112,7 +121,7 @@ def experimental_deps_debug_and_release to_h[:features][:experimental_deps_debug_and_release] end def external_repository - to_h[:external_repository] + to_h[:features][:external_repository] end end end