@@ -4,164 +4,155 @@ import com.github.gradle.node.exec.NodeExecConfiguration
4
4
import java.net.URLEncoder
5
5
import kotlin.text.Charsets.UTF_8
6
6
7
- object NpmProxy {
8
-
9
- // companion object {
10
- // These are the environment variables that HTTPing applications checks, proxy is on and off.
11
- // FTP skipped in hopes of a better future.
12
- private val proxyVariables = listOf (
13
- " HTTP_PROXY" , " HTTPS_PROXY" , " NO_PROXY" , " PROXY"
14
- )
15
-
16
- // And since npm also takes settings in the form of environment variables with the
17
- // NPM_CONFIG_<setting> format, we should check those. Hopefully nobody does this.
18
- // Windows will let you set environment variables with hyphens in them, but shells
19
- // on Linux will fight you. So you'll have to be pretty sneaky to do this.
20
- // I'm adding both here "just in case".
21
- private val npmProxyVariables = listOf (
22
- " NPM_CONFIG_PROXY" , " NPM_CONFIG_HTTPS-PROXY" , " NPM_CONFIG_HTTPS_PROXY" , " NPM_CONFIG_NOPROXY"
23
- )
24
-
25
- /* *
26
- * Creates a map of environment variables with proxy settings.
27
- *
28
- * Will return an empty map if none are set.
29
- */
30
- fun computeNpmProxyEnvironmentVariables (): Map <String , String > {
31
- val proxyEnvironmentVariables = computeProxyUrlEnvironmentVariables()
32
- if (proxyEnvironmentVariables.isNotEmpty()) {
33
- addProxyIgnoredHostsEnvironmentVariable(proxyEnvironmentVariables)
34
- }
35
- return proxyEnvironmentVariables.toMap()
36
- }
7
+ class NpmProxy {
8
+
9
+ companion object {
10
+ // These are the environment variables that HTTPing applications checks, proxy is on and off.
11
+ // FTP skipped in hopes of a better future.
12
+ private val proxyVariables = listOf (
13
+ " HTTP_PROXY" , " HTTPS_PROXY" , " NO_PROXY" , " PROXY"
14
+ )
37
15
38
- /* *
39
- * Helper function for deciding whether proxy settings need to be set or not.
40
- */
41
- fun shouldConfigureProxy (env : Map <String , String >, settings : ProxySettings ): Boolean {
42
- if (settings == ProxySettings .FORCED ) {
43
- return true
44
- } else if (settings == ProxySettings .SMART ) {
45
- return ! hasProxyConfiguration(env)
16
+ // And since npm also takes settings in the form of environment variables with the
17
+ // NPM_CONFIG_<setting> format, we should check those. Hopefully nobody does this.
18
+ // Windows will let you set environment variables with hyphens in them, but shells
19
+ // on Linux will fight you. So you'll have to be pretty sneaky to do this.
20
+ // I'm adding both here "just in case".
21
+ private val npmProxyVariables = listOf (
22
+ " NPM_CONFIG_PROXY" , " NPM_CONFIG_HTTPS-PROXY" , " NPM_CONFIG_HTTPS_PROXY" , " NPM_CONFIG_NOPROXY"
23
+ )
24
+
25
+ /* *
26
+ * Creates a map of environment variables with proxy settings.
27
+ *
28
+ * Will return an empty map if none are set.
29
+ */
30
+ fun computeNpmProxyEnvironmentVariables (): Map <String , String > {
31
+ val proxyEnvironmentVariables = computeProxyUrlEnvironmentVariables()
32
+ if (proxyEnvironmentVariables.isNotEmpty()) {
33
+ addProxyIgnoredHostsEnvironmentVariable(proxyEnvironmentVariables)
34
+ }
35
+ return proxyEnvironmentVariables.toMap()
46
36
}
47
37
48
- return false
49
- }
38
+ /* *
39
+ * Helper function for deciding whether proxy settings need to be set or not.
40
+ */
41
+ fun shouldConfigureProxy (env : Map <String , String >, settings : ProxySettings ): Boolean {
42
+ if (settings == ProxySettings .FORCED ) {
43
+ return true
44
+ } else if (settings == ProxySettings .SMART ) {
45
+ return ! hasProxyConfiguration(env)
46
+ }
50
47
51
- /* *
52
- * Returns true if the given map of environment variables has any
53
- * proxy settings configured.
54
- *
55
- * @param env map of environment variables
56
- */
57
- fun hasProxyConfiguration (env : Map <String , String >): Boolean {
58
- return env.keys.any {
59
- proxyVariables.contains(it.toUpperCase()) || npmProxyVariables.contains(it.toUpperCase())
48
+ return false
60
49
}
61
- }
62
50
63
- /* *
64
- * Get a list of all known keys that affect the proxy configuration
65
- */
66
- fun getKnownProxyConfigurationKeys (): Set <String > {
67
- return proxyVariables.plus(npmProxyVariables).toSet()
68
- }
51
+ /* *
52
+ * Returns true if the given map of environment variables has any
53
+ * proxy settings configured.
54
+ *
55
+ * @param env map of environment variables
56
+ */
57
+ fun hasProxyConfiguration (env : Map <String , String >): Boolean {
58
+ return env.keys.any {
59
+ proxyVariables.contains(it.toUpperCase()) || npmProxyVariables.contains(it.toUpperCase())
60
+ }
61
+ }
69
62
70
- /* *
71
- * Creates a new NodeExecConfiguration with the proxy environment variables configured
72
- */
73
- fun addProxyEnvironmentVariables (
74
- proxySettings : ProxySettings ,
75
- nodeExecConfiguration : NodeExecConfiguration ,
76
- environment : Map <String , String > = System .getenv()
77
- ): NodeExecConfiguration {
78
- val environmentVariables = createProxyEnvironmentVariables(
79
- proxySettings,
80
- nodeExecConfiguration.environment,
81
- environment,
82
- )
83
- return nodeExecConfiguration.copy(environment = environmentVariables)
84
- }
63
+ /* *
64
+ * Get a list of all known keys that affect the proxy configuration
65
+ */
66
+ fun getKnownProxyConfigurationKeys (): Set <String > {
67
+ return proxyVariables.plus(npmProxyVariables).toSet()
68
+ }
69
+
70
+ /* *
71
+ * Creates a new NodeExecConfiguration with the proxy environment variables configured
72
+ */
73
+ fun addProxyEnvironmentVariables (
74
+ proxySettings : ProxySettings ,
75
+ nodeExecConfiguration : NodeExecConfiguration ,
76
+ environment : Map <String , String > = System .getenv()
77
+ ): NodeExecConfiguration {
78
+ val environmentVariables = createProxyEnvironmentVariables(
79
+ proxySettings,
80
+ nodeExecConfiguration.environment,
81
+ environment,
82
+ )
83
+ return nodeExecConfiguration.copy(environment = environmentVariables)
84
+ }
85
85
86
- fun createProxyEnvironmentVariables (
87
- proxySettings : ProxySettings ,
88
- nodeExecConfigurationEnvironment : Map <String , String >,
89
- environment : Map <String , String > = System .getenv()
90
- ): Map <String , String > {
91
- if (shouldConfigureProxy(environment, proxySettings)) {
92
- val npmProxyEnvironmentVariables = computeNpmProxyEnvironmentVariables()
93
- val environmentVariablesToUnset =
94
- if (proxySettings == ProxySettings .FORCED ) getKnownProxyConfigurationKeys()
95
- else emptySet()
96
- if (npmProxyEnvironmentVariables.isNotEmpty()) {
97
- val environmentVariables =
98
- nodeExecConfigurationEnvironment
99
- .minus(environmentVariablesToUnset)
100
- .plus(npmProxyEnvironmentVariables)
101
- return environmentVariables
86
+ fun createProxyEnvironmentVariables (
87
+ proxySettings : ProxySettings ,
88
+ nodeExecConfigurationEnvironment : Map <String , String >,
89
+ environment : Map <String , String > = System .getenv()
90
+ ): Map <String , String > {
91
+ if (shouldConfigureProxy(environment, proxySettings)) {
92
+ val npmProxyEnvironmentVariables = computeNpmProxyEnvironmentVariables()
93
+ val environmentVariablesToUnset =
94
+ if (proxySettings == ProxySettings .FORCED ) getKnownProxyConfigurationKeys()
95
+ else emptySet()
96
+ if (npmProxyEnvironmentVariables.isNotEmpty()) {
97
+ val environmentVariables =
98
+ nodeExecConfigurationEnvironment
99
+ .minus(environmentVariablesToUnset)
100
+ .plus(npmProxyEnvironmentVariables)
101
+ return environmentVariables
102
+ }
102
103
}
104
+ return emptyMap()
103
105
}
104
- return emptyMap()
105
- }
106
106
107
- private fun computeProxyUrlEnvironmentVariables (): MutableMap <String , String > {
108
- val proxyArgs = mutableMapOf<String , String >()
109
- for ((proxyProto, proxyParam) in
110
- listOf (arrayOf(" http" , " HTTP_PROXY" ), arrayOf(" https" , " HTTPS_PROXY" ))) {
111
- var proxyHost = System .getProperty(" $proxyProto .proxyHost" )
112
- val proxyPort = System .getProperty(" $proxyProto .proxyPort" )
113
- if (proxyHost != null && proxyPort != null ) {
114
- proxyHost = proxyHost.replace(" ^https?://" .toRegex(), " " )
115
- val proxyUser = System .getProperty(" $proxyProto .proxyUser" )
116
- val proxyPassword = System .getProperty(" $proxyProto .proxyPassword" )
117
- if (proxyUser != null && proxyPassword != null ) {
118
- proxyArgs[proxyParam] =
119
- " http://${encode(proxyUser)} :${encode(proxyPassword)} @$proxyHost :$proxyPort "
120
- } else {
121
- proxyArgs[proxyParam] = " http://$proxyHost :$proxyPort "
107
+ private fun computeProxyUrlEnvironmentVariables (): MutableMap <String , String > {
108
+ val proxyArgs = mutableMapOf<String , String >()
109
+ for ((proxyProto, proxyParam) in
110
+ listOf (arrayOf(" http" , " HTTP_PROXY" ), arrayOf(" https" , " HTTPS_PROXY" ))) {
111
+ var proxyHost = System .getProperty(" $proxyProto .proxyHost" )
112
+ val proxyPort = System .getProperty(" $proxyProto .proxyPort" )
113
+ if (proxyHost != null && proxyPort != null ) {
114
+ proxyHost = proxyHost.replace(" ^https?://" .toRegex(), " " )
115
+ val proxyUser = System .getProperty(" $proxyProto .proxyUser" )
116
+ val proxyPassword = System .getProperty(" $proxyProto .proxyPassword" )
117
+ if (proxyUser != null && proxyPassword != null ) {
118
+ proxyArgs[proxyParam] =
119
+ " http://${encode(proxyUser)} :${encode(proxyPassword)} @$proxyHost :$proxyPort "
120
+ } else {
121
+ proxyArgs[proxyParam] = " http://$proxyHost :$proxyPort "
122
+ }
122
123
}
123
124
}
125
+ return proxyArgs
124
126
}
125
- return proxyArgs
126
- }
127
127
128
- private fun encode (value : String ): String {
129
- return URLEncoder .encode(value, UTF_8 .toString())
130
- }
128
+ private fun encode (value : String ): String {
129
+ return URLEncoder .encode(value, UTF_8 .toString())
130
+ }
131
131
132
- private fun addProxyIgnoredHostsEnvironmentVariable (
133
- proxyEnvironmentVariables : MutableMap <String , String >,
134
- ) {
135
- val proxyIgnoredHosts = computeProxyIgnoredHosts()
136
- if (proxyIgnoredHosts.isNotEmpty()) {
137
- proxyEnvironmentVariables[" NO_PROXY" ] = proxyIgnoredHosts.joinToString(" , " )
132
+ private fun addProxyIgnoredHostsEnvironmentVariable (
133
+ proxyEnvironmentVariables : MutableMap <String , String >,
134
+ ) {
135
+ val proxyIgnoredHosts = computeProxyIgnoredHosts()
136
+ if (proxyIgnoredHosts.isNotEmpty()) {
137
+ proxyEnvironmentVariables[" NO_PROXY" ] = proxyIgnoredHosts.joinToString(" , " )
138
+ }
138
139
}
139
- }
140
140
141
- private fun computeProxyIgnoredHosts (): List <String > {
142
- return listOf (" http.nonProxyHosts" , " https.nonProxyHosts" )
143
- .map { property ->
144
- val propertyValue = System .getProperty(property)
145
- if (propertyValue != null ) {
146
- val hosts = propertyValue.split(" |" )
147
- return @map hosts
148
- .map { host ->
149
- if (host.contains(" :" )) host.split(" :" )[0 ]
150
- else host
141
+ private fun computeProxyIgnoredHosts (): List <String > {
142
+ return listOf (" http.nonProxyHosts" , " https.nonProxyHosts" )
143
+ .map { property ->
144
+ val propertyValue = System .getProperty(property)
145
+ if (propertyValue != null ) {
146
+ val hosts = propertyValue.split(" |" )
147
+ return @map hosts.map { host ->
148
+ host.substringBefore(" :" )
151
149
}
150
+ }
151
+ return @map listOf ()
152
152
}
153
- return @map listOf ()
154
- }
155
- .flatten()
156
- .distinct()
157
- // .collect(toList())
158
- }
159
-
160
- // }
161
- @Deprecated(
162
- " Replace with regular object" ,
163
- ReplaceWith (" NpmProxy" , imports = [" com.github.gradle.node.npm.proxy.NpmProxy" ])
164
- )
165
- object Companion
153
+ .flatten()
154
+ .distinct()
155
+ }
166
156
157
+ }
167
158
}
0 commit comments