apps-gorm/Tools/gormtool/AppDelegate.m

276 lines
6.5 KiB
Mathematica
Raw Normal View History

2023-06-28 18:56:39 +00:00
/* AppDelegate.m
*
* Copyright (C) 2023 Free Software Foundation, Inc.
*
* Author: Gregory John Casamento <greg.casamento@gmail.com>
* Date: 2023
*
* This file is part of GNUstep.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111
* USA.
*/
#import "ArgPair.h"
#import "GormToolPrivate.h"
#import "AppDelegate.h"
// AppDelegate...
@implementation AppDelegate
2023-06-05 08:36:11 +00:00
- (BOOL) isInTool
{
return YES;
}
- (NSDictionary *) parseArguments
{
GormDocumentController *dc = [GormDocumentController sharedDocumentController];
NSMutableDictionary *result = [NSMutableDictionary dictionary];
NSProcessInfo *pi = [NSProcessInfo processInfo];
NSMutableArray *args = [NSMutableArray arrayWithArray: [pi arguments]];
2023-07-05 23:30:19 +00:00
BOOL filenameIsLastObject = NO;
NSString *file = nil;
// If the --read option isn't specified, we assume that the last argument is
// the file to be processed.
if ([args containsObject: @"--read"] == NO)
{
file = [args lastObject];
filenameIsLastObject = YES;
[args removeObject: file];
}
NSEnumerator *en = [args objectEnumerator];
id obj = nil;
BOOL parse_val = NO;
2023-07-05 23:30:19 +00:00
ArgPair *pair = AUTORELEASE([[ArgPair alloc] init]);
while ((obj = [en nextObject]) != nil)
{
if (parse_val)
{
[pair setValue: obj];
[result setObject: pair forKey: [pair argument]];
parse_val = NO;
continue;
}
else
{
2023-07-05 23:30:19 +00:00
pair = AUTORELEASE([[ArgPair alloc] init]);
if (filenameIsLastObject == YES)
{
NSString *type = [dc typeFromFileExtension: [file pathExtension]];
if (type != nil)
{
[pair setArgument: @"--read"];
[pair setValue: file];
[result setObject: pair forKey: @"--read"];
}
}
else if ([obj isEqualToString: @"--read"])
{
2023-07-05 23:30:19 +00:00
[pair setArgument: obj];
parse_val = YES;
}
2023-07-05 23:30:19 +00:00
if ([obj isEqualToString: @"--write"])
{
[pair setArgument: obj];
parse_val = YES;
}
2023-07-05 23:30:19 +00:00
if ([obj isEqualToString: @"--export-strings-file"])
{
[pair setArgument: obj];
parse_val = YES;
}
2023-07-05 23:30:19 +00:00
if ([obj isEqualToString: @"--import-strings-file"])
{
[pair setArgument: obj];
parse_val = YES;
}
2023-07-05 23:30:19 +00:00
if ([obj isEqualToString: @"--export-class"])
{
[pair setArgument: obj];
parse_val = YES;
}
2023-07-05 23:30:19 +00:00
if ([obj isEqualToString: @"--import-class"])
{
[pair setArgument: obj];
parse_val = YES;
}
if ([obj isEqualToString: @"--output-path"])
{
[pair setArgument: obj];
parse_val = YES;
}
}
}
return result;
}
- (void) process
{
2023-06-28 18:56:39 +00:00
NSProcessInfo *pi = [NSProcessInfo processInfo];
[NSClassSwapper setIsInInterfaceBuilder: YES];
2023-06-28 18:56:39 +00:00
if ([[pi arguments] count] > 1)
{
2023-07-05 23:30:19 +00:00
NSString *file = nil; // [[pi arguments] lastObject];
NSString *outputPath = @"./";
GormDocumentController *dc = [GormDocumentController sharedDocumentController];
GormDocument *doc = nil;
NSDictionary *args = [self parseArguments];
ArgPair *opt = nil;
NSDebugLog(@"args = %@", args);
NSDebugLog(@"file = %@", file);
2023-07-02 16:05:21 +00:00
// Get the file to write out to...
2023-07-05 23:30:19 +00:00
NSString *outputFile = nil;
2023-07-02 16:05:21 +00:00
2023-07-05 23:30:19 +00:00
opt = [args objectForKey: @"--read"];
2023-07-02 16:05:21 +00:00
if (opt != nil)
{
2023-07-05 23:30:19 +00:00
file = [opt value];
2023-07-02 16:05:21 +00:00
}
2023-07-05 23:30:19 +00:00
if (file != nil)
{
doc = [dc openDocumentWithContentsOfFile: file display: NO];
if (doc == nil)
{
NSLog(@"Unable to load document %@", file);
return;
}
}
else
{
NSLog(@"No document specified");
return;
}
NSDebugLog(@"Document = %@", doc);
2023-07-02 16:05:21 +00:00
// Get other options...
opt = [args objectForKey: @"--output-path"];
if (opt != nil)
{
outputPath = [opt value];
}
opt = [args objectForKey: @"--export-strings-file"];
if (opt != nil)
{
NSString *stringsFile = [opt value];
[doc exportStringsToFile: stringsFile];
2023-07-02 16:05:21 +00:00
}
2023-07-05 23:30:19 +00:00
opt = [args objectForKey: @"--import-strings-file"];
if (opt != nil)
{
NSString *stringsFile = [opt value];
[doc importStringsFromFile: stringsFile];
}
opt = [args objectForKey: @"--export-class"];
if (opt != nil)
2023-07-02 16:05:21 +00:00
{
2023-07-05 23:30:19 +00:00
NSString *className = [opt value];
BOOL saved = NO;
GormClassManager *cm = [doc classManager];
NSString *hFile = [className stringByAppendingPathExtension: @"h"];
NSString *mFile = [className stringByAppendingPathExtension: @"m"];
NSString *hPath = [outputPath stringByAppendingPathComponent: hFile];
NSString *mPath = [outputPath stringByAppendingPathComponent: mFile];
2023-07-05 23:30:19 +00:00
saved = [cm makeSourceAndHeaderFilesForClass: className
withName: mPath
and: hPath];
2023-07-05 23:30:19 +00:00
if (saved == NO)
2023-07-02 16:05:21 +00:00
{
2023-07-05 23:30:19 +00:00
NSLog(@"Class named %@ not saved", className);
}
}
opt = [args objectForKey: @"--import-class"];
if (opt != nil)
{
NSString *classFile = [opt value];
GormClassManager *cm = [doc classManager];
[cm parseHeader: classFile];
}
// These options sound always be processed last...
opt = [args objectForKey: @"--write"];
if (opt != nil)
{
outputFile = [opt value];
if (outputFile != nil)
{
2023-07-02 16:27:39 +00:00
BOOL saved = NO;
NSURL *file = [NSURL fileURLWithPath: outputFile isDirectory: YES];
NSString *type = [dc typeFromFileExtension: [outputFile pathExtension]];
NSError *error = nil;
2023-07-02 16:05:21 +00:00
2023-07-02 16:27:39 +00:00
saved = [doc saveToURL: file
ofType: type
forSaveOperation: NSSaveOperation
error: &error];
if (!saved)
{
NSLog(@"Document %@ of type %@ was not saved", file, type);
}
2023-07-05 23:30:19 +00:00
2023-07-02 16:27:39 +00:00
if (error != nil)
{
NSLog(@"Error = %@", error);
}
2023-07-02 16:05:21 +00:00
}
}
2023-06-28 18:56:39 +00:00
}
[NSClassSwapper setIsInInterfaceBuilder: NO];
}
2023-06-05 08:36:11 +00:00
- (void) applicationDidFinishLaunching: (NSNotification *)n
{
puts("== gormtool");
2023-06-28 18:56:39 +00:00
NSDebugLog(@"processInfo: %@", [NSProcessInfo processInfo]);
2023-06-05 09:53:13 +00:00
[self process];
[NSApp terminate: nil];
2023-06-05 08:36:11 +00:00
}
2023-06-12 06:05:53 +00:00
- (void) applicationWillTerminate: (NSNotification *)n
{
puts("== finished...");
}
@end