Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #55 from justeat/remove-code-generation-for-tweak-…
Browse files Browse the repository at this point in the history
…manager

Remove code generation for TweakManager
  • Loading branch information
albertodebortoli authored May 10, 2021
2 parents e20f614 + e2e6035 commit 7fc0391
Show file tree
Hide file tree
Showing 20 changed files with 96 additions and 616 deletions.
100 changes: 35 additions & 65 deletions Example/JustTweak/Accessors/GeneratedTweakAccessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,49 @@ import JustTweak

class GeneratedTweakAccessor {

static let tweakManager: TweakManager = {
var tweakProviders: [TweakProvider] = []
private(set) var tweakManager: TweakManager

// EphemeralTweakProvider
#if DEBUG || CONFIGURATION_UI_TESTS
let ephemeralTweakProvider_1 = NSMutableDictionary()
tweakProviders.append(ephemeralTweakProvider_1)
#endif

// UserDefaultsTweakProvider
#if DEBUG || CONFIGURATION_DEBUG
let userDefaultsTweakProvider_1 = UserDefaultsTweakProvider(userDefaults: UserDefaults.standard)
tweakProviders.append(userDefaultsTweakProvider_1)
#endif

// LocalTweakProvider
#if DEBUG
let jsonFileURL_1 = Bundle.main.url(forResource: "LocalTweaks_TopPriority_example", withExtension: "json")!
let localTweakProvider_1 = LocalTweakProvider(jsonURL: jsonFileURL_1)
tweakProviders.append(localTweakProvider_1)
#endif

// LocalTweakProvider
let jsonFileURL_2 = Bundle.main.url(forResource: "LocalTweaks_example", withExtension: "json")!
let localTweakProvider_2 = LocalTweakProvider(jsonURL: jsonFileURL_2)
tweakProviders.append(localTweakProvider_2)

let tweakManager = TweakManager(tweakProviders: tweakProviders)
tweakManager.useCache = true
return tweakManager
}()

var tweakManager: TweakManager {
return Self.tweakManager
init(with tweakManager: TweakManager) {
self.tweakManager = tweakManager
}

@TweakProperty(feature: Features.general,
variable: Variables.answerToTheUniverse,
tweakManager: tweakManager)
var meaningOfLife: Int
var canShowGreenView: Bool {
get { tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.displayGreenView)?.boolValue ?? false }
set { tweakManager.set(newValue, feature: Features.uiCustomization, variable: Variables.displayGreenView) }
}

@TweakProperty(feature: Features.general,
variable: Variables.greetOnAppDidBecomeActive,
tweakManager: tweakManager)
var shouldShowAlert: Bool
var canShowRedView: Bool {
get { tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.displayRedView)?.boolValue ?? false }
set { tweakManager.set(newValue, feature: Features.uiCustomization, variable: Variables.displayRedView) }
}

@TweakProperty(feature: Features.general,
variable: Variables.tapToChangeColorEnabled,
tweakManager: tweakManager)
var isTapGestureToChangeColorEnabled: Bool
var canShowYellowView: Bool {
get { tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.displayYellowView)?.boolValue ?? false }
set { tweakManager.set(newValue, feature: Features.uiCustomization, variable: Variables.displayYellowView) }
}

@TweakProperty(feature: Features.uiCustomization,
variable: Variables.displayGreenView,
tweakManager: tweakManager)
var canShowGreenView: Bool
var isTapGestureToChangeColorEnabled: Bool {
get { tweakManager.tweakWith(feature: Features.general, variable: Variables.tapToChangeColorEnabled)?.boolValue ?? false }
set { tweakManager.set(newValue, feature: Features.general, variable: Variables.tapToChangeColorEnabled) }
}

@TweakProperty(feature: Features.uiCustomization,
variable: Variables.displayRedView,
tweakManager: tweakManager)
var canShowRedView: Bool
var labelText: String {
get { tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.labelText)?.stringValue ?? "" }
set { tweakManager.set(newValue, feature: Features.uiCustomization, variable: Variables.labelText) }
}

@TweakProperty(feature: Features.uiCustomization,
variable: Variables.displayYellowView,
tweakManager: tweakManager)
var canShowYellowView: Bool
var meaningOfLife: Int {
get { tweakManager.tweakWith(feature: Features.general, variable: Variables.answerToTheUniverse)?.intValue ?? 0 }
set { tweakManager.set(newValue, feature: Features.general, variable: Variables.answerToTheUniverse) }
}

@TweakProperty(feature: Features.uiCustomization,
variable: Variables.labelText,
tweakManager: tweakManager)
var labelText: String
var redViewAlpha: Double {
get { tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.redViewAlphaComponent)?.doubleValue ?? 0.0 }
set { tweakManager.set(newValue, feature: Features.uiCustomization, variable: Variables.redViewAlphaComponent) }
}

@TweakProperty(feature: Features.uiCustomization,
variable: Variables.redViewAlphaComponent,
tweakManager: tweakManager)
var redViewAlpha: Double
var shouldShowAlert: Bool {
get { tweakManager.tweakWith(feature: Features.general, variable: Variables.greetOnAppDidBecomeActive)?.boolValue ?? false }
set { tweakManager.set(newValue, feature: Features.general, variable: Variables.greetOnAppDidBecomeActive) }
}
}
37 changes: 35 additions & 2 deletions Example/JustTweak/Code/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import UIKit
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
let tweakAccessor = GeneratedTweakAccessor()
var tweakAccessor: GeneratedTweakAccessor!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
let navigationController = window?.rootViewController as! UINavigationController
let viewController = navigationController.topViewController as! ViewController
tweakAccessor = GeneratedTweakAccessor(with: makeTweakManager())
viewController.tweakAccessor = tweakAccessor
viewController.tweakManager = TweakAccessor.tweakManager
viewController.tweakManager = tweakAccessor.tweakManager
return true
}

Expand All @@ -29,4 +30,36 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
}

func makeTweakManager() -> TweakManager {
var tweakProviders: [TweakProvider] = []

// EphemeralTweakProvider
#if DEBUG || CONFIGURATION_UI_TESTS
let ephemeralTweakProvider_1 = NSMutableDictionary()
tweakProviders.append(ephemeralTweakProvider_1)
#endif

// UserDefaultsTweakProvider
#if DEBUG || CONFIGURATION_DEBUG
let userDefaultsTweakProvider_1 = UserDefaultsTweakProvider(userDefaults: UserDefaults.standard)
tweakProviders.append(userDefaultsTweakProvider_1)
#endif

// LocalTweakProvider
#if DEBUG
let jsonFileURL_1 = Bundle.main.url(forResource: "LocalTweaks_TopPriority_example", withExtension: "json")!
let localTweakProvider_1 = LocalTweakProvider(jsonURL: jsonFileURL_1)
tweakProviders.append(localTweakProvider_1)
#endif

// LocalTweakProvider
let jsonFileURL_2 = Bundle.main.url(forResource: "LocalTweaks_example", withExtension: "json")!
let localTweakProvider_2 = LocalTweakProvider(jsonURL: jsonFileURL_2)
tweakProviders.append(localTweakProvider_2)

let tweakManager = TweakManager(tweakProviders: tweakProviders)
tweakManager.useCache = true
return tweakManager
}
}
22 changes: 0 additions & 22 deletions Example/JustTweak/CodeGeneration/config.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
{
"tweakProviders": [
{
"type": "EphemeralTweakProvider",
"macros": ["DEBUG", "CONFIGURATION_UI_TESTS"]
},
{
"type": "UserDefaultsTweakProvider",
"parameter": "UserDefaults.standard",
"macros": ["DEBUG", "CONFIGURATION_DEBUG"]
},
{
"type": "LocalTweakProvider",
"parameter": "LocalTweaks_TopPriority_example",
"macros": ["DEBUG"]
},
{
"type": "LocalTweakProvider",
"parameter": "LocalTweaks_example"
}
],
"shouldCacheTweaks": true,
"usePropertyWrappers": true,
"accessorName": "GeneratedTweakAccessor"
}
Binary file not shown.
63 changes: 2 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,70 +77,11 @@ static let tweakManager: TweakManager = {

```json
{
"tweakProviders": [
{
"type": "EphemeralTweakProvider",
"macros": ["DEBUG", "CONFIGURATION_UI_TESTS"]
},
{
"type": "UserDefaultsTweakProvider",
"parameter": "UserDefaults.standard",
"macros": ["DEBUG", "CONFIGURATION_DEBUG"]
},
{
"type": "LocalTweakProvider",
"parameter": "LocalTweaks_TopPriority_example",
"macros": ["DEBUG"]
},
{
"type": "LocalTweakProvider",
"parameter": "LocalTweaks_example"
}
],
"shouldCacheTweaks": true,
"usePropertyWrappers": true,
"accessorName": "GeneratedTweakAccessor"
}
```

Supported tweak provider types are:

- `EphemeralTweakProvider`
- `UserDefaultsTweakProvider`
- `LocalTweakProvider`
- `CustomTweakProvider`

The content of the `parameter` value depends on the type:
- `EphemeralTweakProvider`: is not needed
- `UserDefaultsTweakProvider`: should specify the `UserDefaults` instance to be used
- `LocalTweakProvider`: should specify the filename of the json file containing the tweaks
- `CustomTweakProvider`: should specify the filename of a file containing the setup code needed to instantiate and add your custom tweak provider.

```
...json
{
"type": "CustomTweakProvider",
"parameter": "FirebaseTweakProviderSetupCode",
"macros": ["CONFIGURATION_APPSTORE"]
},
...
```

Example content from `FirebaseTweakProviderSetupCode`. Store any CustomTweakProvider setup code file in the same folder of `config.json` and mind not to add them in any target in Xcode.

```swift
let firebaseTweakProvider = FirebaseTweakProvider()
firebaseTweakProvider.someValue = true
tweakProviders.append(firebaseTweakProvider)
```

It's important to include the `tweakProviders.append(<#property_name#>)` statement at the end of your code block that will be included in the generated code.

Other supported configuration:

`shouldCacheTweaks`: enables/disable the JustTweak cache (see [Caching notes](#caching-notes)).
`usePropertyWrappers`: property wrappers are definitely one of the most exciting new features in Swift 5.1. In order to be used for the stack, they need a single instance of `TweakManager`. If this is not a possible approach for you codebase, you can set this value to `false` to allow the generation of code that doesn't rely on a `static let`.
`accessorName`: the name of the generated class.
the only currently supported value is `accessorName` that defines the name of the generated class.

- Add the following to your `Podfile`

Expand All @@ -164,7 +105,7 @@ Every time the target is built, the code generator tool will regenerate the code
If you have used the code generator tool, the generated stack includes all the feature flags. Simply allocate the accessor object (which name you have defined in the `.json` configuration and use it to access the feature flags.

```swift
let accessor = GeneratedTweakAccessor()
let accessor = GeneratedTweakAccessor(with: <#tweak_manager_instance#>)
if accessor.meaningOfLife == 42 {
...
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
12273D382625E45F00732559 /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = 12273D372625E45F00732559 /* ArgumentParser */; };
12BE964D2626FA5000C1B6C3 /* TweakAccessorCodeGeneratorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12BE964C2626FA5000C1B6C3 /* TweakAccessorCodeGeneratorTests.swift */; };
12BE987A2627243D00C1B6C3 /* Tweaks.json in Resources */ = {isa = PBXBuildFile; fileRef = 12BE98792627243D00C1B6C3 /* Tweaks.json */; };
4F3F85E426400B6D00A7B8B8 /* GeneratedTweakAccessorContent_NoPropertyWrappers in Resources */ = {isa = PBXBuildFile; fileRef = 4F3F85E226400B6D00A7B8B8 /* GeneratedTweakAccessorContent_NoPropertyWrappers */; };
4F3F85E526400B6E00A7B8B8 /* GeneratedTweakAccessorContent_PropertyWrappers in Resources */ = {isa = PBXBuildFile; fileRef = 4F3F85E326400B6D00A7B8B8 /* GeneratedTweakAccessorContent_PropertyWrappers */; };
4F5B53E22632DD6100513C18 /* OptimizelyTweakProviderSetupCode in Resources */ = {isa = PBXBuildFile; fileRef = 4F5B53E02632DD6100513C18 /* OptimizelyTweakProviderSetupCode */; };
4F5B53E32632DD6100513C18 /* FirebaseTweakProviderSetupCode in Resources */ = {isa = PBXBuildFile; fileRef = 4F5B53E12632DD6100513C18 /* FirebaseTweakProviderSetupCode */; };
4F3F85E426400B6D00A7B8B8 /* GeneratedTweakAccessorContent in Resources */ = {isa = PBXBuildFile; fileRef = 4F3F85E226400B6D00A7B8B8 /* GeneratedTweakAccessorContent */; };
4F65E88B2629CF8C009E8C3B /* ValidTweaks_TopPriority.json in Resources */ = {isa = PBXBuildFile; fileRef = 4F65E8892629CF8C009E8C3B /* ValidTweaks_TopPriority.json */; };
4F65E88C2629CF8C009E8C3B /* ValidTweaks_LowPriority.json in Resources */ = {isa = PBXBuildFile; fileRef = 4F65E88A2629CF8C009E8C3B /* ValidTweaks_LowPriority.json */; };
4F7750762627338700A0A3F5 /* TweakValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F7750732627338700A0A3F5 /* TweakValue.swift */; };
Expand Down Expand Up @@ -63,13 +60,8 @@
12BE964C2626FA5000C1B6C3 /* TweakAccessorCodeGeneratorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TweakAccessorCodeGeneratorTests.swift; sourceTree = "<group>"; };
12BE964E2626FA5000C1B6C3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
12BE98792627243D00C1B6C3 /* Tweaks.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = Tweaks.json; sourceTree = "<group>"; };
4F3F85E226400B6D00A7B8B8 /* GeneratedTweakAccessorContent_NoPropertyWrappers */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = GeneratedTweakAccessorContent_NoPropertyWrappers; sourceTree = "<group>"; };
4F3F85E326400B6D00A7B8B8 /* GeneratedTweakAccessorContent_PropertyWrappers */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = GeneratedTweakAccessorContent_PropertyWrappers; sourceTree = "<group>"; };
4F5B53E02632DD6100513C18 /* OptimizelyTweakProviderSetupCode */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = OptimizelyTweakProviderSetupCode; sourceTree = "<group>"; };
4F5B53E12632DD6100513C18 /* FirebaseTweakProviderSetupCode */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = FirebaseTweakProviderSetupCode; sourceTree = "<group>"; };
4F5B54AF263413BE00513C18 /* OptimizelyTweakProviderSetupCode */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = OptimizelyTweakProviderSetupCode; sourceTree = "<group>"; };
4F3F85E226400B6D00A7B8B8 /* GeneratedTweakAccessorContent */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = GeneratedTweakAccessorContent; sourceTree = "<group>"; };
4F5B54B0263413BE00513C18 /* config.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = config.json; sourceTree = "<group>"; };
4F5B54B1263413BE00513C18 /* FirebaseTweakProviderSetupCode */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = FirebaseTweakProviderSetupCode; sourceTree = "<group>"; };
4F5B54B3263413BE00513C18 /* config.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = config.json; sourceTree = "<group>"; };
4F65E8892629CF8C009E8C3B /* ValidTweaks_TopPriority.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = ValidTweaks_TopPriority.json; sourceTree = "<group>"; };
4F65E88A2629CF8C009E8C3B /* ValidTweaks_LowPriority.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = ValidTweaks_LowPriority.json; sourceTree = "<group>"; };
Expand Down Expand Up @@ -151,9 +143,7 @@
4F5B54AE263413BE00513C18 /* TestConfiguration2 */ = {
isa = PBXGroup;
children = (
4F5B54AF263413BE00513C18 /* OptimizelyTweakProviderSetupCode */,
4F5B54B0263413BE00513C18 /* config.json */,
4F5B54B1263413BE00513C18 /* FirebaseTweakProviderSetupCode */,
);
path = TestConfiguration2;
sourceTree = "<group>";
Expand Down Expand Up @@ -191,17 +181,14 @@
4F7750A82627602200A0A3F5 /* Assets */ = {
isa = PBXGroup;
children = (
4F5B53E12632DD6100513C18 /* FirebaseTweakProviderSetupCode */,
4F5B53E02632DD6100513C18 /* OptimizelyTweakProviderSetupCode */,
4FD526F726283EBB000F6780 /* InvalidTweaks_DuplicateGeneratedPropertyName.json */,
4FD526FD26283F91000F6780 /* InvalidTweaks_InvalidJSON.json */,
4F7750C026277B6300A0A3F5 /* InvalidTweaks_MissingValues.json */,
12BE98792627243D00C1B6C3 /* Tweaks.json */,
4F65E88A2629CF8C009E8C3B /* ValidTweaks_LowPriority.json */,
4F65E8892629CF8C009E8C3B /* ValidTweaks_TopPriority.json */,
120B0A1D26285CE70066F2C2 /* GeneratedTweakAccessor+ConstantsContent */,
4F3F85E226400B6D00A7B8B8 /* GeneratedTweakAccessorContent_NoPropertyWrappers */,
4F3F85E326400B6D00A7B8B8 /* GeneratedTweakAccessorContent_PropertyWrappers */,
4F3F85E226400B6D00A7B8B8 /* GeneratedTweakAccessorContent */,
);
path = Assets;
sourceTree = "<group>";
Expand Down Expand Up @@ -311,17 +298,14 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4F5B53E32632DD6100513C18 /* FirebaseTweakProviderSetupCode in Resources */,
120B0A1E26285CE70066F2C2 /* GeneratedTweakAccessor+ConstantsContent in Resources */,
4FD526FE26283F91000F6780 /* InvalidTweaks_InvalidJSON.json in Resources */,
4FD526F826283EBB000F6780 /* InvalidTweaks_DuplicateGeneratedPropertyName.json in Resources */,
4F7750C126277B6300A0A3F5 /* InvalidTweaks_MissingValues.json in Resources */,
4F5B53E22632DD6100513C18 /* OptimizelyTweakProviderSetupCode in Resources */,
4F3F85E426400B6D00A7B8B8 /* GeneratedTweakAccessorContent_NoPropertyWrappers in Resources */,
4F3F85E426400B6D00A7B8B8 /* GeneratedTweakAccessorContent in Resources */,
12BE987A2627243D00C1B6C3 /* Tweaks.json in Resources */,
4F65E88C2629CF8C009E8C3B /* ValidTweaks_LowPriority.json in Resources */,
4F65E88B2629CF8C009E8C3B /* ValidTweaks_TopPriority.json in Resources */,
4F3F85E526400B6E00A7B8B8 /* GeneratedTweakAccessorContent_PropertyWrappers in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
{
"tweakProviders": [
{
"type": "EphemeralTweakProvider",
"macros": ["DEBUG", "CONFIGURATION_UI_TESTS"]
},
{
"type": "UserDefaultsTweakProvider",
"parameter": "UserDefaults.standard",
"macros": ["DEBUG", "CONFIGURATION_DEBUG"]
},
{
"type": "LocalTweakProvider",
"parameter": "LocalTweaks_TopPriority_example",
"macros": ["DEBUG"]
},
{
"type": "LocalTweakProvider",
"parameter": "LocalTweaks_example"
}
],
"shouldCacheTweaks": true,
"usePropertyWrappers": true,
"accessorName": "GeneratedTweakAccessor"
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 7fc0391

Please sign in to comment.