From 110d6c25dc4eb0ee6ee6d19594ae11b4ca67d238 Mon Sep 17 00:00:00 2001 From: Stefan Urbanek Date: Sun, 23 Mar 2003 00:53:49 +0000 Subject: [PATCH] Prepared for application scripting git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/steptalk/trunk@16224 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 10 ++++ Modules/GDL2/STGDL2Module.m | 1 - Source/Headers/StepTalk/STEnvironment.h | 2 +- Source/Headers/StepTalk/STScript.h | 4 +- Source/Headers/StepTalk/STScriptsManager.h | 2 + Source/STEnvironment.m | 12 +++- Source/STLanguage.m | 68 ++++++++++++++-------- Source/STScript.m | 32 +++++++--- Source/STScriptsManager.m | 57 ++++++++++++++++++ 9 files changed, 148 insertions(+), 40 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5070dbf..b18916c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2003 Mar 22 + + * STEnvironment: includeBundle returns NO on failure + * STScriptsManager: added method allScripts that returns all scripts for + domainand; added private method _scriptsAtPath: + * STScript: do not delete original extension when looking for script info + file, so some.st will have some.st.stinfo; renamed localizedScriptName to + localizedName + * STLanguage: new methods: updateFileTypeDictionary and allKnownFileTypes + 2003 Feb 21 * StepTalk.h: Removed #import of STModule.h as it is no longer there diff --git a/Modules/GDL2/STGDL2Module.m b/Modules/GDL2/STGDL2Module.m index fa9d03b..c0c08f1 100644 --- a/Modules/GDL2/STGDL2Module.m +++ b/Modules/GDL2/STGDL2Module.m @@ -44,7 +44,6 @@ extern NSDictionary *STGetGDL2Constants(); + (NSDictionary *)namedObjectsForScripting { - NSLog(@"Testing namedObjects in GDL2 module"); NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict addEntriesFromDictionary:STGetGDL2Constants()]; diff --git a/Source/Headers/StepTalk/STEnvironment.h b/Source/Headers/StepTalk/STEnvironment.h index 4e667b8..abfdaea 100644 --- a/Source/Headers/StepTalk/STEnvironment.h +++ b/Source/Headers/StepTalk/STEnvironment.h @@ -75,7 +75,7 @@ - (void)loadModule:(NSString *)moduleName; -- (void)includeBundle:(NSBundle *)aBundle; +- (BOOL)includeBundle:(NSBundle *)aBundle; - (void)addClassesWithNames:(NSArray *)names; diff --git a/Source/Headers/StepTalk/STScript.h b/Source/Headers/StepTalk/STScript.h index c06c97f..1e7ae65 100644 --- a/Source/Headers/StepTalk/STScript.h +++ b/Source/Headers/StepTalk/STScript.h @@ -40,7 +40,7 @@ - (NSString *)fileName; - (NSString *)source; - (NSString *)scriptName; -- (NSString *)localizedScriptName; -- (NSString *)description; +- (NSString *)localizedName; +- (NSString *)scriptDescription; - (NSString *)language; @end diff --git a/Source/Headers/StepTalk/STScriptsManager.h b/Source/Headers/StepTalk/STScriptsManager.h index 5b5ce64..658c572 100644 --- a/Source/Headers/StepTalk/STScriptsManager.h +++ b/Source/Headers/StepTalk/STScriptsManager.h @@ -42,4 +42,6 @@ - (NSArray *)scriptSearchPaths; - (NSArray *)validScriptSearchPaths; - (STScript *)scriptWithName:(NSString*)name; + +- (NSArray *)allScripts; @end diff --git a/Source/STEnvironment.m b/Source/STEnvironment.m index 4705101..e60a107 100644 --- a/Source/STEnvironment.m +++ b/Source/STEnvironment.m @@ -210,9 +210,10 @@ } /** - Include scripting capabilities advertised by bundle aBundle + Include scripting capabilities advertised by the bundle + aBundle. Loads the bundle if it is not already loaded. */ -- (void)includeBundle:(NSBundle *)aBundle +- (BOOL)includeBundle:(NSBundle *)aBundle { STBundleInfo *info; @@ -224,6 +225,11 @@ info = [STBundleInfo infoForBundle:aBundle]; + if(!info) + { + return NO; + } + [self addNamedObjectsFromDictionary:[info namedObjects]]; [self addClassesWithNames:[info publicClassNames]]; @@ -234,6 +240,8 @@ /* FIXME: is this sufficient? */ [loadedBundles addObject:[aBundle bundlePath]]; + + return YES; } /** diff --git a/Source/STLanguage.m b/Source/STLanguage.m index 0757518..8deff38 100644 --- a/Source/STLanguage.m +++ b/Source/STLanguage.m @@ -111,42 +111,60 @@ static NSDictionary *fileTypeDictionary = nil; return AUTORELEASE([[STLanguage alloc] initWithPath:path]); } +/** Update information about handling various files with StepTalk. */ ++ (NSDictionary *)updateFileTypeDictionary +{ + NSString *path = STUserConfigPath(); + NSFileManager *fm = [NSFileManager defaultManager]; + NSTask *task; + NSDictionary *dict; + RELEASE(fileTypeDictionary); + + path = [path stringByAppendingPathComponent:STLanguagesConfigFile]; + + if( ![fm fileExistsAtPath:path]) + { + NSLog(@"Creating lanugages configuration file..."); + task = [NSTask launchedTaskWithLaunchPath:@"stupdate_languages" + arguments:nil]; + [task waitUntilExit]; + } + + if( ![fm fileExistsAtPath:path]) + { + [NSException raise:STGenericException + format:@"Unable to get languages configuration file"]; + return nil; + } + + dict = [NSDictionary dictionaryWithContentsOfFile:path]; + fileTypeDictionary = [dict objectForKey:@"STFileTypes"]; + + RETAIN(fileTypeDictionary); +} /** Returns name of a language used by files of type fileType. */ + (NSString *)languageNameForFileType:(NSString *)fileType { if(!fileTypeDictionary) { - NSString *path = STUserConfigPath(); - NSFileManager *fm = [NSFileManager defaultManager]; - NSTask *task; - NSDictionary *dict; - - path = [path stringByAppendingPathComponent:STLanguagesConfigFile]; - - if( ![fm fileExistsAtPath:path]) - { - NSLog(@"Creating lanugages configuration file..."); - task = [NSTask launchedTaskWithLaunchPath:@"stupdate_languages" - arguments:nil]; - [task waitUntilExit]; - } - - if( ![fm fileExistsAtPath:path]) - { - [NSException raise:STGenericException - format:@"Unable to get languages configuration file"]; - return nil; - } - - dict = [NSDictionary dictionaryWithContentsOfFile:path]; - fileTypeDictionary = [dict objectForKey:@"STFileTypes"]; - RETAIN(fileTypeDictionary); + [self updateFileTypeDictionary]; } return [fileTypeDictionary objectForKey:fileType]; } +/** Return all known types (extensions) of StepTalk script files */ ++ (NSArray *)allKnownFileTypes +{ + if(!fileTypeDictionary) + { + [self updateFileTypeDictionary]; + } + + return [fileTypeDictionary allKeys]; +} + /** Returns the language bundle for a language used by files of type fileType. */ + (STLanguage *)languageForFileType:(NSString *)fileType diff --git a/Source/STScript.m b/Source/STScript.m index 2a80c1a..0f80a52 100644 --- a/Source/STScript.m +++ b/Source/STScript.m @@ -42,7 +42,7 @@ - (id)localizedObjectForKey:(NSString *)key { NSEnumerator *enumerator; - NSDictionary *dict = [self objectForKey:key]; + NSDictionary *dict;; NSString *language; NSArray *languages; id obj = nil; @@ -53,14 +53,16 @@ while( (language = [enumerator nextObject]) ) { - obj = [dict objectForKey:language]; + dict = [self objectForKey:language]; + obj = [dict objectForKey:key]; + if(obj) { return obj; } } - return [dict objectForKey:@"Default"]; + return [[self objectForKey:@"Default"] objectForKey:key]; } @end @@ -73,6 +75,10 @@ return AUTORELEASE(script); } +/** + Create a new script from file aFile>. Script information will + be read from 'aFile.stinfo' file containing a dictionary property list. +*/ - initWithFile:(NSString *)aFile { @@ -81,8 +87,8 @@ NSString *infoFile; BOOL isDir; - infoFile = [aFile stringByDeletingPathExtension]; - infoFile = [infoFile stringByAppendingPathExtension: @"stinfo"]; + // infoFile = [aFile stringByDeletingPathExtension]; + infoFile = [aFile stringByAppendingPathExtension: @"stinfo"]; if([manager fileExistsAtPath:infoFile isDirectory:&isDir] && !isDir ) { @@ -97,7 +103,8 @@ if(!localizedName) { - localizedName = [fileName lastPathComponent]; + localizedName = [[fileName lastPathComponent] + stringByDeletingPathExtension]; } RETAIN(localizedName); @@ -130,35 +137,42 @@ [super dealloc]; } +/** Return file name of the receiver. */ - (NSString *)fileName { return fileName; } + +/** Return menu item key equivalent for receiver. */ - (NSString *)menuKey { return menuKey; } +/** Returns source string of the receiver script.*/ - (NSString *)source { return [NSString stringWithContentsOfFile:fileName]; } +/** Returns a script name by which the script is identified */ - (NSString *)scriptName { return fileName; } -- (NSString *)localizedScriptName +/** Returns localized name of the receiver script. */ +- (NSString *)localizedName { return localizedName; } -- (NSString *)description +/** Returns localized description of the script. */ +- (NSString *)scriptDescription { return description; } - +/** Returns language of the script. */ - (NSString *)language { return language; diff --git a/Source/STScriptsManager.m b/Source/STScriptsManager.m index a3794b8..82bd70b 100644 --- a/Source/STScriptsManager.m +++ b/Source/STScriptsManager.m @@ -27,6 +27,7 @@ #import #import +#import #import #import @@ -38,17 +39,21 @@ #import #import #import +#import #import static STScriptsManager *sharedScriptsManager = nil; @implementation STScriptsManager +/** Return default domain name for scripts. Usually this is application or +process name.*/ + (NSString *)defaultScriptsDomainName { return [[NSProcessInfo processInfo] processName]; } +/** Returns default scripts manager for current process (application or tool). */ + defaultManager { if(!sharedScriptsManager) @@ -90,6 +95,8 @@ static STScriptsManager *sharedScriptsManager = nil; [super dealloc]; } + +/** Return name of script manager domain. */ - (NSString *)scriptsDomainName { return scriptsDomainName; @@ -211,4 +218,54 @@ static STScriptsManager *sharedScriptsManager = nil; return nil; } + +- (NSArray *)_scriptsAtPath:(NSString *)path +{ + NSMutableArray *scripts = [NSMutableArray array]; + NSFileManager *manager = [NSFileManager defaultManager]; + NSEnumerator *enumerator; + NSString *file; + NSString *str; + NSString *ext; + NSArray *paths; + NSSet *types; + + types = [NSSet setWithArray:[STLanguage allKnownFileTypes]]; + + enumerator = [[manager directoryContentsAtPath:path] objectEnumerator]; + + while( (file = [enumerator nextObject]) ) + { + + ext = [file pathExtension]; + if( [types containsObject:ext] ) + { + STScript *script; + NSLog(@"Found script %@", file); + + script = [STScript scriptWithFile: + [path stringByAppendingPathComponent:file]]; + [scripts addObject:script]; + } + } + + return [NSArray arrayWithArray:scripts]; +} + +/** Return list of all scripts for managed domain. */ +- (NSArray *)allScripts +{ + NSMutableArray *scripts = [NSMutableArray array]; + NSEnumerator *enumerator; + NSString *path; + + enumerator = [[self validScriptSearchPaths] objectEnumerator]; + + while( (path = [enumerator nextObject]) ) + { + [scripts addObjectsFromArray:[self _scriptsAtPath:path]]; + } + + return [NSArray arrayWithArray:scripts]; +} @end