Search in sources :

Example 1 with FileVersionTuple

use of bndtools.editor.contents.PackageInfoDialog.FileVersionTuple in project bndtools by bndtools.

the class ExportPatternsListPart method generatePackageInfos.

private static void generatePackageInfos(final Collection<? extends FileVersionTuple> pkgs) throws CoreException {
    final IWorkspaceRunnable wsOperation = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor monitor) throws CoreException {
            SubMonitor progress = SubMonitor.convert(monitor, pkgs.size());
            MultiStatus status = new MultiStatus(Plugin.PLUGIN_ID, 0, "Errors occurred while creating packageinfo files.", null);
            for (FileVersionTuple pkg : pkgs) {
                IContainer[] locations = ResourcesPlugin.getWorkspace().getRoot().findContainersForLocationURI(pkg.getFile().toURI());
                if (locations != null && locations.length > 0) {
                    IContainer container = locations[0];
                    PackageInfoStyle packageInfoStyle = PackageInfoStyle.calculatePackageInfoStyle(container.getProject());
                    IFile pkgInfoFile = container.getFile(new Path(packageInfoStyle.getFileName()));
                    try {
                        String formattedPackageInfo = packageInfoStyle.format(pkg.getVersion(), pkg.getName());
                        ByteArrayInputStream input = new ByteArrayInputStream(formattedPackageInfo.getBytes("UTF-8"));
                        if (pkgInfoFile.exists())
                            pkgInfoFile.setContents(input, false, true, progress.newChild(1, 0));
                        else
                            pkgInfoFile.create(input, false, progress.newChild(1, 0));
                    } catch (CoreException e) {
                        status.add(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error creating file " + pkgInfoFile.getFullPath(), e));
                    } catch (UnsupportedEncodingException e) {
                    /* just ignore, should never happen */
                    }
                }
            }
            if (!status.isOK())
                throw new CoreException(status);
        }
    };
    IRunnableWithProgress uiOperation = new IRunnableWithProgress() {

        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                ResourcesPlugin.getWorkspace().run(wsOperation, monitor);
            } catch (CoreException e) {
                throw new InvocationTargetException(e);
            }
        }
    };
    try {
        PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, uiOperation);
    } catch (InvocationTargetException e) {
        throw (CoreException) e.getTargetException();
    } catch (InterruptedException e) {
    // ignore
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IFile(org.eclipse.core.resources.IFile) SubMonitor(org.eclipse.core.runtime.SubMonitor) MultiStatus(org.eclipse.core.runtime.MultiStatus) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) FileVersionTuple(bndtools.editor.contents.PackageInfoDialog.FileVersionTuple) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) IContainer(org.eclipse.core.resources.IContainer) PackageInfoStyle(bndtools.editor.contents.PackageInfoStyle)

Example 2 with FileVersionTuple

use of bndtools.editor.contents.PackageInfoDialog.FileVersionTuple in project bndtools by bndtools.

the class ExportPatternsListPart method findSourcePackagesWithoutPackageInfo.

private Collection<FileVersionTuple> findSourcePackagesWithoutPackageInfo(Collection<? extends ExportedPackage> pkgs) throws Exception {
    Map<String, FileVersionTuple> result = new HashMap<String, FileVersionTuple>();
    Project project = getProject();
    if (project != null) {
        Collection<File> sourceDirs = project.getSourcePath();
        for (File sourceDir : sourceDirs) {
            for (ExportedPackage pkg : pkgs) {
                if (!result.containsKey(pkg.getName())) {
                    File pkgDir = new File(sourceDir, pkg.getName().replace('.', '/'));
                    if (pkgDir.isDirectory()) {
                        PackageInfoStyle existingPkgInfo = PackageInfoStyle.findExisting(pkgDir);
                        if (existingPkgInfo == null)
                            result.put(pkg.getName(), new FileVersionTuple(pkg.getName(), pkgDir));
                    }
                }
            }
        }
    }
    return result.values();
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IProject(org.eclipse.core.resources.IProject) Project(aQute.bnd.build.Project) FileVersionTuple(bndtools.editor.contents.PackageInfoDialog.FileVersionTuple) HashMap(java.util.HashMap) ExportedPackage(aQute.bnd.build.model.clauses.ExportedPackage) PackageInfoStyle(bndtools.editor.contents.PackageInfoStyle) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 3 with FileVersionTuple

use of bndtools.editor.contents.PackageInfoDialog.FileVersionTuple in project bndtools by bndtools.

the class ExportPatternsListPart method doAddClauses.

@Override
protected void doAddClauses(Collection<? extends ExportedPackage> pkgs, int index, boolean select) {
    List<FileVersionTuple> missingPkgInfoDirs;
    try {
        missingPkgInfoDirs = new ArrayList<FileVersionTuple>(findSourcePackagesWithoutPackageInfo(pkgs));
    } catch (Exception e) {
        ErrorDialog.openError(getManagedForm().getForm().getShell(), "Error", null, new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error finding source package for exported 1packages.", e));
        missingPkgInfoDirs = Collections.emptyList();
    }
    List<FileVersionTuple> generatePkgInfoDirs = new ArrayList<FileVersionTuple>(missingPkgInfoDirs.size());
    BndPreferences prefs = new BndPreferences();
    boolean noAskPackageInfo = prefs.getNoAskPackageInfo();
    if (noAskPackageInfo || missingPkgInfoDirs.isEmpty()) {
        generatePkgInfoDirs.addAll(missingPkgInfoDirs);
    } else {
        PackageInfoDialog dlg = new PackageInfoDialog(getSection().getShell(), missingPkgInfoDirs);
        if (dlg.open() == Window.CANCEL)
            return;
        prefs.setNoAskPackageInfo(dlg.isDontAsk());
        generatePkgInfoDirs.addAll(dlg.getSelectedPackageDirs());
    }
    try {
        generatePackageInfos(generatePkgInfoDirs);
    } catch (CoreException e) {
        ErrorDialog.openError(getManagedForm().getForm().getShell(), "Error", null, new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error generated packageinfo files.", e));
    }
    // Actually add the new exports
    super.doAddClauses(pkgs, index, select);
}
Also used : MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) BndPreferences(bndtools.preferences.BndPreferences) FileVersionTuple(bndtools.editor.contents.PackageInfoDialog.FileVersionTuple) CoreException(org.eclipse.core.runtime.CoreException) ArrayList(java.util.ArrayList) PackageInfoDialog(bndtools.editor.contents.PackageInfoDialog) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

FileVersionTuple (bndtools.editor.contents.PackageInfoDialog.FileVersionTuple)3 PackageInfoStyle (bndtools.editor.contents.PackageInfoStyle)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 IFile (org.eclipse.core.resources.IFile)2 CoreException (org.eclipse.core.runtime.CoreException)2 IStatus (org.eclipse.core.runtime.IStatus)2 MultiStatus (org.eclipse.core.runtime.MultiStatus)2 Status (org.eclipse.core.runtime.Status)2 Project (aQute.bnd.build.Project)1 ExportedPackage (aQute.bnd.build.model.clauses.ExportedPackage)1 PackageInfoDialog (bndtools.editor.contents.PackageInfoDialog)1 BndPreferences (bndtools.preferences.BndPreferences)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 IContainer (org.eclipse.core.resources.IContainer)1 IProject (org.eclipse.core.resources.IProject)1 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)1