Search in sources :

Example 1 with BuildFileModifier

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();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalHistoryAction(com.intellij.history.LocalHistoryAction) LocalHistory(com.intellij.history.LocalHistory) BuildFileModifier(com.google.idea.blaze.base.buildmodifier.BuildFileModifier) IOException(java.io.IOException) WorkspaceRoot(com.google.idea.blaze.base.model.primitives.WorkspaceRoot) NotNull(org.jetbrains.annotations.NotNull) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) Result(com.intellij.openapi.application.Result)

Example 2 with BuildFileModifier

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));
    }
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Label(com.google.idea.blaze.base.model.primitives.Label) TargetName(com.google.idea.blaze.base.model.primitives.TargetName) WorkspaceRoot(com.google.idea.blaze.base.model.primitives.WorkspaceRoot) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) Kind(com.google.idea.blaze.base.model.primitives.Kind) LocalHistoryAction(com.intellij.history.LocalHistoryAction) BuildFileModifier(com.google.idea.blaze.base.buildmodifier.BuildFileModifier) LocalHistory(com.intellij.history.LocalHistory) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Aggregations

BuildFileModifier (com.google.idea.blaze.base.buildmodifier.BuildFileModifier)2 WorkspaceRoot (com.google.idea.blaze.base.model.primitives.WorkspaceRoot)2 LocalHistory (com.intellij.history.LocalHistory)2 LocalHistoryAction (com.intellij.history.LocalHistoryAction)2 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 File (java.io.File)2 NotNull (org.jetbrains.annotations.NotNull)2 Kind (com.google.idea.blaze.base.model.primitives.Kind)1 Label (com.google.idea.blaze.base.model.primitives.Label)1 TargetName (com.google.idea.blaze.base.model.primitives.TargetName)1 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)1 PsiFile (com.intellij.psi.PsiFile)1 IOException (java.io.IOException)1