How to add post_install script in my podspec file? - ios

How can I add post_install script in podspec file of my custom cocoapod and execute code below?
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '14.0'
end
end
end
I have an issue where I can not get a successful build when running pod spec lint because of dependencies of my cocoapod. When I remove those dependencies and run pod spec lint I get a successful build.
On images below is shown when script fails:
Image 1
Image 2
This is the reason why I wan't to execute post_install in podspec file. I'm not sure will this resolve the issue or should I look for some other solutions.
I've tried adding post_install like this but I get an error:
s.post_install do |installer|
# your script here
end
but I get an error:
[!] Invalid `MyLib.podspec` file: undefined method `post_install' for #<Pod::Specification name="MyLib">

Related

iOS: How to get all pod names in Podfile in a ruby script or shell script?

target_names = installer.pod_targets.map { |target| target.name }in post_install can get all dependent pods, but it include sub pod of pod in Podfile.
Thanks!
I had the answer,below code in post_install can do it.
podfile = Pod::Podfile.from_file(Pathname.new('Podfile'))
podfile_pod_items = podfile.dependencies.map do |pod|
pod.name
end

How can I downgrade the rxdart plugin in flutter?

I followed this as shown (only first 3 minutes are relevant for my question): https://m.youtube.com/watch?v=MYHVyl-juUk
When I try to run it this appears in the console:
Because geoflutterfire 2.0.3+2 depends on rxdart ^0.20.0 and no versions of geoflutterfire match >2.0.3+2 <3.0.0, geoflutterfire ^2.0.3+2 requires rxdart ^0.20.0.
So, because my_app depends on both rxdart ^0.21.0 and geoflutterfire ^2.0.3+2, version solving failed.
The current Version (and the Version I installed) is rxdart 0.21.0. I tried to downgrade it by changing it to 0.20.0 and flutter packages get, but it didn’t work.
Btw, it’s my first time experimenting with plugins, so I don’t have any clue about solving that kind of problems...
What can I do to make this work?
Kind regards
UPDATE:
After I tried this:
rxdart: any
the console threw this:
2019-04-08 12:06:04.178 defaults[36786:354646]
The domain/default pair of (/Users/privat/Documents/Dev/my_app/ios/Runner/Info, CFBundleIdentifier) does not exist
Xcode's output:
↳
/Library/flutter/.pub-cache/hosted/pub.dartlang.org/geoflutterfire-2.0.3+2/ios/Classes/GeoflutterfirePlugin.m:2:9: fatal error:
'geoflutterfire/geoflutterfire-Swift.h' file not found
#import <geoflutterfire/geoflutterfire-Swift.h>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Then I edited the Podfile Code:
Code that needed to be added or edited are marked with "** ...... **"
target 'Runner' do
** use_frameworks! **
........ ......
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO'
** config.build_settings['SWIFT_VERSION'] = '4.0' **
end
end
end
Then I deleted my Podfile.lock and tried again, and the app was compiled successfully. Thanks to C4C!
rxdart:any
Give it a try and let us know what happens
please try this edits in Podfile
Code needs to be added or edited are marked with "** ...... **"
target 'Runner' do
** use_frameworks! **
........ ......
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO'
** config.build_settings['SWIFT_VERSION'] = '4.0' **
end
end
end
delete your Podfile.lock and try
Older versions can be found here - https://pub.dartlang.org/packages/rxdart#-versions-tab-
If you are receiving AndroidX errors due to the older version then please check this page to resolve AndroidX issues - https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility

Script to modify .xcconfig file generated by Cocoapods

How can I modify the OTHER_LDFLAGS field in the .xcconfig generated at pod install step?
End goal: Only weakly link certain Pods, so they can be loaded with dlopen at runtime.
I noticed in the Pod-Target.debug.xcconfig generated at pod install, it has this: OTHER_LDFLAGS = $(inherited) -framework "AFNetworking", if I change that to -weak_framework, it does what I want to do.
This Cocoapods issue talks about doing it through a post_install hook, however config.build_settings['OTHER_LDFLAGS'] doesn't write to that file.
PS: I know that the use of dlopen is not recommended, however I'm working with conflicting hardware libraries where I cannot have two loaded at the same time.
Add this code to your Podfile and run pod install again
post_install do |installer|
workDir = Dir.pwd
xcconfigFilename = "#{workDir}/Pods/Target Support Files/Pods-Target/Pod-Target.debug.xcconfig"
xcconfig = File.read(xcconfigFilename)
newXcconfig = xcconfig.gsub(/-framework "AFNetworking"/, "-weak_framework \"AFNetworking\"")
File.open(xcconfigFilename, "w") { |file| file << newXcconfig }
end
A little modification of answer by VoidLess which I think might be useful:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
xcconfig_path = config.base_configuration_reference.real_path
xcconfig = File.read(xcconfig_path)
xcconfig_mod = xcconfig.gsub(/-framework "YourFramework"/, "-weak_framework \"YourFramework\"")
File.open(xcconfig_path, "w") { |file| file << xcconfig_mod }
end
end
end

How to exclude Pods from Code Coverage in Xcode

Is there a way to exclude Pods from Code Coverage?
I would like to see Code Coverage only for the code I've written.
Not that it should matter, but I'm using Xcode 8.
These steps will help:
1. add these lines to Podfile
# Disable Code Coverage for Pods projects
post_install do |installer_representation|
installer_representation.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'NO'
end
end
end
2. run pod install
Now you won't see pods in test coverage.
Note: It only excludes Objective-c pods but not Swift
XCode 10 Update
In Xcode 10 you can set which targets you want to enable code coverage for in
Edit Schemes > Test > Options
Just select 'Gather coverage for some targets' and add your main project.
To disable coverage for swift code you can use a wrapper for SWIFT_EXEC (I verified this so far with Xcode 9.3). Hence the complete solution (incl. Swift) would be the following:
Append to your Podfile (and invoke pod install after that):
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |configuration|
configuration.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'NO'
configuration.build_settings['SWIFT_EXEC'] = '$(SRCROOT)/SWIFT_EXEC-no-coverage'
end
end
end
Place the following script (name it SWIFT_EXEC-no-coverage) at the root of your source tree (chmod +x as necessary):
#! /usr/bin/perl -w
use strict;
use Getopt::Long qw(:config pass_through);
my $profile_coverage_mapping;
GetOptions("profile-coverage-mapping" => \$profile_coverage_mapping);
exec(
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc",
#ARGV);
Here's a link to the corresponding gist: https://gist.github.com/grigorye/f6dfaa9f7bd9dbb192fe25a6cdb419d4
Click on your Pods project in the Project Navigator on the left
On the right hand side, open project and target list if it's not already open; then click on the Pods project name (NOT the targets).
Click Build Settings.
In the search bar, search for "CLANG_ENABLE_CODE_COVERAGE".
Change "Enable Code Coverage Support" to NO.
Re-run test.
If you are developing a pod and want to have code coverage just for yours:
# Disable Code Coverage for Pods projects except MyPod
post_install do |installer_representation|
installer_representation.pods_project.targets.each do |target|
if target.name == 'MyPod'
target.build_configurations.each do |config|
config.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'YES'
end
else
target.build_configurations.each do |config|
config.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'NO'
end
end
end
end
Based on answer of #Tung Fam adding exclusion list for some pods
# Disable Code Coverage for projects listed in excluded_pods
excluded_pods = ['Pod1', 'Pod2', 'Pod3']
post_install do |installer_representation|
installer_representation.pods_project.targets.each do |target|
next if !excluded_pods.include?(target.name)
target.build_configurations.each do |config|
config.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'NO'
end
end
end

How to add a folder reference in my the Pod project in post install?

So, I am trying to add a folder reference in a target that was created by cocoapods.
I found this solution, but it is not working for me. https://github.com/CocoaPods/CocoaPods/issues/3521
I am on cocoapods 0.39. And I only changed the projects to pods_project since that was deprecated.
Also how do you enable logging when running pod update. I tried pod update --verbose, but my puts is not being displayed. :/
=========== UPDATED ===========
post_install do |installer|
puts "printing tests"
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if target.name == "TargetWhereToAddTheFolderOrFileReference"
source_files = target.source_build_phase.files
folderReference = installer.project.add_file_reference("/AbsolutePath/To/FileOrFolder", installer.project.pod_group("PodGroupWhereToAddTheFileOrFolder"), true)
target.source_build_phase.add_file_reference(btsdata, true)
end
end
end
end
post_install do |installer|
// another post install script is here.
end
I've attached the script that is failing plus there was another post_install script below it. That puts "printing tests" is not being called as well....
=========== UPDATED ENDS ===========
Oh, I think the script will only execute the last post_install and not
both. Can you put the code inside the first post_install into the last one?

Resources