Added and extra include path (defaulting to config dir) for JS scripts.

Engine runtime parameters can be used in javascript.conf for script paths.


git-svn-id: http://voip.null.ro/svn/yate@5799 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
paulc 2014-03-14 14:45:41 +00:00
parent 8b3c72b516
commit f869586d43
4 changed files with 47 additions and 18 deletions

View File

@ -3,8 +3,12 @@
; scripts_dir: string: The absolute or relative path used by default to load
; scripts if no full path is specified
; Note that a trailing path separator should be added
;scripts_dir=share/scripts/
;scripts_dir=${sharedpath}/scripts
; include_dir: string: The absolute or relative path used when including other
; files via #include or #require if no full path is specified
; If the file is not found in include_dir it will be searched in scripts_dir
;include_dir=${configpath}
; routing: string: Name of the file holding the routing instructions
; Example: routing=route.js

View File

@ -1215,7 +1215,7 @@ bool JsCode::preProcessInclude(ParsePoint& expr, bool once, GenObject* context)
String str;
if (ExpEvaluator::getString(expr,str)) {
DDebug(this,DebugAll,"Found include '%s'",str.safe());
parser->adjustPath(str);
parser->adjustPath(str,true);
str.trimSpaces();
bool ok = !str.null();
if (ok) {
@ -3364,11 +3364,14 @@ bool JsFunction::runDefined(ObjList& stack, const ExpOperation& oper, GenObject*
// Adjust a script file include path
void JsParser::adjustPath(String& script) const
void JsParser::adjustPath(String& script, bool extraInc) const
{
if (script.null() || script.startsWith(Engine::pathSeparator()))
return;
script = m_basePath + script;
if (extraInc && m_includePath && File::exists(m_includePath + script))
script = m_includePath + script;
else
script = m_basePath + script;
}
// Create Javascript context

View File

@ -2429,8 +2429,9 @@ public:
/**
* Adjust a file script path to include default if needed
* @param script File path to adjust
* @param extraInc True to check the extra include path first
*/
void adjustPath(String& script) const;
void adjustPath(String& script, bool extraInc = false) const;
/**
* Retrieve the base script path
@ -2439,12 +2440,20 @@ public:
inline const String& basePath() const
{ return m_basePath; }
/**
* Retrieve the extra include script path
* @return Include path added to relative script paths
*/
inline const String& includePath() const
{ return m_includePath; }
/**
* Set the base script path
* @param path Base path to add to relative script paths
* @param incPath Extra include path to add to relative script paths
*/
inline void basePath(const char* path)
{ m_basePath = path; }
inline void basePath(const char* path, const char* incPath = 0)
{ m_basePath = path; m_includePath = incPath; }
/**
* Retrieve the last parsed file name
@ -2464,10 +2473,11 @@ public:
* Check if the script or any includes have changed
* @param file Name of the file to check
* @param path New base path to check
* @param incPath New extra include path to check
* @return True if the script may have changed, false if not changed
*/
inline bool scriptChanged(const char* file, const String& path) const
{ return (path != m_basePath) || scriptChanged(file); }
inline bool scriptChanged(const char* file, const String& path, const String& incPath = String::empty()) const
{ return (path != m_basePath) || (incPath != m_includePath) || scriptChanged(file); }
/**
* Set whether the Javascript code should be linked or not
@ -2520,6 +2530,7 @@ public:
private:
String m_basePath;
String m_includePath;
String m_parsedFile;
bool m_allowLink;
bool m_allowTrace;

View File

@ -535,6 +535,7 @@ private:
};
static String s_basePath;
static String s_libsPath;
static bool s_engineStop = false;
static bool s_allowAbort = false;
static bool s_allowTrace = false;
@ -2596,7 +2597,7 @@ JsGlobal::JsGlobal(const char* scriptName, const char* fileName, bool relPath)
: NamedString(scriptName,fileName),
m_inUse(true)
{
m_jsCode.basePath(s_basePath);
m_jsCode.basePath(s_basePath,s_libsPath);
if (relPath)
m_jsCode.adjustPath(*this);
m_jsCode.link(s_allowLink);
@ -2625,7 +2626,7 @@ JsGlobal::~JsGlobal()
bool JsGlobal::fileChanged(const char* fileName) const
{
return m_jsCode.scriptChanged(fileName,s_basePath);
return m_jsCode.scriptChanged(fileName,s_basePath,s_libsPath);
}
void JsGlobal::markUnused()
@ -2793,7 +2794,7 @@ bool JsModule::commandExecute(String& retVal, const String& line)
bool JsModule::evalContext(String& retVal, const String& cmd, ScriptContext* context)
{
JsParser parser;
parser.basePath(s_basePath);
parser.basePath(s_basePath,s_libsPath);
parser.link(s_allowLink);
parser.trace(s_allowTrace);
if (!parser.parse(cmd)) {
@ -2974,9 +2975,15 @@ void JsModule::initialize()
String tmp = Engine::sharedPath();
tmp << Engine::pathSeparator() << "scripts";
tmp = cfg.getValue("general","scripts_dir",tmp);
if (!tmp.endsWith(Engine::pathSeparator()))
Engine::runParams().replaceParams(tmp);
if (tmp && !tmp.endsWith(Engine::pathSeparator()))
tmp += Engine::pathSeparator();
s_basePath = tmp;
tmp = cfg.getValue("general","include_dir","${configpath}");
Engine::runParams().replaceParams(tmp);
if (tmp && !tmp.endsWith(Engine::pathSeparator()))
tmp += Engine::pathSeparator();
s_libsPath = tmp;
s_allowAbort = cfg.getBoolValue("general","allow_abort");
bool changed = false;
if (cfg.getBoolValue("general","allow_trace") != s_allowTrace) {
@ -2988,12 +2995,13 @@ void JsModule::initialize()
changed = true;
}
tmp = cfg.getValue("general","routing");
Engine::runParams().replaceParams(tmp);
lock();
if (changed || m_assistCode.scriptChanged(tmp,s_basePath)) {
if (changed || m_assistCode.scriptChanged(tmp,s_basePath,s_libsPath)) {
m_assistCode.clear();
m_assistCode.link(s_allowLink);
m_assistCode.trace(s_allowTrace);
m_assistCode.basePath(s_basePath);
m_assistCode.basePath(s_basePath,s_libsPath);
m_assistCode.adjustPath(tmp);
if (m_assistCode.parseFile(tmp))
Debug(this,DebugInfo,"Parsed routing script: %s",tmp.c_str());
@ -3007,8 +3015,11 @@ void JsModule::initialize()
unsigned int len = sect->length();
for (unsigned int i=0; i<len; i++) {
NamedString *n = sect->getParam(i);
if (n)
JsGlobal::initScript(n->name(),*n);
if (n) {
tmp = *n;
Engine::runParams().replaceParams(tmp);
JsGlobal::initScript(n->name(),tmp);
}
}
}
JsGlobal::freeUnused();