Open kalyankrishna1 opened 7 years ago
Yes, it seems to complain about ProjectItems.Count, of all things. ie, in "Remove and Sort All", line 13:
for (var i = 1; i <= projectItems.Count; i++) {
(which comes from dte.Solution.Projects.Item(i).ProjectItems)
Please fix this?
One of the problems:
if (file.SubProject != null) {
formatFile(file);
iterateProjectFiles(file.ProjectItems);
that last line should be
iterateProjectFiles(file.SubProject.ProjectItems);
This makes no sense. Note the unused
variable in iterateProjectFiles
If that parameter is completely removed, this script no longer works correctly.
// Randomly selects a cpp file from the project
// Iterate over all files
// TODO: pass in isSuitableCandidate as lambda
var fileList = iterateFiles(".cpp");
// select one
var file = fileList[Math.floor(Math.random() * fileList.length)];
// for testing the macro:
Macro.InsertText("\nlength: " + fileList.length.toString())
Macro.InsertText("\nwinner: " + file.Name)
file.Open();
file.Document.Activate();
function iterateFiles(ext) {
var arr = [];
for (var i = 1; i <= dte.Solution.Projects.Count; i++) {
var c = dte.Solution.Projects.Item(i).ProjectItems.Count;
var arr2 = iterateProjectFiles(dte.Solution.Projects.Item(i).ProjectItems, c, ext, arr);
arr = arr.concat(arr2)
}
return arr;
}
function iterateProjectFiles(projectItems, unused, ext) {
var arr = [];
for (var i = 1; i <= projectItems.Count; i++) {
var file = projectItems.Item(i);
if (file.Name == "Generated Files")
;//skip these
else
if (isSuitableCandidate(file, ext)) {
arr.push(file);
} else if (file.SubProject != null) {
var arr2 = iterateProjectFiles(file.SubProject.ProjectItems, 0, ext);
arr = arr.concat(arr2)
} else if (file.ProjectItems != null && file.ProjectItems.Count > 0) {
var arr2 = iterateProjectFiles(file.ProjectItems, 0, ext);
arr = arr.concat(arr2)
}
}
return arr;
}
function endsWith(str, end) {
return str.indexOf(end, str.length - end.length) != -1;
}
function startsWith(str, start) {
return str.indexOf(start, 0) == 0;
}
function isSuitableCandidate(file, ext) {
if (endsWith(file.Name, ext)) {
if (file.Name.indexOf("moc_") == 0)
return false;
if (!endsWith(file.Name, "Test.cpp"))
return true;
}
return false;
}
Also, I don't have complete faith in the startsWith
and endsWith
functions, but those are basically copied from "Remove and Sort All".
For example, "Remove and Sort All"