All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ullink.AssemblyInfoVersionPatcher.groovy Maven / Gradle / Ivy

There is a newer version: 2.22
Show newest version
package com.ullink

import org.apache.commons.io.FilenameUtils
import org.gradle.api.file.FileCollection
import org.gradle.api.internal.ConventionTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputFiles
import org.gradle.api.tasks.TaskAction

class AssemblyInfoVersionPatcher extends ConventionTask {
    def files
    List projects = []

    AssemblyInfoVersionPatcher() {
        conventionMapping.map "projects", { [ project.tasks.msbuild.mainProject?.projectName ] }
        conventionMapping.map "files", {
            getProjects()
                .collect { project.tasks.msbuild.projects[it] }
                .collect {
                    it?.getItems('Compile').find { FilenameUtils.getBaseName(it.name) == 'AssemblyInfo' }
                }
        }
        conventionMapping.map "fileVersion", { version }
        conventionMapping.map "informationalVersion", { version }

        project.afterEvaluate {
            if (!version) return;
            project.tasks.withType(Msbuild) { task ->
                task.projects.each { proj ->
                    if (proj.value.getItems('Compile').intersect(getFiles())) {
                        task.dependsOn this
                    }
                }
            }
        }
    }

    @Input
    def version

    @Input
    def fileVersion

    @Input
    def informationalVersion

    @Input
    def charset = "UTF-8"
    
    @InputFiles
    @OutputFiles
    FileCollection getPatchedFiles() {
        project.files(getFiles())
    }

    @TaskAction
    void run() {
        getPatchedFiles().each {
            logger.info("Replacing version attributes in $it")
            replace(it, 'AssemblyVersion', getVersion())
            replace(it, 'AssemblyFileVersion', getFileVersion())
            replace(it, 'AssemblyInformationalVersion', getInformationalVersion())
        }
    }

    void replace(def file, def name, def value) {
        if (FilenameUtils.getExtension(file.name) == 'fs')
            project.ant.replaceregexp(file: file, match: /^\[\]$/, replace: "[]", byline: true, encoding: charset)
        else
            project.ant.replaceregexp(file: file, match: /^\[assembly: $name\s*\(".*"\)\s*\]$/, replace: "[assembly: ${name}(\"${value}\")]", byline: true, encoding: charset)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy