Last active
August 29, 2015 14:23
-
-
Save timvw/9f0848b3400530561081 to your computer and use it in GitHub Desktop.
Update AssemblyVersion and AssemblyFileVersions (in fsharp and scala)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| open System | |
| open System.IO | |
| open System.Text.RegularExpressions | |
| let getAssemblyVersionFiles path = | |
| Directory.GetFiles(path, "AssemblyVersions.cs", SearchOption.AllDirectories) | |
| let assemblyVersionPattern = "AssemblyVersion\(\"(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+).(?<build>\d+)\"\)" | |
| let assemblyFileVersionPattern = "AssemblyFileVersion\(\"(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+).(?<build>\d+)\"\)" | |
| let getVersion versionPattern text = | |
| let matched = Regex.Match(text, versionPattern) | |
| if matched.Success then | |
| let major = Int32.Parse(matched.Groups.Item("major").Value) | |
| let minor = Int32.Parse(matched.Groups.Item("minor").Value) | |
| let patch = Int32.Parse(matched.Groups.Item("patch").Value) | |
| let build = Int32.Parse(matched.Groups.Item("build").Value) | |
| Some(major, minor, patch, build) | |
| else | |
| None | |
| let getAssemblyVersion = getVersion assemblyVersionPattern | |
| let getAssemblyFileVersion = getVersion assemblyFileVersionPattern | |
| let incMajor (major, minor, patch, build) = (major+1, minor, patch, build) | |
| let incMinor (major, minor, patch, build) = (major, minor+1, patch, build) | |
| let incPatch (major, minor, patch, build) = (major, minor, patch+1, build) | |
| let buildAssemblyVersion (major, minor, patch, build) text = | |
| let replacement = sprintf "AssemblyVersion(\"%i.%i.%i.%i\")" major minor patch build | |
| Regex.Replace(text, assemblyVersionPattern,replacement) | |
| let buildAssemblyFileVersion (major, minor, patch, build) text = | |
| let replacement = sprintf "AssemblyFileVersion(\"%i.%i.%i.%i\")" major minor patch build | |
| Regex.Replace(text, assemblyFileVersionPattern,replacement) | |
| let replace matchFn replaceFn text = | |
| match text |> matchFn with | |
| | Some matched -> replaceFn matched text | |
| | None -> text | |
| let replaceVersions replaceVersionFn text = | |
| text | |
| |> replace getAssemblyVersion (replaceVersionFn >> buildAssemblyVersion) | |
| |> replace getAssemblyFileVersion (replaceVersionFn >> buildAssemblyFileVersion) | |
| let bumpMajor = replaceVersions incMajor | |
| let bumpMinor = replaceVersions incMinor | |
| let bumpPatch = replaceVersions incPatch | |
| let readFileText file = File.ReadAllText file | |
| let writeFileText file contents = File.WriteAllText(file,contents) | |
| "c:\src" | |
| |> getAssemblyVersionFiles | |
| |> Seq.iter(fun x -> x |> (File.ReadAllText >> bumpPatch >> writeFileText x)) | |
| printfn "Press any key to continue." | |
| Console.ReadKey() |> ignore; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.tools.nsc.io._ | |
| def getAssemblyVersionFiles(path: String) = | |
| Directory(path) | |
| .deepFiles | |
| .filter(x => "AssemblyInfo.cs".r findFirstIn(x.name) isDefined) | |
| def incMajor (major: Int, minor: Int, patch: Int, build: Int) = (major+1, minor, patch, build) | |
| def incMinor (major: Int, minor: Int, patch: Int, build: Int) = (major, minor+1, patch, build) | |
| def incPatch (major: Int, minor: Int, patch: Int, build: Int) = (major, minor, patch+1, build) | |
| def replaceVersions (replaceVersionFn: (Int,Int,Int,Int) => (Int,Int,Int,Int)) (text: String) = { | |
| def getVersionString(attribute: String)(major: Int, minor: Int, patch: Int, build: Int) = s"""$attribute("$major.$minor.$patch.$build")""" | |
| def replaceVersion (attribute: String)( text: String) = { | |
| val attributeAndVersionRegex = (s"""(?<attribute>$attribute)""" + """\("(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+).(?<build>[\d+|\*])"\)""").r | |
| attributeAndVersionRegex replaceAllIn(text, _ match { | |
| case attributeAndVersionRegex(attribute, major, minor, patch, build) => (getVersionString(attribute) _).tupled(replaceVersionFn(major.toInt, minor.toInt, patch.toInt, build.toInt)) | |
| }) | |
| } | |
| def replaceAssemblyVersion = replaceVersion("AssemblyVersion") _ | |
| def replaceAssemblyFileVersion = replaceVersion("AssemblyFileVersion") _ | |
| (replaceAssemblyVersion andThen replaceAssemblyFileVersion)(text) | |
| } | |
| def bumpMajor = replaceVersions (incMajor) _ | |
| def bumpMinor = replaceVersions (incMinor) _ | |
| def bumpPatch = replaceVersions (incPatch) _ | |
| def readFileText(file: File) = file.slurp() | |
| def writeFileText(file: File, content: String) = println(content)//file writeAll content | |
| def processFile(file: File) = writeFileText(file, (readFileText _ andThen bumpMajor)(file)) | |
| getAssemblyVersionFiles("/Users/timvw/src/keepass/") | |
| .foreach(processFile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment