Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support different working directories for data #7

Merged
merged 9 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Mockingbird.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
216DC9708C747E4014C763B8 /* Pods_Mockingbird.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4287C41673FEE0A7D53CFC08 /* Pods_Mockingbird.framework */; };
737203AC4CFCF605F6FF30D3 /* Pods_MockingbirdTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8952BC9226AA3B164321E36D /* Pods_MockingbirdTests.framework */; };
8F56D1E2265E7139005159E1 /* Configurations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F56D1E1265E7139005159E1 /* Configurations.swift */; };
B63AA30E2469D15200D6026B /* ServerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B63AA30D2469D15200D6026B /* ServerTests.swift */; };
B63AA3102469D76D00D6026B /* UtilTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B63AA30F2469D76D00D6026B /* UtilTests.swift */; };
B640167E245B50FF009EDBBF /* DataHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = B640167D245B50FF009EDBBF /* DataHelper.swift */; };
Expand Down Expand Up @@ -118,6 +119,7 @@
83434BFCBF135F5C552864E6 /* Pods-Mockingbird-MockingbirdUITests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Mockingbird-MockingbirdUITests.debug.xcconfig"; path = "Target Support Files/Pods-Mockingbird-MockingbirdUITests/Pods-Mockingbird-MockingbirdUITests.debug.xcconfig"; sourceTree = "<group>"; };
83BCB3BAD6AC4B8D47908BEF /* Pods-Mockingbird-MockingbirdUITests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Mockingbird-MockingbirdUITests.release.xcconfig"; path = "Target Support Files/Pods-Mockingbird-MockingbirdUITests/Pods-Mockingbird-MockingbirdUITests.release.xcconfig"; sourceTree = "<group>"; };
8952BC9226AA3B164321E36D /* Pods_MockingbirdTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_MockingbirdTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8F56D1E1265E7139005159E1 /* Configurations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Configurations.swift; sourceTree = "<group>"; };
95E8B10A50D0E77C0D935628 /* Pods-MockingbirdTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MockingbirdTests.release.xcconfig"; path = "Target Support Files/Pods-MockingbirdTests/Pods-MockingbirdTests.release.xcconfig"; sourceTree = "<group>"; };
B63AA30D2469D15200D6026B /* ServerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerTests.swift; sourceTree = "<group>"; };
B63AA30F2469D76D00D6026B /* UtilTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UtilTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -423,6 +425,7 @@
B6736AEC23981D310040B29D /* Router.swift */,
B65572C823D094D400D5056A /* Tracking.swift */,
B69F01C723EE29B6005C8B26 /* Util.swift */,
8F56D1E1265E7139005159E1 /* Configurations.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -1028,6 +1031,7 @@
B6736B2D23981D320040B29D /* DataView.swift in Sources */,
B69F01AE23EE064A005C8B26 /* DataStore.swift in Sources */,
B6736B1923981D320040B29D /* URLMatcher.swift in Sources */,
8F56D1E2265E7139005159E1 /* Configurations.swift in Sources */,
B6736B1823981D320040B29D /* URLConvertible.swift in Sources */,
B69F01AA23EE0639005C8B26 /* DataActions.swift in Sources */,
B683949B23F1CFC600882648 /* Default.swift in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions src/Mockingbird/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {

Tracking.initialize()

Default.Folder.workingDirectory = Default.Folder.savedWorkingDirectory
guilherme-fh marked this conversation as resolved.
Show resolved Hide resolved

AppStore.data.dispatch(DataAction.initialize)
AppStore.record.dispatch(RecordAction.initialize)
AppStore.test.dispatch(TestAction.initialize)
Expand Down
17 changes: 16 additions & 1 deletion src/Mockingbird/UI/View/OptionsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,28 @@ public class OptionsView {

Group {

Tree("Server Context", options: .defaultOpen) {
Tree("Setup", options: .defaultOpen) {

Text("Server Context")
RadioButtonGroup(state.contextTitles, selectedIndexState: state.currentContextIndex) { val in

ContextManager.shared.setContext(index: val)
}
NewLine()

Text("WORKING DIRECTORY: \(Default.Folder.workingDirectory)")
LargeButton("CHANGE DIRECTORY") {

if let newDirectory = Util.chooseFolder() {

Default.Folder.savedWorkingDirectory = newDirectory
Default.Folder.workingDirectory = newDirectory
guilherme-fh marked this conversation as resolved.
Show resolved Hide resolved

AppStore.data.dispatch(DataAction.initialize)
AppStore.test.dispatch(TestAction.initialize)
}
}
NewLine()
}

Divider()
Expand Down
12 changes: 12 additions & 0 deletions src/Mockingbird/Utils/Configurations.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Configurations.swift
// Mockingbird
//
// Created by Guilherme Faria on 26/05/2021.
// Copyright © 2021 Farfetch. All rights reserved.
//

struct Configuration: Codable {

let dataFolder: String
}
50 changes: 46 additions & 4 deletions src/Mockingbird/Utils/Default.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Files

final class Default {

Expand All @@ -24,10 +25,51 @@ final class Default {
public enum Folder {

static var main = "/Users/" + NSUserName() + "/.mockingbird"
static var capture: String { main + "/capture" }
static var data: String { main + "/data" }
static var mitm: String { main + "/mitmproxy" }
static var record: String { main + "/record" }
static var test: String { main + "/test" }
static var capture: String { workingDirectory + "/capture" }
static var record: String { workingDirectory + "/record" }
static var data: String { workingDirectory + "/data" }
static var test: String { workingDirectory + "/test" }

static var workingDirectory = ""

static var savedWorkingDirectory: String {

get {

guard let file = try? File(path: "\(Default.Folder.main)/configurations.json"),
guilherme-fh marked this conversation as resolved.
Show resolved Hide resolved
let fileData = try? file.readAsString(),
let startRange = fileData.range(of: "=")?.upperBound else {

return "/Users/" + NSUserName() + "/.mockingbird"
}

return String(fileData.suffix(from: startRange))
}

set {

if let configurationsFile = try? File(path: "\(Default.Folder.main)/configurations.json") {

let configurations = Configuration(dataFolder: newValue)
guilherme-fh marked this conversation as resolved.
Show resolved Hide resolved
guard let writeData = try? configurations.jsonData() else { return }

try? configurationsFile.write(writeData)

} else {

guard let mainFolder = try? Files.Folder(path: Default.Folder.main),
let configurationsFolderFile = try? mainFolder.createFile(named: "configurations.json") else {

return
}

let configurations = Configuration(dataFolder: newValue)
guard let writeData = try? configurations.jsonData() else { return }

try? configurationsFolderFile.write(writeData)
}
}
}
}
}
21 changes: 21 additions & 0 deletions src/Mockingbird/Utils/Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ class Util {
NSWorkspace.shared.selectFile(nil, inFileViewerRootedAtPath: folder)
}

static func chooseFolder() -> String? {

let dialog = NSOpenPanel()

dialog.title = "Choose mocked data directory"
dialog.showsResizeIndicator = true
dialog.showsHiddenFiles = false
dialog.canChooseFiles = false
dialog.canChooseDirectories = true

if (dialog.runModal() == NSApplication.ModalResponse.OK) {

let result = dialog.url
return result?.path

} else {

return nil
}
}

static func openURL(url: String) {

if let url = URL(string: url) {
Expand Down
2 changes: 1 addition & 1 deletion src/MockingbirdTests/Utils/DataHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private extension DataHelper {
}
}

Default.Folder.main = resourcePath
Default.Folder.workingDirectory = resourcePath

} catch {

Expand Down