Skip to content

Commit 1673c57

Browse files
rozelefacebook-github-bot
authored andcommitted
Uses a single code path to link and unlink all platforms
Summary: This commit removes special cases for linking iOS and Android platforms. A previous commit opened up link and other commands for other platforms to provide their own behaviors. It left special cases in tact for iOS and Android. This PR removes the special case. - Added jest tests related to the link command. - Ran the `link` and `unlink` commands for iOS and Android and confirmed no changes. #17745 <!-- Help reviewers and the release process by writing your own release notes **INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.** CATEGORY [----------] TYPE [ CLI ] [-------------] LOCATION [ DOCS ] [ BREAKING ] [-------------] [ GENERAL ] [ BUGFIX ] [-{Component}-] [ INTERNAL ] [ ENHANCEMENT ] [ {File} ] [ IOS ] [ FEATURE ] [ {Directory} ] |-----------| [ ANDROID ] [ MINOR ] [ {Framework} ] - | {Message} | [----------] [-------------] [-------------] |-----------| [CATEGORY] [TYPE] [LOCATION] - MESSAGE EXAMPLES: [IOS] [BREAKING] [FlatList] - Change a thing that breaks other things [ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput [CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with [DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word [GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position [INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see --> [CLI][FEATURE][local-cli/link/link.js] - Removes special cases for linking in iOS and Android. Closes #17961 Differential Revision: D6975951 Pulled By: hramos fbshipit-source-id: 8dd5da35619e2124ce4b3b18db8b694757792363
1 parent 6893a26 commit 1673c57

13 files changed

+97
-154
lines changed

Diff for: local-cli/core/android/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,5 @@ exports.dependencyConfig = function dependencyConfigAndroid(folder, userConfig)
126126

127127
return { sourceDir, folder, manifest, packageImportPath, packageInstance };
128128
};
129+
130+
exports.linkConfig = require('../../link/android');

Diff for: local-cli/core/ios/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,5 @@ exports.projectConfig = function projectConfigIOS(folder, userConfig) {
5757
};
5858

5959
exports.dependencyConfig = exports.projectConfig;
60+
61+
exports.linkConfig = require('../../link/ios');

Diff for: local-cli/link/__tests__/link.spec.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ describe('link', () => {
8181
it('should register native module when android/ios projects are present', (done) => {
8282
const registerNativeModule = sinon.stub();
8383
const dependencyConfig = {android: {}, ios: {}, assets: [], commands: {}};
84+
const androidLinkConfig = require('../android');
85+
const iosLinkConfig = require('../ios');
8486
const config = {
85-
getPlatformConfig: () => ({ios: {}, android: {}}),
87+
getPlatformConfig: () => ({ios: { linkConfig: iosLinkConfig }, android: { linkConfig: androidLinkConfig }}),
8688
getProjectConfig: () => ({android: {}, ios: {}, assets: []}),
8789
getDependencyConfig: sinon.stub().returns(dependencyConfig),
8890
};
@@ -223,8 +225,9 @@ describe('link', () => {
223225
sinon.stub().returns(false)
224226
);
225227

228+
const linkConfig = require('../ios');
226229
const config = {
227-
getPlatformConfig: () => ({ ios: {}}),
230+
getPlatformConfig: () => ({ ios: { linkConfig: linkConfig }}),
228231
getProjectConfig: () => ({ ios: {}, assets: [] }),
229232
getDependencyConfig: sinon.stub().returns({
230233
ios: {}, assets: [], commands: { prelink, postlink },
@@ -251,8 +254,9 @@ describe('link', () => {
251254
copyAssets
252255
);
253256

257+
const linkConfig = require('../ios');
254258
const config = {
255-
getPlatformConfig: () => ({ ios: {} }),
259+
getPlatformConfig: () => ({ ios: { linkConfig: linkConfig } }),
256260
getProjectConfig: () => ({ ios: {}, assets: projectAssets }),
257261
getDependencyConfig: sinon.stub().returns(dependencyConfig),
258262
};

Diff for: local-cli/link/android/copyAssets.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ const groupFilesByType = require('../groupFilesByType');
1717
* For now, the only types of files that are handled are:
1818
* - Fonts (otf, ttf) - copied to targetPath/fonts under original name
1919
*/
20-
module.exports = function copyAssetsAndroid(files, targetPath) {
20+
module.exports = function copyAssetsAndroid(files, project) {
2121
const assets = groupFilesByType(files);
2222

2323
(assets.font || []).forEach(asset =>
24-
fs.copySync(asset, path.join(targetPath, 'fonts', path.basename(asset)))
24+
fs.copySync(asset, path.join(project.assetsPath, 'fonts', path.basename(asset)))
2525
);
2626
};

Diff for: local-cli/link/android/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = function() {
2+
return {
3+
isInstalled: require('./isInstalled'),
4+
register: require('./registerNativeModule'),
5+
unregister: require('./unregisterNativeModule'),
6+
copyAssets: require('./copyAssets'),
7+
unlinkAssets: require('./unlinkAssets')
8+
};
9+
};

Diff for: local-cli/link/android/unlinkAssets.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ const groupFilesByType = require('../groupFilesByType');
1717
* For now, the only types of files that are handled are:
1818
* - Fonts (otf, ttf) - copied to targetPath/fonts under original name
1919
*/
20-
module.exports = function unlinkAssetsAndroid(files, targetPath) {
20+
module.exports = function unlinkAssetsAndroid(files, project) {
2121
const assets = groupFilesByType(files);
2222

2323
(assets.font || []).forEach((file) => {
24-
const filePath = path.join(targetPath, 'fonts', path.basename(file));
24+
const filePath = path.join(project.assetsPath, 'fonts', path.basename(file));
2525
if (fs.existsSync(filePath)) {
2626
fs.unlinkSync(filePath);
2727
}

Diff for: local-cli/link/ios/common/isInstalled.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const isInstalledIOS = require('../isInstalled');
2+
const isInstalledPods = require('../../pods/isInstalled');
3+
4+
module.exports = function isInstalled(config, name) {
5+
return isInstalledIOS(config, name) || isInstalledPods(config, name);
6+
};

Diff for: local-cli/link/ios/common/registerNativeModule.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const registerDependencyIOS = require('../registerNativeModule');
2+
const registerDependencyPods = require('../../pods/registerNativeModule');
3+
4+
module.exports = function registerNativeModule(
5+
name,
6+
dependencyConfig,
7+
params,
8+
projectConfig
9+
) {
10+
if (projectConfig.podfile && dependencyConfig.podspec) {
11+
registerDependencyPods(name, dependencyConfig, projectConfig);
12+
}
13+
else {
14+
registerDependencyIOS(dependencyConfig, projectConfig);
15+
}
16+
};

Diff for: local-cli/link/ios/common/unregisterNativeModule.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const compact = require('lodash').compact;
2+
const isInstalledIOS = require('../isInstalled');
3+
const isInstalledPods = require('../../pods/isInstalled');
4+
const unregisterDependencyIOS = require('../registerNativeModule');
5+
const unregisterDependencyPods = require('../../pods/registerNativeModule');
6+
7+
module.exports = function unregisterNativeModule(
8+
name,
9+
dependencyConfig,
10+
projectConfig,
11+
otherDependencies
12+
) {
13+
const isIosInstalled = isInstalledIOS(projectConfig, dependencyConfig);
14+
const isPodInstalled = isInstalledPods(projectConfig, dependencyConfig);
15+
if (isIosInstalled) {
16+
const iOSDependencies = compact(otherDependencies.map(d => d.config.ios));
17+
unregisterDependencyIOS(dependencyConfig, projectConfig, iOSDependencies);
18+
}
19+
else if (isPodInstalled) {
20+
unregisterDependencyPods(dependencyConfig, projectConfig);
21+
}
22+
};

Diff for: local-cli/link/ios/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = function() {
2+
return {
3+
isInstalled: require('./common/isInstalled'),
4+
register: require('./common/registerNativeModule'),
5+
unregister: require('./common/unregisterNativeModule'),
6+
copyAssets: require('./copyAssets'),
7+
unlinkAssets: require('./unlinkAssets')
8+
};
9+
};

Diff for: local-cli/link/link.js

+2-74
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ const chalk = require('chalk');
2626
* run Flow. */
2727
const isEmpty = require('lodash').isEmpty;
2828
const promiseWaterfall = require('./promiseWaterfall');
29-
const registerDependencyAndroid = require('./android/registerNativeModule');
30-
const registerDependencyIOS = require('./ios/registerNativeModule');
31-
const registerDependencyPods = require('./pods/registerNativeModule');
32-
const isInstalledAndroid = require('./android/isInstalled');
33-
const isInstalledIOS = require('./ios/isInstalled');
34-
const isInstalledPods = require('./pods/isInstalled');
35-
const copyAssetsAndroid = require('./android/copyAssets');
36-
const copyAssetsIOS = require('./ios/copyAssets');
3729
const getProjectDependencies = require('./getProjectDependencies');
3830
const getDependencyConfig = require('./getDependencyConfig');
3931
const pollParams = require('./pollParams');
@@ -47,37 +39,8 @@ log.heading = 'rnpm-link';
4739

4840
const dedupeAssets = (assets) => uniqBy(assets, asset => path.basename(asset));
4941

50-
51-
const linkDependencyAndroid = (androidProject, dependency) => {
52-
if (!androidProject || !dependency.config.android) {
53-
return null;
54-
}
55-
56-
const isInstalled = isInstalledAndroid(androidProject, dependency.name);
57-
58-
if (isInstalled) {
59-
log.info(chalk.grey(`Android module ${dependency.name} is already linked`));
60-
return null;
61-
}
62-
63-
return pollParams(dependency.config.params).then(params => {
64-
log.info(`Linking ${dependency.name} android dependency`);
65-
66-
registerDependencyAndroid(
67-
dependency.name,
68-
dependency.config.android,
69-
params,
70-
androidProject
71-
);
72-
73-
log.info(`Android module ${dependency.name} has been successfully linked`);
74-
});
75-
};
76-
77-
const linkDependencyPlatforms = (platforms, project, dependency) => {
78-
const ignorePlatforms = ['android', 'ios'];
42+
const linkDependency = (platforms, project, dependency) => {
7943
Object.keys(platforms || {})
80-
.filter(platform => ignorePlatforms.indexOf(platform) < 0)
8144
.forEach(platform => {
8245
if (!project[platform] || !dependency.config[platform]) {
8346
return null;
@@ -110,45 +73,12 @@ const linkDependencyPlatforms = (platforms, project, dependency) => {
11073
});
11174
};
11275

113-
const linkDependencyIOS = (iOSProject, dependency) => {
114-
if (!iOSProject || !dependency.config.ios) {
115-
return;
116-
}
117-
118-
const isInstalled = isInstalledIOS(iOSProject, dependency.config.ios) || isInstalledPods(iOSProject, dependency.config.ios);
119-
if (isInstalled) {
120-
log.info(chalk.grey(`iOS module ${dependency.name} is already linked`));
121-
return;
122-
}
123-
124-
log.info(`Linking ${dependency.name} ios dependency`);
125-
if (iOSProject.podfile && dependency.config.ios.podspec) {
126-
registerDependencyPods(dependency, iOSProject);
127-
}
128-
else {
129-
registerDependencyIOS(dependency.config.ios, iOSProject);
130-
}
131-
log.info(`iOS module ${dependency.name} has been successfully linked`);
132-
};
133-
13476
const linkAssets = (platforms, project, assets) => {
13577
if (isEmpty(assets)) {
13678
return;
13779
}
13880

139-
if (project.ios) {
140-
log.info('Linking assets to ios project');
141-
copyAssetsIOS(assets, project.ios);
142-
}
143-
144-
if (project.android) {
145-
log.info('Linking assets to android project');
146-
copyAssetsAndroid(assets, project.android.assetsPath);
147-
}
148-
149-
const ignorePlatforms = ['android', 'ios'];
15081
Object.keys(platforms || {})
151-
.filter(platform => ignorePlatforms.indexOf(platform) < 0)
15282
.forEach(platform => {
15383
const linkConfig = platforms[platform] && platforms[platform].linkConfig && platforms[platform].linkConfig();
15484
if (!linkConfig || !linkConfig.copyAssets) {
@@ -212,9 +142,7 @@ function link(args: Array<string>, config: RNConfig) {
212142

213143
const tasks = flatten(dependencies.map(dependency => [
214144
() => promisify(dependency.config.commands.prelink || commandStub),
215-
() => linkDependencyAndroid(project.android, dependency),
216-
() => linkDependencyIOS(project.ios, dependency),
217-
() => linkDependencyPlatforms(platforms, project, dependency),
145+
() => linkDependency(platforms, project, dependency),
218146
() => promisify(dependency.config.commands.postlink || commandStub),
219147
]));
220148

Diff for: local-cli/link/pods/registerNativeModule.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ const findMarkedLinesInPodfile = require('./findMarkedLinesInPodfile');
1616
const addPodEntry = require('./addPodEntry');
1717
const savePodFile = require('./savePodFile');
1818

19-
module.exports = function registerNativeModulePods(dependency, iOSProject) {
19+
module.exports = function registerNativeModulePods(name, dependencyConfig, iOSProject) {
2020
const podLines = readPodfile(iOSProject.podfile);
2121
const linesToAddEntry = getLinesToAddEntry(podLines, iOSProject);
22-
addPodEntry(podLines, linesToAddEntry, dependency.config.ios.podspec, dependency.name);
22+
addPodEntry(podLines, linesToAddEntry, dependencyConfig.podspec, name);
2323
savePodFile(iOSProject.podfile, podLines);
2424
};
2525

0 commit comments

Comments
 (0)