Skip to content

Commit

Permalink
Create install CLI command plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev committed Nov 15, 2022
1 parent 41bd17b commit de286e0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,17 @@ let package = Package(
exclude: [
"Info.plist",
]),
.plugin(
name: "InstallCLI",
capability: .command(
intent: .custom(
verb: "apollo-cli-install",
description: "Installs the Apollo iOS Command line interface."),
permissions: [
.writeToPackageDirectory(reason: "Creates a symbolic link to the CLI executable in your project directory.")
]),
dependencies: [
"apollo-ios-cli"
]),
]
)
37 changes: 37 additions & 0 deletions Plugins/InstallCLI/InstallCLIPluginCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Foundation
import PackagePlugin

@main
struct InstallCLIPluginCommand: CommandPlugin {

func performCommand(context: PackagePlugin.PluginContext, arguments: [String]) async throws {
let pathToCLI = try context.tool(named: "apollo-ios-cli").path
try createSymbolicLink(from: pathToCLI, to: context.package.directory)
}

func createSymbolicLink(from: PackagePlugin.Path, to: PackagePlugin.Path) throws {
let task = Process()
task.standardInput = nil
task.environment = ProcessInfo.processInfo.environment
task.arguments = ["-c", "ln -s \(from.string) \(to.string)"]
task.executableURL = URL(fileURLWithPath: "/bin/zsh")
try task.run()
task.waitUntilExit()
}

}

#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin

extension InstallCLIPluginCommand: XcodeCommandPlugin {

/// 👇 This entry point is called when operating on an Xcode project.
func performCommand(context: XcodePluginContext, arguments: [String]) throws {
print("Installing Apollo CLI Plugin to Xcode project \(context.xcodeProject.displayName)")
let pathToCLI = try context.tool(named: "apollo-ios-cli").path
try createSymbolicLink(from: pathToCLI, to: context.xcodeProject.directory)
}

}
#endif

0 comments on commit de286e0

Please sign in to comment.