Skip to content

Commit 627f7ad

Browse files
committed
Add proper support for multi-project builds (#23)
1 parent 87a6e81 commit 627f7ad

File tree

9 files changed

+64
-24
lines changed

9 files changed

+64
-24
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ plugin and its `jar` task.
115115

116116
### In a multi-projects build
117117

118-
You can apply this plugin to several subprojects of a multi-projects build.
118+
You can apply this plugin to a multi-projects build.
119+
If only one subproject need it, just apply it to said subproject.
120+
Otherwise, apply it to the root project,
121+
or to a common ancestor of all subprojects that need it.
119122
See [samples/multi-projects](samples/multi-projects/build.gradle).
120123

121124
### Watching for changes
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
/*
2+
* In case several subprojects need this plugin, it is recommended to apply and
3+
* configure it on the root project. For one, it allows you to share
4+
* the sass version (among other configuration). For another, it will save
5+
* disk space as dart-sass will be installed once per project where this plugin
6+
* is applied.
7+
*/
18
plugins {
2-
id 'io.miret.etienne.sass' version '1.4.2' apply false
9+
id 'io.miret.etienne.sass' version '1.4.2'
10+
}
11+
12+
sass {
13+
version = "1.54.4"
14+
// Your customization here
315
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
/* Don't reapply this plugin on subprojects if already done in root project. */
12
plugins {
23
id 'war'
3-
id 'io.miret.etienne.sass'
4+
}
5+
6+
compileSass {
7+
// Configuration for this subproject "compileSass" task.
48
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
/* Don't reapply this plugin on subprojects if already done in root project. */
12
plugins {
23
id 'war'
3-
id 'io.miret.etienne.sass'
4+
}
5+
6+
compileSass {
7+
// Configuration for this subproject "compileSass" task.
48
}
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +0,0 @@
1-
plugins {
2-
id 'io.miret.etienne.sass'
3-
}
4-
5-
sass {
6-
version = '42.0'
7-
baseUrl = System.getenv("URL")
8-
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
plugins {
2+
id 'io.miret.etienne.sass'
3+
}
4+
5+
sass {
6+
version = '42.0'
7+
baseUrl = System.getenv("URL")
8+
}
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +0,0 @@
1-
plugins {
2-
id 'io.miret.etienne.sass'
3-
}
4-
5-
sass {
6-
version = '42.0'
7-
baseUrl = System.getenv("URL")
8-
}

src/main/java/io/miret/etienne/gradle/sass/CompileSass.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import lombok.Setter;
55
import org.apache.tools.ant.taskdefs.condition.Os;
66
import org.gradle.api.DefaultTask;
7+
import org.gradle.api.Project;
78
import org.gradle.api.file.FileCollection;
89
import org.gradle.api.tasks.*;
910
import org.gradle.workers.WorkQueue;
@@ -86,10 +87,7 @@ public FileCollection getInputFiles () {
8687
@InputFile
8788
public File getExecutable () {
8889
String command = Os.isFamily (Os.FAMILY_WINDOWS) ? "sass.bat" : "sass";
89-
SassGradlePluginExtension sassExtension = getProject ()
90-
.getExtensions ()
91-
.findByType (SassGradlePluginExtension.class);
92-
assert sassExtension != null;
90+
SassGradlePluginExtension sassExtension = findExtension();
9391
return sassExtension.getDirectory ()
9492
.toPath ()
9593
.resolve (sassExtension.getVersion ())
@@ -98,6 +96,22 @@ public File getExecutable () {
9896
.toFile ();
9997
}
10098

99+
private SassGradlePluginExtension findExtension() {
100+
Project project = getProject();
101+
SassGradlePluginExtension extension = null;
102+
while (extension == null && project != null) {
103+
extension = project.getExtensions()
104+
.findByType(SassGradlePluginExtension.class);
105+
project = project.getParent();
106+
}
107+
if (extension == null) {
108+
throw new IllegalStateException(
109+
"SassGradlePluginExtension wasn't registered in any parent project."
110+
);
111+
}
112+
return extension;
113+
}
114+
101115
public void loadPath (File loadPath) {
102116
loadPaths.add (loadPath);
103117
}

src/main/java/io/miret/etienne/gradle/sass/SassGradlePlugin.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public void apply (Project project) {
4343
task.from (downloadedFiles);
4444
task.into (new File (extension.getDirectory (), extension.getVersion ()));
4545
});
46+
compileSass(project, extension, installSass);
47+
}
48+
49+
private void compileSass(
50+
Project project,
51+
SassGradlePluginExtension extension,
52+
TaskProvider<?> installSass
53+
) {
4654
TaskProvider<CompileSass> compileSass = project.getTasks ()
4755
.register ("compileSass", CompileSass.class, task -> {
4856
task.setDescription ("Compile sass and scss.");
@@ -56,6 +64,9 @@ public void apply (Project project) {
5664
task.from (compileSass.map (CompileSass::getOutputDir));
5765
}
5866
});
67+
project.getSubprojects().forEach(
68+
subProject -> compileSass(subProject, extension, installSass)
69+
);
5970
}
6071

6172
private String archiveName (String version) {

0 commit comments

Comments
 (0)