/*
 * Copyright 2016 The Bazel Authors. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.idea.blaze.base.lang.buildfile.actions;

import com.google.common.base.Joiner;
import com.google.idea.blaze.base.buildmodifier.BuildFileModifier;
import com.google.idea.blaze.base.lang.buildfile.psi.BuildFile;
import com.google.idea.blaze.base.lang.buildfile.psi.Expression;
import com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression;
import com.google.idea.blaze.base.lang.buildfile.psi.util.BuildElementGenerator;
import com.google.idea.blaze.base.lang.buildfile.references.BuildReferenceManager;
import com.google.idea.blaze.base.model.primitives.Kind;
import com.google.idea.blaze.base.model.primitives.Label;
import com.google.idea.blaze.base.scope.BlazeContext;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;

/**
 * Implementation of BuildFileModifier. Modifies the PSI tree directly.
 */
public class BuildFileModifierImpl implements BuildFileModifier {

  private static final Logger LOG = Logger.getInstance(BuildFileModifierImpl.class);

  @Override
  public boolean addRule(Project project,
                         BlazeContext context,
                         Label newRule,
                         Kind ruleKind) {
    BuildFile buildFile = BuildReferenceManager.getInstance(project).resolveBlazePackage(newRule.blazePackage());
    if (buildFile == null) {
      LOG.error("No BUILD file found at location: " + newRule.blazePackage());
      return false;
    }
    WriteCommandAction.runWriteCommandAction(project, () -> {
      buildFile.add(createRule(project, ruleKind, newRule.ruleName().toString()));
    });
    return true;
  }

  private PsiElement createRule(Project project, Kind ruleKind, String ruleName) {
    String text = Joiner.on("\n").join(
      ruleKind.toString() + "(",
      "    name = \"" + ruleName + "\"",
      ")"
    );
    Expression expr = BuildElementGenerator.getInstance(project).createExpressionFromText(text);
    assert(expr instanceof FuncallExpression);
    return expr;
  }

}

@@ -709,24 +709,72 @@
 ase.Joiner;%0A
+import com.google.common.collect.ImmutableList;%0A
 import com.g
@@ -824,24 +824,24 @@
 leModifier;%0A
-
 import com.g
@@ -1510,16 +1510,110 @@
 roject;%0A
+import com.intellij.openapi.util.Computable;%0Aimport com.intellij.openapi.vfs.LocalFileSystem;%0A
 import c
@@ -1641,16 +1641,38 @@
 ement;%0A%0A
+import java.io.File;%0A%0A
 /**%0A * I
@@ -2087,72 +2087,409 @@
     
-BuildFile buildFile = BuildReferenceManager.getInstance(project)
+return WriteCommandAction.runWriteCommandAction(project, (Computable%3CBoolean%3E) () -%3E %7B%0A      BuildReferenceManager manager = BuildReferenceManager.getInstance(project);%0A      File file = manager.resolvePackage(newRule.blazePackage());%0A      if (file == null) %7B%0A        return null;%0A      %7D%0A      LocalFileSystem.getInstance().refreshIoFiles(ImmutableList.of(file));%0A      BuildFile buildFile = manager
 .res
@@ -2530,24 +2530,26 @@
 age());%0A    
+  
 if (buildFil
@@ -2567,16 +2567,18 @@
 %7B%0A      
+ 
+ 
 LOG.erro
@@ -2644,24 +2644,26 @@
 e());%0A      
+  
 return false
@@ -2672,71 +2672,11 @@
     
-%7D%0A    WriteCommandAction.runWriteCommandAction(project, () -%3E %7B
+  %7D
 %0A   
@@ -2761,14 +2761,8 @@
 ;%0A  
-  %7D);%0A
     
@@ -2774,16 +2774,24 @@
 n true;%0A
+    %7D);%0A
   %7D%0A%0A  p

Fix rule not being added to newly-created package