2016-11-14 19:12:24 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-11-14 19:12:24 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2018-05-11 19:43:49 +00:00
|
|
|
*
|
|
|
|
* @format
|
2016-11-14 19:12:24 +00:00
|
|
|
*/
|
2018-05-11 19:43:49 +00:00
|
|
|
|
2016-11-14 19:12:24 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-20 11:52:08 +00:00
|
|
|
const findAndroidAppFolder = require('./findAndroidAppFolder');
|
|
|
|
const findManifest = require('./findManifest');
|
|
|
|
const findPackageClassName = require('./findPackageClassName');
|
2016-11-14 19:12:24 +00:00
|
|
|
const path = require('path');
|
|
|
|
const readManifest = require('./readManifest');
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
const getPackageName = manifest => manifest.attr.package;
|
2016-05-20 11:52:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets android project config by analyzing given folder and taking some
|
|
|
|
* defaults specified by user into consideration
|
|
|
|
*/
|
2018-08-09 04:01:23 +00:00
|
|
|
exports.projectConfig = function projectConfigAndroid(folder, userConfig = {}) {
|
2016-05-20 11:52:08 +00:00
|
|
|
const src = userConfig.sourceDir || findAndroidAppFolder(folder);
|
|
|
|
|
|
|
|
if (!src) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sourceDir = path.join(folder, src);
|
|
|
|
const isFlat = sourceDir.indexOf('app') === -1;
|
2017-06-06 21:11:54 +00:00
|
|
|
const manifestPath = userConfig.manifestPath
|
|
|
|
? path.join(sourceDir, userConfig.manifestPath)
|
|
|
|
: findManifest(sourceDir);
|
2016-05-20 11:52:08 +00:00
|
|
|
|
|
|
|
if (!manifestPath) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const manifest = readManifest(manifestPath);
|
|
|
|
|
|
|
|
const packageName = userConfig.packageName || getPackageName(manifest);
|
2017-06-06 21:11:54 +00:00
|
|
|
|
|
|
|
if (!packageName) {
|
|
|
|
throw new Error(`Package name not found in ${manifestPath}`);
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
const packageFolder =
|
|
|
|
userConfig.packageFolder || packageName.replace(/\./g, path.sep);
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2016-07-08 08:33:51 +00:00
|
|
|
const mainFilePath = path.join(
|
2016-05-20 11:52:08 +00:00
|
|
|
sourceDir,
|
2018-05-11 19:43:49 +00:00
|
|
|
userConfig.mainFilePath ||
|
|
|
|
`src/main/java/${packageFolder}/MainApplication.java`,
|
2016-05-20 11:52:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const stringsPath = path.join(
|
|
|
|
sourceDir,
|
2018-05-11 19:43:49 +00:00
|
|
|
userConfig.stringsPath || 'src/main/res/values/strings.xml',
|
2016-05-20 11:52:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const settingsGradlePath = path.join(
|
|
|
|
folder,
|
|
|
|
'android',
|
2018-05-11 19:43:49 +00:00
|
|
|
userConfig.settingsGradlePath || 'settings.gradle',
|
2016-05-20 11:52:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const assetsPath = path.join(
|
|
|
|
sourceDir,
|
2018-05-11 19:43:49 +00:00
|
|
|
userConfig.assetsPath || 'src/main/assets',
|
2016-05-20 11:52:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const buildGradlePath = path.join(
|
|
|
|
sourceDir,
|
2018-05-11 19:43:49 +00:00
|
|
|
userConfig.buildGradlePath || 'build.gradle',
|
2016-05-20 11:52:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
sourceDir,
|
|
|
|
isFlat,
|
|
|
|
folder,
|
|
|
|
stringsPath,
|
|
|
|
manifestPath,
|
|
|
|
buildGradlePath,
|
|
|
|
settingsGradlePath,
|
|
|
|
assetsPath,
|
2016-07-08 08:33:51 +00:00
|
|
|
mainFilePath,
|
2016-05-20 11:52:08 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as projectConfigAndroid except it returns
|
|
|
|
* different config that applies to packages only
|
|
|
|
*/
|
2018-05-11 19:43:49 +00:00
|
|
|
exports.dependencyConfig = function dependencyConfigAndroid(
|
|
|
|
folder,
|
2018-08-09 04:01:23 +00:00
|
|
|
userConfig = {},
|
2018-05-11 19:43:49 +00:00
|
|
|
) {
|
2016-05-20 11:52:08 +00:00
|
|
|
const src = userConfig.sourceDir || findAndroidAppFolder(folder);
|
|
|
|
|
|
|
|
if (!src) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sourceDir = path.join(folder, src);
|
2017-06-06 21:11:54 +00:00
|
|
|
const manifestPath = userConfig.manifestPath
|
|
|
|
? path.join(sourceDir, userConfig.manifestPath)
|
|
|
|
: findManifest(sourceDir);
|
2016-05-20 11:52:08 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
const packageImportPath =
|
|
|
|
userConfig.packageImportPath ||
|
2016-05-20 11:52:08 +00:00
|
|
|
`import ${packageName}.${packageClassName};`;
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
const packageInstance =
|
|
|
|
userConfig.packageInstance || `new ${packageClassName}()`;
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
return {sourceDir, folder, manifest, packageImportPath, packageInstance};
|
2016-05-20 11:52:08 +00:00
|
|
|
};
|
2018-02-13 12:57:18 +00:00
|
|
|
|
|
|
|
exports.linkConfig = require('../../link/android');
|