Skip to content

Commit f3eeac8

Browse files
committed
Get batch command line working for (linux) Electron version.
Not super well tested. Need to move setting various directories from in InterSpecAddOn, to BatchCommandLine, and similarly from batch_main.cpp, just to keep it all in one place.
1 parent 180c567 commit f3eeac8

File tree

5 files changed

+87
-19
lines changed

5 files changed

+87
-19
lines changed

target/electron/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set( BUILD_AS_LOCAL_SERVER OFF CACHE BOOL "N/A" )
99
set( USE_OSX_NATIVE_MENU OFF CACHE BOOL "N/A" )
1010
set( USE_SPECRUM_FILE_QUERY_WIDGET ON CACHE BOOL "Enable Spectrum File Query Widget" )
1111
set( USE_REMOTE_RID ON CACHE BOOL "Enables using remote RID tool" )
12+
set( USE_BATCH_TOOLS ON CACHE BOOL "Enables using batch tool" )
1213

1314
IF(WIN32)
1415
add_definitions(-DBOOST_ALL_NO_LIB) #Prevent boost auto-linking, which seems to call in vc141 boost libs instead of vc142

target/electron/InterSpecAddOn.cpp

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
#include "target/electron/ElectronUtils.h"
4040

4141
#if( USE_BATCH_TOOLS )
42+
#include "SpecUtils/StringAlgo.h"
43+
#include "SpecUtils/Filesystem.h"
44+
45+
#include "InterSpec/InterSpec.h"
4246
#include "InterSpec/BatchCommandLine.h"
4347
#endif
4448

@@ -542,16 +546,69 @@ namespace InterSpecAddOn
542546
Napi::Value element = jsArray.Get(i);
543547

544548
// Check if the element is a string
545-
if (!element.IsString()) {
549+
if( !element.IsString() )
550+
{
546551
Napi::TypeError::New(env, "Array elements must be strings").ThrowAsJavaScriptException();
547552
return Napi::Number();
548-
}
553+
}//if( !element.IsString() )
549554

550555
str_args[i] = element.ToString();
551556
if( str_args[i].empty() )
552557
str_args[i] = " ";
553558
str_args_ptrs[i] = &(str_args[i][0]);
554-
}
559+
560+
561+
auto checkarg = [&str_args,i,numargs]( const std::string &teststr ) -> std::string {
562+
563+
if( !SpecUtils::istarts_with(str_args[i], teststr) )
564+
return "";
565+
std::string docroot;
566+
if( (str_args[i].length() > (teststr.size()+1)) && (str_args[i][teststr.size()] == '=') )
567+
docroot = str_args[i].substr(teststr.size() + 1);
568+
else if( (i+1) < numargs )
569+
docroot = str_args[i+1];
570+
else
571+
return "";
572+
573+
if( docroot.length() && ((docroot.front()=='\"') || (docroot.front()=='\'')) )
574+
docroot = docroot.substr(1);
575+
if( docroot.length() && ((docroot.back()=='\"') || (docroot.back()=='\'')) )
576+
docroot = docroot.substr(0, docroot.size() - 1);
577+
return docroot;
578+
};//checkarg lambda
579+
580+
// TODO: this setting directories should either be brought out to node.js stuff, or integrated into BatchCommandLine::run_batch_command(...)
581+
const std::string docroot = checkarg( "--docroot" );
582+
const std::string userdatadir = checkarg( "--userdatadir" );
583+
584+
if( !docroot.empty() )
585+
{
586+
const std::string datadir = SpecUtils::append_path( docroot, "data" );
587+
588+
try
589+
{
590+
InterSpec::setStaticDataDirectory( datadir );
591+
}catch( std::exception &e )
592+
{
593+
std::cerr << "Failed to set static data directory: " << e.what() << std::endl;
594+
Napi::Number::New( env, -7 );
595+
}
596+
}//if( !docroot.empty() )
597+
598+
if( !userdatadir.empty() )
599+
{
600+
try
601+
{
602+
InterSpec::setWritableDataDirectory( userdatadir );
603+
}catch( std::exception &e )
604+
{
605+
std::cerr << "Warning: Failed to set user data directory: " << e.what() << std::endl
606+
<< "Use the '--userdatadir' option to set this to the same one the InterSpec GUI app"
607+
<< " uses, if you would like to share detectors, or other files." << std::endl;
608+
//return -8;
609+
}
610+
}//if( !userdatadir.empty() )
611+
}//for( uint32_t i = 0; i < numargs; i++ )
555612

556613
const int rcode = BatchCommandLine::run_batch_command( static_cast<int>(numargs), &(str_args_ptrs[0]) );
557614

target/electron/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ npm install uglify-js -g
161161
npm install uglifycss -g
162162
npm install cmake-js -g
163163
npm install --save-dev node-addon-api --arch=x64
164+
npm install node-api-headers
164165
npm install electron --arch=x64
165166
npm install electron-packager
166167

target/electron/app/main.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,6 @@ let interspec_url = null;
5858

5959
global.__basedir = __dirname;
6060

61-
// Check if we only want to run
62-
for( let path_string of process.argv ) {
63-
if( path_string.startsWith("--batch") || path_string.startsWith("/batch") ) {
64-
console.log( "Will run batch");
65-
66-
//TODO: redirect stderr/stdout to console.log
67-
const rdcode = interspec.runBatchAnalysis( process.argv );
68-
69-
console.log( "Batch analysis returned code " + rcode );
70-
71-
app.quit();
72-
return;
73-
}
74-
}
7561

7662
// Keep a global reference of the window objects, if you don't, the window will
7763
// be closed automatically when the JavaScript object is garbage collected.
@@ -999,7 +985,30 @@ function browseForDirectory( token, title, msg ){
999985
return (dirs.length<1) ? '' : dirs[0];
1000986
};//function browseForDirectory
1001987

988+
// Check if we only want to run
989+
for( let path_string of process.argv ) {
990+
if( path_string.startsWith("--batch") || path_string.startsWith("/batch") ) {
991+
console.log( "Will run batch");
992+
993+
let has_docroot = false, has_userdata = false;
994+
for( let str of process.argv ) {
995+
has_docroot = (has_docroot || str.startsWith('--docroot'));
996+
has_userdata = (has_userdata || str.startsWith('--userdatadir'));
997+
}
998+
999+
if( !has_docroot )
1000+
process.argv.push( "--docroot=\'" + path.dirname(require.main.filename) + "'");
1001+
if( !has_userdata )
1002+
process.argv.push( "--userdatadir=\'" + userdata + "'")
1003+
1004+
const rcode = interspec.runBatchAnalysis( process.argv );
1005+
1006+
console.log( "Batch analysis returned code " + rcode );
10021007

1008+
app.quit();
1009+
return;
1010+
}
1011+
}
10031012

10041013

10051014
// This method will be called when Electron has finished
@@ -1008,7 +1017,7 @@ function browseForDirectory( token, title, msg ){
10081017
app.on('ready', function(){
10091018
const process_name = require.main.filename;
10101019
//actually process.cwd()==path.dirname(require.main.filename) when running using node from command line
1011-
1020+
10121021
//It looks like we dont need to change the CWD anymore (I think everywhere in
10131022
// InterSpec no longer assumes a specific CWD), but lets do it anyway.
10141023
process.chdir( path.dirname(require.main.filename) );

target/electron/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"license": "LGPL-2.1-only",
2525
"dependencies": {
2626
"cmake-js": "^6.1.0",
27-
"node-api-headers": "^1.1.0"
27+
"node-api-headers": "^1.3.0"
2828
},
2929
"devDependencies": {
3030
"electron": "^21.4.4",

0 commit comments

Comments
 (0)