Skip to content

Commit

Permalink
feat(cli): ability to specify custom platform directories (ionic-team…
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd authored Sep 23, 2020
1 parent c8a0595 commit c6eda55
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 89 deletions.
9 changes: 4 additions & 5 deletions cli/src/android/add.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { homedir } from 'os';
import { join, relative } from 'path';
import { join } from 'path';

import c from '../colors';
import { copyTemplate, runCommand, runTask } from '../common';
import type { Config } from '../definitions';
import { existsAsync, writeFileAsync } from '../util/fs';

export async function addAndroid(config: Config): Promise<void> {
const nativeRelDir = relative(config.app.rootDir, config.android.platformDir);
await runTask(
`Adding native android project in ${c.strong(nativeRelDir)}`,
`Adding native android project in ${c.strong(config.android.platformDir)}`,
async () => {
return copyTemplate(
config.android.assets.templateDir,
config.android.platformDir,
config.android.platformDirAbs,
);
},
);

await runTask('Syncing Gradle', async () => {
return createLocalProperties(config.android.platformDir);
return createLocalProperties(config.android.platformDirAbs);
});
}

Expand Down
27 changes: 8 additions & 19 deletions cli/src/android/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,11 @@ export async function editProjectSettingsAndroid(
const appName = config.app.appName;

const manifestPath = resolve(
config.app.rootDir,
config.android.platformDir,
config.android.platformDirAbs,
'app/src/main/AndroidManifest.xml',
);
const buildGradlePath = resolve(
config.app.rootDir,
config.android.platformDir,
config.android.platformDirAbs,
'app/build.gradle',
);

Expand All @@ -92,8 +90,7 @@ export async function editProjectSettingsAndroid(
const domainPath = appId.split('.').join('/');
// Make the package source path to the new plugin Java file
const newJavaPath = resolve(
config.app.rootDir,
config.android.platformDir,
config.android.platformDirAbs,
`app/src/main/java/${domainPath}`,
);

Expand All @@ -103,8 +100,7 @@ export async function editProjectSettingsAndroid(

await copyAsync(
resolve(
config.app.rootDir,
config.android.platformDir,
config.android.platformDirAbs,
'app/src/main/java/com/getcapacitor/myapp/MainActivity.java',
),
resolve(newJavaPath, 'MainActivity.java'),
Expand All @@ -113,8 +109,7 @@ export async function editProjectSettingsAndroid(
if (appId.split('.')[1] !== 'getcapacitor') {
await removeAsync(
resolve(
config.app.rootDir,
config.android.platformDir,
config.android.platformDirAbs,
'app/src/main/java/com/getcapacitor',
),
);
Expand All @@ -123,18 +118,13 @@ export async function editProjectSettingsAndroid(
// Remove our template 'com' folder if their ID doesn't have it
if (appId.split('.')[0] !== 'com') {
await removeAsync(
resolve(
config.app.rootDir,
config.android.platformDir,
'app/src/main/java/com/',
),
resolve(config.android.platformDirAbs, 'app/src/main/java/com/'),
);
}

// Update the package in the MainActivity java file
const activityPath = resolve(
config.app.rootDir,
config.android.platformDir,
config.android.platformDirAbs,
newJavaPath,
'MainActivity.java',
);
Expand All @@ -157,8 +147,7 @@ export async function editProjectSettingsAndroid(

// Update the settings in res/values/strings.xml
const stringsPath = resolve(
config.app.rootDir,
config.android.platformDir,
config.android.platformDirAbs,
'app/src/main/res/values/strings.xml',
);
let stringsContent = await readFileAsync(stringsPath, 'utf8');
Expand Down
6 changes: 3 additions & 3 deletions cli/src/android/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function doctorAndroid(config: Config): Promise<void> {
}

async function checkAppSrcDirs(config: Config) {
const appDir = join(config.android.platformDir, 'app');
const appDir = join(config.android.platformDirAbs, 'app');
if (!(await existsAsync(appDir))) {
return `${c.strong('app')} directory is missing in ${
config.android.platformDir
Expand Down Expand Up @@ -216,7 +216,7 @@ async function checkPackage(
}

async function checkBuildGradle(config: Config, packageId: string) {
const appDir = join(config.android.platformDir, 'app');
const appDir = join(config.android.platformDirAbs, 'app');
const fileName = 'build.gradle';
const filePath = join(appDir, fileName);

Expand All @@ -241,7 +241,7 @@ async function checkBuildGradle(config: Config, packageId: string) {

async function checkGradlew(config: Config) {
const fileName = 'gradlew';
const filePath = join(config.android.platformDir, fileName);
const filePath = join(config.android.platformDirAbs, fileName);

if (!(await existsAsync(filePath))) {
return `${c.strong(fileName)} file is missing in ${
Expand Down
2 changes: 1 addition & 1 deletion cli/src/android/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { existsSync } from '../util/fs';
export async function openAndroid(config: Config): Promise<void> {
logger.info(`Opening Android project at ${config.android.platformDir}.`);

const dir = config.android.platformDir;
const dir = config.android.platformDirAbs;

switch (config.cli.os) {
case OS.Mac: {
Expand Down
16 changes: 6 additions & 10 deletions cli/src/android/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export async function installGradlePlugins(
'capacitor',
);

const settingsPath = join(config.app.rootDir, 'android');
const dependencyPath = join(config.app.rootDir, 'android', 'app');
const settingsPath = config.android.platformDirAbs;
const dependencyPath = join(config.android.platformDirAbs, 'app');
const relativeCapcitorAndroidPath = convertToUnixPath(
relative(settingsPath, capacitorAndroidPath),
);
Expand Down Expand Up @@ -184,8 +184,7 @@ export async function handleCordovaPluginsGradle(
cordovaPlugins: Plugin[],
): Promise<void> {
const pluginsFolder = resolve(
config.app.rootDir,
'android',
config.android.platformDirAbs,
config.android.assets.pluginsFolderName,
);
const pluginsGradlePath = join(pluginsFolder, 'build.gradle');
Expand Down Expand Up @@ -249,8 +248,7 @@ ext {

function copyPluginsNativeFiles(config: Config, cordovaPlugins: Plugin[]) {
const pluginsRoot = resolve(
config.app.rootDir,
'android',
config.android.platformDirAbs,
config.android.assets.pluginsFolderName,
);
const pluginsPath = join(pluginsRoot, 'src', 'main');
Expand Down Expand Up @@ -304,8 +302,7 @@ function copyPluginsNativeFiles(config: Config, cordovaPlugins: Plugin[]) {

function removePluginsNativeFiles(config: Config) {
const pluginsRoot = resolve(
config.app.rootDir,
'android',
config.android.platformDirAbs,
config.android.assets.pluginsFolderName,
);
removeSync(pluginsRoot);
Expand All @@ -326,8 +323,7 @@ async function replaceFrameworkVariables(
frameworkString: string,
) {
const variablesFile = resolve(
config.app.rootDir,
'android',
config.android.platformDirAbs,
'variables.gradle',
);
let variablesGradle = '';
Expand Down
26 changes: 19 additions & 7 deletions cli/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,26 @@ export async function getCLIVersion(config: Config): Promise<string> {
return getCapacitorPackageVersion(config, 'cli');
}

export async function getPlatformDirectory(
function getPlatformDirectory(config: Config, platform: string): string | null {
switch (platform) {
case 'android':
return config.android.platformDirAbs;
case 'ios':
return config.ios.platformDirAbs;
case 'web':
return config.web.platformDirAbs;
}

return null;
}

export async function getProjectPlatformDirectory(
config: Config,
platform: string,
): Promise<string | null> {
const platformDir = platform === 'web' ? config.app.webDir : platform;
const platformPath = join(config.app.rootDir, platformDir);
const platformPath = getPlatformDirectory(config, platform);

if (await existsAsync(platformPath)) {
if (platformPath && (await existsAsync(platformPath))) {
return platformPath;
}

Expand All @@ -417,7 +429,7 @@ export async function selectPlatforms(

if (!(await isValidPlatform(platformName))) {
logFatal(`Invalid platform: ${c.input(platformName)}`);
} else if (!(await getPlatformDirectory(config, platformName))) {
} else if (!(await getProjectPlatformDirectory(config, platformName))) {
if (platformName === 'web') {
logFatal(
`Could not find the web platform directory.\n` +
Expand Down Expand Up @@ -496,11 +508,11 @@ export async function promptForPlatform(
export async function getAddedPlatforms(config: Config): Promise<string[]> {
const platforms: string[] = [];

if (await getPlatformDirectory(config, config.android.name)) {
if (await getProjectPlatformDirectory(config, config.android.name)) {
platforms.push(config.android.name);
}

if (await getPlatformDirectory(config, config.ios.name)) {
if (await getProjectPlatformDirectory(config, config.ios.name)) {
platforms.push(config.ios.name);
}

Expand Down
33 changes: 26 additions & 7 deletions cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
AndroidConfig,
IOSConfig,
PackageJson,
WebConfig,
} from './definitions';
import { OS } from './definitions';

Expand Down Expand Up @@ -36,11 +37,9 @@ export async function loadConfig(): Promise<Config> {
extConfig.linuxAndroidStudioPath ??
'/usr/local/android-studio/bin/studio.sh',
},
android: await loadAndroidConfig(appRootDir, cli.assetsDir),
ios: await loadIOSConfig(appRootDir, cli.assetsDir),
web: {
name: 'web',
},
android: await loadAndroidConfig(appRootDir, extConfig, cli.assetsDir),
ios: await loadIOSConfig(appRootDir, extConfig, cli.assetsDir),
web: await loadWebConfig(appRootDir, webDir),
cli,
app: {
rootDir: appRootDir,
Expand Down Expand Up @@ -74,10 +73,12 @@ async function loadCLIConfig(rootDir: string): Promise<CLIConfig> {

async function loadAndroidConfig(
rootDir: string,
extConfig: ExternalConfig,
assetDir: string,
): Promise<AndroidConfig> {
const name = 'android';
const platformDir = resolve(rootDir, name);
const platformDir = extConfig.android?.path ?? 'android';
const platformDirAbs = resolve(rootDir, platformDir);
const webDir = 'app/src/main/assets/public';
const resDir = 'app/src/main/res';

Expand All @@ -88,6 +89,7 @@ async function loadAndroidConfig(
name,
minVersion: '21',
platformDir,
platformDirAbs,
webDir,
webDirAbs: resolve(platformDir, webDir),
resDir,
Expand All @@ -103,10 +105,12 @@ async function loadAndroidConfig(

async function loadIOSConfig(
rootDir: string,
extConfig: ExternalConfig,
assetDir: string,
): Promise<IOSConfig> {
const name = 'ios';
const platformDir = resolve(rootDir, name);
const platformDir = extConfig.ios?.path ?? 'ios';
const platformDirAbs = resolve(rootDir, platformDir);
const webDir = 'public';
const nativeProjectName = 'App';
const templateName = 'ios-template';
Expand All @@ -117,6 +121,7 @@ async function loadIOSConfig(
minVersion: '11.0',
cordovaSwiftVersion: '5.1',
platformDir,
platformDirAbs,
webDir,
webDirAbs: resolve(platformDir, nativeProjectName, webDir),
nativeProjectName,
Expand All @@ -129,6 +134,20 @@ async function loadIOSConfig(
};
}

async function loadWebConfig(
rootDir: string,
webDir: string,
): Promise<WebConfig> {
const platformDir = webDir;
const platformDirAbs = resolve(rootDir, platformDir);

return {
name: 'web',
platformDir,
platformDirAbs,
};
}

function determineOS(os: NodeJS.Platform): OS {
switch (os) {
case 'darwin':
Expand Down
7 changes: 3 additions & 4 deletions cli/src/cordova.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export async function autoGenerateConfig(
const fileName = 'config.xml';
if (platform === 'ios') {
xmlDir = join(
config.ios.platformDir,
config.ios.platformDirAbs,
config.ios.nativeProjectName,
config.ios.nativeProjectName,
);
Expand Down Expand Up @@ -343,7 +343,7 @@ export async function logCordovaManualSteps(

async function logiOSPlist(configElement: any, config: Config, plugin: Plugin) {
const plistPath = resolve(
config.ios.platformDir,
config.ios.platformDirAbs,
config.ios.nativeProjectName,
config.ios.nativeProjectName,
'Info.plist',
Expand Down Expand Up @@ -573,8 +573,7 @@ export async function writeCordovaAndroidManifest(
platform: string,
): Promise<void> {
const pluginsFolder = resolve(
config.app.rootDir,
'android',
config.android.platformDirAbs,
config.android.assets.pluginsFolderName,
);
const manifestPath = join(
Expand Down
Loading

0 comments on commit c6eda55

Please sign in to comment.