Skip to content

Commit 5fb59ca

Browse files
committed
refactor: refactored ApplyPlugin code base into seprarated smaller files
1 parent b6b6fd7 commit 5fb59ca

12 files changed

Lines changed: 356 additions & 154 deletions

plugin/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ gradlePlugin {
3838
plugins {
3939
apkPluginBuilder {
4040
id = "io.github.devvigilante.blockidle.plugin-builder"
41-
implementationClass = "com.icst.plugin.builder.ApplyPlugin"
41+
implementationClass = "com.icst.plugin.builder.BlockIdlePlugin"
4242
displayName = "BlockIdle APK Plugin Builder"
4343
description = "Build and configure Android APK plugins for BlockIdle"
4444
tags.set([
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* This file is part of Block IDLE.
3+
*
4+
* Block IDLE is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Block IDLE is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Block IDLE. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.icst.plugin.builder;
19+
20+
import org.gradle.api.Project;
21+
22+
public class AndroidPluginApplier {
23+
24+
public static void apply(Project project) {
25+
if (!project.getPlugins().hasPlugin("com.android.application")) {
26+
project.getPluginManager().apply("com.android.application");
27+
}
28+
}
29+
30+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This file is part of Block IDLE.
3+
*
4+
* Block IDLE is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Block IDLE is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Block IDLE. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.icst.plugin.builder;
19+
20+
import org.gradle.api.Project;
21+
import org.gradle.api.plugins.ExtensionContainer;
22+
23+
import com.android.build.api.variant.AndroidComponentsExtension;
24+
25+
public class AndroidVariantObserver {
26+
27+
public static void observe(Project project) {
28+
ExtensionContainer extContainer = project.getExtensions();
29+
AndroidComponentsExtension<?, ?, ?> androidComponents = extContainer
30+
.getByType(AndroidComponentsExtension.class);
31+
32+
androidComponents.onVariants(androidComponents.selector(), variant -> {
33+
PluginVariantValidator.validate(project, variant);
34+
PluginVariantTaskFactory.create(project, variant);
35+
PluginTaskWiring.wire(project, variant);
36+
});
37+
}
38+
}

plugin/src/main/java/com/icst/plugin/builder/ApplyPlugin.java

Lines changed: 0 additions & 146 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* This file is part of Block IDLE.
3+
*
4+
* Block IDLE is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Block IDLE is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Block IDLE. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.icst.plugin.builder;
19+
20+
import org.gradle.api.Plugin;
21+
import org.gradle.api.Project;
22+
23+
public class BlockIdlePlugin implements Plugin<Project> {
24+
25+
@Override
26+
public void apply(Project project) {
27+
AndroidPluginApplier.apply(project);
28+
PluginExtensionConfigurator.configure(project);
29+
PluginTaskRegistrar.register(project);
30+
AndroidVariantObserver.observe(project);
31+
}
32+
}

plugin/src/main/java/com/icst/plugin/builder/BuildPluginTask.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.Writer;
2525
import java.nio.charset.StandardCharsets;
2626
import java.nio.file.Files;
27+
import java.nio.file.StandardCopyOption;
2728
import java.util.ArrayList;
2829
import java.util.LinkedHashMap;
2930
import java.util.List;
@@ -88,12 +89,6 @@ public void execute() {
8889
throw new RuntimeException("No APKs found!");
8990
}
9091

91-
getLogger().lifecycle("Variant : " + getVariantName().get());
92-
getLogger().lifecycle("BuildType : " + getBuildType().get());
93-
getLogger().lifecycle("Flavors : " + getProductFlavors().get());
94-
getLogger().lifecycle("minSdk : " + getMinSdk().get());
95-
getLogger().lifecycle("targetSdk : " + getAppTargetSdk().get());
96-
9792
BlockIdleSdkExtension ext = getProject().getExtensions().getByType(BlockIdleSdkExtension.class);
9893

9994
Map<String, Object> root = new LinkedHashMap<>();
@@ -126,7 +121,7 @@ public void execute() {
126121
Files.copy(
127122
sourceApk.toPath(),
128123
targetApk.toPath(),
129-
java.nio.file.StandardCopyOption.REPLACE_EXISTING);
124+
StandardCopyOption.REPLACE_EXISTING);
130125

131126
Map<String, Object> apk = new LinkedHashMap<>();
132127
apk.put("apkPath", targetApk.getName()); // RELATIVE PATH ✔
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* This file is part of Block IDLE.
3+
*
4+
* Block IDLE is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Block IDLE is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Block IDLE. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.icst.plugin.builder;
19+
20+
import org.gradle.api.GradleException;
21+
import org.gradle.api.Project;
22+
import org.gradle.api.artifacts.Configuration;
23+
24+
public class PluginExtensionConfigurator {
25+
26+
public static void configure(Project project) {
27+
project.getExtensions().create("blockIdlePlugin", BlockIdleSdkExtension.class);
28+
29+
BlockIdleSdkExtension ext = project.getExtensions().getByType(BlockIdleSdkExtension.class);
30+
31+
Configuration sdkCfg = project.getConfigurations().maybeCreate("blockIdlePluginSdk");
32+
sdkCfg.setCanBeResolved(true);
33+
sdkCfg.setCanBeConsumed(false);
34+
35+
project.afterEvaluate(t -> {
36+
if (!ext.getPluginName().isPresent()) {
37+
throw new GradleException("Please provide a plugin name");
38+
}
39+
if (!ext.getSdkVersion().isPresent()) {
40+
throw new GradleException("Please provide sdk version for building plugin for BlockIDLE");
41+
}
42+
43+
String sdkVersion = ext.getSdkVersion().get();
44+
45+
project.getDependencies().add(
46+
"blockIdlePluginSdk",
47+
"io.github.devvigilante:blockidle-plugin-sdk:" + sdkVersion);
48+
49+
project.getDependencies().add(
50+
"compileOnly",
51+
"io.github.devvigilante:blockidle-plugin-sdk:" + sdkVersion);
52+
});
53+
}
54+
55+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of Block IDLE.
3+
*
4+
* Block IDLE is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Block IDLE is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Block IDLE. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.icst.plugin.builder;
19+
20+
import org.gradle.api.Project;
21+
import org.gradle.api.file.DirectoryProperty;
22+
23+
public class PluginTaskRegistrar {
24+
25+
public static void register(Project project) {
26+
project.getTasks().register("buildPlugin", task -> {
27+
task.setGroup("block-idle");
28+
task.setDescription("Build all debug plugin variants and generates ready to publish plugin");
29+
});
30+
31+
DirectoryProperty buildDir = project.getLayout().getBuildDirectory();
32+
33+
project.getTasks().register("mergePluginMetadata", MergePluginMetadataTask.class, t -> {
34+
t.getInputDir().set(buildDir.dir("outputs/plugin"));
35+
t.getOutputFile().set(buildDir.file("outputs/plugin/plugin-metadata.json"));
36+
});
37+
project.getTasks().named("buildPlugin").configure(t -> t.dependsOn("mergePluginMetadata"));
38+
}
39+
40+
}

0 commit comments

Comments
 (0)