Skip to content

Commit

Permalink
Merge pull request #103 from tudortimi/tidy-up-code-related-to-naming
Browse files Browse the repository at this point in the history
Tidy up code related to naming
  • Loading branch information
tudortimi authored Jan 21, 2023
2 parents 6f3da0d + c3a38bf commit 93a5c76
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void apply(Project project) {
final DefaultHDVLPluginExtension extension = new DefaultHDVLPluginExtension(project);
project.getExtensions().add("hdvl", extension);
project.getExtensions().add("sourceSets", extension.getSourceSets());
extension.getSourceSets().create("main");
SourceSet mainSourceSet = extension.getSourceSets().create("main");

configureCompileConfiguration(project);

Expand All @@ -51,7 +51,7 @@ public void execute(SourceSet sourceSet) {
}
});

configureCompileArtifact(project, toolName);
configureCompileArtifact(project, mainSourceSet, toolName);
}
}

Expand All @@ -66,7 +66,7 @@ public void execute(AbstractGenArgsFile genArgsFile) {
genArgsFile.setPrivateIncludeDirs(project.files().getAsFileTree());
genArgsFile.setExportedIncludeDirs(project.files().getAsFileTree());
genArgsFile.setCSource(project.files().getAsFileTree());
genArgsFile.getDestination().set(new File(project.getBuildDir(), sourceSet.getArgsFileName(toolName)));
genArgsFile.getDestination().set(new File(project.getBuildDir(), Names.of(sourceSet.getName()).getArgsFileName(toolName)));
}
});
}
Expand All @@ -84,14 +84,14 @@ private Class<? extends AbstractGenArgsFile> getGenArgsFileClass(String toolName

private void configureGenFullArgsFile(Project project, SourceSet sourceSet, String toolName) {
AbstractGenArgsFile genArgsFile = (AbstractGenArgsFile) project.getTasks()
.getByName(Names.getGenArgsFileTaskName(sourceSet.getName(), toolName));
project.getTasks().register(Names.getGenFullArgsFileTaskName(sourceSet.getName(), toolName), GenFullArgsFile.class, new Action<GenFullArgsFile>() {
.getByName(sourceSet.getGenArgsFileTaskName(toolName));
project.getTasks().register(sourceSet.getGenFullArgsFileTaskName(toolName), GenFullArgsFile.class, new Action<GenFullArgsFile>() {
@Override
public void execute(GenFullArgsFile genFullArgsFile) {
genFullArgsFile.setDescription("Generates an argument file for the " + sourceSet.getName() + " source code and its dependencies.");
genFullArgsFile.getSource().set(genArgsFile.getDestination());
genFullArgsFile.getDestination().set(new File(project.getBuildDir(), Names.getFullArgsFileName(sourceSet.getName(), toolName)));
genFullArgsFile.setArgsFiles(project.getConfigurations().getByName(Names.getArgsFilesConfigurationName(sourceSet.getName(), toolName)));
genFullArgsFile.getDestination().set(new File(project.getBuildDir(), Names.of(sourceSet.getName()).getFullArgsFileName(toolName)));
genFullArgsFile.setArgsFiles(project.getConfigurations().getByName(sourceSet.getArgsFilesConfigurationName(toolName)));
}
});
}
Expand All @@ -105,23 +105,23 @@ private void configureCompileConfiguration(Project project) {
}

private void configureArgsFilesConfiguration(Project project, SourceSet sourceSet, String toolName) {
Configuration argsFiles = project.getConfigurations().create(Names.getArgsFilesConfigurationName(sourceSet.getName(), toolName));
Configuration argsFiles = project.getConfigurations().create(sourceSet.getArgsFilesConfigurationName(toolName));
argsFiles.extendsFrom(project.getConfigurations().getByName("compile"));
argsFiles.setCanBeConsumed(true);
argsFiles.setCanBeResolved(true);
argsFiles.getAttributes().attribute(HDVLBasePlugin.TOOL_ATTRIBUTE, toolName);
}

private void configureCompileArtifact(Project project, String toolName) {
private void configureCompileArtifact(Project project, SourceSet mainSourceSet, String toolName) {
AbstractGenArgsFile genArgsFile
= (AbstractGenArgsFile) project.getTasks().getByName(Names.getGenArgsFileTaskName("main", toolName));
= (AbstractGenArgsFile) project.getTasks().getByName(mainSourceSet.getGenArgsFileTaskName(toolName));
Action<ConfigurablePublishArtifact> configureAction = new Action<ConfigurablePublishArtifact>() {
@Override
public void execute(ConfigurablePublishArtifact configurablePublishArtifact) {
configurablePublishArtifact.builtBy(genArgsFile);
}
};
project.getArtifacts().add(Names.getArgsFilesConfigurationName("main", toolName), genArgsFile.getDestination(), configureAction);
project.getArtifacts().add(mainSourceSet.getArgsFilesConfigurationName(toolName), genArgsFile.getDestination(), configureAction);
}

}
12 changes: 10 additions & 2 deletions src/main/java/com/verificationgentleman/gradle/hdvl/SourceSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ public interface SourceSet extends Named, ExtensionAware {
String getGenArgsFileTaskName(String toolName);

/**
* Returns the name of the args file for this source set, for the specified tool.
* Returns the name of the gen full args file task for this source set, for the specified tool.
*
* @param toolName The tool for which the args file is generated.
* @return The task name. Never returns null.
*/
String getArgsFileName(String toolName);
String getGenFullArgsFileTaskName(String toolName);

/**
* Returns the name of the args file configuration for this source set, for the specified tool.
*
* @param toolName The tool for which the configuration is used.
* @return The configuration name. Never returns null.
*/
String getArgsFilesConfigurationName(String toolName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@

public abstract class DefaultSourceSet implements SourceSet {
private final String name;
private final Names names;

@Inject
public DefaultSourceSet(String name, ObjectFactory objectFactory) {
this.name = name;
this.names = Names.of(name);
}

@Override
Expand All @@ -36,14 +38,16 @@ public String getName() {

@Override
public String getGenArgsFileTaskName(String toolName) {
return Names.getGenArgsFileTaskName(name, toolName);
return names.getGenArgsFileTaskName(toolName);
}

@Override
public String getArgsFileName(String toolName) {
String toolNameLower = toolName.toLowerCase();
return name.equals("main")
? toolNameLower + "_" + "args.f"
: name + "_" + toolNameLower + "_" + "args.f";
public String getGenFullArgsFileTaskName(String toolName) {
return names.getGenFullArgsFileTaskName(toolName);
}

@Override
public String getArgsFilesConfigurationName(String toolName) {
return names.getArgsFilesConfigurationName(toolName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,103 @@

import org.gradle.util.GUtil;

public class Names {
import java.util.ArrayList;
import java.util.List;

public static String getGenArgsFileTaskName(String sourceSetName, String toolName) {
if (sourceSetName.equals("main"))
return "gen" + toolName + "ArgsFile";
return GUtil.toLowerCamelCase("gen" + " " + sourceSetName + "" + toolName + "ArgsFile");
}
public abstract class Names {

public static String getGenFullArgsFileTaskName(String sourceSetName, String toolName) {
public static Names of(String sourceSetName) {
if (sourceSetName.equals("main"))
return "genFull" + toolName + "ArgsFile";
return GUtil.toLowerCamelCase("genFull" + " " + sourceSetName + "" + toolName + "ArgsFile");
return new NamesForMain();
return new NamesForOther(sourceSetName);
}

public static String getFullArgsFileName(String sourceSetName, String toolName) {
String toolNameLower = toolName.toLowerCase();
if (sourceSetName.equals("main"))
return "full" + "_" + toolNameLower + "_" + "args.f";
return "full" + "_" + sourceSetName + "_"+ toolNameLower + "_" + "args.f";
public abstract String getGenArgsFileTaskName(String toolName);

public abstract String getGenFullArgsFileTaskName(String toolName);

public abstract String getArgsFilesConfigurationName(String toolName);

public abstract String getArgsFileName(String toolName);

public abstract String getFullArgsFileName(String toolName);

private static class NamesForMain extends Names {
@Override
public String getGenArgsFileTaskName(String toolName) {
return toLowerCamelCase("gen", toolName, "argsFile");
}

@Override
public String getGenFullArgsFileTaskName(String toolName) {
return toLowerCamelCase("genFull", toolName, "ArgsFile");
}

@Override
public String getArgsFilesConfigurationName(String toolName) {
return toLowerCamelCase(toolName, "ArgsFiles");
}

@Override
public String getArgsFileName(String toolName) {
return toLowerSnakeCase(toolName, "args.f");
}

@Override
public String getFullArgsFileName(String toolName) {
return toLowerSnakeCase("full", toolName, "args.f");
}
}

public static String getArgsFilesConfigurationName(String sourceSetName, String toolName) {
if (sourceSetName.equals("main"))
return GUtil.toLowerCamelCase(toolName + "ArgsFiles");
return GUtil.toLowerCamelCase(sourceSetName + " " + toolName + "ArgsFiles");
private static class NamesForOther extends Names {
private final String sourceSetName;

NamesForOther(String sourceSetName) {
this.sourceSetName = sourceSetName;
}

@Override
public String getGenArgsFileTaskName(String toolName) {
return toLowerCamelCase("gen", sourceSetName, toolName, "ArgsFile");
}

@Override
public String getGenFullArgsFileTaskName(String toolName) {
return toLowerCamelCase("genFull", sourceSetName, toolName, "ArgsFile");
}

@Override
public String getArgsFilesConfigurationName(String toolName) {
return toLowerCamelCase(sourceSetName, toolName, "ArgsFiles");
}

@Override
public String getArgsFileName(String toolName) {
return toLowerSnakeCase(sourceSetName, toolName, "args.f");
}

@Override
public String getFullArgsFileName(String toolName) {
return toLowerSnakeCase("full", sourceSetName, toolName, "args.f");
}
}

public static String getTestTaskName(String toolName) {
return GUtil.toLowerCamelCase("testWith" + " " + toolName);
return toLowerCamelCase("testWith", toolName);
}

private static String toLowerCamelCase(String ... words) {
StringBuilder stringBuilder = new StringBuilder();
for (String word: words)
stringBuilder.append(word).append(" ");
return GUtil.toLowerCamelCase(stringBuilder.toString());
}

private static String toLowerSnakeCase(String ... words) {
List<String> lowerCaseWords = new ArrayList<>();
for (String word: words)
lowerCaseWords.add(word.toLowerCase());
return String.join("_", lowerCaseWords);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void apply(Project project) {

String[] toolNames = {"Xrun", "Qrun"};
for (String toolName: toolNames) {
configureArgsFilesConfiguration(project, toolName);
configureArgsFilesConfiguration(project, testSourceSet, toolName);
}

configureToolChain(project);
Expand All @@ -71,7 +71,7 @@ public void execute(SourceSet sourceSet) {

String[] toolNames = {"Xrun", "Qrun"};
for (String toolName: toolNames) {
configureTestTask(project, svSourceSet, toolName);
configureTestTask(project, sourceSets.getByName("main"), sourceSet, svSourceSet, toolName);
}
}
});
Expand Down Expand Up @@ -103,8 +103,8 @@ private boolean isSVUnit(Dependency dependency) {
return dependency.getGroup().equals("org.svunit") && dependency.getName().equals("svunit");
}

private void configureArgsFilesConfiguration(Project project, String toolName) {
Configuration argsFiles = project.getConfigurations().getByName(Names.getArgsFilesConfigurationName("test", toolName));
private void configureArgsFilesConfiguration(Project project, SourceSet testSourceSet, String toolName) {
Configuration argsFiles = project.getConfigurations().getByName(testSourceSet.getArgsFilesConfigurationName(toolName));
argsFiles.extendsFrom(project.getConfigurations().getByName("testCompile"));
argsFiles.exclude(getExcludeForSVUnit());
}
Expand All @@ -124,11 +124,11 @@ private void ignoreUnitTests(SystemVerilogSourceSet svSourceSet) {
svSourceSet.getSv().exclude("**/*_unit_test.sv");
}

private void configureTestTask(Project project, SystemVerilogSourceSet testSourceSet, String toolName) {
private void configureTestTask(Project project, SourceSet mainSourceSet, SourceSet testSourceSet, SystemVerilogSourceSet testSvSourceSet, String toolName) {
GenFullArgsFile genFullArgsFile
= (GenFullArgsFile) project.getTasks().getByName(Names.getGenFullArgsFileTaskName("main", toolName));
= (GenFullArgsFile) project.getTasks().getByName(mainSourceSet.getGenFullArgsFileTaskName(toolName));
GenFullArgsFile genFullTestArgsFile
= (GenFullArgsFile) project.getTasks().getByName(Names.getGenFullArgsFileTaskName("test", toolName));
= (GenFullArgsFile) project.getTasks().getByName(testSourceSet.getGenFullArgsFileTaskName(toolName));
Configuration svUnitRoot = project.getConfigurations().getByName("svUnitRoot");
project.getTasks().register(Names.getTestTaskName(toolName), TestTask.class, new Action<TestTask>() {
@Override
Expand All @@ -137,7 +137,7 @@ public void execute(TestTask testTask) {
testTask.getToolName().set(toolName.toLowerCase());
testTask.getMainArgsFile().set(genFullArgsFile.getDestination());
testTask.getTestArgsFile().set(genFullTestArgsFile.getDestination());
testTask.setTestsRoot(testSourceSet.getSv().getSourceDirectories().getSingleFile());
testTask.setTestsRoot(testSvSourceSet.getSv().getSourceDirectories().getSingleFile());
testTask.setSvunitRoot(svUnitRoot);
testTask.getWorkingDir().set(new File(project.getBuildDir(), "svunit"));
testTask.getExtraArgs().set(toolChains.getRunSVUnit().getArgs());
Expand Down

0 comments on commit 93a5c76

Please sign in to comment.