Skip to content

Commit

Permalink
Allow customization of project names
Browse files Browse the repository at this point in the history
  • Loading branch information
guw committed Oct 19, 2023
1 parent 737cdda commit b976ad7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
*/
public class ProjectPerPackageProvisioningStrategy extends BaseProvisioningStrategy {

private static final String PROJECT_NAME_SEPARATOR_CHAR = "project_name_separator_char";

public static final String STRATEGY_NAME = "project-per-package";

private static Logger LOG = LoggerFactory.getLogger(ProjectPerPackageProvisioningStrategy.class);
Expand Down Expand Up @@ -262,6 +264,23 @@ protected List<BazelProject> doProvisionProjects(Collection<BazelTarget> targets
return result;
}

private char getProjectNameSeparatorChar(BazelPackage bazelPackage) throws CoreException {
var separatorChar = bazelPackage.getBazelWorkspace()
.getBazelProjectView()
.targetProvisioningSettings()
.get(PROJECT_NAME_SEPARATOR_CHAR);
if (separatorChar == null) {
return '.';
}

separatorChar = separatorChar.trim();
if (separatorChar.length() != 1) {
throw new CoreException(Status.error("Invalid 'project_name_separator_char' setting in project view!"));
}

return separatorChar.charAt(0);
}

private boolean isSupported(BazelTarget bazeltarget) {
String ruleName;
try {
Expand All @@ -287,7 +306,8 @@ protected BazelProject provisionPackageProject(BazelPackage bazelPackage, List<B
if (!bazelPackage.hasBazelProject()) {
// create project
var packagePath = bazelPackage.getLabel().getPackagePath();
var projectName = packagePath.isBlank() ? "__ROOT__" : packagePath.replace('/', '.');
var projectName = packagePath.isBlank() ? "__ROOT__"
: packagePath.replace('/', getProjectNameSeparatorChar(bazelPackage));

// create the project directly within the package (note, there can be at most one project per package with this strategy anyway)
var projectLocation = bazelPackage.getLocation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void close() {
String targetDiscoveryStrategy, targetProvisioningStrategy;
final LinkedHashSet<Path> importingFiles = new LinkedHashSet<>();
IPath bazelBinary;
final LinkedHashMap<String, String> targetProvisioningSettings = new LinkedHashMap<>();
final LinkedHashMap<String, String> projectMappings = new LinkedHashMap<>();
final LinkedHashSet<String> importPreferences = new LinkedHashSet<>();
final LinkedHashSet<String> projectSettings = new LinkedHashSet<>();
Expand Down Expand Up @@ -131,6 +132,7 @@ public BazelProjectView build() throws IllegalStateException {
bazelBinary,
targetDiscoveryStrategy,
targetProvisioningStrategy,
targetProvisioningSettings,
projectMappings,
preferencesToImport,
projectSettingsToSync,
Expand Down Expand Up @@ -334,6 +336,17 @@ private void parseProjectFile(Path bazelProjectFile, BazelProjectViewBuilder bui
}
break;
}
case "target_provisioning_settings": {
parseSectionBodyIntoList(rawSection).forEach(s -> {
var separator = s.indexOf('=');
if (separator != -1) {
var key = s.substring(0, separator).trim();
var value = s.substring(separator + 1).trim();
builder.targetProvisioningSettings.put(key, value);
}
});
break;
}
case "import_preferences": {
parseSectionBodyIntoList(rawSection).forEach(builder.importPreferences::add);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public record BazelProjectView(
IPath bazelBinary,
String targetDiscoveryStrategy,
String targetProvisioningStrategy,
Map<String, String> targetProvisioningSettings,
Map<String, String> projectMappings,
Collection<WorkspacePath> importPreferences,
Collection<WorkspacePath> projectSettings,
Expand All @@ -52,6 +53,7 @@ public record BazelProjectView(
targets = Collections.unmodifiableCollection(targets);
additionalLanguages = Collections.unmodifiableCollection(additionalLanguages);
tsConfigRules = Collections.unmodifiableCollection(tsConfigRules);
targetProvisioningSettings = Collections.unmodifiableMap(targetProvisioningSettings);
projectMappings = Collections.unmodifiableMap(projectMappings);
importPreferences = Collections.unmodifiableCollection(importPreferences);
projectSettings = Collections.unmodifiableCollection(projectSettings);
Expand Down
6 changes: 6 additions & 0 deletions docs/common/projectviews.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ For details please read the JavaDoc (and Java code) of [ProjectPerPackageProvisi
* It requires running `bazel build` to detect classpath configuration.
* Is not fully implemented and required help/work/contributions.

### `target_provisioning_settings`

A list of settings to further tweak the `target_provisioning_strategy`.
The syntax of each entry is `key = value`, where `key` and `value` are expected string values (without any whitespace characters).


### `project_mappings`

A list of mappings from targets (typically from external repositories) to projects in the IDE.
Expand Down

0 comments on commit b976ad7

Please sign in to comment.