-
Notifications
You must be signed in to change notification settings - Fork 3
/
idea.js
194 lines (166 loc) · 5.87 KB
/
idea.js
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
'use strict'
const paths = require('path')
const ops = require('./ops')
const libs = require('./libs')
const mods = require('./mods')
const buck = require('./buck')
const excludes = []
function libraryXml(lib) {
let classesRoots = lib.jars.map(j => `
<root url="jar://$PROJECT_DIR$/${lib.symlinkJar(j)}!/" />`)
let sourcesRoots = lib.srcs.map(j => `
<root url="jar://$PROJECT_DIR$/${lib.symlinkSrc(j)}!/" />`)
if (lib.options.includeGeneratedSrcs) {
// This is very ad-hoc, convenience stuff so we can tell intellij idea
// to show not only original sources, but also derived sources,
// which otherwise are not available, only decompilable classes in IDE
let genSrcsRules = [].concat(lib.options.includeGeneratedSrcs)
.map(buck.target)
.map(t => lib.target.resolve(t))
.flatMap(buck.info)
.map(info => info[buck.attr.generatedSourcePath])
.filter(Boolean)
.map(path => `
<root url="file://$PROJECT_DIR$/${path}/" />`)
sourcesRoots.push(...genSrcsRules)
}
return `<?xml version="1.0" encoding="UTF-8"?>
<component name="libraryTable">
<library name="${lib.name}">
<CLASSES>${classesRoots.join('')}
</CLASSES>
<JAVADOC />
<SOURCES>${sourcesRoots.join('')}
</SOURCES>
</library>
</component>
`
}
function moduleMainXml(excludes) {
let excludesFolders = excludes.map(dir => `
<excludeFolder url="file://$MODULE_DIR$/../../${dir}" isTestSource="false" />`)
return `<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$/../..">${excludesFolders}
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="sourceFolder" forTests="true" />
</component>
</module>
`
}
function moduleXml(mod) {
let dotFolders = ops.ls(mod.path)
.filter(l => l.isDirectory() && l.name.startsWith('.'))
.map(l => l.name)
let excludesFolders = excludes.concat(dotFolders)
.filter(dir => ops.exists(paths.join(mod.path, dir)))
.map(dir => `
<excludeFolder url="file://$MODULE_DIR$/../../${mod.path}/${dir}" />`)
let folders = Object.entries(mod.srcs).map(([p, f]) => `
<sourceFolder url="file://$MODULE_DIR$/../../${mod.path}/${p}" ${folderAttributes(f)}/>`)
let depmods = Object.values(mod.depmods).map(d => `
<orderEntry type="module" module-name="${d.mod.name}"
scope="${scope(d)}" ${d.exported ? ' exported=""':''} />`)
let deplibs = Object.values(mod.deplibs).map(d => `
<orderEntry type="library" name="${d.lib.name}"
scope="${scope(d)}" level="project"${d.exported ? ' exported=""':''}/>`)
let deps = [...deplibs, ...depmods]
return `<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$/../../${mod.path}">${folders.join('')}${excludesFolders.join('')}
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />${deps.join('')}
</component>
</module>
`
function scope(dep) {
if (dep.test) return 'TEST'
if (dep.provided) return 'PROVIDED'
return 'COMPILE'
}
function folderAttributes(f) {
if (f.res) return f.res.test ? `type="java-test-resource"` : `type="java-resource"`
return `isTestSource="${f.test}" generated="${f.gen}"`
+ (f.package ? ` packagePrefix="${f.package}"` : '')
}
}
function modulesAllXml(mods) {
let element = n => `
<module fileurl="file://$PROJECT_DIR$/.idea/modules/${n}.iml" filepath="$PROJECT_DIR$/.idea/modules/${n}.iml" />`
let modules = [element(mods.rootname), ...mods.all.map(m => element(m.name))]
return `<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>${modules}
</modules>
</component>
</project>
`
}
function miscXml() {
let level = buck.javaLevel()
let lang = `JDK_${level.source.replace('.','_')}`
return `<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" default="false" languageLevel="${lang}" project-jdk-name="${level.source}" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/.idea/.out" />
</component>
</project>
`
}
function kotlincXml() {
let level = buck.javaLevel()
return `<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Kotlin2JvmCompilerArguments">
<option name="jvmTarget" value="${level.target}" />
</component>
</project>
`
}
module.exports = {
excludes,
genProject() {
this.genLibs()
this.genModules()
},
genLibs() {
for (let l of libs.all) {
ops.write(`.idea/libraries/${l.flatname}.xml`, libraryXml(l))
}
},
genModules() {
for (let m of mods.all) {
ops.write(`.idea/modules/${m.name}.iml`, moduleXml(m))
}
ops.write(`.idea/modules/${mods.rootname}.iml`, moduleMainXml(this.excluded()))
ops.write('.idea/modules.xml', modulesAllXml(mods))
// It's questionable if we need to generate misc.xml
// Helps to get initial project setup with some SDKs (Java 8 SDK currently)
// But may conflict by overriding other settings (like JavaScript language level)
if (!ops.exists('.idea/misc.xml')) {
ops.write('.idea/misc.xml', miscXml())
}
if (!ops.exists('.idea/kotlinc.xml')) {
ops.write('.idea/kotlinc.xml', kotlincXml())
}
},
// all folders at project root which do not contain modules
excluded() {
let excludes = ops.ls()
.filter(l => l.isDirectory())
.reduce((acc, l) => (acc[l.name] = true, acc), {})
for (let m of mods.all) {
let dir = m.path.split('/')[0]
delete excludes[dir]
}
return Object.keys(excludes)
}
}