aboutsummaryrefslogtreecommitdiff
path: root/plugins/idea_plugin/IdeaPlugin.scala
blob: 97e53b1f014cc430ced05556b57f5ca2176867e5 (plain) (blame)
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
package idea_plugin

import java.io.{File, FileWriter}

import cbt.{BaseBuild, ExitCode}

trait IdeaPlugin extends BaseBuild {

  import IdeaPlugin._

  // @TODO add only the top level dependencies
  // @TODO return exit code dynamically
  def generateIdeaProject: ExitCode = {

    val dependencyEntry: (String) => String = (url: String) => <orderEntry type="module-library">
      <library>
        <CLASSES>
          <root url={url}/>
        </CLASSES>
        <JAVADOC/>
        <SOURCES/>
      </library>
    </orderEntry>.toString()

    val moduleDir = projectDirectory.getPath
    val projectDependencies: List[String] = for {
      depJarFile <- this.dependencies
        .flatMap(dep => dep.dependenciesArray().toList)
        .flatMap(x => x.exportedClasspathArray().toList)
        .toList
      depIdeaOrderEntry = dependencyEntry(
        s"jar://$moduleDir/../cbt/${depJarFile.getPath.split("/cbt")(1)}!/")
    } yield depIdeaOrderEntry

    val imlFile = new File(projectDirectory.getPath + s"/$name.iml")
    if (!imlFile.exists()) {
      imlFile.createNewFile()
    }
    val fw = new FileWriter(imlFile.getPath, false)
    fw.write(templateWithCBTSources(projectDependencies.mkString("\n"), moduleDir))
    fw.close()
    ExitCode.Success
  }

}

private[idea_plugin] object IdeaPlugin {

  // @TODO inject from the build.scala
  // @TODO infer scala version from project
  // path of cbt relative to module dir

  private val templateWithCBTSources: (String, String) => String = (dependencies: String,
                                                                    moduleRootDir: String) =>
    """<?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$/../cbt/compatibility">
      |      <sourceFolder url="file://$MODULE_DIR$/../cbt/compatibility" isTestSource="false" />
      |      <excludeFolder url="file://$MODULE_DIR$/../cbt/compatibility/target/scala-2.11" />
      |    </content>
      |    <content url="file://$MODULE_DIR$/../cbt/nailgun_launcher">
      |      <sourceFolder url="file://$MODULE_DIR$/../cbt/nailgun_launcher" isTestSource="false" />
      |      <excludeFolder url="file://$MODULE_DIR$/../cbt/nailgun_launcher/target" />
      |    </content>
      |    <content url="file://$MODULE_DIR$/../cbt/stage1">
      |      <sourceFolder url="file://$MODULE_DIR$/../cbt/stage1" isTestSource="false" />
      |      <excludeFolder url="file://$MODULE_DIR$/../cbt/stage1/target/scala-2.11" />
      |    </content>
      |    <content url="file://$MODULE_DIR$/../cbt/stage2">
      |      <sourceFolder url="file://$MODULE_DIR$/../cbt/stage2" isTestSource="false" />
      |      <excludeFolder url="file://$MODULE_DIR$/../cbt/stage2/target/scala-2.11" />
      |    </content>
      |    <content url="file://$MODULE_DIR$">
      |      <sourceFolder url="file://$MODULE_DIR$/src/main/scala/main" isTestSource="false" />
      |      <sourceFolder url="file://$MODULE_DIR$/src/test/scala" isTestSource="false" />
      |    </content>
      |    <orderEntry type="inheritedJdk" />
      |    <orderEntry type="sourceFolder" forTests="false" />
      |    <orderEntry type="library" name="scala-sdk-2.11.7" level="application" /> |
      |    """.stripMargin + dependencies +
      """  </component>
        |</module>""".stripMargin
}