|
| 1 | +/* |
| 2 | + * TODO: add license |
| 3 | + */ |
| 4 | + |
| 5 | +package org.cakephp.netbeans; |
| 6 | + |
| 7 | +import org.netbeans.api.extexecution.ExecutionDescriptor; |
| 8 | +import org.netbeans.api.extexecution.ExternalProcessBuilder; |
| 9 | +import org.netbeans.modules.php.api.phpmodule.PhpModule; |
| 10 | +import org.netbeans.modules.php.api.phpmodule.PhpProgram; |
| 11 | +import org.netbeans.modules.php.api.util.FileUtils; |
| 12 | +import org.openide.filesystems.FileObject; |
| 13 | +import org.openide.filesystems.FileUtil; |
| 14 | +import org.openide.util.NbBundle; |
| 15 | + |
| 16 | +public class CakeScript extends PhpProgram { |
| 17 | + public static final String SCRIPT_NAME = "cake"; // NOI18N |
| 18 | + public static final String SCRIPT_NAME_LONG = SCRIPT_NAME + FileUtils.getScriptExtension(true); |
| 19 | + |
| 20 | + private static final String SCRIPT_DIRECTORY = "cake/console/"; // NOI18N |
| 21 | + private static final String CMD_BAKE = "bake"; // NOI18N |
| 22 | + |
| 23 | + private final PhpModule phpModule; |
| 24 | + |
| 25 | + private CakeScript(String command) { |
| 26 | + this(command, null); |
| 27 | + } |
| 28 | + |
| 29 | + private CakeScript(String command, PhpModule phpModule) { |
| 30 | + super(command); |
| 31 | + this.phpModule = phpModule; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Get the project specific, <b>valid only</b> Cake script. If not found, the {@link InvalidPhpProgramException} is thrown. |
| 36 | + * @param phpModule PHP module for which Cake script is taken |
| 37 | + * @return the project specific, <b>valid only</b> Cake script |
| 38 | + * @throws InvalidPhpProgramException if Zend script is not valid or missing completely |
| 39 | + */ |
| 40 | + public static CakeScript forPhpModule(PhpModule phpModule) throws InvalidPhpProgramException { |
| 41 | + FileObject sourceDirectory = phpModule.getSourceDirectory(); |
| 42 | + |
| 43 | + // locate |
| 44 | + FileObject cake = sourceDirectory.getFileObject(SCRIPT_DIRECTORY + SCRIPT_NAME); |
| 45 | + if (cake == null) { |
| 46 | + cake = sourceDirectory.getFileObject(SCRIPT_DIRECTORY + SCRIPT_NAME_LONG); |
| 47 | + } |
| 48 | + if (cake == null) { |
| 49 | + throw new InvalidPhpProgramException(NbBundle.getMessage(CakeScript.class, "MSG_CakeNotFound")); |
| 50 | + } |
| 51 | + |
| 52 | + // validate |
| 53 | + String cakePath = FileUtil.toFile(cake).getAbsolutePath(); |
| 54 | + String error = validate(cakePath); |
| 55 | + if (error != null) { |
| 56 | + throw new InvalidPhpProgramException(error); |
| 57 | + } |
| 58 | + return new CakeScript(cakePath, phpModule); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String validate() { |
| 63 | + return FileUtils.validateScript(getProgram(), NbBundle.getMessage(CakeScript.class, "LBL_CakeTool")); |
| 64 | + } |
| 65 | + |
| 66 | + public static String validate(String command) { |
| 67 | + return new CakeScript(command).validate(); |
| 68 | + } |
| 69 | + |
| 70 | + // TODO: later, run it via FrameworkCommandSupport |
| 71 | + public void runBake() { |
| 72 | + ExecutionDescriptor executionDescriptor = getExecutionDescriptor() |
| 73 | + .outProcessorFactory(ANSI_STRIPPING_FACTORY) |
| 74 | + .errProcessorFactory(ANSI_STRIPPING_FACTORY); |
| 75 | + |
| 76 | + ExternalProcessBuilder processBuilder = getProcessBuilder() |
| 77 | + .addArgument(CMD_BAKE); |
| 78 | + assert phpModule != null; |
| 79 | + if (phpModule != null) { |
| 80 | + processBuilder = processBuilder |
| 81 | + .workingDirectory(FileUtil.toFile(phpModule.getSourceDirectory())); |
| 82 | + } |
| 83 | + executeLater(processBuilder, executionDescriptor, CMD_BAKE); |
| 84 | + } |
| 85 | +} |
0 commit comments