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 3 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
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
48 changes: 44 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,49 @@ 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)/dataFolder.json"),
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 file = try? File(path: "\(Default.Folder.main)/dataFolder.json") {

let writeData = "dataFolder="+newValue

try? file.write(writeData)

} else {

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

return
}

let writeData = "dataFolder="+newValue

try? dataFolderFile.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