Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit 8b34074

Browse files
committed
now prompts to replace existing file
1 parent 299234b commit 8b34074

4 files changed

Lines changed: 141 additions & 12 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1300"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "C636435D2670E1B800301DC8"
18+
BuildableName = "Download Full Installer.app"
19+
BlueprintName = "Download Full Installer"
20+
ReferencedContainer = "container:FetchInstallerPkg.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES">
30+
<Testables>
31+
</Testables>
32+
</TestAction>
33+
<LaunchAction
34+
buildConfiguration = "Debug"
35+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
36+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
37+
launchStyle = "0"
38+
useCustomWorkingDirectory = "NO"
39+
ignoresPersistentStateOnLaunch = "NO"
40+
debugDocumentVersioning = "YES"
41+
debugServiceExtension = "internal"
42+
enableGPUValidationMode = "1"
43+
allowLocationSimulation = "YES">
44+
<BuildableProductRunnable
45+
runnableDebuggingMode = "0">
46+
<BuildableReference
47+
BuildableIdentifier = "primary"
48+
BlueprintIdentifier = "C636435D2670E1B800301DC8"
49+
BuildableName = "Download Full Installer.app"
50+
BlueprintName = "Download Full Installer"
51+
ReferencedContainer = "container:FetchInstallerPkg.xcodeproj">
52+
</BuildableReference>
53+
</BuildableProductRunnable>
54+
</LaunchAction>
55+
<ProfileAction
56+
buildConfiguration = "Release"
57+
shouldUseLaunchSchemeArgsEnv = "YES"
58+
savedToolIdentifier = ""
59+
useCustomWorkingDirectory = "NO"
60+
debugDocumentVersioning = "YES">
61+
<BuildableProductRunnable
62+
runnableDebuggingMode = "0">
63+
<BuildableReference
64+
BuildableIdentifier = "primary"
65+
BlueprintIdentifier = "C636435D2670E1B800301DC8"
66+
BuildableName = "Download Full Installer.app"
67+
BlueprintName = "Download Full Installer"
68+
ReferencedContainer = "container:FetchInstallerPkg.xcodeproj">
69+
</BuildableReference>
70+
</BuildableProductRunnable>
71+
</ProfileAction>
72+
<AnalyzeAction
73+
buildConfiguration = "Debug">
74+
</AnalyzeAction>
75+
<ArchiveAction
76+
buildConfiguration = "Release"
77+
revealArchiveInOrganizer = "YES">
78+
</ArchiveAction>
79+
</Scheme>

FetchInstallerPkg.xcodeproj/xcuserdata/armin.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
filePath = "FetchInstallerPkg/View/InstallerView.swift"
3131
startingColumnNumber = "9223372036854775807"
3232
endingColumnNumber = "9223372036854775807"
33-
startingLineNumber = "51"
34-
endingLineNumber = "51"
33+
startingLineNumber = "88"
34+
endingLineNumber = "88"
3535
landmarkName = "body"
3636
landmarkType = "24">
3737
<Actions>

FetchInstallerPkg/DownloadManager.swift

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,17 @@ import AppKit
2323

2424
static let shared = DownloadManager()
2525

26-
func download(url: URL?) {
26+
var fileExists: Bool {
27+
let destination = Prefs.downloadURL
28+
if self.filename != nil {
29+
let file = destination.appendingPathComponent(filename!)
30+
return FileManager.default.fileExists(atPath: file.path)
31+
} else {
32+
return false
33+
}
34+
}
35+
36+
func download(url: URL?, replacing: Bool = false) throws {
2737
// reset the variables
2838
progress = 0.0
2939
isDownloading = true
@@ -33,6 +43,13 @@ import AppKit
3343

3444
byteFormatter.countStyle = .file
3545

46+
if replacing {
47+
let destination = Prefs.downloadURL
48+
let suggestedFilename = filename ?? "InstallerAssistant.pkg"
49+
let file = destination.appendingPathComponent(suggestedFilename)
50+
try FileManager.default.removeItem(at: file)
51+
}
52+
3653
if url != nil {
3754
downloadTask = urlSession.downloadTask(with: url!)
3855
downloadTask!.resume()
@@ -51,20 +68,16 @@ import AppKit
5168

5269
func revealInFinder() {
5370
if isComplete {
54-
guard let destination = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first else { return }
55-
NSWorkspace.shared.selectFile(localURL?.path, inFileViewerRootedAtPath: destination.path)
71+
let destination = Prefs.downloadPath
72+
NSWorkspace.shared.selectFile(localURL?.path, inFileViewerRootedAtPath: destination)
5673
}
5774
}
5875
}
5976

6077
extension DownloadManager : URLSessionDownloadDelegate {
6178
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
6279
NSLog("urlSession, didFinishDownloading")
63-
guard
64-
let destination = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first
65-
else {
66-
return
67-
}
80+
let destination = Prefs.downloadURL
6881

6982
// get the suggest file name or create a uuid string
7083
let suggestedFilename = filename ?? downloadTask.response?.suggestedFilename ?? UUID().uuidString

FetchInstallerPkg/View/InstallerView.swift

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import SwiftUI
1010
struct InstallerView: View {
1111
@ObservedObject var product: Product
1212
@StateObject var downloadManager = DownloadManager.shared
13+
@State var isReplacingFile = false
14+
@State var failed = false
15+
@State var filename = "InstallerAssistant.pkg"
16+
1317
var body: some View {
1418
if product.isLoading {
1519
Text("Loading...")
@@ -37,11 +41,44 @@ struct InstallerView: View {
3741

3842

3943
Button(action: {
40-
DownloadManager.shared.filename = "InstallAssistant-\(product.productVersion ?? "V")-\(product.buildVersion ?? "B").pkg"
41-
DownloadManager.shared.download(url: product.installAssistantURL)
44+
filename = "InstallAssistant-\(product.productVersion ?? "V")-\(product.buildVersion ?? "B").pkg"
45+
downloadManager.filename = filename
46+
isReplacingFile = downloadManager.fileExists
47+
48+
if !isReplacingFile {
49+
do {
50+
try downloadManager.download(url: product.installAssistantURL)
51+
} catch {
52+
failed = true
53+
}
54+
}
55+
4256
}) {
4357
Image(systemName: "arrow.down.circle").font(.title)
4458
}
59+
.alert(isPresented: $isReplacingFile) {
60+
Alert(
61+
title: Text("\(filename)” already exists! Do you want to replace it?"),
62+
message: Text("A file with the same name already exists in that location. Replacing it will overwrite its current contents."),
63+
primaryButton: .cancel(Text("Cancel")),
64+
secondaryButton: .destructive(
65+
Text("Replace"),
66+
action: {
67+
do {
68+
try downloadManager.download(url: product.installAssistantURL, replacing: true)
69+
} catch {
70+
failed = true
71+
}
72+
}
73+
)
74+
)
75+
}
76+
// .alert(isPresented: $failed) {
77+
// Alert(
78+
// title: Text("An error occured while downloading"),
79+
// message: Text("Please try again or restart the application.")
80+
// )
81+
// }
4582
.disabled(downloadManager.isDownloading)
4683
.buttonStyle(.borderless)
4784
.controlSize(/*@START_MENU_TOKEN@*/.large/*@END_MENU_TOKEN@*/)

0 commit comments

Comments
 (0)