Skip to content

Commit

Permalink
Validate FRC year during validation function (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse authored Sep 1, 2023
1 parent 1f519be commit 45d9dcb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -480,12 +480,6 @@ private void registerStaticOnlyStandardDependency(
});
}

private final String frcYear = "frc2023";

public String getFrcYear() {
return frcYear;
}

private DependencyVersions versions;

public DependencyVersions getVersions() {
Expand All @@ -497,8 +491,8 @@ public void configureDependencies(Action<DependencyVersions> dependencies) {
return;
}
dependencyVersions = objects.newInstance(DependencyVersions.class);
dependencyVersions.getGoogleTestYear().set(frcYear);
dependencyVersions.getOpencvYear().set(frcYear);
dependencyVersions.getGoogleTestYear().set("Unknown");
dependencyVersions.getOpencvYear().set("Unknown");

dependencyVersions.getWpiVersion().set("-1");
dependencyVersions.getNiLibVersion().set("-1");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package edu.wpi.first.nativeutils.vendordeps;

import edu.wpi.first.nativeutils.vendordeps.WPIVendorDepsExtension.JsonDependency;

public class InvalidVendorDepYearException extends RuntimeException {
public InvalidVendorDepYearException(JsonDependency dependency, String requiredYear) {
super(String.format("Vendor Dependency %s has invalid year %s. Expected to be %s", dependency.name,
dependency.frcYear, requiredYear));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.file.StandardCopyOption;

import org.gradle.api.DefaultTask;
import org.gradle.api.file.Directory;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;

Expand Down Expand Up @@ -78,8 +79,8 @@ private Path computeDest(String filename) {
* @param dest the destination file
*/
private void copyLocal(String filename, Path dest) {
Path localCache = Path.of(wpiExt.getFrcHome()).resolve("vendordeps");
File localFolder = localCache.toFile();
Directory localCache = wpiExt.getFrcHome().dir("vendordeps").get();
File localFolder = localCache.getAsFile();
if (!localFolder.isDirectory()) {
getLogger().error("For some reason " + localFolder + " is not a folder");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import org.gradle.api.Named;
import org.gradle.api.NamedDomainObjectSet;
import org.gradle.api.Project;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.provider.Property;
import org.gradle.internal.os.OperatingSystem;
import org.gradle.nativeplatform.plugins.NativeComponentPlugin;

import com.google.gson.Gson;
Expand All @@ -28,6 +30,18 @@

public abstract class WPIVendorDepsExtension {

private final Property<String> frcYear;

public Property<String> getFrcYear() {
return frcYear;
}

private final DirectoryProperty frcHome;

public DirectoryProperty getFrcHome() {
return frcHome;
}

private final NamedDomainObjectSet<NamedJsonDependency> dependencySet;

public NamedDomainObjectSet<NamedJsonDependency> getDependencySet() {
Expand All @@ -44,6 +58,7 @@ public NamedDomainObjectSet<NamedJsonDependency> getDependencySet() {
private final Project project;

private WPINativeVendorDepsExtension nativeVendor;

public WPINativeVendorDepsExtension getNativeVendor() {
return nativeVendor;
}
Expand All @@ -56,6 +71,9 @@ public WPIJavaVendorDepsExtension getJavaVendor() {

@Inject
public WPIVendorDepsExtension(Project project) {

frcYear = project.getObjects().property(String.class);
frcHome = project.getObjects().directoryProperty();
this.log = ETLoggerFactory.INSTANCE.create("WPIVendorDeps");
this.project = project;
hwSimulation = project.hasProperty(HW_SIM_SWITCH_PROPERTY);
Expand All @@ -79,7 +97,8 @@ private File vendorFolder(Project project) {
String filepath = DEFAULT_VENDORDEPS_FOLDER_NAME;
if (prop != null && !prop.equals(DEFAULT_VENDORDEPS_FOLDER_NAME)) {
log.logErrorHead(
"Warning! You have the property " + NATIVEUTILS_VENDOR_FOLDER_PROPERTY + " set to a non-default value: " + prop);
"Warning! You have the property " + NATIVEUTILS_VENDOR_FOLDER_PROPERTY
+ " set to a non-default value: " + prop);
log.logError("The default path (from the project root) is " + DEFAULT_VENDORDEPS_FOLDER_NAME);
log.logError(
"This can cause NativeUtils/GradleRIO to not be able to find the vendordep JSON files, and the dependencies not being loaded.");
Expand Down Expand Up @@ -115,7 +134,18 @@ public void loadAll() {
}

public void validateDependencies() {
Logger logger = Logging.getLogger(WPIVendorDepsExtension.class);
String requiredFrcYear = frcYear.getOrNull();
for (NamedJsonDependency jsonDep : dependencySet) {
if (requiredFrcYear != null) {
if (jsonDep.dependency.frcYear == null || jsonDep.dependency.frcYear.isBlank()) {
logger.warn("Vendor Dependency %s is missing frcYear. In future years this will be an error",
jsonDep.dependency.name);
} else if (!requiredFrcYear.equals(jsonDep.dependency.frcYear)) {
throw new InvalidVendorDepYearException(jsonDep.dependency, requiredFrcYear);
}
}

VendorDependency[] requiredDependencies = jsonDep.dependency.requires;
if (requiredDependencies != null) {
for (VendorDependency requiredDep : requiredDependencies) {
Expand Down Expand Up @@ -356,7 +386,7 @@ public String getName() {
public void addVendorReposToMaven(boolean enableGroupLimits) {
for (VendorMavenRepo vRepo : vendorRepos) {
project.getRepositories().maven(repo -> {
repo.setName("WPI"+vRepo.getName() + "Vendor");
repo.setName("WPI" + vRepo.getName() + "Vendor");
repo.setUrl(vRepo.getUrl());
if (enableGroupLimits) {
repo.content(desc -> {
Expand All @@ -368,28 +398,4 @@ public void addVendorReposToMaven(boolean enableGroupLimits) {
});
}
}

private String frcHomeCache;
private final String frcYear = "2023";

public String getFrcHome() {
if (frcHomeCache != null) {
return this.frcHomeCache;
}
String frcHome = "";
if (OperatingSystem.current().isWindows()) {
String publicFolder = System.getenv("PUBLIC");
if (publicFolder == null) {
publicFolder = "C:\\Users\\Public";
}
File homeRoot = new File(publicFolder, "wpilib");
frcHome = new File(homeRoot, this.frcYear).toString();
} else {
String userFolder = System.getProperty("user.home");
File homeRoot = new File(userFolder, "wpilib");
frcHome = new File(homeRoot, this.frcYear).toString();
}
frcHomeCache = frcHome;
return frcHomeCache;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class WPIVendorDepsPlugin implements Plugin<Project> {
public void apply(Project project) {
project.getExtensions().create("wpiVendorDeps", WPIVendorDepsExtension.class, project);


project.getTasks().register("vendordep", VendorDepTask.class, task -> {
task.setGroup("NativeUtils");
task.setDescription("Install vendordep JSON file from URL or local wpilib folder");
Expand Down

0 comments on commit 45d9dcb

Please sign in to comment.