use of com.google.idea.blaze.base.buildmodifier.BuildFileModifier in project intellij by bazelbuild.
the class NewBlazePackageAction method createPackageOnDisk.
private Optional<VirtualFile> createPackageOnDisk(Project project, BlazeContext context, Label newRule, Kind ruleKind) {
String commandName = String.format("Creating %s package: %s", Blaze.buildSystemName(project), newRule.toString());
return new WriteCommandAction<Optional<VirtualFile>>(project, commandName) {
@Override
protected void run(@NotNull Result<Optional<VirtualFile>> result) throws Throwable {
LocalHistory localHistory = LocalHistory.getInstance();
LocalHistoryAction action = localHistory.startAction(commandName);
try {
WorkspaceRoot workspaceRoot = WorkspaceRoot.fromProject(project);
File dir = workspaceRoot.fileForPath(newRule.blazePackage());
try {
VirtualFile newDirectory = VfsUtil.createDirectories(dir.getPath());
VirtualFile newFile = newDirectory.createChildData(this, BUILD_FILE_NAME);
BuildFileModifier buildFileModifier = BuildFileModifier.getInstance();
buildFileModifier.addRule(project, newRule, ruleKind);
result.setResult(Optional.of(newFile));
} catch (IOException e) {
String errorMessage = "Error creating new package: " + e.getMessage();
context.output(PrintOutput.error(errorMessage));
logger.warn("Error creating new package", e);
result.setResult(Optional.empty());
}
} finally {
action.finish();
}
}
}.execute().getResultObject();
}
use of com.google.idea.blaze.base.buildmodifier.BuildFileModifier in project intellij by bazelbuild.
the class NewBlazeRuleDialog method doOKAction.
@Override
protected void doOKAction() {
TargetName targetName = newRuleUI.getRuleName();
Kind ruleKind = newRuleUI.getSelectedRuleKind();
WorkspaceRoot workspaceRoot = WorkspaceRoot.fromProject(project);
WorkspacePath workspacePath = workspaceRoot.workspacePathFor(new File(buildFile.getParent().getPath()));
Label newRule = Label.create(workspacePath, targetName);
BuildFileModifier buildFileModifier = BuildFileModifier.getInstance();
String commandName = String.format("Add %s %s rule '%s'", buildSystemName, ruleKind, newRule);
boolean success = new WriteCommandAction<Boolean>(project, commandName) {
@Override
protected void run(@NotNull Result<Boolean> result) throws Throwable {
LocalHistory localHistory = LocalHistory.getInstance();
LocalHistoryAction action = localHistory.startAction(commandName);
try {
result.setResult(buildFileModifier.addRule(project, newRule, ruleKind));
} finally {
action.finish();
}
}
}.execute().getResultObject();
if (success) {
super.doOKAction();
} else {
super.setErrorText(String.format("Could not create new rule, see %s Console for details", buildSystemName));
}
}
Aggregations