Skip to content

Commit

Permalink
starter, pio-inst
Browse files Browse the repository at this point in the history
  • Loading branch information
akaJes committed Mar 15, 2018
1 parent 4a7e968 commit 063ad70
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 10 deletions.
36 changes: 36 additions & 0 deletions app/pio-inst.js
Original file line number Diff line number Diff line change
@@ -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);
45 changes: 45 additions & 0 deletions app/state-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

/** @babel */

/**
* Copyright (c) 2017-present PlatformIO <contact@platformio.org>
* 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));
}

}
61 changes: 54 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)=>
Expand All @@ -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){
Expand All @@ -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<p.length&&(o[key]=p[i+1]),i>=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 => {
Expand All @@ -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({
Expand All @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 .",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
162 changes: 162 additions & 0 deletions views/start.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<style type="text/css">
body {
display: flex;
text-align:center;
}
.starter {
width: 400px;
margin: auto;
}
.component {
width: 150px;
height: 150px;
border-radius: 5px;
display: inline-block;
border: 1px solid red;
background-color: pink;
margin: 5px;
text-align: center;
vertical-align: top;
}
.component>div {
height: 50%;
padding:10px;
box-sizing: border-box;
}
.component .state {
margin:5px;
*padding:5px;
}
.component img {
max-height: 100%;
max-width:100%;
}
.component.installed {
border: 1px solid green;
background-color: lightgreen;
}
.site {
border: 1px solid black;
background-color: lightyellow;
}
.folder {
border: 1px solid black;
background-color: lightblue;
}
button {
margin: auto;
display: block;
background-color: greenyellow;
box-shadow: 2px 2px gray;
border-radius: 3px;
border: solid gray 1px;
transition: opacity .15s ease-in-out;
cursor: pointer;
user-select: none;
padding: 5px 20px;
vertical-align: middle;
outline: none;
margin-bottom: 10px;
}
.installed button {
background-color: lightgray;
}
button:active {
opacity: .3;
box-shadow: 0 0;
}
ul {
list-style: none;
margin: 5px;
padding:0;
}
li {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
direction: rtl;
text-align: left;
}
li:first-child {
text-align:center;
}
li.active {
background-color: gray;
}
li:hover {
opacity: .5;
}

</style>
<script>
typeof $ == 'undefined' && ($ = require('jquery'));
$(function() {
var shell = require('electron').shell;
window.opener = shell.openExternal;

$('a, button[href]').on('click', function (ev) {
ev.preventDefault();
window.opener($(this).attr('href'), '_blank')
})
$('ul').on('click', 'li', function() {
$(this).addClass('active').siblings().removeClass('active');
})
$('.pio button').on('click', function () {
$(this).attr('disabled', '').siblings('.state').text('wait a minute...');
ipc.send('starter-pio');
})
$('.folder button').on('click', function () {
ipc.send('starter-folder', [$('li.active').attr('n')]);
})
var ipc = require('electron').ipcRenderer;
ipc.send('starter-init');
ipc.on('starter-pio', function(ev, data) {
$('.component.pio .state').text(data ? 'instaled' : 'error');
data && $('.component.pio').addClass('installed');
});
ipc.on('starter-init', function(ev, data) {
['git','pio'].map(function(i) {
var c = $('.component.' + i)
data[i] && c.addClass('installed');
c.find('.state').text(data[i] ? 'Installed' : 'NOT Installed');
});
data.pio && $('.component.pio button').text('reinstall')
+(data.folders || []).slice(0,4).map(function(f, n) {
$('ul').append($('<li>').text(f).attr('n', n).attr('title', f))
});
})
})
</script>
<div class="starter">
<div class="component git">
<div><img src="https://git-scm.com/images/logo@2x.png" alt="git" title="git">
</div><div>
<div class="state">not found</div>
<button href="https://git-scm.com/downloads">install</button></div>
</div>
<div class="component pio">
<div><img src="https://platformio.org/images/platformio-logo.17fdc3bc.png" alt="PlatformIO" title="PlatformIO">
</div><div title="it can take up to 5 minutes">
<div class="state">not found</div>
<button>auto install</button>
</div>
</div>

<div class="component site">
<div><a href="https://github.com/akaJes/marlin-config"><img src="https://raw.githubusercontent.com/akaJes/marlin-config/master/build/icons/icon_128x128.png"></a></div>
<div>
<button href="http://lt.rv.ua/mc">configurations</button>
<a href="https://www.paypal.me/AKruk508">
<img src="https://img.shields.io/badge/Donate-PayPal-green.svg"></a>
</div></div>

<div class="component folder">
<br>
<button>open folder</button>
<ul>
<li n="new" class="active">new</li>
</ul>
</div>

</div>

0 comments on commit 063ad70

Please sign in to comment.