public void testMoveProjectWithVirtualFolder() {
    IPath fileLocation = getRandomLocation();
    IPath folderLocation = getRandomLocation();

    IFile file = existingVirtualFolderInExistingProject.getFile(getUniqueString());
    IFolder folder = existingVirtualFolderInExistingProject.getFolder(getUniqueString());
    IFile childFile = folder.getFile(getUniqueString());
    IResource[] oldResources = new IResource[] {existingProject, file, folder, childFile};

    try {
      assertDoesNotExistInWorkspace("1.0", new IResource[] {folder, file, childFile});

      try {
        createFileInFileSystem(fileLocation);
        folderLocation.toFile().mkdir();

        folder.createLink(folderLocation, IResource.NONE, getMonitor());
        file.createLink(fileLocation, IResource.NONE, getMonitor());

        childFile.create(getRandomContents(), true, getMonitor());
      } catch (CoreException e) {
        fail("2.0", e);
      }

      // move the project
      IProject destinationProject = getWorkspace().getRoot().getProject("MoveTargetProject");
      assertDoesNotExistInWorkspace("3.0", destinationProject);

      try {
        existingProject.move(destinationProject.getFullPath(), IResource.SHALLOW, getMonitor());
      } catch (CoreException e) {
        fail("4.0", e);
      }

      IFile newFile = destinationProject.getFile(file.getProjectRelativePath());
      IFolder newFolder = destinationProject.getFolder(folder.getProjectRelativePath());
      IFile newChildFile = newFolder.getFile(childFile.getName());
      IResource[] newResources =
          new IResource[] {destinationProject, newFile, newFolder, newChildFile};

      assertExistsInWorkspace("5.0", newResources);
      assertDoesNotExistInWorkspace("6.1", oldResources);
      assertTrue("7.0", existingProject.isSynchronized(IResource.DEPTH_INFINITE));
      assertTrue("8.0", destinationProject.isSynchronized(IResource.DEPTH_INFINITE));

      assertTrue("9.0", newFile.getParent().isVirtual());
      assertTrue("10.0", newFile.isLinked());

      assertTrue("11.0", newFolder.isLinked());
      assertTrue("12.0", newFolder.getParent().isVirtual());
    } finally {
      Workspace.clear(fileLocation.toFile());
      Workspace.clear(folderLocation.toFile());
    }
  }
 @Override
 protected void checkFile(IFile file, Set<NodeDescriptor> cluster) {
   final String name = parser.containedModule(file);
   if (name == null) {
     return;
   }
   if (file.isLinked()) {
     checkLinkedFile(
         name,
         "Linked from "
             + ClusteringTools.truncate(file.getLocation().removeLastSegments(1).toOSString()));
   } else {
     addNodeToCluster(name, cluster);
   }
 }
 /**
  * Write the passed resource to the current archive.
  *
  * @param resource org.eclipse.core.resources.IFile
  * @param destinationPath java.lang.String
  * @exception java.io.IOException
  * @exception org.eclipse.core.runtime.CoreException
  */
 @Override
 public void write(IFile resource, String destinationPath) throws IOException, CoreException {
   if (!resolveLinks && resource.isLinked(IResource.DEPTH_INFINITE)) {
     return;
   }
   TarEntry newEntry = new TarEntry(destinationPath);
   if (resource.getLocalTimeStamp() != IResource.NULL_STAMP) {
     newEntry.setTime(resource.getLocalTimeStamp() / 1000);
   }
   ResourceAttributes attributes = resource.getResourceAttributes();
   if (attributes != null && attributes.isExecutable()) {
     newEntry.setMode(newEntry.getMode() | 0111);
   }
   if (attributes != null && attributes.isReadOnly()) {
     newEntry.setMode(newEntry.getMode() & ~0222);
   }
   write(newEntry, resource);
 }
 private void handleLinkedFiles(Collection<File> tmpToDelete, final File baseDir) {
   // Handle linked files
   for (IFile file : request.getOnlyOnFiles()) {
     if (file.isLinked()) {
       File tmp = new File(baseDir, file.getProjectRelativePath().toString());
       SonarLintCorePlugin.getDefault()
           .debug(
               file.getName()
                   + " is a linked resource. Will create a temporary copy"
                   + System.lineSeparator());
       try {
         Files.copy(file.getContents(), tmp.toPath());
         tmpToDelete.add(tmp);
       } catch (IOException | CoreException e) {
         SonarLintCorePlugin.getDefault()
             .error(
                 "Unable to create temporary copy for linked resource" + System.lineSeparator(),
                 e);
       }
     }
   }
 }
  public void testCopyProjectWithVirtualFolder() {
    IPath fileLocation = getRandomLocation();
    IPath folderLocation = getRandomLocation();

    IFile linkedFile = existingVirtualFolderInExistingProject.getFile(getUniqueString());
    IFolder linkedFolder = existingVirtualFolderInExistingProject.getFolder(getUniqueString());

    try {
      try {
        createFileInFileSystem(fileLocation, getRandomContents());
        folderLocation.toFile().mkdir();

        linkedFolder.createLink(folderLocation, IResource.NONE, getMonitor());
        linkedFile.createLink(fileLocation, IResource.NONE, getMonitor());
      } catch (CoreException e) {
        fail("1.0", e);
      }

      // copy the project
      IProject destinationProject = getWorkspace().getRoot().getProject("CopyTargetProject");
      try {
        existingProject.copy(destinationProject.getFullPath(), IResource.SHALLOW, getMonitor());
      } catch (CoreException e) {
        fail("2.0", e);
      }

      IFile newFile = destinationProject.getFile(linkedFile.getProjectRelativePath());
      assertTrue("3.0", newFile.isLinked());
      assertEquals("3.1", linkedFile.getLocation(), newFile.getLocation());
      assertTrue("3.2", newFile.getParent().isVirtual());

      IFolder newFolder = destinationProject.getFolder(linkedFolder.getProjectRelativePath());
      assertTrue("4.0", newFolder.isLinked());
      assertEquals("4.1", linkedFolder.getLocation(), newFolder.getLocation());
      assertTrue("4.2", newFolder.getParent().isVirtual());

      // test project deep copy
      try {
        destinationProject.delete(IResource.NONE, getMonitor());
        existingProject.copy(destinationProject.getFullPath(), IResource.NONE, getMonitor());
      } catch (CoreException e) {
        fail("5.0", e);
      }

      assertTrue("5.1", newFile.isLinked());
      assertEquals("5.2", linkedFile.getLocation(), newFile.getLocation());
      assertTrue("5.3", newFile.getParent().isVirtual());
      assertTrue("5.4", newFolder.isLinked());
      assertEquals("5.5", linkedFolder.getLocation(), newFolder.getLocation());
      assertTrue("5.6", newFolder.getParent().isVirtual());

      try {
        destinationProject.delete(IResource.NONE, getMonitor());
      } catch (CoreException e) {
        fail("6.0", e);
      }
    } finally {
      Workspace.clear(fileLocation.toFile());
      Workspace.clear(folderLocation.toFile());
    }
  }
Esempio n. 6
0
  /**
   * @see
   *     org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
   */
  public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);

    Spec spec = Activator.getSpecManager().getSpecLoaded();

    FileDialog openFileDialog = UIHelper.getFileDialog(window.getShell());
    openFileDialog.setText("Add TLA+ module to the spec");
    openFileDialog.setFilterPath(ResourceHelper.getParentDirName(spec.getRootFile()));

    openFileDialog.setFilterExtensions(ACCEPTED_EXTENSIONS);
    String moduleFileName = openFileDialog.open();
    if (moduleFileName != null) {
      IFile module = ResourceHelper.getLinkedFile(spec.getProject(), moduleFileName, false);

      // add .tla extension is the file does not have any extension
      if (module != null && module.getFileExtension() == null) {
        moduleFileName = ResourceHelper.getModuleFileName(moduleFileName);
        module = ResourceHelper.getLinkedFile(spec.getProject(), moduleFileName, false);
      }

      // check if it a TLA file
      if (!ResourceHelper.isModule(module)) {
        // selected non-TLA file
        // module exists and is already registered in the spec
        MessageDialog.openInformation(
            window.getShell(),
            "The selected file is not a TLA+ file",
            "The provided file "
                + module.getName()
                + " is not a TLA+ file.\n Please select a file with .tla extension.");
        return null;

      } else {
        if (module.isLinked()) {
          // module exists and is already registered in the spec
          MessageDialog.openInformation(
              window.getShell(),
              "TLA+ Module is part of the spec",
              "The provided module "
                  + module.getName()
                  + " has already been added to the specification previously.");
        } else {
          IPath modulePath = new Path(moduleFileName);
          // check the folder we are in
          if (!ResourceHelper.isProjectParent(
              modulePath.removeLastSegments(1), spec.getProject())) {
            // the selected resource is not in the same directory as
            // the root file
            MessageDialog.openInformation(
                window.getShell(),
                "TLA+ Module is not part of the current spec.",
                "The provided module "
                    + module.getName()
                    + " is not part of the spec which is currently open. It will therefore be opened in read-only mode.\n"
                    + "If you want to make changes to this file, you will have to open the corresponding spec first.");

            // Open TLA's read-only editor on a .tla file that does
            // *not* belong to the current spec. It is opened
            // read-only, because we want to allow any changes,
            // because we couldn't parse the spec anyway. The reason
            // why this functionality is offered, is to allow users
            // to look at .tla files of closed spec.
            // http://wiki.eclipse.org/FAQ_How_do_I_open_an_editor_on_a_file_outside_the_workspace%3F
            final IFileStore fileStore =
                EFS.getLocalFileSystem().getStore(new Path(moduleFileName));
            if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) {
              UIHelper.openEditor(
                  "org.lamport.tla.toolbox.editor.basic.TLAEditorReadOnly",
                  new FileStoreEditorInput(fileStore));
            } else {
              throw new ExecutionException(
                  moduleFileName
                      + " cannot be opened in read-only mode because its file content could not be obtained.");
            }
            return null;
          }

          // !module.exists()
          if (!modulePath.toFile().exists()) {
            // the provided file does not exist
            boolean createNew =
                MessageDialog.openQuestion(
                    window.getShell(),
                    "TLA+ Module is not found",
                    "The provided module "
                        + module.getName()
                        + " does not exist. Should the new file be created?");
            if (createNew) {
              // the module point to a virtual path /WS_ROOT/SPEC_NAME/module_name.tla
              // assuming the fact that the root file is located in directory
              // /ROOT_DIR/SPEC_NAME.tla
              // and the Spec's project name is /ROOT_DIR/SPEC_NAME.project
              // the file should be created in /ROOT_DIR/module_name.tla and linked to the virtual
              // path.

              try {
                ResourcesPlugin.getWorkspace()
                    .run(ResourceHelper.createTLAModuleCreationOperation(modulePath), null);
              } catch (CoreException e) {
                e.printStackTrace();
                // exception, no chance to recover
                return null;
              }
            } else {
              return null;
            }
          }
          // adding the file to the spec
          module = createModuleFile(spec, moduleFileName, modulePath);
        }

        // create parameters for the handler
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put(
            OpenModuleHandler.PARAM_MODULE,
            ResourceHelper.getModuleNameChecked(module.getName(), false));

        // runs the command and opens the module in the editor
        UIHelper.runCommand(OpenModuleHandler.COMMAND_ID, parameters);
      }
    }

    return null;
  }