From 4dfb88da81335d6027e28a35efe17975527e90a1 Mon Sep 17 00:00:00 2001 From: anna Date: Mon, 5 Dec 2011 21:33:23 +0100 Subject: [PATCH] command line inspections: distinguish profile name/path options (cherry picked from commit 45896e75fe08abf00276621f5aeec2fda42249f5) --- .../codeInspection/InspectionApplication.java | 113 +++++++++++++----- .../codeInspection/InspectionMain.java | 12 +- .../src/messages/InspectionsBundle.properties | 11 +- 3 files changed, 100 insertions(+), 36 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInspection/InspectionApplication.java b/platform/lang-impl/src/com/intellij/codeInspection/InspectionApplication.java index 7674d859c903..7915e3c4c295 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/InspectionApplication.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/InspectionApplication.java @@ -46,6 +46,7 @@ import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiManager; import com.intellij.util.StringBuilderSpinAllocator; import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; +import org.jdom.JDOMException; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.Nullable; @@ -65,7 +66,9 @@ public class InspectionApplication { public String myProjectPath = null; public String myOutPath = null; public String mySourceDirectory = null; + public String myStubProfile = null; public String myProfileName = null; + public String myProfilePath = null; public boolean myRunWithEditorSettings = false; public boolean myRunGlobalToolsOnly = false; private Project myProject; @@ -80,8 +83,13 @@ public class InspectionApplication { @NonNls public static final String XML_EXTENSION = ".xml"; public void startup() { - if (myProjectPath == null || myProfileName == null) { - logError(myProjectPath + myProfileName); + if (myProjectPath == null) { + logError("Project to inspect is not defined"); + printHelp(); + } + + if (myProfileName == null && myProfilePath == null && myStubProfile == null) { + logError("Profile to inspect with is not defined"); printHelp(); } @@ -148,36 +156,9 @@ public class InspectionApplication { logMessageLn(1, InspectionsBundle.message("inspection.done")); logMessage(1, InspectionsBundle.message("inspection.application.initializing.project")); - //fetch profile by name from project file (project profiles can be disabled) - Profile inspectionProfile = InspectionProjectProfileManager.getInstance(myProject).getProfile(myProfileName, false); - if (inspectionProfile != null) { - logMessageLn(1, "Loaded shared project profile \'" + myProfileName + "\'"); - } + Profile inspectionProfile = loadInspectionProfile(); + if (inspectionProfile == null) return; - //check if ide profile is used for project - if (inspectionProfile == null) { - final Collection profiles = InspectionProjectProfileManager.getInstance(myProject).getProfiles(); - for (Profile profile : profiles) { - if (Comparing.strEqual(profile.getName(), myProfileName)) { - inspectionProfile = profile; - logMessageLn(1, "Loaded local profile \'" + myProfileName + "\'"); - break; - } - } - } - - //otherwise look for profile file or use default - if (inspectionProfile == null) { - inspectionProfile = InspectionProfileManager.getInstance().loadProfile(myProfileName); - if (inspectionProfile != null) { - logMessageLn(1, "Loaded profile \'" + inspectionProfile.getName() + "\' from file \'" + myProfileName + "\'"); - } - } - - if (inspectionProfile == null) { - inspectionProfile = InspectionProjectProfileManager.getInstance(myProject).getInspectionProfile(); - logMessageLn(1, "Using default project profile"); - } final InspectionManagerEx im = (InspectionManagerEx)InspectionManager.getInstance(myProject); final GlobalInspectionContextImpl inspectionContext = im.createNewGlobalContext(true); @@ -316,6 +297,76 @@ public class InspectionApplication { } } + @Nullable + private Profile loadInspectionProfile() throws IOException, JDOMException { + Profile inspectionProfile = null; + + //fetch profile by name from project file (project profiles can be disabled) + if (myProfileName != null) { + inspectionProfile = loadProfileByName(myProfileName); + if (inspectionProfile == null) { + logError("Profile with configured name (" + myProfileName + ") was not found (neither in project nor in config directory)"); + if (myErrorCodeRequired) System.exit(1); + return null; + } + return inspectionProfile; + } + + if (myProfilePath != null) { + inspectionProfile = loadProfileByPath(myProfilePath); + if (inspectionProfile == null) { + logError("Failed to load profile from \'" + myProfilePath + "\'"); + if (myErrorCodeRequired) System.exit(1); + return null; + } + return inspectionProfile; + } + + if (myStubProfile != null) { + if (!myRunWithEditorSettings) { + inspectionProfile = loadProfileByName(myStubProfile); + if (inspectionProfile != null) return inspectionProfile; + + inspectionProfile = loadProfileByPath(myStubProfile); + if (inspectionProfile != null) return inspectionProfile; + } + + inspectionProfile = InspectionProjectProfileManager.getInstance(myProject).getInspectionProfile(); + logError("Using default project profile"); + } + return inspectionProfile; + } + + @Nullable + private Profile loadProfileByPath(final String profilePath) throws IOException, JDOMException { + Profile inspectionProfile = InspectionProfileManager.getInstance().loadProfile(profilePath); + if (inspectionProfile != null) { + logMessageLn(1, "Loaded profile \'" + inspectionProfile.getName() + "\' from file \'" + profilePath + "\'"); + } + return inspectionProfile; + } + + @Nullable + private Profile loadProfileByName(final String profileName) { + Profile inspectionProfile = InspectionProjectProfileManager.getInstance(myProject).getProfile(profileName, false); + if (inspectionProfile != null) { + logMessageLn(1, "Loaded shared project profile \'" + profileName + "\'"); + } + else { + //check if ide profile is used for project + final Collection profiles = InspectionProjectProfileManager.getInstance(myProject).getProfiles(); + for (Profile profile : profiles) { + if (Comparing.strEqual(profile.getName(), profileName)) { + inspectionProfile = profile; + logMessageLn(1, "Loaded local profile \'" + profileName + "\'"); + break; + } + } + } + + return inspectionProfile; + } + @Nullable private InspectionsReportConverter getReportConverter(@Nullable final String outputFormat) { diff --git a/platform/lang-impl/src/com/intellij/codeInspection/InspectionMain.java b/platform/lang-impl/src/com/intellij/codeInspection/InspectionMain.java index f1acf39a53e3..a943f007ebcb 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/InspectionMain.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/InspectionMain.java @@ -52,13 +52,13 @@ public class InspectionMain implements ApplicationStarter { } }; myApplication.myProjectPath = args[1]; - myApplication.myProfileName = args[2]; + myApplication.myStubProfile = args[2]; myApplication.myOutPath = args[3]; if (myApplication.myProjectPath == null || myApplication.myOutPath == null - || myApplication.myProfileName == null) { - System.err.println(myApplication.myProjectPath + myApplication.myOutPath + myApplication.myProfileName); + || myApplication.myStubProfile == null) { + System.err.println(myApplication.myProjectPath + myApplication.myOutPath + myApplication.myStubProfile); printHelp(); } @@ -66,7 +66,11 @@ public class InspectionMain implements ApplicationStarter { try { for (int i = 4; i < args.length; i++) { String arg = args[i]; - if ("-d".equals(arg)) { + if ("-profileName".equals(arg)) { + myApplication.myProfileName = args[++i]; + } else if ("-profilePath".equals(arg)) { + myApplication.myProfilePath = args[++i]; + } else if ("-d".equals(arg)) { myApplication.mySourceDirectory = args[++i]; } else if ("-v0".equals(arg)) { diff --git a/platform/platform-resources-en/src/messages/InspectionsBundle.properties b/platform/platform-resources-en/src/messages/InspectionsBundle.properties index 0ae2bc50dcba..cccbbfecb224 100644 --- a/platform/platform-resources-en/src/messages/InspectionsBundle.properties +++ b/platform/platform-resources-en/src/messages/InspectionsBundle.properties @@ -1,5 +1,14 @@ inspection.diff.format.error=Required parameters: [] -inspection.command.line.explanation=Expected parameters: []\n Available options are:\n -d -- directory to be inspected. Optional. Whole project is inspected by default.\n -v[0|1|2] -- verbose level. 0 - silent, 1 - verbose, 2 - most verbose. +inspection.command.line.explanation=Expected parameters: \ + -- use here profile name configured in the project or locally or path to the inspection profile; can be stabbed when one of the -e|-profileName|-profilePath is used\n\ + []\n \ + Available options are:\n \ + -d -- directory to be inspected. Optional. Whole project is inspected by default.\n \ + -e -- skip \n\ + -v[0|1|2] -- verbose level. 0 - silent, 1 - verbose, 2 - most verbose. \n\ + -profileName -- name of a profile defined in project \n \ + -profilePath -- absolute path to the profile file + inspection.action.title=Inspection inspection.action.noun=Inspection inspection.action.profile.label=Inspection profile