Skip to content

Commit

Permalink
refactor(cli): expose srcDir and srcMainDir (#3828)
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd authored Nov 19, 2020
1 parent a37e188 commit b1c2718
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
18 changes: 13 additions & 5 deletions cli/src/android/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ export async function editProjectSettingsAndroid(
const appId = config.app.appId;
const appName = config.app.appName;

const manifestPath = resolve(config.android.srcDirAbs, 'AndroidManifest.xml');
const manifestPath = resolve(
config.android.srcMainDirAbs,
'AndroidManifest.xml',
);
const buildGradlePath = resolve(config.android.appDirAbs, 'build.gradle');

let manifestContent = await readFile(manifestPath, { encoding: 'utf-8' });
Expand All @@ -86,27 +89,32 @@ export async function editProjectSettingsAndroid(

const domainPath = appId.split('.').join('/');
// Make the package source path to the new plugin Java file
const newJavaPath = resolve(config.android.srcDirAbs, `java/${domainPath}`);
const newJavaPath = resolve(
config.android.srcMainDirAbs,
`java/${domainPath}`,
);

if (!(await pathExists(newJavaPath))) {
await mkdirp(newJavaPath);
}

await copy(
resolve(
config.android.srcDirAbs,
config.android.srcMainDirAbs,
'java/com/getcapacitor/myapp/MainActivity.java',
),
resolve(newJavaPath, 'MainActivity.java'),
);

if (appId.split('.')[1] !== 'getcapacitor') {
await remove(resolve(config.android.srcDirAbs, 'java/com/getcapacitor'));
await remove(
resolve(config.android.srcMainDirAbs, 'java/com/getcapacitor'),
);
}

// Remove our template 'com' folder if their ID doesn't have it
if (appId.split('.')[0] !== 'com') {
await remove(resolve(config.android.srcDirAbs, 'java/com/'));
await remove(resolve(config.android.srcMainDirAbs, 'java/com/'));
}

// Update the package in the MainActivity java file
Expand Down
22 changes: 12 additions & 10 deletions cli/src/android/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ async function checkAppSrcDirs(config: Config) {
)} directory is missing in ${c.strong(config.android.platformDir)}`;
}

if (!(await pathExists(config.android.srcDirAbs))) {
if (!(await pathExists(config.android.srcMainDirAbs))) {
return `${c.strong(
config.android.srcDir,
config.android.srcMainDir,
)} directory is missing in ${c.strong(config.android.platformDir)}`;
}

Expand Down Expand Up @@ -61,11 +61,11 @@ async function checkAppSrcDirs(config: Config) {

async function checkAndroidManifestFile(config: Config) {
const manifestFileName = 'AndroidManifest.xml';
const manifestFilePath = join(config.android.srcDirAbs, manifestFileName);
const manifestFilePath = join(config.android.srcMainDirAbs, manifestFileName);

if (!(await pathExists(manifestFilePath))) {
return `${c.strong(manifestFileName)} is missing in ${c.strong(
config.android.srcDir,
config.android.srcMainDir,
)}`;
}

Expand All @@ -81,14 +81,14 @@ async function checkAndroidManifestData(config: Config, xmlData: any) {
const manifestNode: any = xmlData.manifest;
if (!manifestNode) {
return `Missing ${c.input('<manifest>')} XML node in ${c.strong(
config.android.srcDir,
config.android.srcMainDir,
)}`;
}

const packageId = manifestNode.$['package'];
if (!packageId) {
return `Missing ${c.input('<manifest package="">')} attribute in ${c.strong(
config.android.srcDir,
config.android.srcMainDir,
)}`;
}

Expand All @@ -97,7 +97,7 @@ async function checkAndroidManifestData(config: Config, xmlData: any) {
return `Missing ${c.input(
'<application>',
)} XML node as a child node of ${c.input('<manifest>')} in ${c.strong(
config.android.srcDir,
config.android.srcMainDir,
)}`;
}

Expand Down Expand Up @@ -154,14 +154,16 @@ async function checkAndroidManifestData(config: Config, xmlData: any) {

if (!mainApplicationNode) {
return `Missing main ${c.input('<activity>')} XML node in ${c.strong(
config.android.srcDir,
config.android.srcMainDir,
)}`;
}

if (!mainActivityClassPath) {
return `Missing ${c.input(
'<activity android:name="">',
)} attribute for MainActivity class in ${c.strong(config.android.srcDir)}`;
)} attribute for MainActivity class in ${c.strong(
config.android.srcMainDir,
)}`;
}

return checkPackage(config, packageId, mainActivityClassPath);
Expand All @@ -180,7 +182,7 @@ async function checkPackage(
);
}

const appSrcMainJavaDir = join(config.android.srcDirAbs, 'java');
const appSrcMainJavaDir = join(config.android.srcMainDirAbs, 'java');
if (!(await pathExists(appSrcMainJavaDir))) {
return `${c.strong('java')} directory is missing in ${c.strong(
appSrcMainJavaDir,
Expand Down
9 changes: 6 additions & 3 deletions cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ async function loadAndroidConfig(
const platformDir = extConfig.android?.path ?? 'android';
const platformDirAbs = resolve(rootDir, platformDir);
const appDir = 'app';
const srcDir = `${appDir}/src/main`;
const assetsDir = `${srcDir}/assets`;
const srcDir = `${appDir}/src`;
const srcMainDir = `${srcDir}/main`;
const assetsDir = `${srcMainDir}/assets`;
const webDir = `${assetsDir}/public`;
const resDir = `${srcDir}/res`;
const resDir = `${srcMainDir}/res`;
const buildOutputDir = `${appDir}/build/outputs/apk/debug`;

const templateName = 'android-template';
Expand All @@ -175,6 +176,8 @@ async function loadAndroidConfig(
appDirAbs: resolve(platformDir, appDir),
srcDir,
srcDirAbs: resolve(platformDir, srcDir),
srcMainDir,
srcMainDirAbs: resolve(platformDir, srcMainDir),
assetsDir,
assetsDirAbs: resolve(platformDir, assetsDir),
webDir,
Expand Down
2 changes: 2 additions & 0 deletions cli/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export interface AndroidConfig extends PlatformConfig {
readonly appDirAbs: string;
readonly srcDir: string;
readonly srcDirAbs: string;
readonly srcMainDir: string;
readonly srcMainDirAbs: string;
readonly webDir: string;
readonly webDirAbs: string;
readonly assetsDir: string;
Expand Down

0 comments on commit b1c2718

Please sign in to comment.