Skip to content

Commit

Permalink
Better startup check for sonic pi root path...
Browse files Browse the repository at this point in the history
and a bit of code cleanup
  • Loading branch information
llloret committed Jun 1, 2020
1 parent 7675d7d commit 90af995
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"no-throw-literal": "warn",
"indent": ["error", 4, {"ignoreComments": true}],
"no-mixed-spaces-and-tabs": "error",
"no-trailing-spaces": "error",
"no-trailing-spaces": ["error", {"skipBlankLines": true}],
"no-unused-vars": "warn",
"semi": "off"
}
Expand Down
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
A list of things that would be nice to have
- Sonic Pi help
- Better diagnostics in case the server does not start
- Sonic Pi help
- Colorize output panel (log and cues), if possible
- Syntax highlight synths and FX
- I18n
Expand Down
23 changes: 13 additions & 10 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export function activate(context: vscode.ExtensionContext) {
console.log('Ruby detected. Sonic Pi editor extension active!');

let main = new Main();

main.checkSonicPiPath();

let config = vscode.workspace.getConfiguration('sonicpieditor');
if (config.launchSonicPiServerAutomatically === 'start'){
main.startServer();
Expand Down Expand Up @@ -66,7 +69,7 @@ export function activate(context: vscode.ExtensionContext) {
doc = rubyEditors[0].document;
}
let code = doc.getText();
main.sendRunCode(code);
main.runCode(code);
});


Expand Down Expand Up @@ -95,12 +98,12 @@ export function activate(context: vscode.ExtensionContext) {
item => {
if (item === 'Yes, once'){
code = doc.getText();
main.sendRunCode(code);
main.runCode(code);
}
else if (item === 'Yes, always'){
vscode.workspace.getConfiguration('sonicpieditor').update('runFileWhenRunSelectedIsEmpty', 'always', true);
code = doc.getText();
main.sendRunCode(code);
main.runCode(code);
}
else if (item === 'No, never'){
vscode.workspace.getConfiguration('sonicpieditor').update('runFileWhenRunSelectedIsEmpty', 'never', true);
Expand All @@ -114,31 +117,31 @@ export function activate(context: vscode.ExtensionContext) {
}
else if (runFileWhenRunSelectedIsEmpty === 'always'){
code = doc.getText();
main.sendRunCode(code);
main.runCode(code);
}

}
main.sendRunCode(code);
main.runCode(code, textEditor.selection.start.line);
});


disposable = vscode.commands.registerCommand('sonicpieditor.stop', () => {
main.sendStopAllJobs();
main.stopAllJobs();
});

disposable = vscode.commands.registerCommand('sonicpieditor.togglerecording', () => {
isRecording = !isRecording;
if (isRecording){
main.sendStartRecording();
main.startRecording();
}
else{
main.sendStopRecording();
main.stopRecording();
vscode.window.showSaveDialog({filters: {'Wave file': ['wav']}}).then(uri => {
if (uri){
main.sendSaveRecording(uri.fsPath);
main.saveRecording(uri.fsPath);
}
else{
main.sendDeleteRecording();
main.deleteRecording();
}
});
}
Expand Down
75 changes: 48 additions & 27 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export class Main {
platform: string;
guiUuid: any;

runOffset: number;

errorHighlightDecorationType = vscode.window.createTextEditorDecorationType({
border: '2px solid red'
});
Expand All @@ -92,14 +94,17 @@ export class Main {
}
else{
this.rootPath = "/home/user/sonic-pi";
this.rubyPath = this.rootPath + "/app/server/native/ruby/bin/ruby";
this.rubyPath = "ruby";
}

// Override default root path if found in settings
if (vscode.workspace.getConfiguration('sonicpieditor').sonicPiRootDirectory){
this.rootPath = vscode.workspace.getConfiguration('sonicpieditor').sonicPiRootDirectory;
}

console.log('Using Sonic Pi root directory: ' + this.rootPath);
console.log('Using ruby: ' + this.rubyPath);

this.rubyServerPath = this.rootPath + "/app/server/ruby/bin/sonic-pi-server.rb";
this.portDiscoveryPath = this.rootPath + "/app/server/ruby/bin/port-discovery.rb";
this.fetchUrlPath = this.rootPath + "/app/server/ruby/bin/fetch-url.rb";
Expand Down Expand Up @@ -131,6 +136,8 @@ export class Main {
this.oscMidiInPort = -1;
this.websocketPort = -1;

this.runOffset = 0;

// attempt to create log directory
if (!fs.existsSync(this.logPath)){
fs.mkdirSync(this.logPath);
Expand Down Expand Up @@ -160,10 +167,10 @@ export class Main {
let customExtension = vscode.workspace.getConfiguration('sonicpieditor').launchSonicPiServerCustomExtension;
if (!customExtension){
vscode.window.showErrorMessage("Launch is set to custom, but custom extension is empty.",
"Enter custom extension").then(
item => { if (item) {vscode.window.showInputBox().then(
ext =>{ vscode.workspace.getConfiguration('sonicpieditor').update('launchSonicPiServerCustomExtension', ext, true); }
);} });
"Go to settings").then(
item => { if (item) {
vscode.commands.executeCommand('workbench.action.openSettings', 'sonicpieditor.launchSonicPiServerCustomExtension');
}});
}
else if (editors[i].document.fileName.endsWith(customExtension) && !this.serverStarted) {
this.startServer();
Expand All @@ -181,6 +188,16 @@ export class Main {
});
}

checkSonicPiPath() {
if (!fs.existsSync(this.rubyServerPath)){
vscode.window.showErrorMessage("The Sonic Pi root path is not properly configured.",
"Go to settings").then(
item => { if (item) {
vscode.commands.executeCommand('workbench.action.openSettings', 'sonicpieditor.sonicPiRootDirectory');
}});
}
}

sonicPiHomePath(){
return os.homedir();
}
Expand All @@ -201,9 +218,9 @@ export class Main {
this.logOutput.appendLine(str);
}




cueLog(str: string){
this.cuesOutput.appendLine(str);
}

// This is where the incoming OSC messages are processed.
// We are processing most of the incoming OSC messages, but not everything yet.
Expand All @@ -214,13 +231,13 @@ export class Main {
osc.open();
osc.on('/log/info', (message: { args: any; }) => {
console.log("Got /log/info" + " -> " + message.args[0] + ", " + message.args[1]);
this.logOutput.appendLine(message.args[1]);
this.log(message.args[1]);
});

osc.on('/incoming/osc', (message: { args: any; }) => {
console.log("Got /incoming/osc" + " -> " + message.args[0] + ", " + message.args[1] + ", " +
message.args[2] + ", " + message.args[3]);
this.cuesOutput.appendLine(message.args[2] + ": " + message.args[3]);
this.cueLog(message.args[2] + ": " + message.args[3]);
});

osc.on('/log/multi_message', (message: any) => {
Expand Down Expand Up @@ -250,13 +267,13 @@ export class Main {
let job_id = message.args[0];
let desc = message.args[1];
let error_line = message.args[2];
let line = message.args[3];
let line = message.args[3] + this.runOffset;

vscode.window.showErrorMessage('Syntax error on job ' + job_id + ': ' + desc + '\nLine ' + line + ': ' + error_line, 'Goto error').then(
item => { if (item) {
let errorHighlight: vscode.DecorationOptions[] = [];
let editor = vscode.window.activeTextEditor!;
let range = editor.document.lineAt(line-1).range;
let range = editor.document.lineAt(line - 1).range;
editor.selection = new vscode.Selection(range.start, range.end);
editor.revealRange(range, vscode.TextEditorRevealType.InCenterIfOutsideViewport);
errorHighlight.push({range});
Expand All @@ -270,7 +287,7 @@ export class Main {
let job_id = message.args[0];
let desc = message.args[1];
let backtrace = message.args[2];
let line = message.args[3];
let line = message.args[3] + this.runOffset;

vscode.window.showErrorMessage('Error on job ' + job_id + ': ' + desc + '\nLine ' + line + ': ' + backtrace, 'Goto error').then(
item => { if (item) {
Expand Down Expand Up @@ -400,6 +417,7 @@ export class Main {
});

ruby_server.stderr.on('data', (data: any) => {
console.log(`stderr: ${data}`);
this.log(`stderr: ${data}`);
});
}
Expand All @@ -408,23 +426,26 @@ export class Main {
let invert_stereo = vscode.workspace.getConfiguration('sonicpieditor').invertStereo;
let force_mono = vscode.workspace.getConfiguration('sonicpieditor').forceMono;
if (invert_stereo) {
this.sendMixerInvertStereo();
this.mixerInvertStereo();
} else {
this.sendMixerStandardStereo();
this.mixerStandardStereo();
}

if (force_mono) {
this.sendMixerMonoMode();
this.mixerMonoMode();
} else {
this.sendMixerStereoMode();
this.mixerStereoMode();
}
}

sendOsc(message: any){
this.oscSender.send(message);
}

sendRunCode(code: string){
runCode(code: string, offset: number = 0){
// The offset represents the line number of the selection, so we can apply it when we just send a
// selection to Sonic Pi. If we send the full buffer, then this is 0.
this.runOffset = offset;
if (vscode.workspace.getConfiguration('sonicpieditor').logClearOnRun){
this.logOutput.clear();
}
Expand All @@ -436,47 +457,47 @@ export class Main {
this.sendOsc(message);
}

sendStopAllJobs(){
stopAllJobs(){
var message = new OSC.Message('/stop-all-jobs', this.guiUuid);
this.sendOsc(message);
}

sendStartRecording(){
startRecording(){
let message = new OSC.Message('/start-recording', this.guiUuid);
this.sendOsc(message);
}

sendStopRecording(){
stopRecording(){
let message = new OSC.Message('/stop-recording', this.guiUuid);
this.sendOsc(message);
}

sendSaveRecording(path: string){
saveRecording(path: string){
let message = new OSC.Message('/save-recording', this.guiUuid, path);
this.sendOsc(message);
}

sendDeleteRecording(){
deleteRecording(){
let message = new OSC.Message('/delete-recording', this.guiUuid);
this.sendOsc(message);
}

sendMixerInvertStereo(){
mixerInvertStereo(){
let message = new OSC.Message('/mixer-invert-stereo', this.guiUuid);
this.sendOsc(message);
}

sendMixerStandardStereo(){
mixerStandardStereo(){
let message = new OSC.Message('/mixer-standard-stereo', this.guiUuid);
this.sendOsc(message);
}

sendMixerMonoMode(){
mixerMonoMode(){
let message = new OSC.Message('/mixer-mono-mode', this.guiUuid);
this.sendOsc(message);
}

sendMixerStereoMode(){
mixerStereoMode(){
let message = new OSC.Message('/mixer-stereo-mode', this.guiUuid);
this.sendOsc(message);
}
Expand Down

0 comments on commit 90af995

Please sign in to comment.