From 6546a863460274671c469690f465f37e99e6a38a Mon Sep 17 00:00:00 2001 From: Yuren Ju Date: Fri, 22 Aug 2014 10:17:33 +0800 Subject: [PATCH] Bug 1056610 - Migrate snippets for preferences in rule create-default-data into preferences.js r=gduan --- Makefile | 17 ++----------- build/copy-build-stage-data.js | 5 ++++ build/preferences.js | 39 ++++++++++++++++++++++++------ build/test/unit/preference.test.js | 8 +++--- 4 files changed, 44 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index fb7ecb5d9d8c..5865c3ba58fd 100644 --- a/Makefile +++ b/Makefile @@ -492,7 +492,7 @@ export BUILD_CONFIG include build/common.mk # Generate profile/ -$(PROFILE_FOLDER): preferences pre-app post-app test-agent-config offline contacts extensions $(XULRUNNER_BASE_DIRECTORY) .git/hooks/pre-commit create-default-data +$(PROFILE_FOLDER): preferences pre-app post-app test-agent-config offline contacts extensions $(XULRUNNER_BASE_DIRECTORY) .git/hooks/pre-commit ifeq ($(BUILD_APP_NAME),*) @echo "Profile Ready: please run [b2g|firefox] -profile $(CURDIR)$(SEP)$(PROFILE_FOLDER)" endif @@ -544,6 +544,7 @@ ifeq ($(BUILD_APP_NAME),*) ifdef CONTACTS_PATH @echo "Copying preload contacts to profile" @cp $(CONTACTS_PATH) $(PROFILE_FOLDER) + @cp $(CONTACTS_PATH) $(PROFILE_FOLDER)/defaults/contacts.json else @rm -f $(PROFILE_FOLDER)/contacts.json endif @@ -966,20 +967,6 @@ else endif $(ADB) shell start b2g -# create default data, gonk-misc will copy this folder during B2G build time -create-default-data: preferences $(PROFILE_FOLDER)/settings.json contacts -ifeq ($(BUILD_APP_NAME),*) - # create a clean folder to store data for B2G, this folder will copy to b2g output folder. - rm -rf $(PROFILE_FOLDER)/defaults - mkdir -p $(PROFILE_FOLDER)/defaults/pref - # rename user_pref() to pref() in user.js - sed s/user_pref\(/pref\(/ $(PROFILE_FOLDER)/user.js > $(PROFILE_FOLDER)/defaults/pref/user.js - cp $(PROFILE_FOLDER)/settings.json $(PROFILE_FOLDER)/defaults/settings.json -ifdef CONTACTS_PATH - cp $(PROFILE_FOLDER)/contacts.json $(PROFILE_FOLDER)/defaults/contacts.json -endif -endif - # clean out build products clean: rm -rf profile profile-debug profile-test profile-gaia-test-b2g profile-gaia-test-firefox $(PROFILE_FOLDER) $(STAGE_DIR) docs diff --git a/build/copy-build-stage-data.js b/build/copy-build-stage-data.js index 8fe92d8e3bf0..09bb0809aac2 100644 --- a/build/copy-build-stage-data.js +++ b/build/copy-build-stage-data.js @@ -79,8 +79,13 @@ function genWebappJSON(config) { */ function copySettingsJStoProfile(stageDir, profileDir) { var settingsFile = stageDir.clone(); + var defaultsDir = profileDir.clone(); settingsFile.append('settings_stage.json'); settingsFile.copyTo(profileDir, 'settings.json'); + + defaultsDir.append('defaults'); + utils.ensureFolderExists(defaultsDir); + settingsFile.copyTo(defaultsDir, 'settings.json'); } function execute(options) { diff --git a/build/preferences.js b/build/preferences.js index 22a14e18f11b..b973d5453ad5 100644 --- a/build/preferences.js +++ b/build/preferences.js @@ -39,10 +39,35 @@ PreferencesBuilder.prototype.execute = function(config) { this.preparePref(); this.loadBuildPrefs(); if (this.gaia.engine === 'xpcshell') { - this.writePrefs(); + let jsPath = utils.getFile(this.config.PROFILE_DIR, 'user.js').path; + this.writePrefs(jsPath, this.prefs, this.userPrefs); } else if (this.gaia.engine === 'b2g') { this.setPrefs(); } + this.writeDefaultUserJs(); +}; + +PreferencesBuilder.prototype.writeDefaultUserJs = function() { + if (this.config.BUILD_APP_NAME !== '*') { + return; + } + + // create a clean folder to store data for B2G, this folder will copy to + // b2g output folder. + let defaultsDir = utils.getFile(this.config.PROFILE_DIR, 'defaults'); + if (defaultsDir.exists()) { + defaultsDir.remove(true); + } + defaultsDir.append('pref'); + utils.ensureFolderExists(defaultsDir); + let userJs = defaultsDir.clone(); + userJs.append('user.js'); + let allPrefs = utils.cloneJSON(this.prefs); + for (let pref in this.userPrefs) { + allPrefs[pref] = this.userPrefs[pref]; + } + + this.writePrefs(userJs.path, allPrefs, {}); }; PreferencesBuilder.prototype.loadBuildPrefs = function() { @@ -283,19 +308,19 @@ PreferencesBuilder.prototype.setDeviceDebugPref = function() { this.userPrefs['b2g.adb.timeout'] = 0; }; -PreferencesBuilder.prototype.writePrefs = function() { - var userJs = utils.getFile(this.config.PROFILE_DIR, 'user.js'); +PreferencesBuilder.prototype.writePrefs = function(jsPath, prefs, userPrefs) { + var userJs = utils.getFile(jsPath); var content = ''; var pref; // output pref - for (pref in this.prefs) { + for (pref in prefs) { content += 'pref(\'' + pref + '\', ' + - JSON.stringify(this.prefs[pref]) + ');\n'; + JSON.stringify(prefs[pref]) + ');\n'; } // output user_pref - for (pref in this.userPrefs) { + for (pref in userPrefs) { content += 'user_pref(\'' + pref + '\', ' + - JSON.stringify(this.userPrefs[pref]) + ');\n'; + JSON.stringify(userPrefs[pref]) + ');\n'; } utils.writeContent(userJs, content + '\n'); }; diff --git a/build/test/unit/preference.test.js b/build/test/unit/preference.test.js index d3a268a1cc24..35867e3eb64b 100644 --- a/build/test/unit/preference.test.js +++ b/build/test/unit/preference.test.js @@ -20,9 +20,9 @@ suite('preferences.js', function() { return config; } }; - mockUtils.getFile = function(src, path) { + mockUtils.getFile = function() { return { - path: src + path, + path: Array.prototype.join.call(arguments, '/'), exists: function() { return true; }, @@ -141,7 +141,9 @@ suite('preferences.js', function() { preferences.userPrefs = { 'testKey2': 'testContent2' }; - preferences.writePrefs(); + preferences.writePrefs(preferences.config.PROFILE_DIR + 'user.js', + preferences.prefs, preferences.userPrefs + ); assert.deepEqual(fileContent, { file: preferences.config.PROFILE_DIR + 'user.js', content: 'pref(\'' + 'testKey' + '\', ' +