Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(flagship): Make android namespace match package name #829

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/flagship/android/app/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ android_library(

android_build_config(
name = 'build_config',
package = 'com.brandingbrand.reactnative.and.flagship',
package = 'CONFIG_BUNDLE_ID',
)

android_resource(
name = 'res',
res = 'src/main/res',
package = 'com.brandingbrand.reactnative.and.flagship',
package = 'CONFIG_BUNDLE_ID',
)

android_binary(
Expand Down
2 changes: 1 addition & 1 deletion packages/flagship/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ android {

defaultConfig {
multiDexEnabled true
applicationId "com.brandingbrand.reactnative.and.flagship"
applicationId "CONFIG_BUNDLE_ID"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionName project.VERSION_NAME
Expand Down
2 changes: 1 addition & 1 deletion packages/flagship/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.brandingbrand.reactnative.and.flagship">
package="CONFIG_BUNDLE_ID">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.brandingbrand.reactnative.and.flagship;
package CONFIG_BUNDLE_ID;

import android.annotation.SuppressLint;
import android.content.Context;
Expand Down Expand Up @@ -44,4 +44,4 @@ public void setEnv(String name, Promise promise) {

promise.resolve(null);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.brandingbrand.reactnative.and.flagship;
package CONFIG_BUNDLE_ID;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
Expand All @@ -21,4 +21,4 @@ public List<NativeModule> createNativeModules(final ReactApplicationContext reac
add(new EnvSwitcher(reactContext));
}};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.brandingbrand.reactnative.and.flagship;
package CONFIG_BUNDLE_ID;

import com.reactnativenavigation.controllers.SplashActivity;

Expand All @@ -7,4 +7,4 @@ public class MainActivity extends SplashActivity {
public int getSplashLayout() {
return R.layout.splash;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.brandingbrand.reactnative.and.flagship;
package CONFIG_BUNDLE_ID;

import android.app.Application;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.brandingbrand.reactnative.and.flagship;
package CONFIG_BUNDLE_ID;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
Expand All @@ -24,4 +24,4 @@ public Map<String, Object> getConstants() {

return constants;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.brandingbrand.reactnative.and.flagship;
package CONFIG_BUNDLE_ID;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
Expand All @@ -21,4 +21,4 @@ public List<NativeModule> createNativeModules(final ReactApplicationContext reac
add(new NativeConstants(reactContext));
}};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.brandingbrand.reactnative.and.flagship;
package CONFIG_BUNDLE_ID;

import android.support.annotation.Nullable;

Expand Down
10 changes: 10 additions & 0 deletions packages/flagship/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface HandlerArgs {
env: string;
}

const TEMPLATE_ANDROID_PACKAGE = 'com.brandingbrand.reactnative.and.flagship';

export const command = 'init [platform]';

export const describe = 'initialize FLAGSHIP for [platform]';
Expand Down Expand Up @@ -128,8 +130,16 @@ function initAndroid(
fs.clone('android');

const androidConfig = android.androidConfigWithDefault(configuration.android);

// The id should be defined, but set it to a default if it's not for compatibility reasons
const pkgId = configuration.bundleIds && configuration.bundleIds.android ?
configuration.bundleIds.android.toLowerCase() :
`com.brandingbrand.reactnative.and.${configuration.name.toLowerCase()}`;

// Rename the boilerplate project with the app name
rename.source('FLAGSHIP', configuration.name, 'android');
rename.source('CONFIG_BUNDLE_ID', pkgId, 'android');
rename.pkgDirectory(TEMPLATE_ANDROID_PACKAGE, pkgId, path.android.mainPath(), 'java');
rename.files('FLAGSHIP', configuration.name, 'android');

fastlane.configure(path.android.fastfilePath(), configuration); // Update Fastfile
Expand Down
33 changes: 33 additions & 0 deletions packages/flagship/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as android from './lib/android';
import * as cocoapods from './lib/cocoapods';
import * as deeplinking from './lib/deeplinking';
import * as env from './lib/env';
import * as fastlane from './lib/fastlane';
import * as fs from './lib/fs';
import * as ios from './lib/ios';
import * as link from './lib/link';
import * as modules from './lib/modules';
import * as nativeConstants from './lib/native-constants';
import * as os from './lib/os';
import * as path from './lib/path';
import * as rename from './lib/rename';
import * as version from './lib/version';
import * as web from './lib/web';

export {
android,
cocoapods,
deeplinking,
env,
fastlane,
fs,
ios,
link,
modules,
nativeConstants,
os,
path,
rename,
version,
web
};
4 changes: 3 additions & 1 deletion packages/flagship/src/lib/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
copySync,
ensureDirSync,
ensureSymlinkSync,
moveSync,
pathExistsSync,
readdirSync,
readFileSync,
Expand All @@ -18,7 +19,8 @@ export {
ensureDirSync,
renameSync,
pathExistsSync,
ensureSymlinkSync
ensureSymlinkSync,
moveSync
};

import * as helpers from '../helpers';
Expand Down
10 changes: 5 additions & 5 deletions packages/flagship/src/lib/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ function getManifestPath(): string {
* @returns {string} The path to the native project.
*/
function getNativeProjectPathAndriod(configuration: Config): string {
const pkgId = configuration.bundleIds && configuration.bundleIds.android ?
configuration.bundleIds.android :
`com.brandingbrand.reactnative.and.${configuration.name}`;

return resolvePathFromProject(
getMainPath(),
'java',
'com',
'brandingbrand',
'reactnative',
'and',
configuration.name.toLowerCase()
...pkgId.toLowerCase().split('.')
);
}

Expand Down
89 changes: 73 additions & 16 deletions packages/flagship/src/lib/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,54 @@ export function source(oldName: string, newName: string, ...pathComponents: stri
logInfo(`renamed project within source in ${directory}`);
}

/**
* Updates the directory structure to match a new namespace
*
* @param {string} oldPkg The old package name to replace.
* @param {string} newPkg The new package name to use.
* @param {...string} pathComponents Path components to the directory in which to
* replace the project name.
*/
export function pkgDirectory(oldPkg: string, newPkg: string, ...pathComponents: string[]): void {
const directory = path.project.resolve(...pathComponents);
const oldPathPart = oldPkg.replace(/\./g, '/');
const newPathPart = newPkg.replace(/\./g, '/');

try {
const results = getMatchingFiles(directory, oldPathPart);

// Rename matching paths
results.forEach(oldPath => {
const newPath = oldPath.replace(oldPathPart, newPathPart).toLowerCase();
fs.moveSync(oldPath, newPath);
});

// since we only moved the bottom-most directory, traverse through the old
// package directories to delete any empty package folders
oldPkg
.split('.')
.reduce<string[]>((parts, part, index, arr) => {
const pattern = [...arr.slice(0, index), part].join('/');
parts.push(...getMatchingFiles(directory, pattern));
return parts;
}, [])
.reverse()
.forEach(dir => {
const contents = fs.pathExistsSync(dir) && fs.readdirSync(dir);
if (Array.isArray(contents) && contents.length === 0) {
fs.removeSync(dir);
}
});

} catch (err) {
logError('renaming project files', err);
process.exit(1);
}

logInfo(`renamed project files in ${directory}`);
}


/**
* Replaces the project name within boilerplate file names.
*
Expand All @@ -46,21 +94,7 @@ export function files(oldName: string, newName: string, ...pathComponents: strin
const directory = path.project.resolve(...pathComponents);

try {
const globOptions = {
nosort: true,
dot: true
};

// Find files/directories to be renamed
const results = [
...sync(directory + '/**/*' + oldName + '*', globOptions),
...sync(directory + '/**/*' + oldName.toLocaleLowerCase() + '*', globOptions)
];

// Sort the results so highest depth paths are replaced first
results.sort((a, b) => {
return b.length - a.length;
});
const results = getMatchingFiles(directory, oldName);

// Rename each path
results.forEach(oldPath => {
Expand All @@ -79,5 +113,28 @@ export function files(oldName: string, newName: string, ...pathComponents: strin
process.exit(1);
}

logInfo(`renaming project files in ${directory}`);
logInfo(`renamed project files in ${directory}`);
}

function getMatchingFiles(directory: string, oldName: string): string[] {
const globOptions = {
nosort: true,
dot: true
};

// Find files/directories to be renamed
const results = [
...sync(directory + '/**/*' + oldName + '*', globOptions),
...sync(directory + '/**/*' + oldName.toLocaleLowerCase() + '*', globOptions)
];

// Remove duplicate paths from the results array
const uniqueResults = Array.from(new Set(results));

// Sort the results so highest depth paths are replaced first
uniqueResults.sort((a, b) => {
return b.length - a.length;
});

return uniqueResults;
}