Gestion des imports dans les profils

Cette révision appartient à :
wpetit 2015-11-05 14:39:48 +01:00
Parent bb26fc2759
révision 996ccd90f1
8 fichiers modifiés avec 101 ajouts et 23 suppressions

Voir le fichier

@ -1,5 +1,8 @@
{
"items": [
{
"import": "./partial-profile.json"
},
{
"label": "Level 1",
"icon": "chromium-browser",
@ -12,15 +15,13 @@
{
"label": "Chromium Browser 1",
"icon": "chromium-browser",
"exec": "/usr/bin/chromium-browser",
"_key": "item_1444480285022_3"
"exec": "/usr/bin/chromium-browser"
},
{
"label": "Pitaya Edit",
"exec": "PITAYA_MODE=edit PITAYA_AS_DESKTOP=0 npm start"
}
],
"_key": "item_1444480285022_2"
]
},
{
"label": "Level 2-3",
@ -33,32 +34,24 @@
{
"label": "Chromium Browser 2",
"icon": "chromium-browser",
"exec": "/usr/bin/chromium-browser",
"_key": "item_1444480285022_6"
"exec": "/usr/bin/chromium-browser"
},
{
"label": "Atom",
"icon": "atom",
"exec": "/usr/share/atom/atom %U",
"_key": "item_1444480288996_7",
"items": []
},
{
"label": "Firefox Developer Edition Web Browser",
"icon": "firefox",
"exec": "firefox %u",
"_key": "item_1444761351301_2",
"selected": true
"exec": "firefox %u"
}
],
"_key": "item_1444480285022_5"
]
}
],
"_key": "item_1444480285022_4"
]
}
],
"_key": "item_1444480285022_1"
]
}
],
"_key": "item_1444480285021_0"
]
}

5
partial-profile.json Fichier normal
Voir le fichier

@ -0,0 +1,5 @@
{
"label": "Partial Level 1",
"icon": "chromium-browser",
"background": "./img/background2.jpg"
}

Voir le fichier

@ -7,7 +7,9 @@ var ItemForm = React.createClass({
return {
label: '',
icon: '',
exec: ''
exec: '',
import: '',
background: ''
};
},
@ -19,7 +21,8 @@ var ItemForm = React.createClass({
label: props.item.label,
icon: props.item.icon,
exec: props.item.exec,
background: props.item.background
background: props.item.background,
import: props.item.import
});
}
@ -54,6 +57,11 @@ var ItemForm = React.createClass({
placeholder="Fond d'écran" value={state.background}
onChange={this.handleChange.bind(this, 'background')} />
</div>
<div className="form-group">
<input type="text" className="form-control"
placeholder="Import" value={state.import}
onChange={this.handleChange.bind(this, 'import')} />
</div>
</form>
</div>
);

Voir le fichier

@ -21,7 +21,7 @@ var ProfileMenu = React.createClass({
var dispatch = this.props.dispatch;
this.showOpenProfileDialog()
.then(function(profilePath) {
if(profilePath) dispatch(actions.common.loadProfile(profilePath));
if(profilePath) dispatch(actions.common.loadProfile(profilePath, false));
})
;
},

Voir le fichier

@ -4,7 +4,7 @@ var LOAD_PROFILE = exports.LOAD_PROFILE = 'LOAD_PROFILE';
var LOAD_PROFILE_SUCCESS = exports.LOAD_PROFILE_SUCCESS = 'LOAD_PROFILE_SUCCESS';
var LOAD_PROFILE_FAILED = exports.LOAD_PROFILE_FAILED = 'LOAD_PROFILE_FAILED';
exports.loadProfile = function(profilePath) {
exports.loadProfile = function(profilePath, withImports) {
return function(dispatch, getState) {
@ -12,7 +12,7 @@ exports.loadProfile = function(profilePath) {
Util.Logger.info('Loading profile "%s"', profilePath);
return Util.System.loadJSON(profilePath)
return Util.Profile.load(profilePath, withImports)
.then(function(profile) {
Util.Logger.info('Profile loaded.');
dispatch({ type: LOAD_PROFILE_SUCCESS, profile: profile });

Voir le fichier

@ -6,3 +6,4 @@ exports.Tree = require('./tree');
exports.Const = require('./const');
exports.Promises = require('./promises');
exports.App = require('./app');
exports.Profile = require('./profile');

52
src/util/profile.js Fichier normal
Voir le fichier

@ -0,0 +1,52 @@
var RecursiveIterator = require('recursive-iterator');
var System = require('./system');
var Logger = require('./logger');
var _ = require('lodash');
var path = require('path');
// Load a profile file with imports
exports.load = function(profileUrl, withImports) {
withImports = withImports || true;
// Load root profile
return System.loadJSON(profileUrl)
.then(function(profile) {
if(!withImports) {
return profile;
}
var promises = [];
// Search for imports
var iterator = new RecursiveIterator(profile);
for(var node, item = iterator.next(); !item.done; item = iterator.next()) {
node = item.value.node;
// For each import found, load the partial and "mount" it on the current item
if(node.import) {
Logger.info('Loading import "%s"', path.resolve(node.import));
p = exports.load(node.import)
.then(_mountImport.bind(node))
;
promises.push(p);
}
}
// When all imports are loaded, return the complete profile
return Promise.all(promises)
.then(function() {
return profile;
})
;
})
;
// Internal method, extend "this" (the mount point) with the partial profile
function _mountImport(importProfile) {
_.extend(this, importProfile);
}
};

19
test/profile.js Fichier normal
Voir le fichier

@ -0,0 +1,19 @@
var Profile = require('../src/util/profile');
var ProfileSuite = module.exports = {};
ProfileSuite.loadProfileWithImport = function(test) {
Profile.load(__dirname+'/../default-profile.json')
.then(function(profile) {
console.log('%j', profile);
test.ok(profile.items[0].label === "Partial Level 1", "It should have loaded the partial import !");
test.done();
})
.catch(function(err) {
test.ifError(err);
test.done();
})
;
};