File: Context.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (110 lines) | stat: -rw-r--r-- 5,561 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Foundation

/// Provides information about the package for which the plugin is invoked,
/// as well as contextual information based on the plugin's stated intent
/// and requirements.
public struct PluginContext {
    /// Information about the package to which the plugin is being applied.
    public let package: Package

    /// The path of a writable directory into which the plugin or the build
    /// commands it constructs can write anything it wants. This could include
    /// any generated source files that should be processed further, and it
    /// could include any caches used by the build tool or the plugin itself.
    /// The plugin is in complete control of what is written under this di-
    /// rectory, and the contents are preserved between builds.
    ///
    /// A plugin would usually create a separate subdirectory of this directory
    /// for each command it creates, and the command would be configured to
    /// write its outputs to that directory. The plugin may also create other
    /// directories for cache files and other file system content that either
    /// it or the command will need.
    @available(_PackageDescription, deprecated: 6.0, renamed: "pluginWorkDirectoryURL")
    public let pluginWorkDirectory: Path

    /// The path of a writable directory into which the plugin or the build
    /// commands it constructs can write anything it wants. This could include
    /// any generated source files that should be processed further, and it
    /// could include any caches used by the build tool or the plugin itself.
    /// The plugin is in complete control of what is written under this di-
    /// rectory, and the contents are preserved between builds.
    ///
    /// A plugin would usually create a separate subdirectory of this directory
    /// for each command it creates, and the command would be configured to
    /// write its outputs to that directory. The plugin may also create other
    /// directories for cache files and other file system content that either
    /// it or the command will need.
    @available(_PackageDescription, introduced: 6.0)
    public let pluginWorkDirectoryURL: URL

    /// Looks up and returns the path of a named command line executable tool.
    /// The executable must be provided by an executable target or a binary
    /// target on which the package plugin target depends. This function throws
    /// an error if the tool cannot be found. The lookup is case sensitive.
    public func tool(named name: String) throws -> Tool {
        if let tool = self.accessibleTools[name] {
            // For PluginAccessibleTool.builtTool, the triples value is not saved, thus
            // the value is always nil; this is intentional since if we are able to
            // build the tool, it is by definition supporting the target platform.
            // For PluginAccessibleTool.vendedTool, only supported triples are saved,
            // so empty triples means the tool is not supported on the target platform.
            if let triples = tool.triples, triples.isEmpty {
                throw PluginContextError.toolNotSupportedOnTargetPlatform(name: name)
            }
            return Tool(name: name, path: Path(url: tool.path), url: tool.path)
        } else {
            for dir in self.toolSearchDirectoryURLs {
                #if os(Windows)
                let hostExecutableSuffix = ".exe"
                #else
                let hostExecutableSuffix = ""
                #endif
                let path = dir.appendingPathComponent(name + hostExecutableSuffix)
                if FileManager.default.isExecutableFile(atPath: path.path) {
                    return Tool(name: name, path: Path(url: path), url: path)
                }
            }
        }
        throw PluginContextError.toolNotFound(name: name)
    }

    /// A mapping from tool names to their paths and triples. Not directly available
    /// to the plugin, but used by the `tool(named:)` API.
    let accessibleTools: [String: (path: URL, triples: [String]?)]

    /// The paths of directories of in which to search for tools that aren't in
    /// the `toolNamesToPaths` map.
    @available(_PackageDescription, deprecated: 6.0, renamed: "toolSearchDirectoryURLs")
    let toolSearchDirectories: [Path]

    /// The paths of directories of in which to search for tools that aren't in
    /// the `toolNamesToPaths` map.
    @available(_PackageDescription, introduced: 6.0)
    let toolSearchDirectoryURLs: [URL]

    /// Information about a particular tool that is available to a plugin.
    public struct Tool {
        /// Name of the tool (suitable for display purposes).
        public let name: String

        /// Full path of the built or provided tool in the file system.
        @available(_PackageDescription, deprecated: 6.0, renamed: "url")
        public let path: Path

        /// Full path of the built or provided tool in the file system.
        @available(_PackageDescription, introduced: 6.0)
        public let url: URL
    }
}