Skip to content

Commit 0283c5e

Browse files
Handle naming conflict case.
1 parent bd6bb42 commit 0283c5e

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

BuildSettingExtractor/BuildSettingExtractor.m

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,19 @@ - (BOOL)extractBuildSettingsFromProject:(NSURL *)projectWrapperURL toDestination
104104
});
105105
return success;
106106
}
107+
108+
// Get project targets
109+
NSArray *targets = [self objectArrayForDictionary:rootObject key:@"targets"];
110+
111+
// Validate project config name to guard against name conflicts with target names
112+
NSArray *targetNames = [targets valueForKeyPath:@"name"];
113+
NSString *validatedProjectConfigName = [self validatedProjectConfigNameWithTargetNames:targetNames];
107114

108115
// Get project settings
109116
NSString *buildConfigurationListID = rootObject[@"buildConfigurationList"];
110117
NSDictionary *projectSettings = [self buildSettingStringsByConfigurationForBuildConfigurationListID:buildConfigurationListID];
111118

112-
self.buildSettingsByTarget[self.projectConfigName] = projectSettings;
113-
114-
// Get project targets
115-
NSArray *targets = [self objectArrayForDictionary:rootObject key:@"targets"];
119+
self.buildSettingsByTarget[validatedProjectConfigName] = projectSettings;
116120

117121
// Add project targets
118122
for (NSDictionary *target in targets) {
@@ -130,6 +134,31 @@ - (BOOL)extractBuildSettingsFromProject:(NSURL *)projectWrapperURL toDestination
130134
return success;
131135
}
132136

137+
// This will return a validated project config name to guard against naming conflicts with targets
138+
// If a conflict is found, the project config files will have "-Project-Settings" appended to the
139+
// provided project config name, using the user-specified name separator between words.
140+
- (NSString *)validatedProjectConfigNameWithTargetNames:(NSArray *)targetNames {
141+
NSString *validatedProjectConfigName = self.projectConfigName;
142+
if ([targetNames containsObject:self.projectConfigName]) {
143+
validatedProjectConfigName = [validatedProjectConfigName stringByAppendingFormat:@"%@Project%@Settings", self.nameSeparator, self.nameSeparator];
144+
[self presentErrorForNameConflictWithName:self.projectConfigName validatedName:validatedProjectConfigName];
145+
}
146+
return validatedProjectConfigName;
147+
}
148+
149+
// Notify the user we are not using the exact name for the project settings provided in Preferences
150+
- (void)presentErrorForNameConflictWithName:(NSString *)conflictedName validatedName:(NSString *)validatedName {
151+
NSString *errorDescription = [NSString stringWithFormat:@"Project settings filename conflict."];
152+
NSString *errorRecoverySuggestion = [NSString stringWithFormat:@"The target \'%@\' has the same name as the project name set in Preferences.\n\nThe generated project settings files will use the name \'%@\' to avoid a conflict.", conflictedName, validatedName];
153+
NSDictionary *errorUserInfo = @{NSLocalizedDescriptionKey:errorDescription, NSLocalizedRecoverySuggestionErrorKey: errorRecoverySuggestion};
154+
155+
NSError *error = [NSError errorWithDomain:[[NSBundle mainBundle] bundleIdentifier] code:ProjectSettingsNamingConflict userInfo:errorUserInfo];
156+
157+
dispatch_async(dispatch_get_main_queue(), ^{
158+
[NSApp presentError:error];
159+
});
160+
161+
}
133162

134163
/* Writes an xcconfig file for each target / configuration combination to the specified directory.
135164
*/

BuildSettingExtractor/Constants+Categories.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
typedef NS_ENUM(NSUInteger, AppErrorCodes) {
1212
UnsupportedXcodeVersion = 100,
1313
DirectoryContainsBuildConfigFiles = 101,
14+
ProjectSettingsNamingConflict = 102,
1415
};
1516

1617
extern NSString *const TPSOpenDirectoryInFinder;
@@ -37,4 +38,4 @@ extern NSString *const TPSOutputFileNameSeparator;
3738

3839
@interface NSString (TPS_BuildSettingAdditions)
3940
- (NSString *)tps_baseBuildSettingName; // Removes any conditional section of a build setting
40-
@end
41+
@end

0 commit comments

Comments
 (0)