2 * Copyright 2017 Google
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #import "FIRMessagingVersionUtilities.h"
19 #import "FIRMessagingDefines.h"
21 // Convert the macro to a string
22 #define STR_EXPAND(x) #x
23 #define STR(x) STR_EXPAND(x)
25 static NSString *const kSemanticVersioningSeparator = @".";
26 static NSString *const kBetaVersionPrefix = @"-beta";
28 static NSString *libraryVersion;
29 static int majorVersion;
30 static int minorVersion;
31 static int patchVersion;
32 static int betaVersion;
34 void FIRMessagingParseCurrentLibraryVersion(void) {
35 static NSArray *allVersions;
36 static dispatch_once_t onceToken;
37 dispatch_once(&onceToken, ^{
38 NSMutableString *daylightVersion = [NSMutableString stringWithUTF8String:STR(FIRMessaging_LIB_VERSION)];
40 // major, minor, patch[-beta#]
41 allVersions = [daylightVersion componentsSeparatedByString:kSemanticVersioningSeparator];
42 _FIRMessagingDevAssert(allVersions.count == 3, @"Invalid versioning of FIRMessaging library");
43 if (allVersions.count == 3) {
44 majorVersion = [allVersions[0] intValue];
45 minorVersion = [allVersions[1] intValue];
47 // Parse patch and beta versions
48 NSArray *patchAndBetaVersion =
49 [allVersions[2] componentsSeparatedByString:kBetaVersionPrefix];
50 _FIRMessagingDevAssert(patchAndBetaVersion.count <= 2, @"Invalid versioning of FIRMessaging library");
51 if (patchAndBetaVersion.count == 2) {
52 patchVersion = [patchAndBetaVersion[0] intValue];
53 betaVersion = [patchAndBetaVersion[1] intValue];
54 } else if (patchAndBetaVersion.count == 1) {
55 patchVersion = [patchAndBetaVersion[0] intValue];
59 // Copy library version
60 libraryVersion = [daylightVersion copy];
64 NSString *FIRMessagingCurrentLibraryVersion(void) {
65 FIRMessagingParseCurrentLibraryVersion();
66 return libraryVersion;
69 int FIRMessagingCurrentLibraryVersionMajor(void) {
70 FIRMessagingParseCurrentLibraryVersion();
74 int FIRMessagingCurrentLibraryVersionMinor(void) {
75 FIRMessagingParseCurrentLibraryVersion();
79 int FIRMessagingCurrentLibraryVersionPatch(void) {
80 FIRMessagingParseCurrentLibraryVersion();
84 BOOL FIRMessagingCurrentLibraryVersionIsBeta(void) {
85 FIRMessagingParseCurrentLibraryVersion();
86 return betaVersion > 0;