Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I manage an unmanaged Eclipse formatting profile?

Our project has an "Unmanaged profile" and save-time autoformatting. I'd like to be able to modify the settings for this unmanaged profile and be able to check them back in to version control.

Eclipse's help documents are quite unhelpful ("You are not allowed to change such a profile, only the creator (manager) of the profile can change it.").

like image 272
magneticMonster Avatar asked Mar 10 '11 21:03

magneticMonster


3 Answers

I'm not sure if this will allow you to check your profile back in to version control, but the following process will allow you to edit your profile on any computer that has the source checked out.

To edit your profile, you have to recreate the profile in Eclipse, which you can easily do as follows:

  • Create a new profile by clicking "New..."
  • Give the new profile the same name as your existing unmanaged profile.
  • Before clicking OK, make sure you selected your unmanaged profile in "Initialize settings with the following profile" drop down list.

This will let you recreate the profile, and allow you to modify it in Eclipse, as normal.

Note: This process works with Eclipse Indigo

like image 174
Brad Parks Avatar answered Nov 20 '22 22:11

Brad Parks


The problem is that a managed profile is actually stored in your workspace not your project. Settings are pushed into your project when you make project specific changes such as selecting a different profile. But the settings in the project are a different format to those in in the profile (in the workspace).

At present eclipse does not have the ability to perform this in reverse. That is it can't take settings from your project folder to create a profile in the workspace. Effectively an "Unmanaged Profile" is a profile to which you have lost the source code.

The simplest way I've found to reverse-engineer the profile is to generate an XML profile file that can be imported (under the formatter settings --> import button).

To reverse-engineer the the settings from a project; I wrote the following program. It reads the settings from a project folder and writes them out as an XML file:

import java.io.*;



public class ExtractFormatter {

    public static void main( String args[] ) throws IOException {
        if (args.length < 2)
            throw new RuntimeException("No arguements specified; expected <project folder> <output file>");

        File inFile = new File(args[0]);
        File outFile = new File(args[1]);

        BufferedReader reader = new BufferedReader(new FileReader(new File(inFile,
                ".settings/org.eclipse.jdt.core.prefs")));
        PrintWriter writer = new PrintWriter(outFile);
        writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");

        // Retain the date from the file as a comment
        String line = reader.readLine();
        writer.println("<!-- Exported from " + inFile + " -->");
        writer.println("<!-- " + line + " -->");

        writer.println("<profiles version=\"12\">");
        writer.println("<profile kind=\"CodeFormatterProfile\" name=\"" + inFile.getName()
                + "\" version=\"12\">\")");


        // Now read every setting for the formatter and write it out as an XML tag.
        for (line = reader.readLine(); line != null; line = reader.readLine()) {
            if (line.startsWith("org.eclipse.jdt.core.formatter.")) {
                String[] parts = line.split("=", 2);
                writer.println("<setting id=\"" + parts[0] + "\" value=\"" + parts[1] + "\" />");
            }
        }

        writer.println("</profile>");
        writer.println("</profiles>");

        reader.close();
        writer.close();
    }
}
like image 24
Philip Couling Avatar answered Nov 20 '22 20:11

Philip Couling


Eclipse should put a .settings folder in your project dir when you have an unmanaged profile. The only way I've found to change the settings so far is to go into the .settings folder within the project and hand edit the .prefs files in there.

like image 2
depsypher Avatar answered Nov 20 '22 22:11

depsypher