Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #23261 from yurenju/prefs-create-default-data
Browse files Browse the repository at this point in the history
Bug 1056610 - Migrate snippets for preferences in rule create-default-data into preferences.js r=gduan
  • Loading branch information
yurenju committed Aug 27, 2014
2 parents 50ecd2b + 6546a86 commit 111257a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
17 changes: 2 additions & 15 deletions Makefile
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions build/copy-build-stage-data.js
Expand Up @@ -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) {
Expand Down
39 changes: 32 additions & 7 deletions build/preferences.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -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');
};
Expand Down
8 changes: 5 additions & 3 deletions build/test/unit/preference.test.js
Expand Up @@ -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;
},
Expand Down Expand Up @@ -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' + '\', ' +
Expand Down

0 comments on commit 111257a

Please sign in to comment.