mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-23 20:01:01 +08:00
Further RNPM integration
Summary: This commit removes `rnpm` folder that we left during initial merge to keep the diff cleaner. The `core`, `link` and `install` have now the same directory structure as any other command to make development more natural for all of us. From most notable differences: 1) the `src` folder is now gone. The new structure should make it easier for people to work with the stuff and also move us closer to 100% rnpm integration, 2) There's also no `package.json` present in any of the `rnpm` packages, since they are no longer standalone modules, 3) There's no `bugs.url` in link.js since main package.json of React doesn't specify it. Decided to hardcode it to facebook/react-native since it's really unlikely to change. If one would prefer to use pkg.bugs.url as before, a separate PR modifying package.json should be sent. Closes https://github.com/facebook/react-native/pull/9509 Differential Revision: D3751115 fbshipit-source-id: 74ae8330f7634df0887ad676808f47eee4b8de85
This commit is contained in:
committed by
Facebook Github Bot 3
parent
25f2a26ce9
commit
0af640bfae
21
local-cli/core/config/android/findAndroidAppFolder.js
Normal file
21
local-cli/core/config/android/findAndroidAppFolder.js
Normal file
@@ -0,0 +1,21 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* @param {String} folder Folder to seek in
|
||||
* @return {String}
|
||||
*/
|
||||
module.exports = function findAndroidAppFolder(folder) {
|
||||
const flat = 'android';
|
||||
const nested = path.join('android', 'app');
|
||||
|
||||
if (fs.existsSync(path.join(folder, nested))) {
|
||||
return nested;
|
||||
}
|
||||
|
||||
if (fs.existsSync(path.join(folder, flat))) {
|
||||
return flat;
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
17
local-cli/core/config/android/findManifest.js
Normal file
17
local-cli/core/config/android/findManifest.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const glob = require('glob');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* Find an android application path in the folder
|
||||
*
|
||||
* @param {String} folder Name of the folder where to seek
|
||||
* @return {String}
|
||||
*/
|
||||
module.exports = function findManifest(folder) {
|
||||
const manifestPath = glob.sync(path.join('**', 'AndroidManifest.xml'), {
|
||||
cwd: folder,
|
||||
ignore: ['node_modules/**', '**/build/**', 'Examples/**', 'examples/**'],
|
||||
})[0];
|
||||
|
||||
return manifestPath ? path.join(folder, manifestPath) : null;
|
||||
};
|
||||
20
local-cli/core/config/android/findPackageClassName.js
Normal file
20
local-cli/core/config/android/findPackageClassName.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const glob = require('glob');
|
||||
|
||||
/**
|
||||
* Gets package's class name (class that implements ReactPackage)
|
||||
* by searching for its declaration in all Java files present in the folder
|
||||
*
|
||||
* @param {String} folder Folder to find java files
|
||||
*/
|
||||
module.exports = function getPackageClassName(folder) {
|
||||
const files = glob.sync('**/*.java', { cwd: folder });
|
||||
|
||||
const packages = files
|
||||
.map(filePath => fs.readFileSync(path.join(folder, filePath), 'utf8'))
|
||||
.map(file => file.match(/class (.*) implements ReactPackage/))
|
||||
.filter(match => match);
|
||||
|
||||
return packages.length ? packages[0][1] : null;
|
||||
};
|
||||
111
local-cli/core/config/android/index.js
Normal file
111
local-cli/core/config/android/index.js
Normal file
@@ -0,0 +1,111 @@
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const glob = require('glob');
|
||||
const findAndroidAppFolder = require('./findAndroidAppFolder');
|
||||
const findManifest = require('./findManifest');
|
||||
const readManifest = require('./readManifest');
|
||||
const findPackageClassName = require('./findPackageClassName');
|
||||
|
||||
const getPackageName = (manifest) => manifest.attr.package;
|
||||
|
||||
/**
|
||||
* Gets android project config by analyzing given folder and taking some
|
||||
* defaults specified by user into consideration
|
||||
*/
|
||||
exports.projectConfig = function projectConfigAndroid(folder, userConfig) {
|
||||
const src = userConfig.sourceDir || findAndroidAppFolder(folder);
|
||||
|
||||
if (!src) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const sourceDir = path.join(folder, src);
|
||||
const isFlat = sourceDir.indexOf('app') === -1;
|
||||
const manifestPath = findManifest(sourceDir);
|
||||
|
||||
if (!manifestPath) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const manifest = readManifest(manifestPath);
|
||||
|
||||
const packageName = userConfig.packageName || getPackageName(manifest);
|
||||
const packageFolder = userConfig.packageFolder ||
|
||||
packageName.replace(/\./g, path.sep);
|
||||
|
||||
const mainFilePath = path.join(
|
||||
sourceDir,
|
||||
userConfig.mainFilePath || `src/main/java/${packageFolder}/MainApplication.java`
|
||||
);
|
||||
|
||||
const stringsPath = path.join(
|
||||
sourceDir,
|
||||
userConfig.stringsPath || 'src/main/res/values/strings.xml'
|
||||
);
|
||||
|
||||
const settingsGradlePath = path.join(
|
||||
folder,
|
||||
'android',
|
||||
userConfig.settingsGradlePath || 'settings.gradle'
|
||||
);
|
||||
|
||||
const assetsPath = path.join(
|
||||
sourceDir,
|
||||
userConfig.assetsPath || 'src/main/assets'
|
||||
);
|
||||
|
||||
const buildGradlePath = path.join(
|
||||
sourceDir,
|
||||
userConfig.buildGradlePath || 'build.gradle'
|
||||
);
|
||||
|
||||
return {
|
||||
sourceDir,
|
||||
isFlat,
|
||||
folder,
|
||||
stringsPath,
|
||||
manifestPath,
|
||||
buildGradlePath,
|
||||
settingsGradlePath,
|
||||
assetsPath,
|
||||
mainFilePath,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Same as projectConfigAndroid except it returns
|
||||
* different config that applies to packages only
|
||||
*/
|
||||
exports.dependencyConfig = function dependencyConfigAndroid(folder, userConfig) {
|
||||
const src = userConfig.sourceDir || findAndroidAppFolder(folder);
|
||||
|
||||
if (!src) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const sourceDir = path.join(folder, src);
|
||||
const manifestPath = findManifest(sourceDir);
|
||||
|
||||
if (!manifestPath) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const manifest = readManifest(manifestPath);
|
||||
const packageName = userConfig.packageName || getPackageName(manifest);
|
||||
const packageClassName = findPackageClassName(sourceDir);
|
||||
|
||||
/**
|
||||
* This module has no package to export
|
||||
*/
|
||||
if (!packageClassName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const packageImportPath = userConfig.packageImportPath ||
|
||||
`import ${packageName}.${packageClassName};`;
|
||||
|
||||
const packageInstance = userConfig.packageInstance ||
|
||||
`new ${packageClassName}()`;
|
||||
|
||||
return { sourceDir, folder, manifest, packageImportPath, packageInstance };
|
||||
};
|
||||
10
local-cli/core/config/android/readManifest.js
Normal file
10
local-cli/core/config/android/readManifest.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const fs = require('fs');
|
||||
const xml = require('xmldoc');
|
||||
|
||||
/**
|
||||
* @param {String} manifestPath
|
||||
* @return {XMLDocument} Parsed manifest's content
|
||||
*/
|
||||
module.exports = function readManifest(manifestPath) {
|
||||
return new xml.XmlDocument(fs.readFileSync(manifestPath, 'utf8'));
|
||||
};
|
||||
20
local-cli/core/config/findAssets.js
Normal file
20
local-cli/core/config/findAssets.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const glob = require('glob');
|
||||
const path = require('path');
|
||||
|
||||
const findAssetsInFolder = (folder) =>
|
||||
glob.sync(path.join(folder, '**'), { nodir: true });
|
||||
|
||||
/**
|
||||
* Given an array of assets folders, e.g. ['Fonts', 'Images'],
|
||||
* it globs in them to find all files that can be copied.
|
||||
*
|
||||
* It returns an array of absolute paths to files found.
|
||||
*/
|
||||
module.exports = function findAssets(folder, assets) {
|
||||
return (assets || [])
|
||||
.map(assetsFolder => path.join(folder, assetsFolder))
|
||||
.reduce((assets, assetsFolder) =>
|
||||
assets.concat(findAssetsInFolder(assetsFolder)),
|
||||
[]
|
||||
);
|
||||
};
|
||||
44
local-cli/core/config/index.js
Normal file
44
local-cli/core/config/index.js
Normal file
@@ -0,0 +1,44 @@
|
||||
const path = require('path');
|
||||
|
||||
const android = require('./android');
|
||||
const ios = require('./ios');
|
||||
const findAssets = require('./findAssets');
|
||||
const wrapCommands = require('./wrapCommands');
|
||||
|
||||
const getRNPMConfig = (folder) =>
|
||||
require(path.join(folder, './package.json')).rnpm || {};
|
||||
|
||||
/**
|
||||
* Returns project config from the current working directory
|
||||
* @return {Object}
|
||||
*/
|
||||
exports.getProjectConfig = function getProjectConfig() {
|
||||
const folder = process.cwd();
|
||||
const rnpm = getRNPMConfig(folder);
|
||||
|
||||
return Object.assign({}, rnpm, {
|
||||
ios: ios.projectConfig(folder, rnpm.ios || {}),
|
||||
android: android.projectConfig(folder, rnpm.android || {}),
|
||||
assets: findAssets(folder, rnpm.assets),
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a dependency config from node_modules/<package_name>
|
||||
* @param {String} packageName Dependency name
|
||||
* @return {Object}
|
||||
*/
|
||||
exports.getDependencyConfig = function getDependencyConfig(packageName) {
|
||||
const folder = path.join(process.cwd(), 'node_modules', packageName);
|
||||
const rnpm = getRNPMConfig(
|
||||
path.join(process.cwd(), 'node_modules', packageName)
|
||||
);
|
||||
|
||||
return Object.assign({}, rnpm, {
|
||||
ios: ios.dependencyConfig(folder, rnpm.ios || {}),
|
||||
android: android.dependencyConfig(folder, rnpm.android || {}),
|
||||
assets: findAssets(folder, rnpm.assets),
|
||||
commands: wrapCommands(rnpm.commands),
|
||||
params: rnpm.params || [],
|
||||
});
|
||||
};
|
||||
47
local-cli/core/config/ios/findProject.js
Normal file
47
local-cli/core/config/ios/findProject.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const glob = require('glob');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* Glob pattern to look for xcodeproj
|
||||
*/
|
||||
const GLOB_PATTERN = '**/*.xcodeproj';
|
||||
|
||||
/**
|
||||
* Regexp matching all test projects
|
||||
*/
|
||||
const TEST_PROJECTS = /test|example|sample/i;
|
||||
|
||||
/**
|
||||
* Base iOS folder
|
||||
*/
|
||||
const IOS_BASE = 'ios';
|
||||
|
||||
/**
|
||||
* These folders will be excluded from search to speed it up
|
||||
*/
|
||||
const GLOB_EXCLUDE_PATTERN = ['**/@(Pods|node_modules)/**'];
|
||||
|
||||
/**
|
||||
* Finds iOS project by looking for all .xcodeproj files
|
||||
* in given folder.
|
||||
*
|
||||
* Returns first match if files are found or null
|
||||
*
|
||||
* Note: `./ios/*.xcodeproj` are returned regardless of the name
|
||||
*/
|
||||
module.exports = function findProject(folder) {
|
||||
const projects = glob
|
||||
.sync(GLOB_PATTERN, {
|
||||
cwd: folder,
|
||||
ignore: GLOB_EXCLUDE_PATTERN,
|
||||
})
|
||||
.filter(project => {
|
||||
return path.dirname(project) === IOS_BASE || !TEST_PROJECTS.test(project);
|
||||
});
|
||||
|
||||
if (projects.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return projects[0];
|
||||
};
|
||||
45
local-cli/core/config/ios/index.js
Normal file
45
local-cli/core/config/ios/index.js
Normal file
@@ -0,0 +1,45 @@
|
||||
const path = require('path');
|
||||
const findProject = require('./findProject');
|
||||
|
||||
/**
|
||||
* For libraries specified without an extension, add '.tbd' for those that
|
||||
* start with 'lib' and '.framework' to the rest.
|
||||
*/
|
||||
const mapSharedLibaries = (libraries) => {
|
||||
return libraries.map(name => {
|
||||
if (path.extname(name)) {
|
||||
return name;
|
||||
}
|
||||
return name + (name.indexOf('lib') === 0 ? '.tbd' : '.framework');
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns project config by analyzing given folder and applying some user defaults
|
||||
* when constructing final object
|
||||
*/
|
||||
exports.projectConfig = function projectConfigIOS(folder, userConfig) {
|
||||
const project = userConfig.project || findProject(folder);
|
||||
|
||||
/**
|
||||
* No iOS config found here
|
||||
*/
|
||||
if (!project) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const projectPath = path.join(folder, project);
|
||||
|
||||
return {
|
||||
sourceDir: path.dirname(projectPath),
|
||||
folder: folder,
|
||||
pbxprojPath: path.join(projectPath, 'project.pbxproj'),
|
||||
projectPath: projectPath,
|
||||
projectName: path.basename(projectPath),
|
||||
libraryFolder: userConfig.libraryFolder || 'Libraries',
|
||||
sharedLibraries: mapSharedLibaries(userConfig.sharedLibraries || []),
|
||||
plist: userConfig.plist || [],
|
||||
};
|
||||
};
|
||||
|
||||
exports.dependencyConfig = exports.projectConfig;
|
||||
9
local-cli/core/config/wrapCommands.js
Normal file
9
local-cli/core/config/wrapCommands.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const makeCommand = require('../makeCommand');
|
||||
|
||||
module.exports = function wrapCommands(commands) {
|
||||
const mappedCommands = {};
|
||||
Object.keys(commands || []).forEach((k) =>
|
||||
mappedCommands[k] = makeCommand(commands[k])
|
||||
);
|
||||
return mappedCommands;
|
||||
};
|
||||
Reference in New Issue
Block a user