Commonize into PathUtils; On Linux/BSD try to use xdg-open/x-www-browser if Java...
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / util / TestDetection.java
blob3b464c03dbb535b5395e25ba61f319ed1787c859
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.plugin.util;
12 import java.io.File;
13 import java.nio.file.Path;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Deque;
17 import java.util.LinkedList;
18 import org.gradle.api.Project;
19 import org.gradle.api.file.DirectoryTree;
20 import org.gradle.api.plugins.JavaPluginConvention;
21 import org.gradle.api.tasks.SourceSet;
23 /**
24 * Utilities regarding test detection.
26 * @since 2020/08/28
28 public final class TestDetection
30 /**
31 * Not used.
33 * @since 2020/08/28
35 private TestDetection()
39 /**
40 * Is this considered a test?
42 * @param __className The class name to check.
43 * @return If this is considered a test.
44 * @since 2020/08/28
46 public static boolean isTest(String __className)
47 throws NullPointerException
49 if (__className == null)
50 throw new NullPointerException("NARG");
52 // Get base name of the class
53 int ld = __className.lastIndexOf('.');
54 String base = (ld < 0 ? __className : __className.substring(ld + 1));
56 return (base.startsWith("Do") || base.startsWith("Test") ||
57 base.endsWith("Test"));
60 /**
61 * Returns a collection of source set files.
63 * @param __project The __project to scan.
64 * @param __sourceSet The source set to read files from.
65 * @return A collection of files within the source set.
66 * @throws NullPointerException On null arguments.
67 * @since 2020/08/29
69 public static Collection<FileLocation> sourceSetFiles(Project __project,
70 String __sourceSet)
71 throws NullPointerException
73 if (__project == null || __sourceSet == null)
74 throw new NullPointerException("NARG");
76 // Get base source sets
77 SourceSet sourceSet = __project.getConvention().getPlugin(
78 JavaPluginConvention.class).getSourceSets().getByName(__sourceSet);
80 // Add normal Java sources along with resources (for Jasmin)
81 Deque<DirectoryTree> queue = new LinkedList<>();
82 queue.addAll(sourceSet.getJava().getSrcDirTrees());
83 queue.addAll(sourceSet.getResources().getSrcDirTrees());
85 // Discover all the input files (in sources)
86 Collection<FileLocation> result = new ArrayList<>();
87 while (!queue.isEmpty())
89 DirectoryTree dir = queue.removeFirst();
90 Path baseDir = dir.getDir().toPath();
92 // Process all files in each directory
93 for (File file : __project.files(dir))
95 Path path = file.toPath();
97 result.add(new FileLocation(path, baseDir.relativize(path)));
101 return result;