Skip to content

Commit

Permalink
Refactor plugin to write to top level .vercel directory
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBarba committed Jan 24, 2023
1 parent 3eddfe0 commit 6c9dbcb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
9 changes: 5 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ let package = Package(
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime")
]),
.plugin(name: "VercelPackager", capability: .command(
intent: .custom(
verb: "vercel",
description: "Build and deploy your Swift application to Vercel"
)
intent: .custom(verb: "vercel", description: "Build and deploy your Swift application to Vercel")
// TODO: Add back explicit permission when SPM isn't so annoying about asking every run
// permissions: [
// .writeToPackageDirectory(reason: "Produce .vercel/output folder compliant with the Vercel Build Output API"),
// ]
)),
.testTarget(name: "VercelTests", dependencies: [
.byName(name: "Vercel")
Expand Down
4 changes: 3 additions & 1 deletion Plugins/VercelPackager/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ struct VercelPackager: CommandPlugin {
let vercelOutput = VercelOutput(packageManager: packageManager, context: context, arguments: arguments)
try await vercelOutput.prepare()
try await vercelOutput.build()
try await vercelOutput.deploy()
if vercelOutput.isDeploy {
try await vercelOutput.deploy()
}
}
}
67 changes: 35 additions & 32 deletions Plugins/VercelPackager/VercelOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public struct VercelOutput {
}

public func deploy() async throws {
print("")
print("-------------------------------------------------------------------------")
print("Deploying to Vercel: \"\(product.name)\"")
print("-------------------------------------------------------------------------")
print("")

var deployArguments = [
"--cwd", context.pluginWorkDirectory.string,
"deploy",
Expand Down Expand Up @@ -82,6 +88,14 @@ extension VercelOutput {
return deployableProducts[0]
}

public var isDeploy: Bool {
arguments.contains("--deploy")
}

public var isProduction: Bool {
arguments.contains("--prod")
}

public var functionMemory: String {
argument("memory") ?? "512"
}
Expand Down Expand Up @@ -130,8 +144,12 @@ extension VercelOutput {

extension VercelOutput {

public var projectDirectory: Path {
context.package.directory
}

public var vercelDirectory: Path {
context.pluginWorkDirectory.appending(".vercel")
projectDirectory.appending(".vercel")
}

public var vercelOutputDirectory: Path {
Expand All @@ -146,24 +164,12 @@ extension VercelOutput {
vercelFunctionsDirectory.appending("\(product.name).func")
}

public var pluginGitDirectory: Path {
context.pluginWorkDirectory.appending(".git")
}

public var projectGitDirectory: Path {
context.package.directory.appending(".git")
}

public func createDirectoryStructure() throws {
// Clean the directory
try? fs.removeItem(atPath: vercelDirectory.string)
// Copy git directory to populate vercel commits
if fs.fileExists(atPath: projectGitDirectory.string) {
try? fs.removeItem(atPath: pluginGitDirectory.string)
try fs.copyItem(atPath: projectGitDirectory.string, toPath: pluginGitDirectory.string)
}
// Ensure we have a top level vercel directory
try? fs.createDirectory(atPath: vercelDirectory.string, withIntermediateDirectories: true)
// Clean the vercel output directory
try? fs.removeItem(atPath: vercelOutputDirectory.string)
// Create new directories
try fs.createDirectory(atPath: vercelDirectory.string, withIntermediateDirectories: true)
try fs.createDirectory(atPath: vercelOutputDirectory.string, withIntermediateDirectories: true)
try fs.createDirectory(atPath: vercelFunctionsDirectory.string, withIntermediateDirectories: true)
// Create directories for each product
Expand All @@ -178,7 +184,7 @@ extension VercelOutput {
extension VercelOutput {

public var projectPublicDirectory: Path {
context.package.directory.appending("public")
projectDirectory.appending("public")
}

public var vercelStaticDirectory: Path {
Expand Down Expand Up @@ -213,8 +219,7 @@ extension VercelOutput {
}

public func localProjectConfiguration() -> ProjectConfiguration? {
let localPath = context.package.directory.appending(".vercel").appending("project.json")
guard let data = fs.contents(atPath: localPath.string) else {
guard let data = fs.contents(atPath: vercelProjectConfigurationPath.string) else {
return nil
}
guard let config = try? JSONDecoder().decode(ProjectConfiguration.self, from: data) else {
Expand Down Expand Up @@ -324,21 +329,23 @@ extension VercelOutput {
extension VercelOutput {

public func buildProduct(_ product: Product) async throws -> Path {
if Utils.isAmazonLinux {
return try await buildNativeProduct(product)
} else {
print("")
print("-------------------------------------------------------------------------")
print("Building product: \"\(product.name)\"")
print("-------------------------------------------------------------------------")
print("")

if isDeploy, Utils.isAmazonLinux == false {
return try await buildDockerProduct(product)
} else {
return try await buildNativeProduct(product)
}
}

private func buildNativeProduct(_ product: Product) async throws -> Path {
print("-------------------------------------------------------------------------")
print("Building product: \"\(product.name)\"")
print("-------------------------------------------------------------------------")

var parameters = PackageManager.BuildParameters()
parameters.configuration = .release
parameters.otherSwiftcFlags = ["-static-stdlib"]
parameters.otherSwiftcFlags = Utils.isAmazonLinux ? ["-static-stdlib"] : []
parameters.logging = .concise

let result = try packageManager.build(
Expand All @@ -357,10 +364,6 @@ extension VercelOutput {
let dockerToolPath = try context.tool(named: "docker").path
let baseImage = "swift:5.7-amazonlinux2"

print("-------------------------------------------------------------------------")
print("Building product: \"\(product.name)\"")
print("-------------------------------------------------------------------------")

// update the underlying docker image, if necessary
print("updating \"\(baseImage)\" docker image")
try await Shell.execute(
Expand Down

0 comments on commit 6c9dbcb

Please sign in to comment.