command line inspections: distinguish profile name/path options

(cherry picked from commit 45896e75fe08abf00276621f5aeec2fda42249f5)
This commit is contained in:
anna
2011-12-06 10:38:02 +01:00
parent 6d97d05e1c
commit 4dfb88da81
3 changed files with 100 additions and 36 deletions
@@ -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<Profile> 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<Profile> 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) {
@@ -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)) {
@@ -1,5 +1,14 @@
inspection.diff.format.error=Required parameters: <old_file> <new_file> [<delta_file_name>]
inspection.command.line.explanation=Expected parameters: <project_file_path> <inspection_profile_file_path> <output_path> [<options>]\n Available options are:\n -d <directory_path> -- 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: <project_file_path> <inspection_profile> <output_path> \
<inspections_profile> -- 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\
[<options>]\n \
Available options are:\n \
-d <directory_path> -- 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