patterncsharpModerate
Increment up the build number in AssemblyInfo.cs on every build
Viewed 0 times
incrementnumbertheeverybuildassemblyinfo
Problem
I just wrote this short little program to increment up the build number for my projects every time I build them.
After compiling this exe, I just call it in the pre-build command line.
I did take out the filename string because I used it thrice, and I figured I'd get some flack if I didn't.
I realize this is complex, and the average person wont be able to read it. I use magic numbers, and I do some things specifically to keep the glory on 1 line. Feel free to bask in the presence of psychedelic code (which just wants to be your friend).
After compiling this exe, I just call it in the pre-build command line.
I did take out the filename string because I used it thrice, and I figured I'd get some flack if I didn't.
static void Main()
{
int num;
string file = "AssemblyInfo.cs";
if (File.Exists(file))
File.WriteAllLines(file, File.ReadAllLines(file).Select(s => !s.Trim().StartsWith("[assembly: AssemblyFileVersion(") ? s : string.Join(".", s.Trim(']').Trim(')').Trim('"').Split('.').Select((n, i) => i != 3 ? n : !int.TryParse(n, out num) ? n : (num + 1).ToString())) + "\")]"));
}I realize this is complex, and the average person wont be able to read it. I use magic numbers, and I do some things specifically to keep the glory on 1 line. Feel free to bask in the presence of psychedelic code (which just wants to be your friend).
Solution
Why not use
After taking into account delegates used with
Still only one semi-colon, but at least it's possible to look at it and recognize each piece for what it does. Note I split the parentheses onto separate lines here for parameter readability, but some programmers disagree with this style of line-breaking.
It basically works similar to your original solution, but it basically finds and replaces the
Regex.Match instead of using those confusing joins/splits? Unless you are purposely trying to code golf this, doing it using regular expression is much easier to follow logically.After taking into account delegates used with
Regex.Replace (as explained in svick's comment), here is a functionally superior solution to the previous answer I posted. This eliminates much of the unnecessary lambda pieces. The original solution is arguably not a good use of LINQ because of the changes being made to the collection (ie. not a very strong use of queries, despite lambdas having that functionality).static void Main()
{
string file = "AssemblyInfo.cs";
if (File.Exists(file))
File.WriteAllText(file,
Regex.Replace(
File.ReadAllText(file),
@"(? (Convert.ToInt16(m.Value) + 1).ToString()
)
);
}
}Still only one semi-colon, but at least it's possible to look at it and recognize each piece for what it does. Note I split the parentheses onto separate lines here for parameter readability, but some programmers disagree with this style of line-breaking.
It basically works similar to your original solution, but it basically finds and replaces the
Regex.Match with an incremented value.Code Snippets
static void Main()
{
string file = "AssemblyInfo.cs";
if (File.Exists(file))
File.WriteAllText(file,
Regex.Replace(
File.ReadAllText(file),
@"(?<=\[assembly: AssemblyFileVersion\(""[0-9]*.[0-9]*.)[0-9]*(?=.[0-9]*""\)\])",
m => (Convert.ToInt16(m.Value) + 1).ToString()
)
);
}
}Context
StackExchange Code Review Q#47900, answer score: 16
Revisions (0)
No revisions yet.