-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathSetupTask.groovy
More file actions
196 lines (170 loc) · 5.05 KB
/
SetupTask.groovy
File metadata and controls
196 lines (170 loc) · 5.05 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.moowork.gradle.node.task
import com.moowork.gradle.node.NodeExtension
import com.moowork.gradle.node.variant.Variant
import org.gradle.api.DefaultTask
import org.gradle.api.artifacts.repositories.ArtifactRepository
import org.gradle.api.artifacts.repositories.IvyArtifactRepository
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
class SetupTask
extends DefaultTask
{
public final static String NAME = 'nodeSetup'
private NodeExtension config
protected Variant variant
private IvyArtifactRepository repo
private List<ArtifactRepository> allRepos;
SetupTask()
{
this.group = 'Node'
this.description = 'Download and install a local node/npm version.'
this.enabled = false
}
@Input
public Set<String> getInput()
{
configureIfNeeded()
def set = new HashSet<>()
set.add( this.config.download )
set.add( this.variant.archiveDependency )
set.add( this.variant.exeDependency )
return set
}
@OutputDirectory
public File getNodeDir()
{
configureIfNeeded()
return this.variant.nodeDir
}
private void configureIfNeeded()
{
if ( this.config != null )
{
return
}
this.config = NodeExtension.get( this.project )
this.variant = this.config.variant
}
@TaskAction
void exec()
{
configureIfNeeded()
addRepository()
if ( this.variant.exeDependency )
{
copyNodeExe()
}
deleteExistingNode()
unpackNodeArchive()
setExecutableFlag()
restoreRepositories()
}
private void copyNodeExe()
{
this.project.copy {
from getNodeExeFile()
into this.variant.nodeBinDir
rename 'node.+\\.exe', 'node.exe'
}
}
private void deleteExistingNode()
{
this.project.delete( getNodeDir().parent )
}
private void unpackNodeArchive()
{
if ( getNodeArchiveFile().getName().endsWith( 'zip' ) )
{
this.project.copy {
from this.project.zipTree( getNodeArchiveFile() )
into getNodeDir().parent
}
}
else if ( this.variant.exeDependency )
{
//Remap lib/node_modules to node_modules (the same directory as node.exe) because that's how the zip dist does it
this.project.copy {
from this.project.tarTree( getNodeArchiveFile() )
into this.variant.nodeBinDir
eachFile {
def m = it.path =~ /^.*?[\\/]lib[\\/](node_modules.*$)/
if ( m.matches() )
{
// remap the file to the root
it.path = m.group( 1 )
}
else
{
it.exclude()
}
}
includeEmptyDirs = false
}
}
else
{
this.project.copy {
from this.project.tarTree( getNodeArchiveFile() )
into getNodeDir().parent
}
// Fix broken symlink
Path npm = Paths.get( variant.nodeBinDir.path, 'npm' )
if ( Files.deleteIfExists( npm ) )
{
Files.createSymbolicLink(
npm,
variant.nodeBinDir.toPath().relativize(Paths.get(variant.npmScriptFile)))
}
}
}
private void setExecutableFlag()
{
if ( !this.variant.windows )
{
new File( this.variant.nodeExec ).setExecutable( true )
}
}
@Internal
protected File getNodeExeFile()
{
return resolveSingle( this.variant.exeDependency )
}
@Internal
protected File getNodeArchiveFile()
{
return resolveSingle( this.variant.archiveDependency )
}
private File resolveSingle( String name )
{
def dep = this.project.dependencies.create( name )
def conf = this.project.configurations.detachedConfiguration( dep )
conf.transitive = false
return conf.resolve().iterator().next();
}
private void addRepository()
{
this.allRepos = new ArrayList<>()
this.allRepos.addAll( this.project.repositories )
this.project.repositories.clear()
def distUrl = this.config.distBaseUrl
this.repo = this.project.repositories.ivy {
url distUrl
metadataSources {
artifact()
}
layout 'pattern', {
artifact 'v[revision]/[artifact](-v[revision]-[classifier]).[ext]'
}
}
}
private void restoreRepositories()
{
this.project.repositories.clear();
this.project.repositories.addAll( this.allRepos );
}
}