Skip to content

Commit

Permalink
Support multiple file extensions per language
Browse files Browse the repository at this point in the history
Fixes #71
  • Loading branch information
oehme committed Aug 29, 2021
1 parent 870fc16 commit d9ddb26
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.xtext.gradle.test

import org.gradle.testkit.runner.TaskOutcome
import org.junit.Test

//TODO use a different language than Xtend
Expand Down Expand Up @@ -125,4 +126,17 @@ class BuildingAPlainLanguageProject extends AbstractIntegrationTest {
// then
staleFile.shouldExist
}

/*
* We currently lack a language with multiple file extensions,
* so we test that an empty set will be detected as "no sources"
* to at least have some coverage for the fact that fileExtensions
* is indeed a Set.
*/
@Test
def void canOverrideFileExtensions() {
buildFile << '''xtext.languages.xtend.fileExtensions = []'''
file('src/main/java/HelloWorld.xtend').content = '''class HelloWorld {}'''
build('generateXtext').xtextTask.shouldBe(TaskOutcome.NO_SOURCE)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,22 @@ class GradleBuildTester extends ExternalResource {
}

def void shouldBeUpToDate(BuildTask task) {
if(task.outcome != TaskOutcome.UP_TO_DATE) {
fail('''Expected task '«task.path»' to be <UP-TO-DATE> but was: <«task.outcome»>''')
task.shouldBe(TaskOutcome.UP_TO_DATE)
}

def void shouldBe(BuildTask task, TaskOutcome outcome) {
if(task.outcome != outcome) {
fail('''Expected task '«task.path»' to be «outcome» but was: <«task.outcome»>''')
}
}

def void shouldNotBeUpToDate(BuildTask task) {
if(task.outcome == TaskOutcome.UP_TO_DATE) {
fail('''Expected task '«task.path»' not to be <UP-TO-DATE> but it was.''')
task.shouldNotBe(TaskOutcome.UP_TO_DATE)
}

def void shouldNotBe(BuildTask task, TaskOutcome outcome) {
if(task.outcome == outcome) {
fail('''Expected task '«task.path»' not to be «outcome» but it was.''')
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class XtendLanguageBasePlugin implements Plugin<Project> {
]
xtext = project.extensions.getByType(XtextExtension)
val xtend = xtext.languages.create("xtend") [
fileExtension = "xtend"
setup = "org.eclipse.xtend.core.XtendStandaloneSetup"
generator.outlet => [
producesJava = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ class XtextBuilderPlugin implements Plugin<Project> {
project.afterEvaluate [
xtext.languages.all [ lang |
xtext.sourceSets.all [
filter.include("**/*." + lang.fileExtension)
lang.fileExtensions.forEach[ ext |
filter.include("**/*." + ext)
]
]
]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.xtext.gradle.tasks.internal.DefaultXtextSourceDirectorySet
import org.xtext.gradle.tasks.internal.Version

import static extension org.xtext.gradle.GradleExtensions.*
import java.util.Set

class XtextExtension {
@Accessors String version
Expand Down Expand Up @@ -89,7 +90,7 @@ class XtextExtension {
class Language implements Named {
@Input val String name
@Input String qualifiedName
@Input String fileExtension
@Input Set<String> fileExtensions
@Input String setup
@Nested val GeneratorConfig generator
@Nested val DebuggerConfig debugger
Expand All @@ -101,14 +102,22 @@ class Language implements Named {
this.generator = project.instantiate(typeof(GeneratorConfig), project, this)
this.debugger = project.instantiate(typeof(DebuggerConfig))
this.validator = project.instantiate(typeof(ValidatorConfig))
fileExtensions = newLinkedHashSet(name)
}

def getQualifiedName() {
qualifiedName ?: setup.replace("StandaloneSetup", "")
}

@Internal
@Deprecated
def getFileExtension() {
fileExtension ?: name
fileExtensions.head
}

@Deprecated
def setFileExtension(String ext) {
fileExtensions = newLinkedHashSet(ext)
}

def generator(Action<GeneratorConfig> action) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.xtext.gradle.tasks;

import com.google.common.base.Charsets
import com.google.common.io.Files
import com.google.common.io.Resources
import java.io.File
import java.util.Collection
import java.util.Set
Expand All @@ -18,16 +20,14 @@ import org.gradle.api.tasks.SkipWhenEmpty
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.incremental.IncrementalTaskInputs
import org.gradle.api.tasks.util.PatternSet
import org.xtext.gradle.XtextBuilderPlugin
import org.xtext.gradle.protocol.GradleBuildRequest
import org.xtext.gradle.protocol.GradleGeneratorConfig
import org.xtext.gradle.protocol.GradleInstallDebugInfoRequest
import org.xtext.gradle.protocol.GradleInstallDebugInfoRequest.GradleSourceInstallerConfig
import org.xtext.gradle.protocol.GradleOutputConfig
import org.xtext.gradle.protocol.IncrementalXtextBuilder
import org.xtext.gradle.tasks.internal.IncrementalXtextBuilderProvider
import com.google.common.io.Resources
import org.xtext.gradle.XtextBuilderPlugin
import com.google.common.io.Files

class XtextGenerate extends DefaultTask {

Expand Down Expand Up @@ -66,7 +66,9 @@ class XtextGenerate extends DefaultTask {
def getMainSources() {
val patterns = new PatternSet
languages.filter[!generator.outlets.empty].forEach [lang |
patterns.include("**/*." + lang.fileExtension)
lang.fileExtensions.forEach[ ext |
patterns.include("**/*." + ext)
]
]
project.files(sources.srcDirs).asFileTree.matching(patterns)
}
Expand Down Expand Up @@ -163,11 +165,16 @@ class XtextGenerate extends DefaultTask {
val request = new GradleInstallDebugInfoRequest => [
generatedJavaFiles = generatedFiles.filter[name.endsWith(".java")].toSet
it.classesDir = classesDir
sourceInstallerByFileExtension = languages.toMap[fileExtension].mapValues[lang|
new GradleSourceInstallerConfig() => [
sourceInstaller = lang.debugger.sourceInstaller
hideSyntheticVariables = lang.debugger.hideSyntheticVariables
sourceInstallerByFileExtension = newLinkedHashMap

languages.forEach[lang|
lang.fileExtensions.forEach[ext |
sourceInstallerByFileExtension.put(ext, new GradleSourceInstallerConfig() => [
sourceInstaller = lang.debugger.sourceInstaller
hideSyntheticVariables = lang.debugger.hideSyntheticVariables
])
]

]
]
builder.installDebugInfo(request)
Expand Down

0 comments on commit d9ddb26

Please sign in to comment.