Prepared for application scripting

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/steptalk/trunk@16224 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Stefan Urbanek 2003-03-23 00:53:49 +00:00
parent 036646c86a
commit 110d6c25dc
9 changed files with 148 additions and 40 deletions

View file

@ -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

View file

@ -44,7 +44,6 @@ extern NSDictionary *STGetGDL2Constants();
+ (NSDictionary *)namedObjectsForScripting
{
NSLog(@"Testing namedObjects in GDL2 module");
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict addEntriesFromDictionary:STGetGDL2Constants()];

View file

@ -75,7 +75,7 @@
- (void)loadModule:(NSString *)moduleName;
- (void)includeBundle:(NSBundle *)aBundle;
- (BOOL)includeBundle:(NSBundle *)aBundle;
- (void)addClassesWithNames:(NSArray *)names;

View file

@ -40,7 +40,7 @@
- (NSString *)fileName;
- (NSString *)source;
- (NSString *)scriptName;
- (NSString *)localizedScriptName;
- (NSString *)description;
- (NSString *)localizedName;
- (NSString *)scriptDescription;
- (NSString *)language;
@end

View file

@ -42,4 +42,6 @@
- (NSArray *)scriptSearchPaths;
- (NSArray *)validScriptSearchPaths;
- (STScript *)scriptWithName:(NSString*)name;
- (NSArray *)allScripts;
@end

View file

@ -210,9 +210,10 @@
}
/**
Include scripting capabilities advertised by bundle <ivar>aBundle</ivar>
Include scripting capabilities advertised by the bundle
<ivar>aBundle</ivar>. 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;
}
/**

View file

@ -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 <var>fileType</var>. */
+ (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
<var>fileType</var>. */
+ (STLanguage *)languageForFileType:(NSString *)fileType

View file

@ -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 <var>aFile></var>. 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;

View file

@ -27,6 +27,7 @@
#import <StepTalk/STScriptsManager.h>
#import <StepTalk/STExterns.h>
#import <StepTalk/STLanguage.h>
#import <StepTalk/STScript.h>
#import <Foundation/NSArray.h>
@ -38,17 +39,21 @@
#import <Foundation/NSFileManager.h>
#import <Foundation/NSPathUtilities.h>
#import <Foundation/NSProcessInfo.h>
#import <Foundation/NSSet.h>
#import <Foundation/NSString.h>
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