Batch rename my classes to change my project prefix? - xcode

I've created a project and built out a several model classes. I've decided that I'd like to recycle them as a separate framework. In doing so, I want to change my class prefix of those files. The issue is, of course, that I now have to batch rename my files and my classes. I could use the refactor tool, but I have tens of classes, so it would be tedious. Is there a good way to automate the process? For reference, I'm using Xcode 5.

For anyone looking for an answer, I think I have a solution, although it's something I'd like to see built in to Xcode. I've filed a radar.(#15261680)
I had three things to worry about here:
The class names themselves. The class prefixes didn't match the framework that I wanted to put them in.
The file names. Even if I did a careful global search and replace and changed the class prefixes, the file names would be wrong.
The Xcode project references. Xcode keeps a list of your files, and I had to be sure that Xcode would know where to find my files, in the event that I renamed them.
What I did:
I did a global search and replace. Instead of clicking "Replace All", I used the "Review" pane, so I was able to exclude about ten or so cases that I wanted to keep with the original prefix.
I found a utility to do a batch rename. Now my files and classes all match nicely.
Uh oh! Xcode doesn't know where my files are, now. To fix this, I opened the .pbxproj file and did another careful search and replace. (I made sure to quit Xcode first, so my changes wouldn't be overwritten automatically.)
This was a bit of an involved process, and I was hoping that there was a tool or Xcode feature to help me do this, but alas, not yet.

Related

Can I batch edit Xcode search scopes?

Xcode has this feature that allows to create search scopes to determine the files that will be considered when Cmd+F something.
I have created a bunch of scopes for different scenarios (eg. only interfaces, only implementation files, only files within a particular feature, only tests, etc).
But now I would like to make some changes and don't want to click around on each scope.
Is there any way to manually edit this as a text file or similar?
As it turns out, it is possible.
The search scopes are stored in a file named IDEFindNavigatorScopes.plist . This file can be found either in ~/Library/Developer/Xcode/UserData/ or in the xcuserdata of your workspace, such as FindNavigator.xcodeproj/project.xcworkspace/xcuserdata/.xcuserdatad/
https://patrickbalestra.com/blog/2020/02/09/xcode-find-navigator.html

Why does adding a solution folder in Visual Studio change project files, and is there any way to prevent this?

I am adding a test project to many solutions in our project's primary git repository.
In the process, I noticed that adding a "Solution Folder" to the .sln file modifies many of the projects in the solution. I can think of no logical reason this would be the case. I am only talking about adding the solution file, before I add the test project.
All of the projects in the solution are C# projects, if that would make any difference.
Does anybody know why this is the case. Is this intended behavior, or a bug? Also, is there any way to prevent this from happening?
The two images below highlight the behavior I am talking about:
Before adding the new solution folder:
After adding the new solution folder:
As far as I can tell, the changes are meaningless:
It's a bit weird, but these things happen when people have multiple solutions and fix the casing in one Solution, but not the other. When the project file is re-rendered (because a project is added, renamed, solution item changed etc), then it will pick up the corrected casing. If the solution file has the 'correct' casing, nothing may change, but if the solution file mismatches it can cause this cascade.
Fixing the casing in one solution may trigger the reverse behavior in another solution. Hence, this must be fixed in all solution files at once. Be careful if you have multiple branches.
The best way to fix this is to fix the casing issues on the file system, project files and solution files in a single go. Doing it with a text editor is usually easier than through the Visual Studio project system. Regex search&replace can do wonders here. Make sure you fix all of these at once:
The contents of (all of) the solution file(s)
The .*proj file causing the problem
The file system path (you may have to change more than the case first for the case change to take). Also make sure your Version Control system will take the change.
The project references in other .*proj files
When the casing of a single project is fixed, the change may cascade to other project files that reference the problematic project. The ProjectReference element in the project file has a relative file system path to the problematic project and also captures its name. You can see this clearly in the screenshot you posted:

Delete unused ressources from Localizable.strings

I'm using BartyCrouch to maintain an up-to-date localized project. My project is currently translated in 4 different languages, and I'm going to add some new languages. My issue is that my base Localizable.strings file has about 200-300 resources, even though there are probably less than 100 currently used in the project.
I'm looking for the best way to remove the un-referenced localized strings in the project, all while keeping the currently localized values.
Is there any native solution or do I have to use a third party? And if so, is there anything automatic?
Well, I definitely didn't look hard enough. I think it's worth posting an answer instead of deleting the question because I don't think the answer is on Stackoverflow yet.
https://github.com/ijoshsmith/abandoned-strings
This little gem will do exactly this. Easy to use. Simply download the zip file, use the Terminal to go into the "AbandonedStrings" folder containing a single "main.swift" file.
Then, on the terminal, enter: ./main.swift /the/path/to/your/project
This will display a list of all the unused ressources.
Then, if you also want the script to delete them, simply add "write" at the end of the command.
Watch out though: it will delete all the Storyboard localized ressources, so commit your project before and simply discard the changes.

Visual Studio Solution -- Any way to create a "special" folder?

Basically, I want one of my folders to appear above the other folders as a type of "special folder", similar to how Properties has it's own special place even though it's a folder, same with App_Data, etc.
Is this possible?
By default, Visual Studio doesn't support adding special project folders. The Properties folder is hard-coded to behave the way that it does.
However, anything is possible with code. You could build an extension to do this, but it wouldn't be simple. You'd probably need to mess around with the IVsHierarchy or even implement a project subtype.
Basically, I want one of my folders to
appear above the other folders as a
type of "special folder", similar to
how Properties has it's own special
place even though it's a folder, same
with App_Data, etc.
Is this possible?
Yes:
Do it manually through the IDE
Write your own script to
generate/modify your *.sln/*.vcproj
For (1) "manual" on solutions in the IDE: Solution Explorer, right-click on Solution node==>Add==>New Solution Folder.
While typically the folders are sorted alphabetically (I'd insert a leading underscore to force your special folder to the top), solution folders inserted manually on my MSVS2008 leave the new folder "at the top", even though it should have bumped down when alphabetically sorted. However, folders under a Project (which are called "Filters") are always sorted alphabetically, and added similarly from the right-click, and then you can modify their "filter properties" with file name globs for what you want in there (e.g., add a filter glob for "*.MY_EXTENSION1;*.MY_EXTENSION2").
We chose (2), and we generate our own *.sln and *.vcproj, adding our own folders/filters. I've not seen any utilities on the web to help with that (so we had to write our own). The formats are not too hard to reverse engineer, but it's largely undocumented XML, so you have to experiment. There are only a couple good web articles explaining what's in the file, like this one:
http://tim.oreilly.com/pub/a/dotnet/excerpt/vshacks_chap1/index.html?page=4
On the "bright side", the files are only XML, so in developing our scripts we merely made changes through the IDE, saved, and compared the "diffs" for what change we want. Those changes are what our scripts insert when we modify our files. So, if you modify the file manually, you can similarly just "diff" the file to see what changed, and make your own script. (IMHO, this is the fastest and easiest route, since tools generally do not exist to manipulate these files.)
Tools like CMake and QMake generate *.vcproj/*.sln, but don't really do the folder customization thing like you're talking. However, we look at their output too, because, "there's more than one way to do things" in these files, and the files seem to have many undocumented features for doing different clever things that somehow these tools have "discovered" (so you can try to copy their generated output).
We found the .NET APIs to work with these files as too much work, and not really designed for that type of manipulation, but YMMV.
VS 2012 has a feature that I just found, and it solved this problem for me. It may not be new to VS.
Create a folder under the project with a leading "_" (to get it sorted first).
On the folder's properties set "Namespace Provider" to false.
VS (or ReSharper?) code analysis then does not complain that "the namespace does not match file location", which was the source of irritation for me that would otherwise have kept me from going this route.
Although there is no easy way to add Custom Folder, there is an easy way to "steal" Properties custom folder.
Add a regular folder to the project. For example MyCustomerFolder.
Open proj file xml. Find line
<AppDesignerFolder>Properties</AppDesignerFolder>
replace with
<AppDesignerFolder>MyCustomFolder</AppDesignerFolder>
Reload the project.
Now you've got a custom folder, that will always stick to the top.

Rename a batch of Class Objects in VS

I have a VisualStudio project with a few sub folders like Accounting, Store etc. I need to make a duplicate of the "Accounting" folder and also rename all the Class Objects in that folder. Each object in that folder is prefixed with "Accounting".
Basically I want to name the duplicate "Customer" and change all the prefix of the Class Objects to Customer..
What's the fastest way in doing this?
The fastest way to do this is probably the Unix command line tools. (On Windows, you can install, e.g., MSys or Cygwin to use them.) If you haven't done this, however, it will take quite a while to set this up and get familiar with them.
Here on my Windows box I would probably use a combination of TotalCommander (for file copying and batch file renaming) and VS itself (for batch identifier replacement).
I have, however, never come into the situation to do this, since, in general, I shy away from code duplication. Have you asked yourself why you're doing this and whether there's a better way? If the code duplication is so massive that it's worth the time asking us how to do it the fastest way, this isn't just a code smell. That's a dead skunk decaying under your pillow.
I'd suggest that you copy the folders using Windows Explorer and use a tool like notepad++ or TextPad to rename multiple files.
You can use a macro to rename the classes and the file names. If the solution is bound to Visual Source Safe, it will pickup the file rename. More info and code example on http://tseonet.blogspot.com/2010/12/using-macros-in-visual-studio-to-rename.html.

Resources