Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/appetizermonster/hain
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Liu committed May 25, 2016
2 parents 6efd8d4 + f95dace commit 3355bdb
Show file tree
Hide file tree
Showing 22 changed files with 212 additions and 47 deletions.
9 changes: 6 additions & 3 deletions app/main-es6/conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ const PLUGIN_PREF_DIR = `${HAIN_USER_PATH}/prefs/plugins`;
const APP_PREF_DIR = `${HAIN_USER_PATH}/prefs/app`;

const __PLUGIN_PREINSTALL_DIR = path.resolve('./pre_install');
const __PLUGIN_PREUNINSTALL_FILE = path.resolve('./pre_uninstall');
const __PLUGIN_UNINSTALL_LIST_FILE = path.resolve('./pre_uninstall');
const __PLUGIN_UPDATE_LIST_FILE = path.resolve('./pre_update');

const INTERNAL_PLUGIN_REPO = path.join(__dirname, './plugins');
const MAIN_PLUGIN_REPO = path.resolve(`${HAIN_USER_PATH}/plugins`);
const DEV_PLUGIN_REPO = path.resolve(`${HAIN_USER_PATH}/devplugins`);

const CURRENT_API_VERSION = `hain-${pkgJson.version}`;
const CURRENT_API_VERSION = 'hain-0.4.0';
const COMPATIBLE_API_VERSIONS = [
'hain0',
'hain-0.1.0',
'hain-0.3.0',
CURRENT_API_VERSION
];

Expand All @@ -38,7 +40,8 @@ module.exports = {
LOCAL_STORAGE_DIR,
PLUGIN_REPOS,
__PLUGIN_PREINSTALL_DIR,
__PLUGIN_PREUNINSTALL_FILE,
__PLUGIN_UNINSTALL_LIST_FILE,
__PLUGIN_UPDATE_LIST_FILE,
CURRENT_API_VERSION,
COMPATIBLE_API_VERSIONS
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

jest.unmock('../check-update');
jest.mock('got');

const mock_got = require('got');
const checkForUpdate = require('../check-update');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

jest.unmock('../search-client');
jest.unmock('../util');
jest.mock('got');

const mock_got = require('got');
const searchClient = require('../search-client');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

jest.unmock('../util');

const util = require('../util');

describe('util.js', () => {
Expand Down
10 changes: 6 additions & 4 deletions app/main-es6/plugins/hain-package-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const semver = require('semver');

const Packman = require('./packman');
const searchClient = require('./search-client');
const util = require('./util');

const COMMANDS_RE = / (install|update|uninstall|list)(\s+([^\s]+))?/i;
const NAME = 'hain-package-manager (experimental)';
Expand All @@ -25,7 +26,8 @@ module.exports = (context) => {
internalRepo: context.INTERNAL_PLUGIN_REPO,
tempDir: path.resolve('./_temp'),
installDir: context.__PLUGIN_PREINSTALL_DIR,
uninstallFile: context.__PLUGIN_PREUNINSTALL_FILE
uninstallListFile: context.__PLUGIN_UNINSTALL_LIST_FILE,
updateListFile: context.__PLUGIN_UPDATE_LIST_FILE
};
const pm = new Packman(packmanOpts);
const toast = context.toast;
Expand Down Expand Up @@ -99,7 +101,7 @@ module.exports = (context) => {
payload: cmdType,
title: `${customName || pkgInfo.name} ` +
` <span style='font-size: 9pt'>${pkgInfo.internal ? 'internal' : pkgInfo.version}` +
`${!pkgInfo.internal ? ` by <b>${pkgInfo.author}</b>` : ''}` +
`${!pkgInfo.internal ? ` by <b>${util.parseAuthor(pkgInfo.author)}</b>` : ''}` +
`</span>`,
desc: `${pkgInfo.desc}`,
group
Expand Down Expand Up @@ -228,7 +230,7 @@ module.exports = (context) => {

function uninstallPackage(packageName) {
try {
pm.removePackage(packageName);
pm.uninstallPackage(packageName);
toast.enqueue(`${packageName} has uninstalled, Reload plugins to take effect`, 3000);
} catch (e) {
toast.enqueue(e.toString());
Expand Down Expand Up @@ -258,7 +260,7 @@ module.exports = (context) => {
logger.log(`Updating ${packageName}`);
currentStatus = `Updating <b>${packageName}</b>`;
try {
pm.removePackage(packageName);
pm.uninstallPackageForUpdate(packageName);
yield pm.installPackage(packageName, 'latest');
toast.enqueue(`${packageName} has updated, Reload plugins to take effect`, 3000);
logger.log(`${packageName} has pre-installed (for update)`);
Expand Down
29 changes: 20 additions & 9 deletions app/main-es6/plugins/hain-package-manager/packman.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ const packageControl = require('./package-control');
const fileutil = require('../../utils/fileutil');
const fs = require('fs');

function _createPackegeInfo(name, data, internal) {
const util = require('./util');

function _createPackageInfo(name, data, internal) {
return {
name,
version: data.version || 'none',
desc: data.description || '',
author: data.author || '',
author: util.parseAuthor(data.author) || '',
homepage: data.homepage || '',
internal: !!internal
};
Expand All @@ -27,7 +29,8 @@ class Packman {
this.internalRepoDir = opts.internalRepo;
this.tempDir = opts.tempDir;
this.installDir = opts.installDir;
this.uninstallFile = opts.uninstallFile;
this.updateListFile = opts.updateListFile;
this.uninstallListFile = opts.uninstallListFile;

this.packages = [];
this.internalPackages = [];
Expand All @@ -44,7 +47,7 @@ class Packman {
try {
const fileContents = yield fileutil.readFile(packageJsonFile);
const pkgJson = JSON.parse(fileContents.toString());
const pkgInfo = _createPackegeInfo(_packageDir, pkgJson);
const pkgInfo = _createPackageInfo(_packageDir, pkgJson);
self.packages.push(pkgInfo);
} catch (e) {
console.log(e);
Expand All @@ -59,7 +62,7 @@ class Packman {
try {
const fileContents = yield fileutil.readFile(packageJsonFile);
const pkgJson = JSON.parse(fileContents.toString());
const pkgInfo = _createPackegeInfo(_packageDir, pkgJson, true);
const pkgInfo = _createPackageInfo(_packageDir, pkgJson, true);
self.internalPackages.push(pkgInfo);
} catch (e) {
console.log(e);
Expand All @@ -85,7 +88,7 @@ class Packman {
return (this.getPackage(packageName) !== undefined);
}

installPackage(packageName, versionRange, proxyAgent) {
installPackage(packageName, versionRange) {
const self = this;
return co(function* () {
if (self.hasPackage(packageName))
Expand All @@ -94,18 +97,26 @@ class Packman {
const saveDir = path.join(self.installDir, packageName);
const data = yield packageControl.installPackage(packageName, versionRange, saveDir, self.tempDir);

self.packages.push(_createPackegeInfo(packageName, data));
self.packages.push(_createPackageInfo(packageName, data));
});
}

removePackage(packageName) {
_uninstallPackage(targetListFile, packageName) {
if (!this.hasPackage(packageName))
throw `Can't find a package: ${packageName}`;

fs.appendFileSync(this.uninstallFile, `${packageName}\n`);
fs.appendFileSync(targetListFile, `${packageName}\n`);
lo_remove(this.packages, x => x.name === packageName);
}

uninstallPackageForUpdate(packageName) {
this._uninstallPackage(this.updateListFile, packageName);
}

uninstallPackage(packageName) {
this._uninstallPackage(this.uninstallListFile, packageName);
}

}

module.exports = Packman;
10 changes: 9 additions & 1 deletion app/main-es6/plugins/hain-package-manager/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const lo_isString = require('lodash.isstring');

function hasCompatibleAPIKeywords(apiVersions, keywords) {
for (const keyword of keywords) {
if (apiVersions.indexOf(keyword) >= 0)
Expand All @@ -8,4 +10,10 @@ function hasCompatibleAPIKeywords(apiVersions, keywords) {
return false;
}

module.exports = { hasCompatibleAPIKeywords };
function parseAuthor(author) {
if (lo_isString(author))
return author;
return author.name;
}

module.exports = { hasCompatibleAPIKeywords, parseAuthor };
5 changes: 5 additions & 0 deletions app/main-es6/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ rpc.on('renderPreview', (evt, msg) => {
sendmsg('renderPreview', { ticket, pluginId, id, payload });
});

rpc.on('buttonAction', (evt, msg) => {
const { pluginId, id, payload } = msg;
sendmsg('buttonAction', { pluginId, id, payload });
});

rpc.define('close', function* () {
app.close();
});
Expand Down
40 changes: 30 additions & 10 deletions app/main-es6/worker/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ module.exports = (workerContext) => {
DEV_PLUGIN_REPO: conf.DEV_PLUGIN_REPO,
INTERNAL_PLUGIN_REPO: conf.INTERNAL_PLUGIN_REPO,
__PLUGIN_PREINSTALL_DIR: conf.__PLUGIN_PREINSTALL_DIR,
__PLUGIN_PREUNINSTALL_FILE: conf.__PLUGIN_PREUNINSTALL_FILE,
__PLUGIN_UNINSTALL_LIST_FILE: conf.__PLUGIN_UNINSTALL_LIST_FILE,
__PLUGIN_UPDATE_LIST_FILE: conf.__PLUGIN_UPDATE_LIST_FILE,
CURRENT_API_VERSION: conf.CURRENT_API_VERSION,
COMPATIBLE_API_VERSIONS: conf.COMPATIBLE_API_VERSIONS,
// Utilities
Expand Down Expand Up @@ -170,19 +171,25 @@ module.exports = (workerContext) => {
logger.log('startup: end');
}

function removeUninstalledPlugins() {
const listFile = conf.__PLUGIN_PREUNINSTALL_FILE;
function removeUninstalledPlugins(listFile, removeData) {
if (!fs.existsSync(listFile))
return;

try {
const contents = fs.readFileSync(listFile, { encoding: 'utf8' });
const targetPlugins = contents.split('\n').filter((val) => (val && val.trim().length > 0));
const repoDir = conf.MAIN_PLUGIN_REPO;

for (const packageName of targetPlugins) {
const packageDir = path.join(repoDir, packageName);
const packageDir = path.join(conf.MAIN_PLUGIN_REPO, packageName);
fse.removeSync(packageDir);

if (removeData) {
const storageDir = path.join(conf.LOCAL_STORAGE_DIR, packageName);
const prefFile = path.join(conf.PLUGIN_PREF_DIR, packageName);
fse.removeSync(storageDir);
fse.removeSync(prefFile);
}

logger.log(`${packageName} has uninstalled successfully`);
}
fse.removeSync(listFile);
Expand Down Expand Up @@ -211,7 +218,8 @@ module.exports = (workerContext) => {
}

function* initialize() {
removeUninstalledPlugins();
removeUninstalledPlugins(conf.__PLUGIN_UNINSTALL_LIST_FILE, true);
removeUninstalledPlugins(conf.__PLUGIN_UPDATE_LIST_FILE, false);
yield movePreinstalledPlugins();

const ret = pluginLoader.loadPlugins(generatePluginContext);
Expand Down Expand Up @@ -256,9 +264,7 @@ module.exports = (workerContext) => {
try {
plugin.search(_query, pluginResponse);
} catch (e) {
logger.log(e);
if (e.stack)
logger.log(e.stack);
logger.log(e.stack || e);
}
}

Expand Down Expand Up @@ -293,6 +299,19 @@ module.exports = (workerContext) => {
}
}

function buttonAction(pluginId, id, payload) {
if (plugins[pluginId] === undefined)
return;
const buttonActionFunc = plugins[pluginId].buttonAction;
if (buttonActionFunc === undefined)
return;
try {
buttonActionFunc(id, payload);
} catch (e) {
logger.log(e.stack || e);
}
}

function getPrefIds() {
return pluginPrefIds;
}
Expand Down Expand Up @@ -336,10 +355,11 @@ module.exports = (workerContext) => {
searchAll,
execute,
renderPreview,
buttonAction,
getPrefIds,
getPreferences,
updatePreferences,
commitPreferences,
resetPreferences
};
};
};
4 changes: 4 additions & 0 deletions app/main-es6/worker/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ const msgHandlers = {
};
plugins.renderPreview(pluginId, id, payload, render);
},
buttonAction: (_payload) => {
const { pluginId, id, payload } = _payload;
plugins.buttonAction(pluginId, id, payload);
},
getPluginPrefIds: (payload) => {
const prefIds = plugins.getPrefIds();
procMsg.send('on-get-plugin-pref-ids', prefIds);
Expand Down
6 changes: 5 additions & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"name": "hain",
"version": "0.3.0",
"version": "0.4.0",
"description": "An `alt+space` launcher for Windows, built with Electron",
"main": "main-es6/main.js",
"author": "Heejin Lee <[email protected]>",
"license": "MIT",
"jest": {
"automock": false,
"testEnvironment": "node"
},
"dependencies": {
"application-config-path": "^0.1.0",
"co": "^4.6.0",
Expand Down
Loading

0 comments on commit 3355bdb

Please sign in to comment.