Skip to content

Commit f573eec

Browse files
committed
Creating operations over binaries. Incomplete commit.
1 parent 8637ab1 commit f573eec

File tree

7 files changed

+160
-17
lines changed

7 files changed

+160
-17
lines changed

check.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<link href="css/check.css" type="text/css" rel="stylesheet">
88
<script>window.$ = window.jQuery = require('./js/jquery.js');</script>
99
<script src="js/translate.js" type="text/javascript" charset="utf-8"></script>
10+
<script src="js/binary.operations.js" type="text/javascript" charset="utf-8"></script>
1011
<script src="js/check.js" type="text/javascript" charset="utf-8"></script>
1112
</head>
1213
<body>

index.html

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ <h4 class="modal-title" data-string="Settings"></h4>
7676
<!-- Nav tabs -->
7777
<ul class="nav nav-tabs" role="tablist">
7878
<li role="presentation" class="active"><a href="#settings-general" role="tab" data-toggle="tab" data-string="General"></a></li>
79-
<li role="presentation"><a href="#settings-presentation" role="tab" data-toggle="tab" data-string="Presentation"></a></li>
8079
<li role="presentation"><a href="#settings-editor" role="tab" data-toggle="tab" data-string="Editor"></a></li>
80+
<li role="presentation"><a href="#settings-presentation" role="tab" data-toggle="tab" data-string="Presentation"></a></li>
8181
<li role="presentation"><a href="#settings-php" role="tab" data-toggle="tab" data-string="PHP"></a></li>
8282
</ul>
8383

@@ -229,16 +229,24 @@ <h4 class="modal-title" data-string="Settings"></h4>
229229

230230
<!-- Settings: PHP tab -->
231231
<div role="tabpanel" class="tab-pane" id="settings-php">
232-
<form class="form-horizontal">
233232

234-
<!-- Binary path -->
235-
<div class="form-group">
236-
<label class="col-sm-3 control-label" data-string="Binary path"></label>
237-
<div class="col-sm-9">
238-
<input type="text" class="form-control" data-settings="php.path" disabled>
239-
</div>
240-
</div>
241-
</form>
233+
<p class="text-right">
234+
<button id="binary-add" class="btn btn-info"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add binary</button>
235+
</p>
236+
237+
<table class="table">
238+
<thead>
239+
<tr>
240+
<th width="15%" data-string="Version"></th>
241+
<th data-string="Binary path"></th>
242+
<th width="20%">&nbsp;</th>
243+
</tr>
244+
</thead>
245+
246+
<tbody id="binary-list">
247+
248+
</tbody>
249+
</table>
242250
</div>
243251

244252
<!-- / Tab panes -->
@@ -312,6 +320,7 @@ <h2 data-string="Multi Display Mode"></h2>
312320
<script src="ace/ace.js" type="text/javascript" charset="utf-8"></script>
313321
<script src="js/translate.js" type="text/javascript" charset="utf-8"></script>
314322
<script src="js/presentation.js" type="text/javascript" charset="utf-8"></script>
323+
<script src="js/binary.operations.js" type="text/javascript" charset="utf-8"></script>
315324
<script src="js/index.js" type="text/javascript" charset="utf-8"></script>
316325
<script src="js/autorun.js" type="text/javascript" charset="utf-8"></script>
317326
</html>

js/binary.operations.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
2+
//runner.
3+
4+
/**
5+
* Modal functions
6+
*/
7+
function binaryAdd() {
8+
var file = dialog.showOpenDialog({
9+
"title": i18n.__("Find PHP binary")
10+
});
11+
12+
if (file && file[0]) {
13+
// Get path
14+
var path = file[0];
15+
16+
// Get version
17+
// We cannot save data with dots with "configstore" package :(
18+
// Workaround = replace . with : (the "true" is for returning replaced)
19+
var version = binaryGetVersion(path, true);
20+
21+
// Oops! Invalid PHP binary!
22+
if (!version)
23+
dialog.showErrorBox(i18n.__("Error"), i18n.__("Invalid PHP binary"));
24+
25+
// Save new version to config
26+
conf.set("php.versions." + version, path);
27+
28+
// Update list
29+
binaryUpdateList();
30+
}
31+
}
32+
33+
function binaryRemove() {
34+
35+
}
36+
37+
function binaryMakeDefault(version) {
38+
conf.set("php.default", binaryConvertVersionToSave(version));
39+
}
40+
41+
function binaryConvertVersionToSave(version) {
42+
return version.replace(/\./g, ":");
43+
}
44+
45+
function binaryConvertVersionToShow(version) {
46+
return version.replace(/\:/g, ".");
47+
}
48+
49+
/**
50+
* replaced = replacing . with : (configstore workaround)
51+
*/
52+
function binaryGetVersion(path, replaced) {
53+
var response = runner.execSync(path + " --version", {"encoding": "utf8"});
54+
55+
// Is this PHP?
56+
if (/^PHP/.test(response)) {
57+
// Get PHP version
58+
var result = response.match(/^PHP ([0-9\.]+)/);
59+
if (result && result[1]) {
60+
if (replaced)
61+
return binaryConvertVersionToSave(result[1]);
62+
else
63+
return result[1];
64+
}
65+
return false;
66+
}
67+
}
68+
69+
/**
70+
* Binary list functions
71+
*/
72+
function binaryUpdateList() {
73+
$("#binary-list").empty();
74+
75+
var versions = conf.get("php.versions");
76+
var inUse = conf.get("php.default");
77+
78+
for (var v in versions) {
79+
$("#binary-list").append(binaryLineGetTemplate(v, versions[v], (inUse == v)));
80+
}
81+
}
82+
83+
function binaryLineGetTemplate(version, path, inUse) {
84+
return [
85+
'<tr ' + (inUse ? 'class="info"' : '') + '>',
86+
' <td>' + binaryConvertVersionToShow(version) + '</td>',
87+
' <td>' + path + '</td>',
88+
' <td class="text-right">',
89+
' <div class="btn-group">',
90+
' <button class="btn btn-default btn-xs" onclick="makeDefaultVersion(\'' + version + '\')">',
91+
' <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>',
92+
' </button>',
93+
' <button class="btn btn-default btn-xs" onclick="removeVersion(\'' + version + '\')">',
94+
' <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>',
95+
' </button>',
96+
' </div>',
97+
' </td>',
98+
'</tr>'
99+
].join("\n");
100+
}
101+
102+
/**
103+
* Commands from modal
104+
*/
105+
function makeDefaultVersion(version) {
106+
binaryMakeDefault(version);
107+
binaryUpdateList();
108+
php_path = conf.get("php.versions." + conf.get("php.default"));
109+
}
110+
111+
function removeVersion(version) {
112+
var opt = dialog.showMessageBox({
113+
"type": "question",
114+
"title": i18n.__("Are you sure?"),
115+
"message": i18n.__("Removing {{version}} version. Are you sure?", {"version": binaryConvertVersionToShow(version)}),
116+
"buttons": [i18n.__("Yes"), i18n.__("No")],
117+
});
118+
119+
console.log(opt);
120+
}

js/check.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const pkg = require('./package.json');
77
const conf = new Configstore(pkg.name);
88
const dialog = electron.remote.dialog;
99
const fs = require("fs");
10+
const runner = require("child_process");
1011
let os;
1112

1213
var unix_paths = [
@@ -84,7 +85,11 @@ function phpFound(path) {
8485
$("#output").toggleClass("alert-danger alert-success");
8586
console.log("Found! ("+path+")");
8687
console.log("Storing data...");
87-
conf.set("php.path", path);
88+
89+
var ver = binaryGetVersion(path, true);
90+
conf.set("php.versions." + ver, path);
91+
conf.set("php.default", ver);
92+
8893
console.log("Done!");
8994
console.log("Starting app...");
9095

@@ -94,7 +99,7 @@ function phpFound(path) {
9499
}
95100

96101
function phpNotFound() {
97-
console.log("Not found. Install PHP and try again.");
102+
console.log("PHP Not found.");
98103

99104
checkWrite(i18n.__("Could not find PHP binary!"));
100105
phpSearchOptions(true);

js/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ const Configstore = require("configstore");
1818
const package_info = require(Path.join(__dirname, 'package.json'));
1919
const conf = new Configstore(package_info.name);
2020
const settings_default = {
21-
// php.path doesn't matter, because it's responsability of check.js
22-
"php.path": null,
2321
// Defaults
2422
"general.locale": "en",
2523
"general.mode": "both",
@@ -33,7 +31,7 @@ const settings_default = {
3331
}
3432

3533
// Editor
36-
var php_path = conf.get("php.path");
34+
var php_path = conf.get("php.versions." + conf.get("php.default"));
3735
var editor = ace.edit("editor");
3836
editor.$blockScrolling = Infinity;
3937
editor.commands.removeCommand("showSettingsMenu"); // Prevents ACE bindings at Cmd + ,
@@ -95,6 +93,9 @@ function renderApp(refresh) {
9593
}
9694
});
9795

96+
// Fetch binary paths
97+
binaryUpdateList();
98+
9899
// Sidebar
99100
if (isMainWindow) {
100101
// "Run code" button click
@@ -125,6 +126,9 @@ function renderApp(refresh) {
125126
// Settings modal
126127
// "Save" button click
127128
$("#settings-save").click(saveSettings) // Invoke saveSettings()
129+
130+
// Binary add
131+
$("#binary-add").click(binaryAdd); // Invoke binaryAdd()
128132
}
129133

130134
// Shows the app

locales/pt-BR.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,9 @@
7272
"Presentation": "Apresentação",
7373
"Try to open secondary window in another display": "Tentar abrir janela secundária em outro monitor",
7474
"Single Display Mode": "Modo Monitor Único",
75-
"Multi Display Mode": "Modo Múltiplos Monitores"
75+
"Multi Display Mode": "Modo Múltiplos Monitores",
76+
"Invalid PHP binary": "Binário PHP inválido",
77+
"Version": "Versão",
78+
"Are you sure?": "Tem certeza?",
79+
"Removing {{version}} version. Are you sure?": "Removendo versão {{version}}. Tem certeza?"
7680
}

main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ app.on('ready', function() {
7676
});
7777

7878
// Check if php_path is already known
79-
if (conf.get("php.path")) {
79+
if (conf.get("php.default")) {
8080
// Yes! I know where PHP is!
8181
startApp();
8282
console.log('Starting');

0 commit comments

Comments
 (0)