Skip to content

Commit

Permalink
Support using a prefix on GTLR imports.
Browse files Browse the repository at this point in the history
To support different someone that moves source the GTLR code, support an arg on
the ServiceGenerator to add a prefix on the #import directives.
  • Loading branch information
thomasvl committed Mar 14, 2017
1 parent a630e1b commit 620a5e3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
5 changes: 3 additions & 2 deletions Source/Tools/ServiceGenerator/SGGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ typedef NS_OPTIONS(NSUInteger, SGGeneratorOptions) {
kSGGeneratorOptionAllowRootOverride = 1 << 1,
kSGGeneratorOptionAllowGuessFormattedName = 1 << 2,
kSGGeneratorOptionLegacyObjectNaming = 1 << 3,
kSGGeneratorOptionImportPrefixIsFramework = 1 << 4,
};

@interface SGGenerator : NSObject

@property(readonly) GTLRDiscovery_RestDescription* api;
@property(readonly) SGGeneratorOptions options;
@property(readonly) NSUInteger verboseLevel;
@property(readonly) NSString *frameworkName;
@property(readonly) NSString *importPrefix;

// The API name formatted for use as a directory name.
@property (readonly) NSString *formattedAPIName;
Expand All @@ -47,7 +48,7 @@ typedef NS_OPTIONS(NSUInteger, SGGeneratorOptions) {
options:(SGGeneratorOptions)options
verboseLevel:(NSUInteger)verboseLevel
formattedNameOverride:(NSString *)formattedNameOverride
useFrameworkName:(NSString *)frameworkName;
importPrefix:(NSString *)importPrefix;

// Keys are the file names; values are the contents of the files.
- (NSDictionary *)generateFilesWithHandler:(SGGeneratorMessageHandler)messageHandler;
Expand Down
31 changes: 17 additions & 14 deletions Source/Tools/ServiceGenerator/SGGenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ @implementation SGGenerator {
@synthesize api = _api,
options = _options,
verboseLevel = _verboseLevel,
frameworkName = _frameworkName;
importPrefix = _importPrefix;

@synthesize warnings = _warnings,
infos = _infos;
Expand All @@ -257,26 +257,26 @@ + (instancetype)generatorForApi:(GTLRDiscovery_RestDescription *)api
options:(SGGeneratorOptions)options
verboseLevel:(NSUInteger)verboseLevel
formattedNameOverride:(NSString *)formattedNameOverride
useFrameworkName:(NSString *)frameworkName {
importPrefix:(NSString *)importPrefix {
return [[self alloc] initWithApi:api
options:options
verboseLevel:verboseLevel
formattedNameOverride:formattedNameOverride
useFrameworkName:frameworkName];
importPrefix:importPrefix];
}

- (instancetype)initWithApi:(GTLRDiscovery_RestDescription *)api
options:(SGGeneratorOptions)options
verboseLevel:(NSUInteger)verboseLevel
formattedNameOverride:(NSString *)formattedNameOverride
useFrameworkName:(NSString *)frameworkName {
importPrefix:(NSString *)importPrefix {
self = [super init];
if (self != nil) {
_api = api;
_options = options;
_verboseLevel = verboseLevel;
_formattedName = [formattedNameOverride copy];
_frameworkName = [frameworkName copy];
_importPrefix = [importPrefix copy];
if (api) {
NSValue *generatorAsValue = [NSValue valueWithNonretainedObject:self];
[self.api sg_setProperty:generatorAsValue forKey:kWrappedGeneratorKey];
Expand Down Expand Up @@ -2830,18 +2830,21 @@ - (NSString *)authorizationScopeToConstant:(NSString *)scope {
}

- (NSString *)frameworkedImport:(NSString *)headerName {
NSMutableString *result = [NSMutableString string];

if (self.frameworkName) {
[result appendFormat:@"#import <%@/%@.h>\n", self.frameworkName, headerName];
} else {
[result appendFormat:@"#if %@\n", kFrameworkIncludeGate];
[result appendFormat:@" #import \"%@/%@.h\"\n", kProjectPrefix, headerName];
[result appendString:@"#else\n"];
[result appendFormat:@" #import \"%@.h\"\n", headerName];
[result appendString:@"#endif\n"];
if (self.importPrefix) {
if ((_options & kSGGeneratorOptionImportPrefixIsFramework) != 0) {
return [NSString stringWithFormat:@"#import <%@/%@.h>\n", self.importPrefix, headerName];
} else {
return [NSString stringWithFormat:@"#import \"%@/%@.h\"\n", self.importPrefix, headerName];
}
}

NSMutableString *result = [NSMutableString string];
[result appendFormat:@"#if %@\n", kFrameworkIncludeGate];
[result appendFormat:@" #import \"%@/%@.h\"\n", kProjectPrefix, headerName];
[result appendString:@"#else\n"];
[result appendFormat:@" #import \"%@.h\"\n", headerName];
[result appendString:@"#endif\n"];
return result;
}

Expand Down
30 changes: 29 additions & 1 deletion Source/Tools/ServiceGenerator/SGMain.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
" you'll likely want to pass \"GoogleApiClientForRest\" as the value for"
" this."
},
{ "--gtlrImportPrefix PREFIX",
"Will generate sources that include GTLR's headers as if they are in the"
" given directory."
},
{ "--apiLogDir DIR",
"Write out a file into DIR for each JSON API description processed. These"
" can be useful for reporting bugs if generation fails with an error."
Expand Down Expand Up @@ -204,6 +208,7 @@ @interface SGMain ()
@property(copy) NSString *outputDir;
@property(copy) NSString *discoveryRootURLString;
@property(copy) NSString *gtlrFrameworkName;
@property(copy) NSString *gtlrImportPrefix;
@property(copy) NSString *apiLogDir;
@property(copy) NSString *httpLogDir;
@property(copy) NSString *messageFilterPath;
Expand Down Expand Up @@ -278,6 +283,7 @@ @implementation SGMain {
outputDir = _outputDir,
discoveryRootURLString = _discoveryRootURLString,
gtlrFrameworkName = _gtlrFrameworkName,
gtlrImportPrefix = _gtlrImportPrefix,
apiLogDir = _apiLogDir,
httpLogDir = _httpLogDir,
messageFilterPath = _messageFilterPath,
Expand Down Expand Up @@ -743,6 +749,7 @@ - (void)stateParseArgs {
{ "outputDir", required_argument, NULL, 'o' },
{ "discoveryRootURL", required_argument, NULL, 'd' },
{ "gtlrFrameworkName", required_argument, NULL, 'n' },
{ "gtlrImportPrefix", required_argument, NULL, 'i' },
{ "apiLogDir", required_argument, NULL, 'a' },
{ "httpLogDir", required_argument, NULL, 'h' },
{ "generatePreferred", no_argument, &generatePreferred, 1 },
Expand Down Expand Up @@ -773,6 +780,9 @@ - (void)stateParseArgs {
case 'n':
self.gtlrFrameworkName = @(optarg);
break;
case 'i':
self.gtlrImportPrefix = @(optarg);
break;
case 'a':
self.apiLogDir = @(optarg);
break;
Expand Down Expand Up @@ -887,6 +897,18 @@ - (void)stateParseArgs {
if (self.gtlrFrameworkName.length == 0) {
self.gtlrFrameworkName = nil;
}
while ([self.gtlrImportPrefix hasSuffix:@"/"]) {
self.gtlrImportPrefix = [self.gtlrImportPrefix substringToIndex:self.gtlrImportPrefix.length - 1];
}
if (self.gtlrImportPrefix.length == 0) {
self.gtlrImportPrefix = nil;
}

if (self.gtlrFrameworkName && self.gtlrImportPrefix) {
[self reportError:@"Cannot use both --gtlrFrameworkName and --gtlrImportPrefix."];
[self printUsage:stderr brief:NO];
return;
}

// Make sure output dir exists.
NSFileManager *fm = [NSFileManager defaultManager];
Expand Down Expand Up @@ -1339,11 +1361,17 @@ - (void)stateGenerate {
options |= kSGGeneratorOptionLegacyObjectNaming;
}

NSString *importPrefix = self.gtlrImportPrefix;
if (self.gtlrFrameworkName) {
importPrefix = self.gtlrFrameworkName;
options |= kSGGeneratorOptionImportPrefixIsFramework;
}

SGGenerator *aGenerator = [SGGenerator generatorForApi:api
options:options
verboseLevel:self.verboseLevel
formattedNameOverride:formattedNameOverride
useFrameworkName:self.gtlrFrameworkName];
importPrefix:importPrefix];

NSDictionary *generatedFiles =
[aGenerator generateFilesWithHandler:^(SGGeneratorHandlerMessageType msgType,
Expand Down

0 comments on commit 620a5e3

Please sign in to comment.