Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to add pod dependency to a target? #1

Open
adrianso opened this issue Jun 13, 2023 · 2 comments
Open

Question: How to add pod dependency to a target? #1

adrianso opened this issue Jun 13, 2023 · 2 comments

Comments

@adrianso
Copy link

Hi @EvanBacon , first of all thanks and congratulations to creating this great library. I have been looking for something like for a while, and this is a lot closer to anything else that I have come across.

When create native Apple targets in Swift, one of the common things to do is for them to have their own Pod dependencies. However, it is not immediately obvious how to add one based on the examples provided. Am I missing something obvious?

This is for a project using Expo Prebuild.

@maxmandia
Copy link

@EvanBacon :)

@Armster15
Copy link

Created a config plugin to achieve this :)

with-add-pod-deps-to-targets.ts

import { withDangerousMod, type ConfigPlugin } from "@expo/config-plugins";
import { mergeContents } from "@expo/config-plugins/build/utils/generateCode";
import fs from "node:fs";
import path from "node:path";

const SRC_TO_ADD_TO_PODFILE = `
target 'widgets' do
  pod 'KeychainSwift', '~> 20.0'
end

target 'siriIntent' do
  pod 'KeychainSwift', '~> 20.0'
end
`;

export const withAddPodDepsToTargets: ConfigPlugin = (config) => {
  return withDangerousMod(config, [
    "ios",
    async (config) => {
      const file = path.join(config.modRequest.platformProjectRoot, "Podfile");
      const contents = await fs.promises.readFile(file, "utf8");
      await fs.promises.writeFile(file, addPodDepsToTargets(contents), "utf8");
      return config;
    },
  ]);
};

function addPodDepsToTargets(src: string) {
  return mergeContents({
    tag: `with-add-pod-deps-to-targets`,
    src,
    newSrc: SRC_TO_ADD_TO_PODFILE.trim(),
    anchor: /target ['"]([^'"]*)['"] do/,
    offset: 0,
    comment: "#",
  }).contents;
}

app.config.ts

import "esbuild-register"; // So we can import TypeScript files

const config: ExpoConfig = {
    // ...
    plugins: [
        require("./plugins/with-add-pod-deps-to-targets").withAddPodDepsToTargets
    ]
}

export default config;

To add stuff to your Podfile edit the SRC_TO_ADD_TO_PODFILE in the config plugin

For more information on the syntax of the Podfile, https://guides.cocoapods.org/using/the-podfile.html is a great resource

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants