<commit_before>import { EOL } from "os";
import * as path from "path";
import * as _ from "lodash";
import { PlatformTypes, PODFILE_NAME, NS_BASE_PODFILE } from "../constants";
import { regExpEscape, getHash } from "../common/helpers";
import { IPluginData } from "../definitions/plugins";
import {
	IRubyFunction,
	IProjectData,
	ICocoaPodsPlatformManager,
	ICocoaPodsService,
	IPodfilePlatformData,
} from "../definitions/project";
import { IPlatformData } from "../definitions/platform";
import { IConfiguration, IXcconfigService } from "../declarations";
import {
	IFileSystem,
	IChildProcess,
	IErrors,
	ISpawnResult,
} from "../common/declarations";
import { injector } from "../common/yok";

export class CocoaPodsService implements ICocoaPodsService {
	private static PODFILE_POST_INSTALL_SECTION_NAME = "post_install";
	private static INSTALLER_BLOCK_PARAMETER_NAME = "installer";
	private getCocoaPodsFromPodfile: Function;
	constructor(
		private $cocoaPodsPlatformManager: ICocoaPodsPlatformManager,
		private $fs: IFileSystem,
		private $childProcess: IChildProcess,
		private $errors: IErrors,
		private $logger: ILogger,
		private $config: IConfiguration,
		private $xcconfigService: IXcconfigService
	) {
		this.getCocoaPodsFromPodfile = _.memoize(
			this._getCocoaPodsFromPodfile,
			getHash
		);
	}

	public getPodfileHeader(targetName: string): string {
		return `use_frameworks!${EOL}${EOL}target "${targetName}" do${EOL}`;
	}

	public getPodfileFooter(): string {
		return `${EOL}end`;
	}

	public getProjectPodfilePath(projectRoot: string): string {
		return path.join(projectRoot, PODFILE_NAME);
	}

	public async executePodInstall(
		projectRoot: string,
		xcodeProjPath: string
	): Promise<ISpawnResult> {
		this.$logger.info("Installing pods...");
		let podTool = this.$config.USE_POD_SANDBOX ? "sandbox-pod" : "pod";
		const args = ["install"];

		if (process.platform === "darwin" && process.arch === "arm64") {
			this.$logger.trace("Running on arm64 - running pod through rosetta2.");
			args.unshift(podTool);
			args.unshift("-x86_64");
			podTool = "arch";
		}
		// cocoapods print a lot of non-error information on stderr. Pipe the `stderr` to `stdout`, so we won't polute CLI's stderr output.
		const podInstallResult = await this.$childProcess.spawnFromEvent(
			podTool,
			args,
			"close",<commit_msg>fix(m1): cocoapods command when installed natively (#5584)<commit_after>import { EOL } from "os";
import * as path from "path";
import * as _ from "lodash";
import { PlatformTypes, PODFILE_NAME, NS_BASE_PODFILE } from "../constants";
import { regExpEscape, getHash } from "../common/helpers";
import { IPluginData } from "../definitions/plugins";
import {
	IRubyFunction,
	IProjectData,
	ICocoaPodsPlatformManager,
	ICocoaPodsService,
	IPodfilePlatformData,
} from "../definitions/project";
import { IPlatformData } from "../definitions/platform";
import { IConfiguration, IXcconfigService } from "../declarations";
import {
	IFileSystem,
	IChildProcess,
	IErrors,
	ISpawnResult,
} from "../common/declarations";
import { injector } from "../common/yok";

export class CocoaPodsService implements ICocoaPodsService {
	private static PODFILE_POST_INSTALL_SECTION_NAME = "post_install";
	private static INSTALLER_BLOCK_PARAMETER_NAME = "installer";
	private getCocoaPodsFromPodfile: Function;
	constructor(
		private $cocoaPodsPlatformManager: ICocoaPodsPlatformManager,
		private $fs: IFileSystem,
		private $childProcess: IChildProcess,
		private $errors: IErrors,
		private $logger: ILogger,
		private $config: IConfiguration,
		private $xcconfigService: IXcconfigService
	) {
		this.getCocoaPodsFromPodfile = _.memoize(
			this._getCocoaPodsFromPodfile,
			getHash
		);
	}

	public getPodfileHeader(targetName: string): string {
		return `use_frameworks!${EOL}${EOL}target "${targetName}" do${EOL}`;
	}

	public getPodfileFooter(): string {
		return `${EOL}end`;
	}

	public getProjectPodfilePath(projectRoot: string): string {
		return path.join(projectRoot, PODFILE_NAME);
	}

	public async executePodInstall(
		projectRoot: string,
		xcodeProjPath: string
	): Promise<ISpawnResult> {
		this.$logger.info("Installing pods...");
		let podTool = this.$config.USE_POD_SANDBOX ? "sandbox-pod" : "pod";
		const args = ["install"];

		if (process.platform === "darwin" && process.arch === "arm64") {
			// check if pod is installed as an x86_64 binary or a native arm64 one
			// we run the following:
			// arch -x86_64 pod --version
			// if it's an arm64 binary, we'll get something like this as a result:
			// arch: posix_spawnp: pod: Bad CPU type in executable
			// in which case, we should run it natively.
			const res: string = await this.$childProcess
				.exec("arch -x86_64 pod --version", null, {
					showStderr: true,
				})
				.then((res) => res.stdout + " " + res.stderr)
				.catch((err) => err.message);

			if (!res.includes("Bad CPU type in executable")) {
				this.$logger.trace(
					"Running on arm64 but pod is installed under rosetta2 - running pod through rosetta2"
				);
				args.unshift(podTool);
				args.unshift("-x86_64");
				podTool = "arch";
			}
		}
		// cocoapods print a lot of non-error information on stderr. Pipe the `stderr` to `stdout`, so we won't polute CLI's stderr output.
		const podInstallResult = await this.$childProcess.spawnFromEvent(
			podTool,
			args,
			"close",
{'fraction_non_alphanumeric': 0.12346139500186498, 'fraction_numerical': 0.007459903021260724, 'mean_word_length': 4.563278008298755, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7200275', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1772, 'n_words': 446}