Skip to content

Commit

Permalink
Allow running commands when specific cdb sheets change
Browse files Browse the repository at this point in the history
Commands are define through props -> cdb.onChangeHooks
  • Loading branch information
Speedphoenix committed Apr 12, 2023
1 parent 72e3a6b commit d23a7b6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bin/defaultProps.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
// "cdb.mySheetName.myColumnName": { /* Applies only to File column with name myColumnName, located in mySheetName */ }
// "cdb.mySheetName.mySubSheet.myColumnName": { /* Applies to colum in a subsheet (Properties/lists) */ }

// It's possible to run arbitrary commands at the root of the project when specific cdb sheets change
// "cdb.onChangeHooks": [{ "cmd": "touch done.txt", "sheets": [ "sheetName" ] }],

// script support
"script.api.files" : [],
"script.cdbPackage" : "",
Expand Down
5 changes: 5 additions & 0 deletions hide/Ide.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,11 @@ class Ide {
window.title = title != null ? title : ((isCDB ? "CastleDB" : "HIDE") + " - " + projectDir);
}

public function runCommand(cmd, ?callb) {
var changeDir = isWindows ? "chdir" : "cd";
js.node.ChildProcess.exec('$changeDir $projectDir && $cmd', callb);
}

public function initMenu() {

if( subView != null ) return;
Expand Down
38 changes: 38 additions & 0 deletions hide/comp/cdb/Editor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -806,8 +806,46 @@ class Editor extends Component {
}));
}

static var runningHooks = false;
static var queuedCommand: Void -> Void = null;
function save() {
api.save();

function hookEnd() {
runningHooks = false;
if (queuedCommand != null) {
var a = queuedCommand;
queuedCommand = null;
a();
}
}
var hooks: Array<{cmd: String, sheets: Array<String>}> = this.config.get("cdb.onChangeHooks");
if( hooks != null ) {
var s = getCurrentSheet();
var commands = [for (h in hooks) if (h.sheets.has(s)) h.cmd];
function runRec(i: Int) {
runningHooks = true;
ide.runCommand(commands[i], (e, stdout, stderr) -> {
if (e != null) {
ide.error('Hook error:\n$e');
hookEnd();
} else {
if (i < commands.length - 1) {
runRec(i + 1);
} else {
hookEnd();
}
}
});
}
if (!commands.isEmpty()) {
if (runningHooks) {
queuedCommand = () -> runRec(0);
} else {
runRec(0);
}
}
}
}


Expand Down

0 comments on commit d23a7b6

Please sign in to comment.