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

Create folder for downloading CLI if needed #1069

Merged
merged 2 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ package-lock.json
scripts/apollo
scripts/apollo.tar.gz
SwiftScripts/ApolloCLI
Tests/ApolloCodegenTests/scripts
2 changes: 2 additions & 0 deletions Sources/ApolloCodegenLib/CLIDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ struct CLIDownloader {
/// - zipFileURL: The URL where downloaded data should be saved.
/// - timeout: The maximum time to wait before indicating that the download timed out, in seconds.
private static func download(to zipFileURL: URL, timeout: Double) throws {
try FileManager.default.apollo_createContainingFolderIfNeeded(for: zipFileURL)

CodegenLogger.log("Downloading zip file with the CLI...")
let semaphore = DispatchSemaphore(value: 0)
var errorToThrow: Error? = CLIDownloaderError.downloadTimedOut(after: timeout)
Expand Down
11 changes: 11 additions & 0 deletions Tests/ApolloCodegenTests/CLIDownloaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ class CLIDownloaderTests: XCTestCase {
XCTAssertEqual(try FileManager.default.apollo_shasum(at: zipFileURL), CLIExtractor.expectedSHASUM)
}

func testDownloadingToFolderThatDoesntAlreadyExistWorks() throws {
let scriptsURL = CodegenTestHelper.cliFolderURL()
try FileManager.default.apollo_deleteFolder(at: scriptsURL)

XCTAssertFalse(FileManager.default.apollo_folderExists(at: scriptsURL))

try CLIDownloader.downloadIfNeeded(cliFolderURL: scriptsURL, timeout: 90.0)

XCTAssertTrue(FileManager.default.apollo_folderExists(at: scriptsURL))
}

func testTimeoutThrowsCorrectError() throws {
let scriptsURL = CodegenTestHelper.cliFolderURL()

Expand Down
9 changes: 5 additions & 4 deletions Tests/ApolloCodegenTests/CodegenTestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ struct CodegenTestHelper {
static var timeout: Double = 90.0

static func sourceRootURL() -> URL {
let parentFolder = FileFinder.findParentFolder()
return parentFolder
FileFinder.findParentFolder()
.deletingLastPathComponent() // Tests
.deletingLastPathComponent() // apollo-ios
}

static func cliFolderURL() -> URL {
let sourceRoot = self.sourceRootURL()
return sourceRoot.appendingPathComponent("scripts")
self.sourceRootURL()
.appendingPathComponent("Tests")
.appendingPathComponent("ApolloCodegenTests")
.appendingPathComponent("scripts")
}

static func apolloFolderURL() -> URL {
Expand Down
6 changes: 5 additions & 1 deletion docs/source/swift-scripting.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ You can use this to get the URL of the folder you plan to download the CLI to:
let cliFolderURL = sourceRootURL
.appendingPathComponent("Codegen")
.appendingPathComponent("ApolloCLI")
```
```

>**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.
>
> 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.

Now, with access to both the `sourceRootURL` and the `cliFolderURL`, it's time to use your script to do neat stuff for you!

Expand Down