How do you go about finding unused icons, images, strings in .resx files that may have become 'orphaned' and are no long开发者_开发技巧er required?
Recently ResXManager 1.0.0.41 added a feature to show the number of references to a string resource.
I couldn't find any existing solution that would search for string resource references in XAML files and batch-remove unused ones.
So I wrote this: https://github.com/Microsoft/RESX-Unused-Finder
It searches a project directory for references to string resources, then displays a list of ones it couldn't find a match for. You can specify a template to search for so it can find references in XAML files.
I created a free open-source VS extension that looks for unused images in a project, just published the first version: https://marketplace.visualstudio.com/items?itemName=Jitbit1.VSUnusedImagesFinder
This is no information an algorithm can reliably compute. The inspected program could fetch a list of all resources and do something with them, like letting the user choose from several icons.
Your best bet is probably to search for all references to your resource-access API of choice and inspect those manually. Using grep
/sed
you might be able to reduce the sites you have to inspect manually by handling all "easy" ones where a simple string is used.
Since I could not find a simple and fast solution yet, I found at least a solution that allows me to get the result I'm looking for, even if it takes some time (ideal for a lazy sunday afternoon).
The solution involves Visual Studio .NET 2010 and ReSharper (I'm using version 7.1) and goes like the following.
Step-by-step solution
1.) Right-click your primary RESX file in VS.NET and select "Find Usages" from the context menu:
This will bring up ReSharper's "Find Results" window.
2.) Double-click each occurrence in the solution window:
This will open the source code window with the resource.
3.) Rename this resource from within the source code window:
It will bring up ReSharper's "Rename Resource" dialog.
4.) Give the resource a new name with a unique prefix. In my example this is "TaskDialog_":
It will rename both the resource and also the auto-generated C# wrapper/access class.
5.) Repeat the above steps 2, 3 and 4 for all resources in the "Usages" window.
6.) Open the RESX file in Visual Studio's resource editor and select all files without the prefix:
7.) Now click the "Remove Resource" button on the top of the window or simply press the Del key:
You finally have a RESX file with only the acutally used resources in your file.
8.) (Optionally) If you have resources in multiple languages (e.g. "Resources.de.resx" for German), repeat steps 7 and 8 for those RESX files, too.
Warning
Please note that this will not work if you access your strings other than through the strongly-typed, auto-generated C# class Resources
.
I recently built a tool that detects and removes unused string resources. I used the information in this post as a reference. The tool may not be perfect, but it does the heavy-lifting part and will be useful if you have a big project with the long history. We used this tool internally to consolidate resource files, and remove unused resources (we got rid of 4,000+ resources out of 10,000).
You can look at the source code, or just install ClickOnce from here: https://resxutils.codeplex.com/
I had a similar problem. Several thousand resource strings that I'd created for a translation table, many of which were no longer required or reference by code. With around 180 dependent code files, there was no way I was going to manually go through each resource string.
The following code (in vb.net) will go through your project finding orphaned resources (in the project resources, not any individual forms' resources). It took around 1 minute for my project. It can be modified to find strings, images or any other resource type.
In summary it;
- 1) Uses the solution project file to gather all the included code modules and appends them into a single string variable;
- 2) Loops through all the project resource objects, and creates a list (in my case) of those which are strings;
- 3) Does a string search finding resource string codes in the combined project text variable;
- 4) Reports resource objects that are not referenced.
The function returns the object names on the windows clipboard for pasting in a spreadsheet or as a list array of the resource names.
edit : example call in module : modTest
? modTest.GetUnusedResources("C:\Documents and Settings\me\My Documents\Visual Studio 2010\Projects\myProj\myProj.vbproj", True, true)
'project file is the vbproj file for my solution
Public Function GetUnusedResources(projectFile As String, useClipboard As Boolean, strict As Boolean) As List(Of String)
Dim myProjectFiles As New List(Of String)
Dim baseFolder = System.IO.Path.GetDirectoryName(projectFile) + "\"
'get list of project files
Dim reader As Xml.XmlTextReader = New Xml.XmlTextReader(projectFile)
Do While (reader.Read())
Select Case reader.NodeType
Case Xml.XmlNodeType.Element 'Display beginning of element.
If reader.Name.ToLowerInvariant() = "compile" Then ' only get compile included files
If reader.HasAttributes Then 'If attributes exist
While reader.MoveToNextAttribute()
If reader.Name.ToLowerInvariant() = "include" Then myProjectFiles.Add((reader.Value))
End While
End If
End If
End Select
Loop
'now collect files into a single string
Dim fileText As New System.Text.StringBuilder
For Each fileItem As String In myProjectFiles
Dim textFileStream As System.IO.TextReader
textFileStream = System.IO.File.OpenText(baseFolder + fileItem)
fileText.Append(textFileStream.ReadToEnd)
textFileStream.Close()
Next
' Debug.WriteLine(fileText)
' Create a ResXResourceReader for the file items.resx.
Dim rsxr As New System.Resources.ResXResourceReader(baseFolder + "My Project\Resources.resx")
rsxr.BasePath = baseFolder + "Resources"
Dim resourceList As New List(Of String)
' Iterate through the resources and display the contents to the console.
For Each resourceValue As DictionaryEntry In rsxr
' Debug.WriteLine(resourceValue.Key.ToString())
If TypeOf resourceValue.Value Is String Then ' or bitmap or other type if required
resourceList.Add(resourceValue.Key.ToString())
End If
Next
rsxr.Close() 'Close the reader.
'finally search file string for occurances of each resource string
Dim unusedResources As New List(Of String)
Dim clipBoardText As New System.Text.StringBuilder
Dim searchText = fileText.ToString()
For Each resourceString As String In resourceList
Dim resourceCall = "My.Resources." + resourceString ' find code reference to the resource name
Dim resourceAttribute = "(""" + resourceString + """)" ' find attribute reference to the resource name
Dim searchResult As Boolean = False
searchResult = searchResult Or searchText.Contains(resourceCall)
searchResult = searchResult Or searchText.Contains(resourceAttribute)
If Not strict Then searchResult = searchResult Or searchText.Contains(resourceString)
If Not searchResult Then ' resource name no found so add to list
unusedResources.Add(resourceString)
clipBoardText.Append(resourceString + vbCrLf)
End If
Next
'make clipboard object
If useClipboard Then
Dim dataObject As New DataObject ' Make a DataObject clipboard
dataObject.SetData(DataFormats.Text, clipBoardText.ToString()) ' Add the data in string format.
Clipboard.SetDataObject(dataObject) ' Copy data to the clipboard.
End If
Return unusedResources
End Function
I use ReSharper for finding unused resource fields and then remove them manually if project contains small amount of resources. Some short script can be used if we already have list of unused items.
The solution is next:
- show all unused members as described in this article
- temporary remove *.Designer.cs from Generated file masks (ReSharper → Options → CodeInspection → GeneratedCode)
- Also comment or remove comment (that indicates that code is auto generated) from top of Designer.cs file attached to resource file.
You will have list of all unused resources, left to remove them from resx.
I've been considering this myself and I believe I have two options. Both of these rely on the fact that I use a helper method to extract the required resource from the resource files.
Logging
Add some code to the "getresource" method or methods so that every time a resource is accessed, the resource key is written to a log. Then try to access every part of the site (a testing script might be helpful here). The resultant log entries should give a list of all the active resource keys, the rest can be junked.Code Analysis
I am looking at whether T4 is capable of working through the solution and creating a list of all references to the "getresource" helper method. The resultant list of keys will be active, the rest can be deleted.
There are limitations of both methods. The logging method is only as good as the code covered by the test and the code analysis might not always find keys rather than strings containg the keys so there will be some extra manual work required there.
I think I'll try both. I'll let you know how it goes.
Rename your current image directory and then create a new one, do a find-in-files search within VS for your image path, i.e. '/content/images', multiselect all the used images and drag them into the new image folder. You can then exclude the old directory from the project, or just delete it.
精彩评论