-
Notifications
You must be signed in to change notification settings - Fork 1
RDKEMW-3106 : Ensure modules path works in all environments #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d8faa44
5e51018
093e915
c1941be
d26bde6
cca9675
bb4c05c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,30 +25,35 @@ | |
| #ifdef ENABLE_ESSOS | ||
| #include <EssosInstance.h> | ||
| #endif | ||
| #include <sys/stat.h> | ||
| #include <cstdlib> | ||
|
|
||
| std::string JavaScriptContextBase::sThunderJSCode = ""; | ||
| std::string JavaScriptContextBase::sWebBridgeCode = ""; | ||
| std::string JavaScriptContextBase::sModulesPath = "" ; | ||
|
|
||
| JavaScriptContextFeatures::JavaScriptContextFeatures(bool embedThunderJS, bool embedWebBridge, bool enableWebSockerServer, ModuleSettings& moduleSettings):mEmbedThunderJS(embedThunderJS), mEmbedWebBridge(embedWebBridge), mEnableWebSockerServer(enableWebSockerServer), mModuleSettings(moduleSettings) | ||
| { | ||
| } | ||
|
|
||
| JavaScriptContextBase::JavaScriptContextBase(JavaScriptContextFeatures& features, std::string url, IJavaScriptEngine* jsEngine): mApplicationUrl(url), mEngine(jsEngine), mEmbedThunderJS(features.mEmbedThunderJS), mEmbedWebBridge(features.mEmbedWebBridge), mEnableWebSockerServer(features.mEnableWebSockerServer), mModuleSettings(features.mModuleSettings) | ||
| { | ||
| getModulesPath(); | ||
| if (mEmbedThunderJS) | ||
| { | ||
| if (sThunderJSCode.empty()) | ||
| { | ||
| sThunderJSCode = readFile("modules/thunderJS.js"); | ||
| { | ||
| sThunderJSCode = readFile("thunderJS.js"); | ||
| } | ||
| } | ||
| if (mEmbedWebBridge) | ||
| { | ||
| if (sWebBridgeCode.empty()) | ||
| { | ||
| sWebBridgeCode = readFile("modules/webbridgesdk.js"); | ||
| { | ||
| sWebBridgeCode = readFile("webbridgesdk.js"); | ||
| } | ||
| } | ||
|
|
||
| #ifdef ENABLE_ESSOS | ||
| EssosInstance::instance()->registerKeyListener(this); | ||
| #endif | ||
|
|
@@ -78,6 +83,22 @@ std::string JavaScriptContextBase::readFile(const char *file) | |
| std::ifstream src_file(file); | ||
| std::stringstream src_script; | ||
| src_script << src_file.rdbuf(); | ||
| if(src_script.str().empty()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1, Get one argument to readFile isModule (by default true). Pass it false, when we are calling this api for running local file/application |
||
| { | ||
| std::string fileName=sModulesPath; | ||
| fileName.append(file); | ||
| struct stat path; | ||
| if (stat(fileName.c_str(), &path) == 0) { | ||
| std::cout << "File exists at: " << fileName << std::endl; | ||
| std::ifstream src_file(fileName.c_str()); | ||
| src_script.str(""); | ||
| src_script.clear(); | ||
| src_script << src_file.rdbuf(); | ||
| } | ||
| else { | ||
| std::cout << file << "does not exist at: " << fileName << std::endl; | ||
| } | ||
| } | ||
| return src_script.str(); | ||
| } | ||
|
|
||
|
|
@@ -94,7 +115,8 @@ bool JavaScriptContextBase::runFile(const char *file, const char* args, bool isA | |
| scriptToRun = readFile(file); | ||
| if(scriptToRun.empty()) | ||
| { | ||
| std::string fileName("/home/root/"); | ||
| //newly added | ||
| std::string fileName=sModulesPath; | ||
| fileName.append(file); | ||
| scriptToRun = readFile(fileName.c_str()); | ||
| NativeJSLogger::log(INFO, "Checking in [%s]\n", fileName.c_str()); | ||
|
|
@@ -103,7 +125,6 @@ bool JavaScriptContextBase::runFile(const char *file, const char* args, bool isA | |
| NativeJSLogger::log(ERROR, "%s ... load error / not found. %s\n", __PRETTY_FUNCTION__, file); | ||
| fflush(stdout); | ||
| return false; | ||
| } | ||
| } | ||
| return evaluateScript(scriptToRun.c_str(), isApplication?file:nullptr, args, isApplication); | ||
| } | ||
|
|
@@ -127,3 +148,35 @@ void JavaScriptContextBase::onKeyRelease(struct JavaScriptKeyDetails& details) | |
| { | ||
| processKeyEvent(details, false); | ||
| } | ||
|
|
||
| std::string JavaScriptContextBase::getModulesPath(){ | ||
| if(!sModulesPath.empty()){ | ||
| return sModulesPath; | ||
| } | ||
| else{ | ||
| struct stat info; | ||
| std::string home; | ||
| char* cwd = getcwd(nullptr,0); | ||
| std::string PWD=cwd; | ||
| PWD=PWD+"/modules/"; | ||
| if (stat(PWD.c_str(), &info) == 0 && (info.st_mode & S_IFDIR)){ | ||
| home = PWD; // "/home/root/modules/" | ||
| } | ||
| else if(stat(PWD.c_str(), &info) == 0 && (info.st_mode & S_IFDIR)){ | ||
| home = PWD; // "/runtime/modules/" | ||
| } | ||
| else if(stat(PWD.c_str(), &info) == 0 && (info.st_mode & S_IFDIR)){ | ||
| home = PWD; | ||
| } | ||
| sModulesPath = home; | ||
| if(setenv("JSRUNTIME_MODULES_PATH",home.c_str(),1)==0){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1, Perfrom getenv and use it as sModulesPath if environment is set |
||
| std::cout<<"JSRUNTIME_MODULES_PATH:"<<std::getenv("JSRUNTIME_MODULES_PATH")<<std::endl; | ||
| std::cout<<"Modules path variable set successfully"<<std::endl; | ||
| } | ||
| else{ | ||
| std::cout<<"Modules path variable cannot be set!"<<std::endl; | ||
| } | ||
| return sModulesPath; | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename as populateModulesPath