Support multiple Automation autoload dirs. (I hope. As usual, not tested.)

Originally committed to SVN as r1219.
This commit is contained in:
Niels Martin Hansen 2007-06-10 01:49:11 +00:00
parent 77e2f20424
commit ac662477b9
4 changed files with 47 additions and 30 deletions

View file

@ -512,7 +512,7 @@ namespace Automation4 {
// copied from auto3 // copied from auto3
include_path.clear(); include_path.clear();
include_path.EnsureFileAccessible(filename); include_path.EnsureFileAccessible(filename);
wxStringTokenizer toker(Options.AsText(_T("Automation Include Path")), _T("|"), false); wxStringTokenizer toker(Options.AsText(_T("Automation Include Path")), _T("|"), wxTOKEN_STRTOK);
while (toker.HasMoreTokens()) { while (toker.HasMoreTokens()) {
// todo? make some error reporting here // todo? make some error reporting here
wxFileName path(toker.GetNextToken()); wxFileName path(toker.GetNextToken());
@ -641,25 +641,32 @@ namespace Automation4 {
void AutoloadScriptManager::Reload() void AutoloadScriptManager::Reload()
{ {
wxDir dir;
if (!dir.Exists(path)) {
return;
}
if (!dir.Open(path)) {
return;
}
RemoveAll(); RemoveAll();
int error_count = 0; int error_count = 0;
wxStringTokenizer tok(path, _T("|"), wxTOKEN_STRTOK);
while (tok.HasMoreTokens()) {
wxDir dir;
wxString dirname = tok.GetNextToken();
if (!dir.Exists(dirname)) {
wxLogWarning(_T("A directory was specified in the Automation autoload path, but it doesn't exist: %s"), dirname.c_str());
continue;
}
if (!dir.Open(dirname)) {
wxLogWarning(_T("Failed to open a directory in the Automation autoload path: %s"), dirname.c_str());
continue;
}
wxString fn; wxString fn;
wxFileName script_path(path, _T("")); wxFileName script_path(path, _T(""));
bool more = dir.GetFirst(&fn, wxEmptyString, wxDIR_FILES); bool more = dir.GetFirst(&fn, wxEmptyString, wxDIR_FILES);
while (more) { while (more) {
script_path.SetName(fn); script_path.SetName(fn);
try { try {
Add(ScriptFactory::CreateFromFile(script_path.GetFullPath())); Script *s = ScriptFactory::CreateFromFile(script_path.GetFullPath(), true);
Add(s);
if (!s->GetLoadedState()) error_count++;
} }
catch (const wchar_t *e) { catch (const wchar_t *e) {
error_count++; error_count++;
@ -671,7 +678,8 @@ namespace Automation4 {
} }
more = dir.GetNext(&fn); more = dir.GetNext(&fn);
} }
if (error_count) { }
if (error_count > 0) {
wxLogWarning(_T("One or more scripts placed in the Automation autoload directory failed to load\nPlease review the errors above, correct them and use the Reload Autoload dir button in Automation Manager to attempt loading the scripts again.")); wxLogWarning(_T("One or more scripts placed in the Automation autoload directory failed to load\nPlease review the errors above, correct them and use the Reload Autoload dir button in Automation Manager to attempt loading the scripts again."));
} }
} }
@ -699,7 +707,7 @@ namespace Automation4 {
for (std::vector<ScriptFactory*>::iterator i = factories->begin(); i != factories->end(); ++i) { for (std::vector<ScriptFactory*>::iterator i = factories->begin(); i != factories->end(); ++i) {
if (*i == factory) { if (*i == factory) {
throw _T("Automation 4: Attempt to register the same script factory multiple times."); throw _T("Automation 4: Attempt to register the same script factory multiple times. This should never happen.");
} }
} }
factories->push_back(factory); factories->push_back(factory);
@ -718,7 +726,7 @@ namespace Automation4 {
} }
} }
Script* ScriptFactory::CreateFromFile(const wxString &filename) Script* ScriptFactory::CreateFromFile(const wxString &filename, bool log_errors)
{ {
if (!factories) if (!factories)
factories = new std::vector<ScriptFactory*>(); factories = new std::vector<ScriptFactory*>();
@ -726,7 +734,13 @@ namespace Automation4 {
for (std::vector<ScriptFactory*>::iterator i = factories->begin(); i != factories->end(); ++i) { for (std::vector<ScriptFactory*>::iterator i = factories->begin(); i != factories->end(); ++i) {
try { try {
Script *s = (*i)->Produce(filename); Script *s = (*i)->Produce(filename);
if (s) return s; if (s) {
if (!s->GetLoadedState() && log_errors) {
wxLogError(_("An Automation script failed to load. File name: '%s', error reported:"), filename.c_str());
wxLogError(s->GetDescription());
}
return s;
}
} }
catch (Script *e) { catch (Script *e) {
// This was the wrong script factory, but it throwing a Script object means it did know what to do about the file // This was the wrong script factory, but it throwing a Script object means it did know what to do about the file
@ -734,6 +748,9 @@ namespace Automation4 {
return e; return e;
} }
} }
if (log_errors) {
wxLogWarning(_("The file was not recognised as an Automation script: %s"), filename.c_str());
}
return new UnknownScript(filename); return new UnknownScript(filename);
} }

View file

@ -345,7 +345,7 @@ namespace Automation4 {
static void Register(ScriptFactory *factory); static void Register(ScriptFactory *factory);
static void Unregister(ScriptFactory *factory); static void Unregister(ScriptFactory *factory);
static Script* CreateFromFile(const wxString &filename); static Script* CreateFromFile(const wxString &filename, bool log_errors);
static const std::vector<ScriptFactory*>& GetFactories(); static const std::vector<ScriptFactory*>& GetFactories();
}; };

View file

@ -203,7 +203,7 @@ void DialogAutomation::OnAdd(wxCommandEvent &evt)
try { try {
ExtraScriptInfo ei; ExtraScriptInfo ei;
ei.script = Automation4::ScriptFactory::CreateFromFile(fname); ei.script = Automation4::ScriptFactory::CreateFromFile(fname, false);
local_manager->Add(ei.script); local_manager->Add(ei.script);
ei.is_global = false; ei.is_global = false;
AddScript(ei); AddScript(ei);

View file

@ -847,7 +847,7 @@ void FrameMain::SynchronizeProject(bool fromSubs) {
sfname.MakeAbsolute(basepath); sfname.MakeAbsolute(basepath);
if (sfname.FileExists()) { if (sfname.FileExists()) {
sfnames = sfname.GetFullPath(); sfnames = sfname.GetFullPath();
local_scripts->Add(Automation4::ScriptFactory::CreateFromFile(sfnames)); local_scripts->Add(Automation4::ScriptFactory::CreateFromFile(sfnames, true));
} else { } else {
wxLogWarning(_T("Automation Script referenced could not be found.\nFilename specified: %s%s\nSearched relative to: %s\nResolved filename: %s"), wxLogWarning(_T("Automation Script referenced could not be found.\nFilename specified: %s%s\nSearched relative to: %s\nResolved filename: %s"),
sfnamel.c_str(), sfnames.c_str(), basepath.c_str(), sfname.GetFullPath().c_str()); sfnamel.c_str(), sfnames.c_str(), basepath.c_str(), sfname.GetFullPath().c_str());