Skip to content

Commit e1734c0

Browse files
authored
Applied common Spotless Eclipse framework to CDT (#238)
Applied common Spotless Eclipse framework to CDT
1 parent f70e1c7 commit e1734c0

File tree

9 files changed

+75
-232
lines changed

9 files changed

+75
-232
lines changed

_ext/eclipse-cdt/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# spotless-cdt-eclipse
1+
# spotless-eclipse-cdt
22

3-
Eclipse CDT is not available in a form which can be easily consumed by maven or gradle. To fix this, we publish Eclipse's formatter and all its dependencies, along with a small amount of glue code, into the `com.diffplug.gradle.spotless:spotless-ext-eclipse-cdt` artifact.
3+
Eclipse CDT is not available in a form which can be easily consumed by maven or gradle. To fix this, we publish Eclipse's formatter and all its dependencies, along with a small amount of glue code, into the `com.diffplug.gradle.spotless:spotless-eclipse-cdt` artifact.
44

55
To publish a new version, update the `_ext/eclipse-cdt/gradle.properties` appropriately and run this from the root directory:
66

_ext/eclipse-cdt/build.gradle

+16-200
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,28 @@
1-
plugins {
2-
// bintray uploading
3-
id 'com.jfrog.bintray' version '1.3.1'
4-
// p2 dependencies
5-
id 'com.diffplug.gradle.p2.asmaven' version '3.9.0'
6-
}
7-
8-
repositories {
9-
mavenCentral()
10-
maven { url 'https://plugins.gradle.org/m2/' }
11-
}
12-
13-
apply plugin: 'java'
14-
sourceCompatibility = VER_JAVA
15-
targetCompatibility = VER_JAVA
16-
17-
import java.io.File
18-
19-
// The dependencies to pull from CDT's p2 repositories
20-
def eclipseCdtDeps = [
21-
'org.eclipse.cdt.core':'+', // CodeFormatter and related
22-
]
23-
24-
// The dependencies to pull from Eclipse's p2 repositories
25-
def eclipseDeps = [
26-
'org.eclipse.core.jobs':'+', // Required by CDT ParserUtil
27-
'org.eclipse.core.resources':'+',
28-
'org.eclipse.core.runtime':'+', // Provides central logging and plugin interface
29-
'org.eclipse.equinox.common':'+', // Provides runtime status used during exception reporting
30-
'org.eclipse.jface':'+', // PreferenceStore for creating preferences from properties
31-
'org.eclipse.text':'+', // Provides Document data structure for formatter
32-
'org.eclipse.osgi':'+', // CCorePlugin requires OSGI bundle interfaces (but not effectively used)
33-
'org.eclipse.osgi.services':'+',
34-
]
1+
ext {
2+
developers = [
3+
fvgh: [ name: 'Frank Vennemeyer', email: 'frankgh@zoho.com' ],
4+
]
355

6+
p2Repository = "http://download.eclipse.org/tools/cdt/releases/${VER_ECLIPSE_CDT}"
7+
8+
p2Dependencies = [
9+
'org.eclipse.cdt.core':'+', // CodeFormatter and related
10+
]
3611

37-
// build a maven repo in our build folder containing these artifacts
38-
p2AsMaven {
39-
group 'p2', {
40-
repoEclipse cdt_VER_ECLIPSE
41-
eclipseDeps.keySet.each { p2.addIU(it) }
42-
eclipseDeps.keySet.each { p2.addIU(it + '.source') }
43-
repo "http://download.eclipse.org/tools/cdt/releases/${cdt_VER_ECLIPSE_CDT}"
44-
eclipseCdtDeps.keySet.each { p2.addIU(it) }
45-
eclipseCdtDeps.keySet.each { p2.addIU(it + '.source') }
46-
}
4712
}
4813

49-
configurations
50-
{
51-
embeddedJars // JARs (Eclipse and WTP) the fat-jar is based uppon
52-
embeddedSource // Source for Eclipse JARS (GrEclipse provides no source packages)
53-
compile.extendsFrom(embeddedJars)
54-
}
14+
apply from: rootProject.file('../gradle/p2-fat-jar-setup.gradle')
15+
apply from: rootProject.file('../gradle/java-publish.gradle')
16+
5517

5618
dependencies {
57-
// Add the Eclipse and Eclipse-WTP jars to the embedded configuration.
58-
eclipseDeps.each { groupArtifact, version ->
59-
embeddedJars "p2:${groupArtifact}:${version}"
60-
embeddedSource "p2:${groupArtifact}:${version}:sources"
61-
}
62-
eclipseCdtDeps.each { groupArtifact, version ->
63-
embeddedJars "p2:${groupArtifact}:${version}"
64-
embeddedSource "p2:${groupArtifact}:${version}:sources"
19+
compile "com.diffplug.spotless:spotless-eclipse-base:${VER_SPOTLESS_ECLISPE_BASE}"
20+
// Provides text partitioners for formatters
21+
compile ("org.eclipse.platform:org.eclipse.jface.text:${VER_ECLISPE_JFACE}") {
22+
exclude group: 'org.eclipse.platform', module: 'org.eclipse.swt'
6523
}
66-
67-
testCompile "junit:junit:${cdt_VER_JUNIT}"
6824
}
6925

70-
jar {
71-
// this embeds the eclipse jars into our "fat jar"
72-
from {
73-
configurations.embeddedJars.collect{ it.isDirectory() ? it : zipTree(it) }
74-
}
75-
// the eclipse jars are signed, and our fat jar breaks the signatures
76-
// so we've got to be sure to filter out the signatures
77-
exclude 'META-INF/*.RSA'
78-
exclude 'META-INF/*.SF'
79-
}
8026

8127
//////////
8228
// Test //
@@ -85,133 +31,3 @@ sourceSets {
8531
// Use JAR file with all resources for Eclipse-XML integration-tests
8632
test.runtimeClasspath = jar.outputs.files + sourceSets.test.output + sourceSets.test.compileClasspath
8733
}
88-
89-
/////////
90-
// IDE //
91-
/////////
92-
93-
apply plugin: 'eclipse'
94-
95-
eclipse {
96-
classpath {
97-
downloadSources true
98-
downloadJavadoc true
99-
}
100-
}
101-
102-
// always create fresh projects
103-
tasks.eclipse.dependsOn(cleanEclipse)
104-
105-
////////////////
106-
// Publishing //
107-
////////////////
108-
apply plugin: 'maven-publish'
109-
110-
task sourcesJar(type: Jar) {
111-
classifier = 'sources'
112-
from sourceSets.main.allJava
113-
}
114-
115-
task javadocJar(type: Jar, dependsOn: javadoc) {
116-
classifier = 'javadoc'
117-
from javadoc.destinationDir
118-
}
119-
120-
def isSnapshot = cdt_version.endsWith('-SNAPSHOT')
121-
// pulls the credentials from either the environment variable or gradle.properties
122-
def cred = {
123-
if (System.env[it] != null) {
124-
return System.env[it]
125-
} else if (project.hasProperty(it)) {
126-
return project[it]
127-
} else {
128-
return 'unknown_' + it
129-
}
130-
}
131-
132-
model {
133-
publishing {
134-
publications {
135-
mavenJava(MavenPublication) {
136-
137-
groupId project.cdt_group
138-
artifactId project.cdt_artifactId
139-
version project.cdt_version
140-
from components.java
141-
142-
pom.withXml {
143-
144-
// add MavenCentral requirements to the POM
145-
asNode().children().last() + {
146-
resolveStrategy = Closure.DELEGATE_FIRST
147-
name project.cdt_artifactId
148-
description project.cdt_description
149-
url "https://github.com/${project.cdt_org}/${project.name}"
150-
scm {
151-
url "https://github.com/${project.cdt_org}/${project.name}"
152-
connection "scm:git:git://github.com/${project.cdt_org}/${project.name}"
153-
developerConnection "scm:git:ssh:git@github.com/${project.cdt_org}/${project.name}"
154-
}
155-
licenses {
156-
license {
157-
name 'Eclipse Public License - v 1.0'
158-
url 'https://www.eclipse.org/legal/epl-v10.html'
159-
distribution 'repo'
160-
}
161-
}
162-
developers {
163-
developer {
164-
id 'fvgh'
165-
name 'Frank Vennemeyer'
166-
email 'frankgh@zoho.com'
167-
}
168-
}
169-
}
170-
}
171-
}
172-
}
173-
if (isSnapshot) {
174-
// upload snapshots to oss.sonatype.org
175-
repositories {
176-
maven {
177-
url = 'https://oss.sonatype.org/content/repositories/snapshots'
178-
credentials {
179-
username = cred('nexus_user')
180-
password = cred('nexus_pass')
181-
}
182-
} }
183-
}
184-
}
185-
}
186-
187-
if (!isSnapshot) {
188-
// upload releases to bintray and then mavenCentral
189-
bintray {
190-
user = cred('bintray_user')
191-
key = cred('bintray_pass')
192-
publications = [
193-
'mavenJava'
194-
]
195-
publish = true
196-
pkg {
197-
repo = 'opensource'
198-
name = project.cdt_artifactId
199-
userOrg = project.cdt_org
200-
version {
201-
name = project.cdt_version
202-
mavenCentralSync {
203-
user = cred('nexus_user')
204-
password = cred('nexus_pass')
205-
}
206-
}
207-
}
208-
}
209-
210-
publish.dependsOn(bintrayUpload)
211-
bintrayUpload.dependsOn([
212-
'generatePomFileForMavenJavaPublication',
213-
jar,
214-
sourcesJar,
215-
javadocJar
216-
])
217-
}

_ext/eclipse-cdt/gradle.properties

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# Mayor/Minor versions are in line with the one of the Eclipse C/C++ IDE version.
2-
# Patch version is an incremental counter for the Spotless plugin.
3-
cdt_version=9.4.0-SNAPSHOT
4-
cdt_artifactId=spotless-ext-eclipse-cdt
5-
cdt_description=Eclipse's CDT C/C++ formatter bundled for Spotless
6-
VER_JAVA=1.8
1+
# Versions correspond to the Eclipse-CDT version used for the fat-JAR.
2+
# See https://www.eclipse.org/cdt/ for further information about Eclipse-CDT versions.
3+
# Patch version can be incremented independently for backward compatible patches of this library.
4+
ext_version=9.4.3
5+
ext_artifactId=spotless-eclipse-cdt
6+
ext_description=Eclipse's CDT C/C++ formatter bundled for Spotless
7+
ext_org=diffplug
8+
ext_group=com.diffplug.spotless
79

8-
cdt_org=diffplug
9-
cdt_group=com.diffplug.spotless
10+
# Build requirements
11+
ext_VER_JAVA=1.8
1012

1113
# Compile dependencies
12-
cdt_VER_ECLIPSE=4.7.2
13-
cdt_VER_ECLIPSE_CDT=9.4
14-
15-
# Test
16-
cdt_VER_JUNIT=4.12
14+
VER_ECLIPSE_CDT=9.4
15+
VER_SPOTLESS_ECLISPE_BASE=3.+
16+
VER_ECLISPE_JFACE=[3.12.0,4.0.0[

_ext/eclipse-cdt/src/main/java/com/diffplug/gradle/spotless/cdt/eclipse/EclipseCdtFormatterStepImpl.java _ext/eclipse-cdt/src/main/java/com/diffplug/spotless/extra/eclipse/cdt/EclipseCdtFormatterStepImpl.java

+14-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.diffplug.gradle.spotless.cdt.eclipse;
16+
package com.diffplug.spotless.extra.eclipse.cdt;
1717

1818
import java.util.Map;
1919
import java.util.Map.Entry;
@@ -26,24 +26,31 @@
2626
import org.eclipse.jface.text.IDocument;
2727
import org.eclipse.text.edits.TextEdit;
2828

29-
/** Formatter step which calls out to the Eclipse formatter. */
30-
public class EclipseCdtFormatterStepImpl {
31-
/** Spotless always uses \n internally as line delimiter */
32-
public static final String LINE_DELIMITER = "\n";
29+
import com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework;
3330

31+
/** Formatter step which calls out to the Eclipse CDT formatter. */
32+
public class EclipseCdtFormatterStepImpl {
3433
private final CodeFormatter codeFormatter;
3534

36-
public EclipseCdtFormatterStepImpl(Properties settings) {
35+
public EclipseCdtFormatterStepImpl(Properties settings) throws Exception {
36+
SpotlessEclipseFramework.setup(
37+
bundles -> {}, //CDT does not use the internal Eclipse feature
38+
config -> {
39+
config.changeSystemLineSeparator();
40+
},
41+
plugins -> {} //CDT does not use other Eclipse plugins
42+
);
3743
Stream<Entry<Object, Object>> stream = settings.entrySet().stream();
3844
Map<String, String> settingsMap = stream.collect(Collectors.toMap(
3945
e -> String.valueOf(e.getKey()),
4046
e -> String.valueOf(e.getValue())));
4147
codeFormatter = org.eclipse.cdt.core.ToolFactory.createDefaultCodeFormatter(settingsMap);
4248
}
4349

50+
/** Formatting C/C++ string */
4451
public String format(String raw) throws Exception {
4552
//The 'kind' can be set to CodeFormatter.K_UNKNOWN, since it is anyway ignored by the internal formatter
46-
TextEdit edit = codeFormatter.format(CodeFormatter.K_UNKNOWN, raw, 0, raw.length(), 0, LINE_DELIMITER);
53+
TextEdit edit = codeFormatter.format(CodeFormatter.K_UNKNOWN, raw, 0, raw.length(), 0, SpotlessEclipseFramework.LINE_DELIMITER);
4754
if (edit == null) {
4855
throw new IllegalArgumentException("Invalid C/C++ syntax for formatting.");
4956
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
/** Eclipse CDT based Spotless formatter */
17+
@ParametersAreNonnullByDefault
18+
package com.diffplug.spotless.extra.eclipse.cdt;
19+
20+
import javax.annotation.ParametersAreNonnullByDefault;

_ext/eclipse-cdt/src/test/java/com/diffplug/gradle/spotless/cdt/eclipse/EclipseCdtFormatterStepImplTest.java _ext/eclipse-cdt/src/test/java/com/diffplug/spotless/extra/eclipse/cdt/EclipseCdtFormatterStepImplTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.diffplug.gradle.spotless.cdt.eclipse;
16+
package com.diffplug.spotless.extra.eclipse.cdt;
1717

18-
import static com.diffplug.gradle.spotless.cdt.eclipse.EclipseCdtFormatterStepImpl.LINE_DELIMITER;
18+
import static com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework.LINE_DELIMITER;
1919
import static org.junit.Assert.*;
2020

2121
import java.util.Properties;
@@ -41,7 +41,7 @@ public class EclipseCdtFormatterStepImplTest {
4141
"\treturn 0;\n" +
4242
"}\n".replaceAll("\n", LINE_DELIMITER);
4343

44-
private final static String DOXYGEN_HTML = "/** <ul> <li>Hello</li> </ul> */" + LINE_DELIMITER;
44+
private final static String DOXYGEN_HTML = "/**\n *<pre>void f() {int a =1;} </pre>\n */\n".replaceAll("\n", LINE_DELIMITER);
4545

4646
private final static String ILLEGAL_CHAR = Character.toString((char) 254);
4747

_ext/eclipse-groovy/gradle.properties

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Versions correspond to the Eclipse-Groovy version used for th FAT JAR
2-
# See https://github.com/groovy/groovy-eclipse/releases for further information about Eclipse-Groovy versions.
3-
# Patch version can be is incremented independently for backward compatible patches of this library.
1+
# Versions correspond to the Groovy-Eclipse version used for the fat-JAR.
2+
# See https://github.com/groovy/groovy-eclipse/releases for further information about Groovy-Eclipse versions.
3+
# Patch version can be incremented independently for backward compatible patches of this library.
44
ext_version=2.9.2
55
ext_artifactId=spotless-eclipse-groovy
66
ext_description=Groovy Eclipse's formatter bundled for Spotless

_ext/eclipse-wtp/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ext {
55

66
p2Repository = "http://download.eclipse.org/webtools/repository/${VER_ECLIPSE_WTP}"
77

8-
p2Dependencies = [
8+
p2Dependencies = [
99
// XML/HTML Formatter - Dependencies
1010
'org.eclipse.wst.xml.core':'+', // DefaultXMLPartitionFormatter and XMLAssociationProvider
1111
'org.eclipse.wst.sse.core':'+', // Structure models

_ext/eclipse-wtp/gradle.properties

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Versions correspond to the Eclipse-WTP version used for th FAT JAR
2-
# See https://www.eclipse.org/webtools/ for further information about Eclipse-Groovy versions.
3-
# Patch version can be is incremented independently for backward compatible patches of this library.
1+
# Versions correspond to the Eclipse-WTP version used for the fat-JAR.
2+
# See https://www.eclipse.org/webtools/ for further information about Eclipse-WTP versions.
3+
# Patch version can be incremented independently for backward compatible patches of this library.
44
ext_version=3.9.5
55
ext_artifactId=spotless-eclipse-wtp
66
ext_description=Eclipse's WTP formatters bundled for Spotless

0 commit comments

Comments
 (0)