Skip to content

Commit

Permalink
added IDEA plugin, ported to Xtext 2.8
Browse files Browse the repository at this point in the history
  • Loading branch information
oehme committed Mar 13, 2015
1 parent db19db8 commit c106708
Show file tree
Hide file tree
Showing 25 changed files with 509 additions and 61 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Configure your languages

```groovy
xtext {
version = '2.7.2' // the current default, can be omitted
version = '2.8.0' // the current default, can be omitted
encoding = 'UTF-8' //the default, can be omitted
/* Java sourceDirs are added automatically,
Expand Down
81 changes: 44 additions & 37 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,47 +1,54 @@
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.xtend:xtend-gradle-plugin:0.4.3'
classpath 'com.github.oehme.sobula:sobula:0.4.1'
}
repositories {
jcenter()
}
dependencies {
classpath 'org.xtend:xtend-gradle-plugin:0.4.5'
classpath 'com.github.oehme.sobula:sobula:0.4.2'
}
}

/*
* TODO this weird order is a workaround for a bug in the nebula publishing plugin
* If they don't fix it soon, sobula should be rewritten to work without it.
*/
configure(subprojects.findAll{p-> p.name.contains('plugin')}){
apply plugin: 'org.xtend.xtend'
apply plugin: 'com.github.oehme.sobula.plugin-release'
apply plugin: 'java-gradle-plugin'

dependencies {
compile 'org.eclipse.xtend:org.eclipse.xtend.lib:2.8.0'
}
}

subprojects {
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'com.github.oehme.sobula.bintray-release'
apply plugin: 'com.github.oehme.sobula.stefan'
apply plugin: 'eclipse'
apply plugin: 'java'

sourceCompatibility = 1.6
}

group = "org.xtext"
bintray.pkg.name = "$project.group:$project.name"

contacts {
"[email protected]" {
moniker "The Xtext team"
roles "owner"
github "xtext"
}
}
allprojects {
apply plugin: 'com.github.oehme.sobula.bintray-release'

group = "org.xtext"
bintray.pkg.name = "$rootProject.group:$rootProject.name"

contacts {
"[email protected]" {
moniker "The Xtext team"
roles "owner"
github "xtext"
}
}

//TODO create bintray and central users
bintray.user = "oehme"
bintray.pkg.version.mavenCentralSync.user = "oehme"

repositories {
//TODO create bintray and central users
bintray.user = "oehme"
bintray.pkg.version.mavenCentralSync.user = "oehme"
repositories {
jcenter()
}

sourceCompatibility = 1.5
}

configure(subprojects.findAll{p-> p.name.contains('plugin')}){
apply plugin: 'com.github.oehme.sobula.plugin-release'
apply plugin: 'java-gradle-plugin'
apply plugin: 'org.xtend.xtend'

dependencies {
compile 'org.eclipse.xtend:org.eclipse.xtend.lib:2.7.3'
}
}
bintray.publications = [] // don't publish a POM for the root project
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-2.1-bin.zip
distributionUrl=http\://services.gradle.org/distributions/gradle-2.2.1-bin.zip
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include 'xtext-gradle-lib', 'xtext-gradle-plugin'
include 'xtext-gradle-lib', 'xtext-gradle-plugin', 'xtext-idea-gradle-plugin'
2 changes: 1 addition & 1 deletion xtext-gradle-lib/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
description 'A small wrapper for Xtext to make it easier to use from Gradle'

dependencies {
compile ("org.eclipse.xtext:org.eclipse.xtext.builder.standalone:[2.5.4,)") {
compile ("org.eclipse.xtext:org.eclipse.xtext.builder.standalone:[2.8.0,)") {
exclude group:'asm', module:'asm'
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.xtext.builder.standalone;

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
Expand All @@ -12,7 +11,6 @@
import org.eclipse.xtext.builder.standalone.LanguageAccessFactory;
import org.eclipse.xtext.builder.standalone.StandaloneBuilder;
import org.eclipse.xtext.builder.standalone.StandaloneBuilderModule;
import org.eclipse.xtext.parser.IEncodingProvider;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
Expand All @@ -28,7 +26,6 @@ public static boolean generate(String[] args) throws Exception {
builder.setEncoding("UTF-8");

List<String> sourcePath = Lists.newArrayList();
File workingDirectory = new File("");
LanguageConfigurationParser languageParser = new LanguageConfigurationParser();

Iterator<String> arguments = Arrays.asList(args).iterator();
Expand All @@ -41,31 +38,18 @@ public static boolean generate(String[] args) throws Exception {
} else if ("-encoding".equals(argument.trim())) {
builder.setEncoding(arguments.next().trim());
} else if ("-cwd".equals(argument.trim())) {
workingDirectory = new File(arguments.next().trim());
builder.setBaseDir(arguments.next().trim());
} else if (argument.trim().startsWith("-L")) {
languageParser.addArgument(argument);
} else {
sourcePath.add(argument);
}
}
Map<String, LanguageAccess> languages = new LanguageAccessFactory().createLanguageAccess(
languageParser.getLanguages(), Main.class.getClassLoader(), workingDirectory);
fixEncoding(languages, builder);
languageParser.getLanguages(), Main.class.getClassLoader());
builder.setLanguages(languages);
builder.setSourceDirs(sourcePath);
try {
Method javaSourceDirsSetter = StandaloneBuilder.class.getMethod("setJavaSourceDirs", Iterable.class);
javaSourceDirsSetter.invoke(builder, sourcePath);
} catch (Exception ignored) {}
builder.setJavaSourceDirs(sourcePath);
return builder.launch();
}

private static void fixEncoding(Map<String, LanguageAccess> languages, StandaloneBuilder builder) {
for (LanguageAccess language : languages.values()) {
IEncodingProvider encodingProvider = language.getEncodingProvider();
if (encodingProvider instanceof IEncodingProvider.Runtime) {
((IEncodingProvider.Runtime) encodingProvider).setDefaultEncoding(builder.getEncoding());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.gradle.api.tasks.SourceSet
import org.gradle.util.ConfigureUtil

class XtextExtension {
@Accessors String version = "2.7.2"
@Accessors String version = "2.8.0"
@Accessors String encoding = "UTF-8"
@Accessors SourceDirectorySet sources
@Accessors NamedDomainObjectContainer<Language> languages;
Expand Down
3 changes: 3 additions & 0 deletions xtext-idea-gradle-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.settings/
.project
.classpath
5 changes: 5 additions & 0 deletions xtext-idea-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
description 'A plugin for for building IntelliJ IDEA Xtext plugins'

dependencies {
compile 'org.apache.httpcomponents:httpclient:4.4'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.xtext.gradle.idea

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.plugins.ide.eclipse.model.Classpath
import org.gradle.plugins.ide.eclipse.model.EclipseModel
import org.gradle.plugins.ide.eclipse.model.Library
import org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory
import org.xtext.gradle.idea.tasks.DownloadIdea

import static extension org.xtext.gradle.idea.tasks.GradleExtensions.*
import org.gradle.plugins.ide.eclipse.EclipsePlugin

class IdeaComponentPlugin implements Plugin<Project> {

override apply(Project project) {
project.plugins.<IdeaDevelopmentPlugin>apply(IdeaDevelopmentPlugin)
project.plugins.<JavaPlugin>apply(JavaPlugin)

val compile = project.configurations.getAt("compile")
compile.exclude(#{"module" -> "guava"})
compile.exclude(#{"module" -> "log4j"})

val ideaProvided = project.configurations.create("ideaProvided")

project.afterEvaluate [
val downloadTask = project.tasks.getByName("downloadIdea") as DownloadIdea
val ideaHomeDir = downloadTask.ideaHomeDir

val ideaLibs = project.fileTree(ideaHomeDir + "/lib").builtBy(downloadTask).include("*.jar")
project.dependencies.add(ideaProvided.name, ideaLibs)
project.convention.getPlugin(JavaPluginConvention).sourceSets.forEach [
compileClasspath = compileClasspath.plus(ideaProvided)
]

project.plugins.withType(EclipsePlugin) [
project.tasks.getByName("eclipseClasspath").dependsOn(downloadTask)
project.extensions.getByType(EclipseModel).classpath => [
plusConfigurations.add(ideaProvided)

val fileReferenceFactory = new FileReferenceFactory
file.whenMerged.add [ Classpath it |
val sourceZip = ideaHomeDir / "sources.zip"
entries.filter(Library).filter[library.file.parentFile == ideaHomeDir / 'lib'].forEach [
sourcePath = fileReferenceFactory.fromFile(sourceZip)
]
]
]
]
]
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.xtext.gradle.idea

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.xtext.gradle.idea.tasks.DownloadIdea
import org.xtext.gradle.idea.tasks.IdeaExtension

import static extension org.xtext.gradle.idea.tasks.GradleExtensions.*

class IdeaDevelopmentPlugin implements Plugin<Project> {

override apply(Project project) {
val idea = project.extensions.create("ideaDevelopment", IdeaExtension) => [
ideaHome = project.rootDir / "ideaHome"
ideaVersion = "140.2683.2"
sandboxDir = project.rootProject.buildDir / "ideaSandbox"
]
val downloadTask = project.tasks.create("downloadIdea", DownloadIdea)
project.afterEvaluate [
downloadTask.ideaHome = idea.ideaHome
downloadTask.ideaVersion = idea.ideaVersion
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.xtext.gradle.idea

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPluginConvention
import org.xtext.gradle.idea.tasks.AssembleSandbox
import org.xtext.gradle.idea.tasks.IdeaExtension
import org.xtext.gradle.idea.tasks.IdeaZip

import static extension org.xtext.gradle.idea.tasks.GradleExtensions.*
import org.gradle.api.Action
import org.xtext.gradle.idea.tasks.IdeaPluginSpec

class IdeaPluginPlugin implements Plugin<Project> {
override apply(Project project) {
project.plugins.<IdeaComponentPlugin>apply(IdeaComponentPlugin)
val idea = project.extensions.getByType(IdeaExtension)
val mainSourceSet = project.convention.getPlugin(JavaPluginConvention).sourceSets.getByName("main")
val providedDependencies = project.configurations.getAt("ideaProvided")
val runtimeDependencies = project.configurations.getAt("runtime")

val Action<? super IdeaPluginSpec> archiveConfig = [
classes.from(mainSourceSet.output)
libraries.from(runtimeDependencies.minus(providedDependencies))
metaInf.from("META-INF")
]

project.tasks.create("ideaZip", IdeaZip, archiveConfig)

val assembleSandboxTask = project.tasks.create("assembleSandbox", AssembleSandbox, archiveConfig)

project.afterEvaluate [
assembleSandboxTask.destinationDir = project.file(idea.sandboxDir) / project.name
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.xtext.gradle.idea

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.xtext.gradle.idea.tasks.AssembleSandbox
import org.xtext.gradle.idea.tasks.DownloadIdea
import org.xtext.gradle.idea.tasks.IdeaExtension
import org.xtext.gradle.idea.tasks.IdeaRepository
import org.xtext.gradle.idea.tasks.IdeaZip
import org.xtext.gradle.idea.tasks.RunIdea

import static extension org.xtext.gradle.idea.tasks.GradleExtensions.*

class IdeaRepositoryPlugin implements Plugin<Project> {

override apply(Project project) {
project.plugins.<IdeaDevelopmentPlugin>apply(IdeaDevelopmentPlugin)
val idea = project.extensions.getByType(IdeaExtension)
val repositoryTask = project.tasks.create("ideaRepository", IdeaRepository) [
into(project.buildDir / 'ideaRepository')
]
project.allprojects [
tasks.withType(IdeaZip) [ zip |
repositoryTask.from(zip)
]
]
val runIdea = project.tasks.create("runIdea", RunIdea)
val downloadTask = project.tasks.getAt("downloadIdea") as DownloadIdea
project.afterEvaluate [
val ideaLibs = project.fileTree(downloadTask.ideaHomeDir + "/lib")
ideaLibs.builtBy(downloadTask).include("*.jar")
subprojects.map[tasks.withType(AssembleSandbox)].flatten.forEach[runIdea.dependsOn(it)]
runIdea.sandboxDir = project.file(idea.sandboxDir)
runIdea.ideaHome = project.file(idea.ideaHome)
val tools = project.files('''«System.getenv("JAVA_HOME")»/lib/tools.jar''')
runIdea.classpath = ideaLibs.plus(tools)
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.xtext.gradle.idea.tasks

import org.eclipse.xtend.lib.annotations.Accessors
import org.gradle.api.file.CopySpec
import org.gradle.api.tasks.Sync

@Accessors
class AssembleSandbox extends Sync implements IdeaPluginSpec {
CopySpec classes
CopySpec libraries
CopySpec metaInf

new() {
classes = rootSpec.addChild.into("classes")
libraries = rootSpec.addChild.into("lib")
metaInf = rootSpec.addChild.into("META-INF")
}
}
Loading

0 comments on commit c106708

Please sign in to comment.