Skip to content

Commit

Permalink
refactor: write env vars if not production deploy type
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtcoolguy committed May 28, 2019
1 parent 97428ca commit 531eb81
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 61 deletions.
64 changes: 27 additions & 37 deletions android/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const ADB = require('node-titanium-sdk/lib/adb'),
tiappxml = require('node-titanium-sdk/lib/tiappxml'),
url = require('url'),
util = require('util'),
wrench = require('wrench'),

afs = appc.fs,
i18nLib = appc.i18n(__dirname),
Expand Down Expand Up @@ -970,6 +969,9 @@ AndroidBuilder.prototype.validate = function validate(logger, config, cli) {
this.minifyJS = false;
}

// Do we write out process.env into a file in the app to use?
this.writeEnvVars = this.deployType !== 'production';

// check the app name
if (cli.tiapp.name.indexOf('&') !== -1) {
if (config.get('android.allowAppNameAmpersands', false)) {
Expand Down Expand Up @@ -2245,10 +2247,11 @@ AndroidBuilder.prototype.checkIfNeedToRecompile = function checkIfNeedToRecompil
// check if we need to do a rebuild
this.forceRebuild = this.checkIfShouldForceRebuild();

if (this.forceRebuild && fs.existsSync(this.buildGenAppIdDir)) {
wrench.rmdirSyncRecursive(this.buildGenAppIdDir);
if (this.forceRebuild) {
fs.emptyDirSync(this.buildGenAppIdDir);
} else {
fs.ensureDirSync(this.buildGenAppIdDir);
}
fs.existsSync(this.buildGenAppIdDir) || wrench.mkdirSyncRecursive(this.buildGenAppIdDir);

// now that we've read the build manifest, delete it so if this build
// becomes incomplete, the next build will be a full rebuild
Expand Down Expand Up @@ -2281,34 +2284,30 @@ AndroidBuilder.prototype.createBuildDirs = function createBuildDirs(next) {
// that build.pre.compile was fired.
ti.validateAppJsExists(this.projectDir, this.logger, 'android');

fs.existsSync(this.buildDir) || wrench.mkdirSyncRecursive(this.buildDir);
fs.ensureDirSync(this.buildDir);

// make directories if they don't already exist
let dir = this.buildAssetsDir;
if (this.forceRebuild) {
fs.existsSync(dir) && wrench.rmdirSyncRecursive(dir);
fs.emptyDirSync(dir);
Object.keys(this.lastBuildFiles).forEach(function (file) {
if (file.indexOf(dir + '/') === 0) {
delete this.lastBuildFiles[file];
}
}, this);
wrench.mkdirSyncRecursive(dir);
} else if (!fs.existsSync(dir)) {
wrench.mkdirSyncRecursive(dir);
} else {
fs.ensureDirSync(dir);
}

// we always destroy and rebuild the res directory
if (fs.existsSync(this.buildResDir)) {
wrench.rmdirSyncRecursive(this.buildResDir);
}
wrench.mkdirSyncRecursive(this.buildResDir);
fs.emptyDirSync(this.buildResDir);

fs.existsSync(dir = this.buildBinAssetsResourcesDir) || wrench.mkdirSyncRecursive(dir);
fs.existsSync(dir = path.join(this.buildDir, 'gen')) || wrench.mkdirSyncRecursive(dir);
fs.existsSync(dir = path.join(this.buildDir, 'lib')) || wrench.mkdirSyncRecursive(dir);
fs.existsSync(dir = this.buildResDrawableDir) || wrench.mkdirSyncRecursive(dir);
fs.existsSync(dir = path.join(this.buildResDir, 'values')) || wrench.mkdirSyncRecursive(dir);
fs.existsSync(dir = this.buildSrcDir) || wrench.mkdirSyncRecursive(dir);
fs.ensureDirSync(this.buildBinAssetsResourcesDir);
fs.ensureDirSync(path.join(this.buildDir, 'gen'));
fs.ensureDirSync(path.join(this.buildDir, 'lib'));
fs.ensureDirSync(this.buildResDrawableDir);
fs.ensureDirSync(path.join(this.buildResDir, 'values'));
fs.ensureDirSync(this.buildSrcDir);

// create the deploy.json file which contains debugging/profiling info
const deployJsonFile = path.join(this.buildBinAssetsDir, 'deploy.json'),
Expand Down Expand Up @@ -2359,7 +2358,7 @@ AndroidBuilder.prototype.copyResources = function copyResources(next) {

function copyFile(from, to, next) {
var d = path.dirname(to);
fs.existsSync(d) || wrench.mkdirSyncRecursive(d);
fs.ensureDirSync(d);

if (fs.existsSync(to)) {
_t.logger.warn(__('Overwriting file %s', to.cyan));
Expand Down Expand Up @@ -2482,7 +2481,7 @@ AndroidBuilder.prototype.copyResources = function copyResources(next) {
}

// if the destination directory does not exists, create it
fs.existsSync(destDir) || wrench.mkdirSyncRecursive(destDir);
fs.ensureDirSync(destDir);

const ext = filename.match(extRegExp);

Expand Down Expand Up @@ -2783,7 +2782,7 @@ AndroidBuilder.prototype.copyResources = function copyResources(next) {
this.tiSymbols[to] = modified.symbols;

const dir = path.dirname(to);
fs.existsSync(dir) || wrench.mkdirSyncRecursive(dir);
fs.ensureDirSync(dir);

if (symlinkFiles && newContents === originalContents) {
this.logger.debug(__('Copying %s => %s', from.cyan, to.cyan));
Expand Down Expand Up @@ -2835,7 +2834,7 @@ AndroidBuilder.prototype.copyResources = function copyResources(next) {
fs.writeFileSync(
envVarsFile,
// for non-development builds, DO NOT WRITE OUT ENV VARIABLES TO APP
this.deployType === 'development' ? JSON.stringify(process.env) : {}
this.writeEnvVars ? JSON.stringify(process.env) : {}
);
this.encryptJS && jsFilesToEncrypt.push('_env_.json');
delete this.lastBuildFiles[envVarsFile];
Expand Down Expand Up @@ -3272,8 +3271,7 @@ AndroidBuilder.prototype.copyModuleResources = function copyModuleResources(next
this.logger.info(__('Extracting module resources: %s', resFile.cyan));

const tmp = temp.path();
fs.existsSync(tmp) && wrench.rmdirSyncRecursive(tmp);
wrench.mkdirSyncRecursive(tmp);
fs.emptyDirSync(tmp);

appc.zip.unzip(resFile, tmp, {}, function (ex) {
if (ex) {
Expand Down Expand Up @@ -3957,9 +3955,7 @@ AndroidBuilder.prototype.generateAndroidManifest = function generateAndroidManif
AndroidBuilder.prototype.packageApp = function packageApp(next) {
this.ap_File = path.join(this.buildBinDir, 'app.ap_');
const bundlesPath = path.join(this.buildIntermediatesDir, 'bundles');
if (!fs.existsSync(bundlesPath)) {
wrench.mkdirSyncRecursive(bundlesPath);
}
fs.ensureDirSync(bundlesPath);

const aaptHook = this.cli.createHook('build.android.aapt', this, function (exe, args, opts, done) {
this.logger.info(__('Running AAPT: %s', (exe + ' "' + args.join('" "') + '"').cyan));
Expand Down Expand Up @@ -4162,10 +4158,7 @@ AndroidBuilder.prototype.compileJavaClasses = function compileJavaClasses(next)
fs.writeFileSync(javaSourcesFile, '"' + javaFiles.join('"\n"').replace(/\\/g, '/') + '"');

// if we're recompiling the java files, then nuke the classes dir
if (fs.existsSync(this.buildBinClassesDir)) {
wrench.rmdirSyncRecursive(this.buildBinClassesDir);
}
wrench.mkdirSyncRecursive(this.buildBinClassesDir);
fs.emptyDirSync(this.buildBinClassesDir);

const javacHook = this.cli.createHook('build.android.javac', this, function (exe, args, opts, done) {
this.logger.info(__('Building Java source files: %s', (exe + ' "' + args.join('" "') + '"').cyan));
Expand Down Expand Up @@ -4273,10 +4266,7 @@ AndroidBuilder.prototype.runDexer = function runDexer(next) {
}

// nuke and create the folder holding all the classes*.dex files
if (fs.existsSync(this.buildBinClassesDex)) {
wrench.rmdirSyncRecursive(this.buildBinClassesDex);
}
wrench.mkdirSyncRecursive(this.buildBinClassesDex);
fs.emptyDirSync(this.buildBinClassesDex);

// Wipe existing outjar
fs.existsSync(outjar) && fs.unlinkSync(outjar);
Expand Down Expand Up @@ -4646,7 +4636,7 @@ AndroidBuilder.prototype.writeBuildManifest = function writeBuildManifest(callba
this.logger.info(__('Writing build manifest: %s', this.buildManifestFile.cyan));

this.cli.createHook('build.android.writeBuildManifest', this, function (manifest, cb) {
fs.existsSync(this.buildDir) || wrench.mkdirSyncRecursive(this.buildDir);
fs.ensureDirSync(this.buildDir);
fs.existsSync(this.buildManifestFile) && fs.unlinkSync(this.buildManifestFile);
fs.writeFile(this.buildManifestFile, JSON.stringify(this.buildManifest = manifest, null, '\t'), cb);
})({
Expand Down
48 changes: 24 additions & 24 deletions iphone/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const appc = require('node-appc'),
spawn = require('child_process').spawn, // eslint-disable-line security/detect-child-process
ti = require('node-titanium-sdk'),
util = require('util'),
wrench = require('wrench'),
xcode = require('xcode'),
xcodeParser = require('xcode/lib/parser/pbxproj'),
i18n = appc.i18n(__dirname),
Expand Down Expand Up @@ -1800,6 +1799,9 @@ iOSBuilder.prototype.validate = function validate(logger, config, cli) {
// this may have already been called in an option validate() callback
this.initTiappSettings();

// Do we write out process.env into a file in the app to use?
this.writeEnvVars = this.deployType !== 'production';

// Transpilation details
this.transpile = cli.tiapp['transpile'] !== false; // Transpiling is an opt-out process now
this.sourceMaps = cli.tiapp['source-maps'] === true; // opt-in to generate inline source maps
Expand Down Expand Up @@ -2866,15 +2868,13 @@ iOSBuilder.prototype.initBuildDir = function initBuildDir() {

if (this.forceCleanBuild && buildDirExists) {
this.logger.debug(__('Recreating %s', cyan(this.buildDir)));
wrench.rmdirSyncRecursive(this.buildDir);
wrench.mkdirSyncRecursive(this.buildDir);
} else if (!buildDirExists) {
this.logger.debug(__('Creating %s', cyan(this.buildDir)));
wrench.mkdirSyncRecursive(this.buildDir);
this.forceCleanBuild = true;
}
fs.emptyDirSync(this.buildDir);

fs.existsSync(this.xcodeAppDir) || wrench.mkdirSyncRecursive(this.xcodeAppDir);
fs.ensureDirSync(this.xcodeAppDir);
};

iOSBuilder.prototype.generateXcodeUuid = function generateXcodeUuid(xcodeProject) {
Expand Down Expand Up @@ -3817,7 +3817,7 @@ iOSBuilder.prototype.createXcodeProject = function createXcodeProject(next) {
this.forceRebuild = true;
}
this.logger.debug(__('Writing %s', dest.cyan));
fs.existsSync(parent) || wrench.mkdirSyncRecursive(parent);
fs.ensureDirSync(parent);
fs.writeFileSync(dest, contents);
} else {
this.logger.trace(__('No change, skipping %s', dest.cyan));
Expand Down Expand Up @@ -3885,7 +3885,7 @@ iOSBuilder.prototype._embedCapabilitiesAndWriteEntitlementsPlist = function _emb
this.forceRebuild = true;
}
this.logger.debug(__('Writing %s', dest.cyan));
fs.existsSync(parent) || wrench.mkdirSyncRecursive(parent);
fs.ensureDirSync(parent);
fs.writeFileSync(dest, contents);
} else {
this.logger.trace(__('No change, skipping %s', dest.cyan));
Expand Down Expand Up @@ -4405,7 +4405,7 @@ iOSBuilder.prototype.copyTitaniumLibraries = function copyTitaniumLibraries() {
this.logger.info(__('Copying Titanium libraries'));

const libDir = path.join(this.buildDir, 'lib');
fs.existsSync(libDir) || wrench.mkdirSyncRecursive(libDir);
fs.ensureDirSync(libDir);

[ 'libtiverify.a' ].forEach(function (filename) {
const src = path.join(this.platformPath, filename),
Expand Down Expand Up @@ -4565,7 +4565,7 @@ iOSBuilder.prototype.copyTitaniumiOSFiles = function copyTitaniumiOSFiles() {

if (fileChanged) {
this.logger.debug(__('Writing %s', dest.cyan));
fs.existsSync(destDir) || wrench.mkdirSyncRecursive(destDir);
fs.ensureDirSync(destDir);
fs.writeFileSync(dest, contents);
} else {
this.logger.trace(__('No change, skipping %s', dest.cyan));
Expand Down Expand Up @@ -4791,7 +4791,7 @@ iOSBuilder.prototype.cleanXcodeDerivedData = function cleanXcodeDerivedData(next
} else {
done = true;
lastErr = null;
fs.existsSync(this.xcodeAppDir) || wrench.mkdirSyncRecursive(this.xcodeAppDir);
fs.ensureDirSync(this.xcodeAppDir);
out.split('\n').forEach(function (line) {
line = line.trim();
line && this.logger.trace(line);
Expand Down Expand Up @@ -5045,7 +5045,7 @@ iOSBuilder.prototype.copyResources = function copyResources(next) {
this.forceRebuild = true;
}
this.logger.debug(__('Writing %s', dest.cyan));
fs.existsSync(parent) || wrench.mkdirSyncRecursive(parent);
fs.ensureDirSync(parent);
fs.writeFileSync(dest, contents);
} else {
this.logger.trace(__('No change, skipping %s', dest.cyan));
Expand Down Expand Up @@ -5139,7 +5139,7 @@ iOSBuilder.prototype.copyResources = function copyResources(next) {
}
}, this);

fs.existsSync(appIconSetDir) || wrench.mkdirSyncRecursive(appIconSetDir);
fs.ensureDirSync(appIconSetDir);

Object.keys(appIcons).forEach(function (filename) {
const info = appIcons[filename];
Expand Down Expand Up @@ -5332,7 +5332,7 @@ iOSBuilder.prototype.copyResources = function copyResources(next) {
};
let launchLogo = null;

fs.existsSync(assetCatalogDir) || wrench.mkdirSyncRecursive(assetCatalogDir);
fs.ensureDirSync(assetCatalogDir);

// loop over each of the launch logos that we found, then for each remove it from the lookup
// anything left in the lookup will be considered missing
Expand Down Expand Up @@ -5643,7 +5643,7 @@ iOSBuilder.prototype.copyResources = function copyResources(next) {
},
found = {};

fs.existsSync(launchImageDir) || wrench.mkdirSyncRecursive(launchImageDir);
fs.ensureDirSync(launchImageDir);

Object.keys(launchImages).forEach(function (filename) {
const info = launchImages[filename];
Expand Down Expand Up @@ -5831,7 +5831,7 @@ iOSBuilder.prototype.copyResources = function copyResources(next) {
if (this.minifyCSS) {
this.logger.debug(__('Copying and minifying %s => %s', info.src.cyan, info.dest.cyan));
const dir = path.dirname(info.dest);
fs.existsSync(dir) || wrench.mkdirSyncRecursive(dir);
fs.ensureDirSync(dir);
fs.writeFileSync(info.dest, new CleanCSS({ processImport: false }).minify(fs.readFileSync(info.src).toString()).styles);
} else if (!this.copyFileSync(info.src, info.dest, { forceCopy: unsymlinkableFileRegExp.test(path.basename(file)) })) {
this.logger.trace(__('No change, skipping %s', info.dest.cyan));
Expand Down Expand Up @@ -5906,7 +5906,7 @@ iOSBuilder.prototype.copyResources = function copyResources(next) {
this.tiSymbols[to] = modified.symbols;

const dir = path.dirname(to);
fs.existsSync(dir) || wrench.mkdirSyncRecursive(dir);
fs.ensureDirSync(dir);

const exists = fs.existsSync(to);
// dest doesn't exist, or new contents differs from existing dest file
Expand Down Expand Up @@ -5986,8 +5986,8 @@ iOSBuilder.prototype.copyResources = function copyResources(next) {
if (!fs.existsSync(appPropsFile) || contents !== fs.readFileSync(appPropsFile).toString()) {
this.logger.debug(__('Writing %s', appPropsFile.cyan));

if (this.encryptJS && !fs.existsSync(this.buildAssetsDir)) {
wrench.mkdirSyncRecursive(this.buildAssetsDir);
if (this.encryptJS) {
fs.ensureDirSync(this.buildAssetsDir);
}
fs.writeFileSync(appPropsFile, contents);
} else {
Expand All @@ -5998,7 +5998,7 @@ iOSBuilder.prototype.copyResources = function copyResources(next) {
},

function writeEnvironmentVariables() {
this.logger.info(__('Writing ENV variables'));
this.logger.debug(__('Writing ENV variables'));

const envVarsFile = this.encryptJS ? path.join(this.buildAssetsDir, '_env__json') : path.join(this.xcodeAppDir, '_env_.json');

Expand All @@ -6008,12 +6008,12 @@ iOSBuilder.prototype.copyResources = function copyResources(next) {
}

// for non-development builds, DO NOT WRITE OUT ENV VARIABLES TO APP
const contents = this.deployType === 'development' ? JSON.stringify(process.env) : {};
const contents = this.writeEnvVars ? JSON.stringify(process.env) : {};
if (!fs.existsSync(envVarsFile) || contents !== fs.readFileSync(envVarsFile).toString()) {
this.logger.debug(__('Writing %s', envVarsFile.cyan));

if (this.encryptJS && !fs.existsSync(this.buildAssetsDir)) {
wrench.mkdirSyncRecursive(this.buildAssetsDir);
if (this.encryptJS) {
fs.ensureDirSync(this.buildAssetsDir);
}
fs.writeFileSync(envVarsFile, contents);
} else {
Expand Down Expand Up @@ -6232,7 +6232,7 @@ iOSBuilder.prototype.writeI18NFiles = function writeI18NFiles() {
if (keys.length) {
keys.forEach(function (lang) {
const dir = path.join(this.xcodeAppDir, lang + '.lproj');
fs.existsSync(dir) || wrench.mkdirSyncRecursive(dir);
fs.ensureDirSync(dir);

add.call(this, data[lang].app, path.join(dir, 'InfoPlist.strings'), { appname: 'CFBundleDisplayName' });
add.call(this, data[lang].strings, path.join(dir, 'Localizable.strings'));
Expand Down Expand Up @@ -6741,7 +6741,7 @@ iOSBuilder.prototype.invokeXcodeBuild = function invokeXcodeBuild(next) {

iOSBuilder.prototype.writeBuildManifest = function writeBuildManifest(next) {
this.cli.createHook('build.ios.writeBuildManifest', this, function (manifest, cb) {
fs.existsSync(this.buildDir) || wrench.mkdirSyncRecursive(this.buildDir);
fs.ensureDirSync(this.buildDir);
fs.existsSync(this.buildManifestFile) && fs.unlinkSync(this.buildManifestFile);
fs.writeFile(this.buildManifestFile, JSON.stringify(this.buildManifest = manifest, null, '\t'), cb);
})(this.currentBuildManifest, next);
Expand Down

0 comments on commit 531eb81

Please sign in to comment.