@@ -30,6 +30,30 @@ struct FetchDeprecatedDiagnostic: DiagnosticData {
30
30
)
31
31
}
32
32
33
+ struct RequiredArgumentDiagnostic : DiagnosticData {
34
+ static let id = DiagnosticID (
35
+ type: RequiredArgumentDiagnostic . self,
36
+ name: " org.swift.diags.required-argument " ,
37
+ defaultBehavior: . error,
38
+ description: {
39
+ $0 <<< " missing required argument " <<< { " \( $0. argument) " }
40
+ }
41
+ )
42
+
43
+ let argument : String
44
+ }
45
+
46
+ struct RequiredSubcommandDiagnostic : DiagnosticData {
47
+ static let id = DiagnosticID (
48
+ type: RequiredSubcommandDiagnostic . self,
49
+ name: " org.swift.diags.required-subcommand " ,
50
+ defaultBehavior: . error,
51
+ description: {
52
+ $0 <<< " missing required subcommand; use --help to list available subcommands "
53
+ }
54
+ )
55
+ }
56
+
33
57
/// swift-package tool namespace
34
58
public class SwiftPackageTool : SwiftTool < PackageToolOptions > {
35
59
@@ -48,6 +72,51 @@ public class SwiftPackageTool: SwiftTool<PackageToolOptions> {
48
72
case . version:
49
73
print ( Versioning . currentVersion. completeDisplayString)
50
74
75
+ case . config:
76
+ guard let configMode = options. configMode else {
77
+ diagnostics. emit ( data: RequiredSubcommandDiagnostic ( ) )
78
+ return
79
+ }
80
+
81
+ let config = try getSwiftPMConfig ( )
82
+ try config. load ( )
83
+
84
+ switch configMode {
85
+ case . getMirror:
86
+ guard let packageURL = options. configOptions. packageURL else {
87
+ diagnostics. emit ( data: RequiredArgumentDiagnostic ( argument: " --package-url " ) )
88
+ return
89
+ }
90
+
91
+ if let mirror = config. getMirror ( forURL: packageURL) {
92
+ print ( mirror)
93
+ } else {
94
+ stderrStream <<< " not found \n "
95
+ stderrStream. flush ( )
96
+ executionStatus = . failure
97
+ }
98
+
99
+ case . unsetMirror:
100
+ guard let packageOrMirror = options. configOptions. packageURL ?? options. configOptions. mirrorURL else {
101
+ diagnostics. emit ( data: RequiredArgumentDiagnostic ( argument: " --package-url or --mirror-url " ) )
102
+ return
103
+ }
104
+
105
+ try config. unset ( packageOrMirrorURL: packageOrMirror)
106
+
107
+ case . setMirror:
108
+ guard let packageURL = options. configOptions. packageURL else {
109
+ diagnostics. emit ( data: RequiredArgumentDiagnostic ( argument: " --package-url " ) )
110
+ return
111
+ }
112
+ guard let mirrorURL = options. configOptions. mirrorURL else {
113
+ diagnostics. emit ( data: RequiredArgumentDiagnostic ( argument: " --mirror-url " ) )
114
+ return
115
+ }
116
+
117
+ try config. set ( mirrorURL: mirrorURL, forPackageURL: packageURL)
118
+ }
119
+
51
120
case . initPackage:
52
121
// FIXME: Error handling.
53
122
let cwd = localFileSystem. currentWorkingDirectory!
@@ -347,6 +416,58 @@ public class SwiftPackageTool: SwiftTool<PackageToolOptions> {
347
416
usage: " Set tools version of package to the current tools version in use " ) ,
348
417
to: { if $1 { $0. toolsVersionMode = . setCurrent } } )
349
418
419
+ // SwiftPM config subcommand.
420
+ let configParser = parser. add (
421
+ subparser: PackageMode . config. rawValue,
422
+ overview: " Manipulate configuration of the package " )
423
+ binder. bind ( parser: configParser,
424
+ to: { $0. configMode = PackageToolOptions . ConfigMode ( rawValue: $1) ! } )
425
+
426
+ let setMirrorParser = configParser. add (
427
+ subparser: PackageToolOptions . ConfigMode. setMirror. rawValue,
428
+ overview: " Set a mirror for a dependency " )
429
+
430
+ binder. bind (
431
+ setMirrorParser. add (
432
+ option: " --package-url " , kind: String . self,
433
+ usage: " The package dependency url " ) ,
434
+ setMirrorParser. add (
435
+ option: " --mirror-url " , kind: String . self,
436
+ usage: " The mirror url " ) ,
437
+ to: {
438
+ $0. configOptions. packageURL = $1
439
+ $0. configOptions. mirrorURL = $2
440
+ }
441
+ )
442
+
443
+ let unsetMirrorParser = configParser. add (
444
+ subparser: PackageToolOptions . ConfigMode. unsetMirror. rawValue,
445
+ overview: " Remove an existing mirror " )
446
+ binder. bind (
447
+ unsetMirrorParser. add (
448
+ option: " --package-url " , kind: String . self,
449
+ usage: " The package dependency url " ) ,
450
+ unsetMirrorParser. add (
451
+ option: " --mirror-url " , kind: String . self,
452
+ usage: " The mirror url " ) ,
453
+ to: {
454
+ $0. configOptions. packageURL = $1
455
+ $0. configOptions. mirrorURL = $2
456
+ }
457
+ )
458
+
459
+ let getMirrorParser = configParser. add (
460
+ subparser: PackageToolOptions . ConfigMode. getMirror. rawValue,
461
+ overview: " Print mirror configuration for the given package dependency " )
462
+ binder. bind (
463
+ option: getMirrorParser. add (
464
+ option: " --package-url " , kind: String . self, usage: " The package dependency url " ) ,
465
+ to: {
466
+ $0. configOptions. packageURL = $1
467
+ }
468
+ )
469
+
470
+ // Xcode project generation.
350
471
let generateXcodeParser = parser. add (
351
472
subparser: PackageMode . generateXcodeproj. rawValue,
352
473
overview: " Generates an Xcode project " )
@@ -482,10 +603,24 @@ public class PackageToolOptions: ToolOptions {
482
603
case setCurrent
483
604
}
484
605
var toolsVersionMode : ToolsVersionMode = . display
606
+
607
+ enum ConfigMode : String {
608
+ case setMirror = " set-mirror "
609
+ case unsetMirror = " unset-mirror "
610
+ case getMirror = " get-mirror "
611
+ }
612
+ var configMode : ConfigMode ?
613
+
614
+ struct ConfigOptions {
615
+ var packageURL : String ?
616
+ var mirrorURL : String ?
617
+ }
618
+ var configOptions = ConfigOptions ( )
485
619
}
486
620
487
621
public enum PackageMode : String , StringEnumArgument {
488
622
case clean
623
+ case config
489
624
case describe
490
625
case dumpPackage = " dump-package "
491
626
case edit
@@ -548,6 +683,12 @@ extension PackageToolOptions.CompletionToolMode: StringEnumArgument {
548
683
}
549
684
}
550
685
686
+ extension PackageToolOptions . ConfigMode : StringEnumArgument {
687
+ static var completion : ShellCompletion {
688
+ return . none
689
+ }
690
+ }
691
+
551
692
extension SwiftPackageTool : ToolName {
552
693
static var toolName : String {
553
694
return " swift package "
0 commit comments