Skip to content

Commit d5b49d1

Browse files
author
Riccardo Cipolleschi
committed
[Feat] Add support for .xcode.env file in init and doctor commands
1 parent e044e7b commit d5b49d1

File tree

9 files changed

+92
-16
lines changed

9 files changed

+92
-16
lines changed

packages/cli-doctor/src/tools/healthchecks/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import cocoaPods from './cocoaPods';
1111
import iosDeploy from './iosDeploy';
1212
import {Healthchecks, HealthCheckCategory} from '../../types';
1313
import loadConfig from '@react-native-community/cli-config';
14+
import xcodeEnv from './xcodeEnv';
1415

1516
export const HEALTHCHECK_TYPES = {
1617
ERROR: 'ERROR',
@@ -55,7 +56,7 @@ export const getHealthchecks = ({contributor}: Options): Healthchecks => {
5556
? {
5657
ios: {
5758
label: 'iOS',
58-
healthchecks: [xcode, cocoaPods, iosDeploy],
59+
healthchecks: [xcode, cocoaPods, iosDeploy, xcodeEnv],
5960
},
6061
}
6162
: {}),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {HealthCheckInterface} from '../../types';
2+
import fs from 'fs';
3+
import path from 'path';
4+
import {promisify} from 'util';
5+
import {findProjectRoot} from '@react-native-community/cli-tools';
6+
import {findPodfilePaths} from '@react-native-community/cli-platform-ios';
7+
8+
const xcodeEnvFile = '.xcode.env';
9+
const pathSeparator = '/';
10+
11+
function removeLastPathComponent(pathString: string): string {
12+
const components = pathString.split(pathSeparator);
13+
components.splice(components.length - 1, 1);
14+
return components.join(pathSeparator);
15+
}
16+
17+
function pathHasXcodeEnvFile(pathString: string): boolean {
18+
const xcodeEnvPath = pathString + pathSeparator + xcodeEnvFile;
19+
return fs.existsSync(xcodeEnvPath);
20+
}
21+
22+
function pathDoesNotHaveXcodeEnvFile(pathString: string): boolean {
23+
return !pathHasXcodeEnvFile(pathString);
24+
}
25+
26+
export default {
27+
label: '.xcode.env',
28+
description: 'File to customize Xcode environment',
29+
getDiagnostics: async () => {
30+
const projectRoot = findProjectRoot();
31+
const allPathsHasXcodeEnvFile = findPodfilePaths(projectRoot)
32+
.map((pathString) => {
33+
const basePath = removeLastPathComponent(pathString);
34+
return pathHasXcodeEnvFile(basePath);
35+
})
36+
.reduce((previousValue, currentValue) => previousValue && currentValue);
37+
return {
38+
needsToBeFixed: !allPathsHasXcodeEnvFile,
39+
};
40+
},
41+
runAutomaticFix: async () => {
42+
const templateXcodeEnv = '_xcode.env';
43+
const projectRoot = findProjectRoot();
44+
45+
const templateIosPath = path.dirname(
46+
require.resolve('react-native/template/ios'),
47+
);
48+
49+
const src = templateIosPath + templateXcodeEnv;
50+
const copyFileAsync = promisify(fs.copyFile);
51+
52+
findPodfilePaths(projectRoot)
53+
.map(removeLastPathComponent)
54+
// avoid overriding existing .xcode.env
55+
.filter(pathDoesNotHaveXcodeEnvFile)
56+
.forEach(async (pathString) => {
57+
const destFilePath = pathString + pathSeparator + xcodeEnvFile;
58+
await copyFileAsync(src, destFilePath);
59+
});
60+
},
61+
} as HealthCheckInterface;

packages/cli-doctor/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"references": [
88
{"path": "../tools"},
99
{"path": "../cli-types"},
10-
{"path": "../cli-config"}
10+
{"path": "../cli-config"},
11+
{"path": "../platform-ios"}
1112
]
1213
}

packages/cli/src/commands/init/editTemplate.ts

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const UNDERSCORED_DOTFILES = [
6969
'editorconfig',
7070
'bundle',
7171
'ruby-version',
72+
'xcode.env',
7273
];
7374

7475
async function processDotfiles(filePath: string) {

packages/cli/src/tools/generator/copyProjectTemplateAndReplace.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ function translateFilePath(filePath: string) {
148148
.replace('_prettierrc.js', '.prettierrc.js')
149149
.replace('_bundle', '.bundle')
150150
.replace('_ruby-version', '.ruby-version')
151-
.replace('_watchmanconfig', '.watchmanconfig');
151+
.replace('_watchmanconfig', '.watchmanconfig')
152+
.replace('_xcode.env', '.xcode.env');
152153
}
153154

154155
function upgradeFileContentChangedCallback(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
import glob from 'glob';
9+
10+
// These folders will be excluded from search to speed it up
11+
const GLOB_EXCLUDE_PATTERN = ['**/@(Pods|node_modules|Carthage)/**'];
12+
13+
export default function findAllPodfilePaths(cwd: string) {
14+
return glob.sync('**/Podfile', {
15+
cwd,
16+
ignore: GLOB_EXCLUDE_PATTERN,
17+
});
18+
}

packages/platform-ios/src/config/findPodfilePath.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,17 @@
77
*/
88

99
import {inlineString, logger} from '@react-native-community/cli-tools';
10-
import glob from 'glob';
1110
import path from 'path';
11+
import findAllPodfilePaths from './findAllPodfilePaths';
1212

1313
// Regexp matching all test projects
1414
const TEST_PROJECTS = /test|example|sample/i;
1515

1616
// Base iOS folder
1717
const IOS_BASE = 'ios';
1818

19-
// These folders will be excluded from search to speed it up
20-
const GLOB_EXCLUDE_PATTERN = ['**/@(Pods|node_modules|Carthage)/**'];
21-
2219
export default function findPodfilePath(cwd: string) {
23-
/**
24-
* First, we're going to look for all Podfiles within the `cwd`
25-
*/
26-
const podfiles = glob
27-
.sync('**/Podfile', {
28-
cwd,
29-
ignore: GLOB_EXCLUDE_PATTERN,
30-
})
20+
const podfiles = findAllPodfilePaths(cwd)
3121
/**
3222
* Then, we will run a simple test to rule out most example projects,
3323
* unless they are located in a `ios` folder

packages/platform-ios/src/config/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import fs from 'fs';
1010
import findPodfilePath from './findPodfilePath';
1111
import findXcodeProject from './findXcodeProject';
1212
import findPodspec from './findPodspec';
13+
import findAllPodfilePaths from './findAllPodfilePaths';
1314
import {
1415
IOSProjectParams,
1516
IOSDependencyParams,
@@ -65,3 +66,5 @@ export function dependencyConfig(
6566
scriptPhases: userConfig.scriptPhases || [],
6667
};
6768
}
69+
70+
export const findPodfilePaths = findAllPodfilePaths;

packages/platform-ios/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
*/
44

55
export {default as commands} from './commands';
6-
export {projectConfig, dependencyConfig} from './config';
6+
export {projectConfig, dependencyConfig, findPodfilePaths} from './config';

0 commit comments

Comments
 (0)