| /******************************************************************************* |
| * Copyright (c) 2008-2010, Istvan Rath and Daniel Varro |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Istvan Rath - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.viatra2.frameworkgui.views.console.commands.builtin; |
| |
| import java.io.BufferedReader; |
| import java.io.File; |
| import java.io.FileReader; |
| import java.io.IOException; |
| import java.net.URI; |
| import java.util.List; |
| |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.resources.IFolder; |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.resources.ResourcesPlugin; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.NullProgressMonitor; |
| import org.eclipse.core.runtime.Path; |
| import org.eclipse.viatra2.framework.IFramework; |
| import org.eclipse.viatra2.frameworkgui.views.console.commands.CommandExecutor; |
| import org.eclipse.viatra2.frameworkgui.views.console.commands.IVIATRAConsoleCommandProvider; |
| import org.eclipse.viatra2.frameworkgui.views.console.commands.ParsedCommand; |
| |
| public class ExecuteCommandScript implements IVIATRAConsoleCommandProvider { |
| |
| public void executeCommand(IFramework fw, List<String> parameters) { |
| try { |
| if (parameters.size()>0) |
| { |
| String pURI = parameters.get(0); |
| |
| // get rid of any trailing / and append file:// prefix if necessary |
| if (pURI.startsWith("/")) { |
| pURI = pURI.substring(1); |
| } |
| if (!pURI.startsWith("file://")) { |
| pURI = "file://"+pURI; |
| } |
| else { |
| // check if there is a surplus / inside |
| String _pURI = pURI.substring(7); |
| if (_pURI.startsWith("/")) { |
| _pURI = _pURI.substring(1); |
| pURI = "file://"+_pURI; |
| } |
| } |
| URI uri = new URI(pURI); |
| |
| String path = uri.getAuthority() + uri.getPath(); |
| // check whether the path exists |
| // if not, create projects |
| IFile ifile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(path)); |
| IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(uri.getAuthority()); |
| IProgressMonitor mon = new NullProgressMonitor(); |
| try { |
| if (!p.exists()) { |
| fw.getLogger().error("top-level project "+p.getName()+" does not exist in the workspace"); |
| return; |
| } |
| p.open(mon); |
| } catch (CoreException cex) { |
| throw new IOException("Target project cannot be created/opened"); |
| } |
| // make sure project-relative path exists |
| //try { |
| String[] segments = uri.getPath().split("/"); |
| if (segments.length > 2) // first segment is always "" due to path beginning with '/' |
| { |
| // there is at least one directory below the project |
| // root |
| IFolder curr = null; |
| for (int i = 1; i < segments.length - 1; i++) // last segment will always be the file name |
| { |
| if (curr == null) |
| curr = p.getFolder(segments[i]); |
| else |
| curr = curr.getFolder(segments[i]); |
| if (!curr.exists()) { |
| fw.getLogger().error("folder "+curr.getName()+" does not exist"); |
| return; |
| //curr.create(true, true, mon); |
| } |
| } |
| } |
| // } catch (CoreException cex) { |
| // throw new IOException("Target folder structure cannot be created"); |
| // } |
| // // open reader to the file |
| FileReader ret = new FileReader(new File(ifile.getLocation().toOSString())); |
| |
| fw.getLogger().info("Executing command script from file "+ifile.getLocation().toPortableString()); |
| BufferedReader br = new BufferedReader(ret); |
| while (br.ready()) { |
| String cmdLine = br.readLine(); |
| // try execution |
| ParsedCommand cmd = CommandExecutor.getParsedCommand(cmdLine); |
| if (cmd.isValid) { |
| CommandExecutor.executeCommand(fw, cmd); |
| } |
| } |
| ret.close(); |
| fw.getLogger().info("Execution finished."); |
| } |
| } catch (Throwable t) { |
| fw.getLogger().printStackTrace(t); |
| } |
| |
| } |
| |
| public String getCommandSignature() { |
| return "executecommandscript(workspace_relative_path_to_commandscript_file)"; |
| } |
| |
| public String getDescription() { |
| return "Executes a VIATRA2 Console command script"; |
| } |
| |
| public String getHelpText() { |
| return "This command allows you to put a sequence of commands into a line-separated file, load it from the workspace and execute it as a simple macro."; |
| } |
| |
| } |