Skip to content

Commit db87979

Browse files
Merge pull request #1069 from apollographql/add/create-folder
Create folder for downloading CLI if needed
2 parents eede443 + 4bcbb5f commit db87979

5 files changed

Lines changed: 24 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ package-lock.json
5656
scripts/apollo
5757
scripts/apollo.tar.gz
5858
SwiftScripts/ApolloCLI
59+
Tests/ApolloCodegenTests/scripts

Sources/ApolloCodegenLib/CLIDownloader.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ struct CLIDownloader {
6767
/// - zipFileURL: The URL where downloaded data should be saved.
6868
/// - timeout: The maximum time to wait before indicating that the download timed out, in seconds.
6969
private static func download(to zipFileURL: URL, timeout: Double) throws {
70+
try FileManager.default.apollo_createContainingFolderIfNeeded(for: zipFileURL)
71+
7072
CodegenLogger.log("Downloading zip file with the CLI...")
7173
let semaphore = DispatchSemaphore(value: 0)
7274
var errorToThrow: Error? = CLIDownloaderError.downloadTimedOut(after: timeout)

Tests/ApolloCodegenTests/CLIDownloaderTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ class CLIDownloaderTests: XCTestCase {
2121
XCTAssertEqual(try FileManager.default.apollo_shasum(at: zipFileURL), CLIExtractor.expectedSHASUM)
2222
}
2323

24+
func testDownloadingToFolderThatDoesntAlreadyExistWorks() throws {
25+
let scriptsURL = CodegenTestHelper.cliFolderURL()
26+
try FileManager.default.apollo_deleteFolder(at: scriptsURL)
27+
28+
XCTAssertFalse(FileManager.default.apollo_folderExists(at: scriptsURL))
29+
30+
try CLIDownloader.downloadIfNeeded(cliFolderURL: scriptsURL, timeout: 90.0)
31+
32+
XCTAssertTrue(FileManager.default.apollo_folderExists(at: scriptsURL))
33+
}
34+
2435
func testTimeoutThrowsCorrectError() throws {
2536
let scriptsURL = CodegenTestHelper.cliFolderURL()
2637

Tests/ApolloCodegenTests/CodegenTestHelper.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ struct CodegenTestHelper {
1515
static var timeout: Double = 90.0
1616

1717
static func sourceRootURL() -> URL {
18-
let parentFolder = FileFinder.findParentFolder()
19-
return parentFolder
18+
FileFinder.findParentFolder()
2019
.deletingLastPathComponent() // Tests
2120
.deletingLastPathComponent() // apollo-ios
2221
}
2322

2423
static func cliFolderURL() -> URL {
25-
let sourceRoot = self.sourceRootURL()
26-
return sourceRoot.appendingPathComponent("scripts")
24+
self.sourceRootURL()
25+
.appendingPathComponent("Tests")
26+
.appendingPathComponent("ApolloCodegenTests")
27+
.appendingPathComponent("scripts")
2728
}
2829

2930
static func apolloFolderURL() -> URL {

docs/source/swift-scripting.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ You can use this to get the URL of the folder you plan to download the CLI to:
7474
let cliFolderURL = sourceRootURL
7575
.appendingPathComponent("Codegen")
7676
.appendingPathComponent("ApolloCLI")
77-
```
77+
```
78+
79+
>**Note**: We recommend adding this folder to your `.gitignore`, because otherwise you'll be adding the zip file and a ton of JS code to your repo.
80+
>
81+
> If you're on versions prior to `0.24.0`, throw an empty `.keep` file and force-add it to git to preserve the folder structure. Versions after `0.24.0` automatically create the folder being downloaded to if it doesn't exist.
7882
7983
Now, with access to both the `sourceRootURL` and the `cliFolderURL`, it's time to use your script to do neat stuff for you!
8084

0 commit comments

Comments
 (0)