Skip to content

Commit

Permalink
Working UpdateService to apply update scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
leeroybrun committed Dec 11, 2014
1 parent 7a50bf3 commit 7205795
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
53 changes: 39 additions & 14 deletions src/background/UpdateService.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
(function(Logger){
(function(Logger, ChromeHelper, Spotify, Shazam, Tags){
var UpdateService = {
update: function(initVersionTxt, finalVersionTxt) {
var rePoint = new RegExp('\\.', 'g');

// TODO: handle versions 0.2.10 -> 210 -> bigger than 0.3.1 -> 31 !
var initVersion = parseInt(initVersionTxt.replace(rePoint, ''));
var finalVersion = parseInt(finalVersionTxt.replace(rePoint, ''));

var startIndex = null;
var endIndex = null;

console.log(UpdateService._updates);
var updatesToApply = [];

for(var i = 0; i < UpdateService._updates.length && (startIndex === null || endIndex === null); i++) {
console.log(i, UpdateService._updates[i].version, initVersion, finalVersion, startIndex, endIndex);
// If initVersion is the same or bigger than final, nothing to do...
var shouldStop = initVersion >= finalVersion;

for(var i = 0; i < UpdateService._updates.length && shouldStop === false; i++) {
if(startIndex === null && UpdateService._updates[i].version > initVersion) {
startIndex = i;
}

console.log(i, UpdateService._updates[i].version, initVersion, finalVersion, startIndex, endIndex);

if(startIndex !== null && UpdateService._updates[i].version <= finalVersion) {
endIndex = i;

updatesToApply.push(UpdateService._updates[i]);
}

console.log(i, UpdateService._updates[i].version, initVersion, finalVersion, startIndex, endIndex);
shouldStop = (startIndex !== null && endIndex !== null && UpdateService._updates[i].version >= finalVersion);
}

if(startIndex !== null && endIndex !== null) {
Logger.info('[Updater] '+ (endIndex-startIndex+1) +' update scripts to call to go from v'+ initVersionTxt +' to v'+ finalVersionTxt +'.');
Logger.info('[Updater] '+ (endIndex-startIndex+1) +' update scripts ('+startIndex+'->'+endIndex+') to call to go from v'+ initVersionTxt +' to v'+ finalVersionTxt +'.');

async.eachSeries(updatesToApply, function(update, cbe) {
Logger.info('[Updater] Calling update script for v'+update.version);
update.perform(function(err) {
if(err) { Logger.error(err); }

cbe();
});
}, function() {
Logger.info('[Updater] All update scripts applied !');
});
} else {
Logger.info('[Updater] No update script defined to go from v'+ initVersionTxt +' to v'+ finalVersionTxt +'.');
}
Expand All @@ -43,17 +56,29 @@
},

_updates: [
{'version': 10, 'update': function(callback) {
{'version': 20, 'perform': function(callback) {
s2s.Logger.info('[Update] Cleaning extension\'s background data.');

var popups = chrome.extension.getViews({type: 'popup'});
if(popups && popups.length) {
popups[0].window.close();
}

ChromeHelper.clearStorage();

// Clear cached data from background script
Tags.data.clearCache();
Spotify.data.clearCache();

}},
{'version': 20, 'update': function(callback) {
// Reload tags, will reset list & lastUpdate
s2s.Tags.load();

}},
{'version': 30, 'update': function(callback) {
UpdateService.openUpdatePage('0.2.0');

callback();
}}
]
};

window.s2s.UpdateService = UpdateService;
})(window.s2s.Logger);
})(window.s2s.Logger, window.s2s.ChromeHelper, window.s2s.Spotify, window.s2s.Shazam, window.s2s.Tags);
2 changes: 1 addition & 1 deletion static/update-0.2.0-en.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="container">
<h1>Shazam2Spotify has been updated - v0.2.0</h1>
<p>This new version should be a lot more reliable than the previous one.<br />If you had troubles syncing tags to Spotify, this should now be fixed.</p>
<p>If you experience any bug or want to request a new feature, please <a href="https://github.com/leeroybrun/chrome-shazam2spotify/issues" target="_blank">open a new issue on our Github repository</a>.</p>
<p>If you experience any bug or want to request a new feature, please don't hesitate to <a href="https://github.com/leeroybrun/chrome-shazam2spotify/issues" target="_blank">open a new issue on our Github repository</a>.</p>
<p>Thanks for using Shazam2Spotify !</p>
<p class="signature">Leeroy</p>
<p><a href="https://github.com/leeroybrun/chrome-shazam2spotify" target="_blank"><img src="img/promo_600.jpg" alt="" /></a></p>
Expand Down
2 changes: 1 addition & 1 deletion static/update-0.2.0-fr.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="container">
<h1>Shazam2Spotify a été mis à jour - v0.2.0</h1>
<p>Cette nouvelle version devrait être bien plus fiable que la précédente.<br />Si vous aviez des problèmes pour synchroniser vos tags vers Spotify, cela doit maintenant être résolu.</p>
<p>Si vous rencontrez des problèmes ou souhaiteriez de nouvelles fonctions, n'hésitez pas à <a href="https://github.com/leeroybrun/chrome-shazam2spotify/issues" target="_blank">ouvrir un nouveau ticket sur notre page Github</a>.</p>
<p>Si vous rencontrez d'autres difficultés ou souhaiteriez de nouvelles fonctions, n'hésitez pas à <a href="https://github.com/leeroybrun/chrome-shazam2spotify/issues" target="_blank">ouvrir un nouveau ticket sur notre page Github</a>.</p>
<p>Merci d'utiliser Shazam2Spotify !</p>
<p class="signature">Leeroy</p>
<p><a href="https://github.com/leeroybrun/chrome-shazam2spotify" target="_blank"><img src="img/promo_600.jpg" alt="" /></a></p>
Expand Down

0 comments on commit 7205795

Please sign in to comment.