Browse Source

Merge branch 'feature/auto-launch' into develop

oldj 8 years ago
parent
commit
1cace69e43

+ 0 - 1
README.md

@@ -20,7 +20,6 @@ Homepage: [https://oldj.github.io/SwitchHosts/](https://oldj.github.io/SwitchHos
 
  - [SwitchHosts! 下载地址1](https://github.com/oldj/SwitchHosts/releases)
  - [SwitchHosts! 下载地址2](http://pan.baidu.com/share/link?shareid=150951&uk=3607385901)
- - [SwitchHosts! for Linux 下载地址](http://pan.baidu.com/s/1slGQ6kP)
 
 
 ## 文件备份

File diff suppressed because it is too large
+ 462 - 428
app/build/bundle.js


+ 3 - 0
app/main.js

@@ -14,6 +14,9 @@ const app = electron.app;
 // Module to create native browser window.
 const BrowserWindow = electron.BrowserWindow;
 
+// const yargs = require('yargs');
+// console.log('argv', yargs.argv);
+
 const tray = require('./src/modules/tray');
 const pref = require('./src/libs/pref');
 let user_language = pref.get('user_language') || (app.getLocale() || '').split('-')[0].toLowerCase() || 'en';

+ 3 - 2
app/package.json

@@ -23,7 +23,8 @@
     "react": "^15.3.1",
     "react-addons-update": "^15.3.1",
     "react-dom": "^15.3.1",
-    "request": "^2.74.0"
+    "request": "^2.74.0",
+    "yargs": "^6.6.0"
   },
   "devDependencies": {}
-}
+}

+ 1 - 1
app/src/components/frame/frame.less

@@ -44,7 +44,7 @@
         .title {
           float: left;
           width: 100px;
-          line-height: 18px;
+          //line-height: 18px;
         }
         .cnt {
           margin-left: 100px;

+ 30 - 1
app/src/components/frame/preferences.js

@@ -11,6 +11,8 @@ import Frame from './frame';
 import './preferences.less';
 import lang from '../../lang';
 
+const AUTO_LAUNCH = 'auto_launch';
+
 export default class PreferencesPrompt extends React.Component {
     constructor(props) {
         super(props);
@@ -20,11 +22,14 @@ export default class PreferencesPrompt extends React.Component {
             choice_mode = 'multiple';
         }
 
+        console.log(AUTO_LAUNCH, SH_Agent.pref.get(AUTO_LAUNCH));
+
         this.state = {
             show: false,
             lang_key: SH_Agent.lang_key,
             after_cmd: SH_Agent.pref.get('after_cmd') || '',
-            choice_mode: choice_mode
+            choice_mode: choice_mode,
+            auto_launch: !!SH_Agent.pref.get(AUTO_LAUNCH)
         };
 
     }
@@ -88,6 +93,15 @@ export default class PreferencesPrompt extends React.Component {
         });
     }
 
+    updateAutoLaunch(v) {
+        SH_Agent.pref.set(AUTO_LAUNCH, v);
+        this.setState({
+            auto_launch: v
+        });
+
+        // todo set auto launch
+    }
+
     prefLanguage() {
         return (
             <div className="ln">
@@ -143,6 +157,20 @@ export default class PreferencesPrompt extends React.Component {
         )
     }
 
+    prefAutoLaunch() {
+        return (
+            <div className="ln">
+                <div className="title">{SH_Agent.lang.auto_launch}</div>
+                <div className="cnt">
+                    <input type="checkbox" name=""
+                           defaultChecked={this.state.auto_launch}
+                           onChange={(e) => this.updateAutoLaunch(e.target.checked)}
+                    />
+                </div>
+            </div>
+        )
+    }
+
     body() {
         return (
             <div ref="body">
@@ -152,6 +180,7 @@ export default class PreferencesPrompt extends React.Component {
                 {this.prefLanguage()}
                 {this.prefChoiceMode()}
                 {this.prefAfterCmd()}
+                {/*{this.prefAutoLaunch()}*/}
             </div>
         )
     }

+ 1 - 0
app/src/lang/cn.js

@@ -8,6 +8,7 @@
 exports.content = {
     _lang_name: '简体中文'
     , add: '添加'
+    , auto_launch: '随系统一起启动'
     , comment: '注释'
     , new: '新建'
     , quit: '退出'

+ 2 - 1
app/src/lang/en.js

@@ -8,6 +8,7 @@
 exports.content = {
     _lang_name: 'English'
     , add: 'Add'
+    , auto_launch: 'Run at login'
     , comment: 'Comment'
     , new: 'New'
     , quit: 'Quit'
@@ -15,7 +16,7 @@ exports.content = {
     , ok: 'OK'
     , set_and_back: 'Set and back'
     , set_and_relaunch_app: 'Set and Relaunch App'
-    , add_host: 'Add new rules.'
+    , add_host: 'Add new rules'
     , edit_host: 'Edit host'
     , import: 'Import'
     , export: 'Export'

+ 0 - 98
app/src/libs/lang.js

@@ -1,98 +0,0 @@
-/**
- * @author oldj
- * @blog http://oldj.net
- */
-
-'use strict';
-
-const languages = {
-    'en': {
-        add: 'Add'
-        , cancel: 'Cancel'
-        , ok: 'OK'
-        , add_host: 'Add new rules.'
-        , edit_host: 'Edit host'
-        , host_title: 'Host title'
-        , host_title_cant_be_empty: 'Host title could not be empty!'
-        , sys_host_title: 'System Hosts'
-        , input_sudo_pswd: 'Input your sudo password'
-        , sudo_pswd: 'Password'
-        , del_host: 'Delete current host'
-        , confirm_del: 'Are you sure you want to delete this host?'
-        , tmp_clean: 'Temporarily turn off all rules.'
-        , tmp_recover: 'Recover rules.'
-        , new_version_available: 'New version available, download now?'
-        , is_updated_title: 'You are up to date!'
-        , is_updated: 'You already have the latest version of SwitchHosts! installed.'
-        , readonly: 'Read only'
-        , where_local: 'local'
-        , where_remote: 'remote'
-        , remote_hosts: 'Remote hosts'
-        , url: 'URL'
-        , bad_url: 'URL is not valid.'
-        , auto_refresh: 'Auto refresh'
-        , last_refresh: 'Last refresh'
-        , refresh: 'Refresh'
-        , never: 'never'
-        , hour: 'hour'
-        , hours: 'hours'
-        , day: 'day'
-        , days: 'days'
-        , hide_dock_icon: 'Hide Dock Icon'
-        , show_dock_icon: 'Show Dock Icon'
-        , toggle_dock_icon: 'Toggle Dock Icon'
-        , no_valid_host_found: 'There is no valid host in the file.'
-        , confirm_import: 'You sure you want to import it? The original rules will be overwriten, this operation can not be undone.'
-    },
-    'cn': {
-        add: '添加'
-        , cancel: '取消'
-        , ok: '确定'
-        , add_host: '添加 host 规则'
-        , edit_host: '修改 host'
-        , host_title: 'host 方案名'
-        , host_title_cant_be_empty: 'Host 方案名不能为空!'
-        , sys_host_title: '系统 Hosts'
-        , input_sudo_pswd: '请输入您的开机密码'
-        , sudo_pswd: '密码'
-        , del_host: '删除当前 host'
-        , confirm_del: '确定要删除此 host 吗?'
-        , tmp_clean: '临时去掉所有绑定'
-        , tmp_recover: '恢复绑定'
-        , new_version_available: '检测到新版本,立刻下载?'
-        , is_updated_title: '已是最新'
-        , is_updated: '当前版本是最新版本。'
-        , readonly: '只读'
-        , where_local: '本地'
-        , where_remote: '远程'
-        , remote_hosts: '远程方案'
-        , url: 'URL 地址'
-        , bad_url: 'URL 地址有误。'
-        , auto_refresh: '自动更新'
-        , last_refresh: '上次更新'
-        , refresh: '刷新'
-        , never: '从不'
-        , hour: '小时'
-        , hours: '小时'
-        , day: '天'
-        , days: '天'
-        , hide_dock_icon: '隐藏 Dock 图标'
-        , show_dock_icon: '显示 Dock 图标'
-        , toggle_dock_icon: '显示/隐藏 Dock 图标'
-        , no_valid_host_found: '所指定的文件中未找到合法的 host 配置'
-        , confirm_import: '确定要导入吗?原方案列表将被覆盖,此操作不可撤销。'
-    }
-};
-
-module.exports = {
-    languages: languages,
-    getLang: function (lang) {
-        lang = lang.toLowerCase();
-        if (lang == 'cn' || lang == 'zh-cn') {
-            lang = 'cn';
-        } else {
-            lang = 'en';
-        }
-        return languages[lang] || languages['en'];
-    }
-};

+ 20 - 5
app/src/libs/paths.js

@@ -16,10 +16,25 @@ const work_path = path.join(home_path, '.SwitchHosts');
 const data_path = path.join(work_path, 'data.json');
 const preference_path = path.join(work_path, 'preferences.json');
 
+function getCurrentAppPath() {
+    let a = __dirname.split(path.sep);
+    // console.log(a);
+    while (a.length > 0) {
+        let i = a[a.length - 1];
+        if (i.endsWith('.app')) {
+            return a.join(path.sep);
+        }
+        a.pop();
+    }
+
+    return null;
+}
+
 module.exports = {
-    home_path: home_path,
-    work_path: work_path,
-    data_path: data_path,
-    preference_path: preference_path,
-    sys_host_path: sys_host_path
+    home_path: home_path
+    , work_path: work_path
+    , data_path: data_path
+    , preference_path: preference_path
+    , sys_host_path: sys_host_path
+    // ,current_app_path: getCurrentAppPath()
 };

+ 6 - 0
app/src/libs/pref.js

@@ -8,11 +8,17 @@
 const fs = require('fs');
 const paths = require('./paths');
 const util = require('./util');
+// const AutoLaunch = require('auto-launch');
+// const auto_launch = new AutoLaunch({
+//     name: 'SwitchHosts!',
+//     path: paths.current_app_path,
+// });
 
 let is_loaded;
 let data = {};
 let _t;
 
+
 function load() {
     if (util.isFile(paths.preference_path)) {
         let cnt = fs.readFileSync(paths.preference_path, 'utf-8');

+ 1 - 1
app/version.js

@@ -1 +1 @@
-exports.version = [3,2,2,4182];
+exports.version = [3,2,2,4210];

+ 28 - 11
gulpfile.js

@@ -9,6 +9,7 @@
  * 注:ELECTRON_VERSION 为对应的 Electron 版本
  * 直接运行命令会自动下载对应文件,
  * 也可手动从 https://github.com/electron/electron/releases 下载最新版本,放到 ~/.electron 目录下
+ * 淘宝镜像:https://npm.taobao.org/mirrors/electron/
  */
 const ELECTRON_VERSION = '1.4.15';
 
@@ -20,7 +21,8 @@ const gulp = require('gulp');
 const shell = require('gulp-shell');
 const beautify = require('js-beautify').js_beautify;
 
-// const args = require('yargs').argv;
+const args = require('yargs').argv;
+console.log(args);
 // const IS_DEBUG = !!args.debug;
 // const TPL_FILE_INFO = "echo '> (DEBUG " + (IS_DEBUG ? "on" : "off") + ") <%= file.path %>'";
 
@@ -47,39 +49,53 @@ gulp.task('ver', () => {
     updatePackage(path.join(__dirname, 'app', 'package.json'));
 });
 
-gulp.task('pack', () => {
+gulp.task('pack', (done) => {
     let version = require('./app/version').version;
     let v1 = version.slice(0, 3).join('.');
     let v2 = version[3];
 
-    let cmds = `
+    let pack = {};
+    pack.macOS = `
 # for macOS
 electron-packager ./app 'SwitchHosts!' --platform=darwin --arch=x64 --version=${ELECTRON_VERSION} --overwrite --asar=true --prune --icon=./assets/app.icns --ignore=node_modules/.bin --ignore=.git --ignore=dist --ignore=node_modules/electron-* --out=dist --app-version=${v1} --build-version=${v2}
-cp ../assets/Credits.rtf dist/SwitchHosts\!-darwin-x64/SwitchHosts\!.app/Contents/Resources/en.lproj
-
+cp ./assets/Credits.rtf dist/SwitchHosts\!-darwin-x64/SwitchHosts\!.app/Contents/Resources/en.lproj
+`;
+    pack.win64 = `
 # for windows x64
 electron-packager ./app 'SwitchHosts!' --platform=win32  --arch=x64 --version=${ELECTRON_VERSION} --overwrite --asar=true --prune --icon=./assets/app.ico  --ignore=node_modules/.bin --ignore=.git --ignore=dist --ignore=node_modules/electron-* --out=dist --app-version=${v1} --build-version=${v2}
-
+`;
+    pack.win32 = `
 # for windows ia32
 electron-packager ./app 'SwitchHosts!' --platform=win32  --arch=ia32 --version=${ELECTRON_VERSION} --overwrite --asar=true --prune --icon=./assets/app.ico  --ignore=node_modules/.bin --ignore=.git --ignore=dist --ignore=node_modules/electron-* --out=dist --app-version=${v1} --build-version=${v2}
-
+`;
+    pack.linux = `
 # for linux x86_64
 electron-packager ./app 'SwitchHosts!' --platform=linux  --arch=x64 --version=${ELECTRON_VERSION} --overwrite --asar=true --prune --icon=./assets/app.ico  --ignore=node_modules/.bin --ignore=.git --ignore=dist --ignore=node_modules/electron-* --out=dist --app-version=${v1} --build-version=${v2}
 `;
 
+    let cmds = [];
+    if (!args.platform) {
+        cmds = [pack.macOS, pack.win64, pack.win32, pack.linux];
+    } else {
+        let a = args.platform.split(',');
+        a.map(i => cmds.push(pack[i] || ''));
+    }
+
     console.log(`start packing, v: ${v1}.${v2} ..`);
-    console.log(cmds);
-    exec(cmds, (error, stdout, stderr) => {
+    console.log(cmds.join('\n'));
+    exec(cmds.join('\n'), (error, stdout, stderr) => {
         console.log('end pack.');
         if (error) {
             console.error(`exec error: ${error}`);
         }
         // if (stdout) console.log(`${stdout}`);
         // if (stderr) console.log(`${stderr}`);
+
+        done();
     });
 });
 
-gulp.task('zip', () => {
+gulp.task('zip', (done) => {
     let version = require('./app/version').version;
     let v = version.join('.');
 
@@ -101,12 +117,13 @@ cd ..
         }
         // if (stdout) console.log(`${stdout}`);
         // if (stderr) console.log(`${stderr}`);
+        done();
     });
 
 });
 
 gulp.task('default', () => {
-    gulp.start('ver');
+    // gulp.start('ver');
 
     gulp.watch([
         './app/main.js',

+ 2 - 1
package.json

@@ -9,8 +9,9 @@
     "dev": "ENV=dev electron app",
     "build": "gulp ver && webpack",
     "pack": "gulp pack",
+    "pack-mac": "gulp pack --platform=macOS",
     "zip": "gulp zip",
-    "make": "webpack && npm run pack && npm run zip"
+    "make": "webpack && gulp pack && gulp zip"
   },
   "repository": {
     "type": "git",

Some files were not shown because too many files changed in this diff