From 063ad706ea2e46a342fbb8e9d73e0cf17a297e10 Mon Sep 17 00:00:00 2001 From: jes Date: Thu, 15 Mar 2018 15:21:14 +0200 Subject: [PATCH] starter, pio-inst --- app/pio-inst.js | 36 ++++++++++ app/state-storage.js | 45 ++++++++++++ index.js | 61 ++++++++++++++-- package.json | 8 ++- views/start.html | 162 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 302 insertions(+), 10 deletions(-) create mode 100644 app/pio-inst.js create mode 100644 app/state-storage.js create mode 100644 views/start.html diff --git a/app/pio-inst.js b/app/pio-inst.js new file mode 100644 index 0000000000..38655ab875 --- /dev/null +++ b/app/pio-inst.js @@ -0,0 +1,36 @@ +const PIO_CORE_MIN_VERSION = '3.5.2-rc.3'; +const STORAGE_STATE_KEY = 'platformio-ide:installer-state'; + +const pioNodeHelpers = require('platformio-node-helpers'); +const StateStorage = require('./state-storage'); + +class PythonPrompt { + constructor() { + this.STATUS_TRY_AGAIN = 0; + this.STATUS_ABORT = 1; + this.STATUS_CUSTOMEXE = 2; + } + async prompt(){ + return { status: this.STATUS_ABORT }; + } +} + +function installer() { + var obj = {}; + obj.stateStorage = new StateStorage(STORAGE_STATE_KEY); + obj.onDidStatusChange = function () { console.log('onDidStatusChange', arguments)} + + var i = new pioNodeHelpers.installer.PlatformIOCoreStage(obj.stateStorage, obj.onDidStatusChange, { + pioCoreMinVersion: PIO_CORE_MIN_VERSION, + useBuiltinPIOCore: true, + setUseBuiltinPIOCore: (value) => console.log('platformio-ide.advanced.useBuiltinPIOCore', value), + useDevelopmentPIOCore: false, + pythonPrompt: new PythonPrompt() + }) + if(process.platform.startsWith('win') && process.env.PATH.indexOf('.platformio') < 0) + process.env.PATH+=";"+process.env.USERPROFILE+"\\.platformio\\penv\\Scripts;"; + return i; +} +module.exports=installer(); +//i.check().then(console.log).catch(console.error); +//installer().install().then(console.log).catch(console.error); diff --git a/app/state-storage.js b/app/state-storage.js new file mode 100644 index 0000000000..0b31af0037 --- /dev/null +++ b/app/state-storage.js @@ -0,0 +1,45 @@ + +/** @babel */ + +/** + * Copyright (c) 2017-present PlatformIO + * All rights reserved. + * + * This source code is licensed under the license found in the LICENSE file in + * the root directory of this source tree. + */ + +module.exports = class StateStorage { + + constructor(stateKey) { + this.stateKey = stateKey; + } + + _loadState() { + const value = localStorage.getItem(this.stateKey); + if (!value) { + return {}; + } + try { + return JSON.parse(value); + } catch (err) { + console.error(err); + } + return {}; + } + + getValue(key) { + const data = this._loadState(); + if (data && data.hasOwnProperty(key)) { + return data[key]; + } + return null; + } + + setValue(key, value) { + const data = this._loadState(); + data[key] = value; + localStorage.setItem(this.stateKey, JSON.stringify(data)); + } + +} \ No newline at end of file diff --git a/index.js b/index.js index 352e8ec0e8..808f62c22a 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,9 @@ var fs = require('fs'); var promisify = require('./app/helpers').promisify; var git = require('./app/git-tool'); var server = require('./app/server'); +const store = require('data-store')('marlin-config'); +const pio = require('./app/pio-inst'); +const gitExe = 'git'; var mainWindow = null; var getFolder=()=>new Promise((done,fail)=> @@ -24,6 +27,7 @@ var getFolder=()=>new Promise((done,fail)=> }) ) app.on('window-all-closed', () => { + console.log('window-all-closed'); app.quit() }) function showNotify(text){ @@ -39,18 +43,49 @@ ipcMain.on('search-text', (event, arg) => { var wc = mainWindow && mainWindow.webContents; arg.length && wc.findInPage(arg) || wc.stopFindInPage('clearSelection'); }) +ipcMain.on('starter-pio', function(ev) { + pio.install() + .then(result => ev.sender.send('starter-pio', result)) + .catch(e => dialog.showErrorBox('Error', e)) +}) app.on('ready', function() { - promisify(which)('git') - .then(function(){ - var is={'-G':0} - .filter((v,key,o,p,i)=>(p=process.argv,i=p.indexOf(key),!v&&i>=0&&i+1=0)); - var chain = () => getFolder().then(dir => + var status = new BrowserWindow({width: 400, height: 400, maximizable: false}) + status.setMenu(null); + var folders = store.get('folders') || []; + var opts = {'-G': 0}; + Object.keys(opts).reduce((v, key) => (v.i = v.p.indexOf(key), v.i >= 0 && (opts[key] = v.p[v.i + 1]), v), {p: process.argv}); + + Promise.all([ + promisify(which)(gitExe).then(a => 1).catch(a => 0), + promisify(which)('pio').then(a => 1).catch(a => 0), + promisify(fs.readFile)(path.join(__dirname, 'views', 'start.html')), + ]) + .then(p => { + var statusFile = 'data:text/html;charset=UTF-8,' + encodeURIComponent(p[2].toString()); + status.loadURL(statusFile); + status.show() + ipcMain.on('starter-init', ev => { + ev.sender.send('starter-init', { + git: p[0], + pio: p[1], + folders: folders, + }) + }) + return (new Promise(function(resolve, reject) { + ipcMain.on('starter-folder', function(ev, num) { + resolve(num == 'new' ? '' : folders[num]); + }) + })) + }) + .then(folder => promisify(which)(gitExe).then(a => folder)) + .then(function(folder) { + const check = dir => promisify(fs.access)(dir, fs.constants.W_OK) .then(a => dir) .catch(e => (dialog.showErrorBox('Access','The application hasn\'t access to this folder,\nselect a folder from my Documents or Desktop'),chain())) - ); - return is['-G'] || chain(); + const chain = () => getFolder().then(check); + return folder && check(folder) || opts['-G'] || chain(); }) .then(dir => git.root(dir) .catch(e => { @@ -63,6 +98,13 @@ app.on('ready', function() { .catch(e => git.root(dir)); }) ) + .then(folder => { + var i = folders.indexOf(folder); + i >= 0 && folders.splice(i, 1); + folders.unshift(folder); + store.set('folders', folders); + return folder; + }) .then(()=>server.main(1)) .then(function(url){ mainWindow = new BrowserWindow({ @@ -74,12 +116,17 @@ app.on('ready', function() { }, }); mainWindow.loadURL(url); + status.close(); mainWindow.webContents.on('found-in-page', function (event, result) { var count = 0; if (result && result.finalUpdate) count = result.matches; event.sender.send('search-found', count); }); + mainWindow.on('close', function() { + console.log('quit()') + app.quit(); + }) }) .catch(e => { console.error(e.message); diff --git a/package.json b/package.json index 6f767b6a85..f8bd073b7c 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "marlin-conf", - "version": "2.8.2", + "version": "2.9.0", "description": "configuration tool for Marlin project", "main": "./index.js", "scripts": { "start": "electron . -G ~/TEST", "build": "build --dir", - "build-dev": "build -m --x64", + "build-dev": "build -w --x64", "dist": "GH_TOKEN=`./node_modules/.bin/json -f ~/.github.json -c 'console.log(this.OAuth)'` build -mwl --x64 --ia32 -p always", "test": "echo \"Error: no test specified\" && exit 1", "lint": "eslint .", @@ -59,6 +59,7 @@ "body-parser": "^1.17.2", "bootstrap": "4.0.0-alpha.6", "cropper": "^3.1.5", + "data-store": "^1.0.0", "express": "^4.16.2", "fix-path": "^2.1.0", "font-awesome": "^4.7.0", @@ -76,6 +77,7 @@ "node-machine-id": "^1.1.10", "node-notifier": "^5.2.1", "opn": "^5.2.0", + "platformio-node-helpers": "^0.4.3", "qr-image": "^3.2.0", "rtcmulticonnection-v3": "^3.4.4", "serialport": "^6.1.0", @@ -92,7 +94,7 @@ }, "devDependencies": { "devtron": "^1.4.0", - "electron": "^1.8.2", + "electron": "1.7.x", "electron-builder": "^20.2.0", "electron-debug": "^1.5.0", "eslint": "^4.18.1", diff --git a/views/start.html b/views/start.html new file mode 100644 index 0000000000..9002241043 --- /dev/null +++ b/views/start.html @@ -0,0 +1,162 @@ + + + +
+
+
git +
+
not found
+
+
+
+
PlatformIO +
+
not found
+ +
+
+ +
+
+
+ + + +
+ +
+
+ +
    +
  • new
  • +
+
+ +