Esempio n. 1
0
    public void Execute()
    {
        if (SvnHelper == null)
        {
            SvnHelper = new SvnHelper();
        }

        var customAttributes = ModuleDefinition.Assembly.CustomAttributes;

        var svnDir = SvnHelper.TreeWalkForSvnDir(SolutionDirectoryPath);

        if (svnDir == null)
        {
            LogWarning("No .svn directory found.");
            return;
        }

        dotSvnDirExists = true;

        VersionInfo ver = null;

        try
        {
            ver = SvnHelper.GetSvnInfo(svnDir);
        }
        catch (Exception ex)
        {
            LogWarning("GetSvnInfo error: " + ex.ToString());
        }

        LogInfo("svnInfo found.");

        assemblyVersion = ModuleDefinition.Assembly.Name.Version;

        assemblyVersionReplaced = ReplaceVersion3rd(assemblyVersion.ToString(), ver.Revision);

        /* AssemblyVersionAttribute */
        var customAttribute = customAttributes.FirstOrDefault(x => x.AttributeType.Name == "AssemblyVersionAttribute");

        if (customAttribute != null)
        {
            assemblyInfoVersion = (string)customAttribute.ConstructorArguments[0].Value;
            assemblyInfoVersion = ReplaceVersion3rd(assemblyInfoVersion, ver.Revision);
            VerifyStartsWithVersion(assemblyInfoVersion);
            customAttribute.ConstructorArguments[0] = new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion);

            ModuleDefinition.Assembly.Name.Version = new Version(assemblyInfoVersion);
        }
        else
        {
            var versionAttribute = GetVersionAttribute();

            if (versionAttribute == null)
            {
                throw new InvalidOperationException("versionAttribute not found.");
            }

            var constructor = ModuleDefinition.Import(versionAttribute.Methods.First(x => x.IsConstructor));

            customAttribute = new CustomAttribute(constructor);

            assemblyInfoVersion = customAttribute.ConstructorArguments.Count == 1
                ? (string)customAttribute.ConstructorArguments[0].Value
                : assemblyVersion.ToString();

            assemblyInfoVersion = ReplaceVersion3rd(assemblyInfoVersion, ver.Revision);

            ModuleDefinition.Assembly.Name.Version = new Version(assemblyInfoVersion);
        }
        LogInfo("AssemblyVersionAttribute processed.");

        /* AssemblyFileVersionAttribute */
        customAttribute = customAttributes.FirstOrDefault(x => x.AttributeType.Name == "AssemblyFileVersionAttribute");
        if (customAttribute != null)
        {
            assemblyInfoVersion = (string)customAttribute.ConstructorArguments[0].Value;
            assemblyInfoVersion = ReplaceVersion3rd(assemblyInfoVersion, ver.Revision);
            VerifyStartsWithVersion(assemblyInfoVersion);
            customAttribute.ConstructorArguments[0] = new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion);
        }
        else
        {
            var versionAttribute = GetFileVersionAttribute();
            var constructor      = ModuleDefinition.Import(versionAttribute.Methods.First(x => x.IsConstructor));
            customAttribute = new CustomAttribute(constructor);

            assemblyInfoVersion = customAttribute.ConstructorArguments.Count == 1
                ? (string)customAttribute.ConstructorArguments[0].Value
                : assemblyVersion.ToString();

            assemblyInfoVersion = ReplaceVersion3rd(assemblyInfoVersion, ver.Revision);

            customAttribute.ConstructorArguments.Add(new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion));
            customAttributes.Add(customAttribute);
        }
        LogInfo("AssemblyFileVersionAttribute processed.");

        if (true)
        {
            customAttribute     = new CustomAttribute(ModuleDefinition.Import(GetInformationalVersionAttribute().Methods.First(x => x.IsConstructor)));
            assemblyInfoVersion = AttributeHack(assemblyVersionReplaced, ver);
            customAttribute.ConstructorArguments.Add(new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion));
            customAttributes.Add(customAttribute);
        }
        else
        {
            /* AssemblyInformationalVersionAttribute */
            customAttribute = customAttributes.FirstOrDefault(x => x.AttributeType.Name == "AssemblyInformationalVersionAttribute");
            if (customAttribute != null)
            {
                assemblyInfoVersion = (string)customAttribute.ConstructorArguments[0].Value;
                assemblyInfoVersion = formatStringTokenResolver.ReplaceTokens(assemblyInfoVersion, ModuleDefinition, ver);
                VerifyStartsWithVersion(assemblyInfoVersion);
                customAttribute.ConstructorArguments[0] = new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion);
            }
            else
            {
                var versionAttribute = GetInformationalVersionAttribute();
                var constructor      = ModuleDefinition.Import(versionAttribute.Methods.First(x => x.IsConstructor));
                customAttribute = new CustomAttribute(constructor);
                if (!ver.HasChanges)
                {
                    assemblyInfoVersion = string.Format("{0} {1} Path:'{2}' r{3}",
                                                        assemblyVersionReplaced,
                                                        Environment.MachineName,
                                                        ver.BranchName,
                                                        ver.Revision);
                }
                else
                {
                    assemblyInfoVersion = string.Format("{0} {1} Path:'{2}' r{3} HasChanges",
                                                        assemblyVersionReplaced,
                                                        Environment.MachineName,
                                                        ver.BranchName,
                                                        ver.Revision);
                }
                customAttribute.ConstructorArguments.Add(new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion));
                customAttributes.Add(customAttribute);
            }
        }
        LogInfo(string.Format("AssemblyInformationalVersionAttribute processed: {0}", assemblyInfoVersion));
    }
Esempio n. 2
0
    public void Execute()
    {
        if (SvnHelper == null)
        {
            SvnHelper = new SvnHelper();
        }

        var customAttributes = ModuleDefinition.Assembly.CustomAttributes;

        var svnDir = SvnHelper.TreeWalkForSvnDir(SolutionDirectoryPath);
        if (svnDir == null)
        {
            LogWarning("No .svn directory found.");
            return;
        }

        dotSvnDirExists = true;

        VersionInfo ver = null;
        try
        {
            ver = SvnHelper.GetSvnInfo(svnDir);
        }
        catch (Exception ex)
        {
            LogWarning("GetSvnInfo error: " + ex.ToString());
        }

        LogInfo("svnInfo found.");

        assemblyVersion = ModuleDefinition.Assembly.Name.Version;

        assemblyVersionReplaced = ReplaceVersion3rd(assemblyVersion.ToString(), ver.Revision);

        /* AssemblyVersionAttribute */
        var customAttribute = customAttributes.FirstOrDefault(x => x.AttributeType.Name == "AssemblyVersionAttribute");
        if (customAttribute != null)
        {
            assemblyInfoVersion = (string)customAttribute.ConstructorArguments[0].Value;
            assemblyInfoVersion = ReplaceVersion3rd(assemblyInfoVersion, ver.Revision);
            VerifyStartsWithVersion(assemblyInfoVersion);
            customAttribute.ConstructorArguments[0] = new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion);

            ModuleDefinition.Assembly.Name.Version = new Version(assemblyInfoVersion);
        }
        else
        {
            var versionAttribute = GetVersionAttribute();

            if (versionAttribute == null)
            {
                throw new InvalidOperationException("versionAttribute not found.");
            }

            var constructor = ModuleDefinition.Import(versionAttribute.Methods.First(x => x.IsConstructor));

            customAttribute = new CustomAttribute(constructor);

            assemblyInfoVersion = customAttribute.ConstructorArguments.Count == 1
                ? (string)customAttribute.ConstructorArguments[0].Value
                : assemblyVersion.ToString();

            assemblyInfoVersion = ReplaceVersion3rd(assemblyInfoVersion, ver.Revision);

            ModuleDefinition.Assembly.Name.Version = new Version(assemblyInfoVersion);
        }
        LogInfo("AssemblyVersionAttribute processed.");

        /* AssemblyFileVersionAttribute */
        customAttribute = customAttributes.FirstOrDefault(x => x.AttributeType.Name == "AssemblyFileVersionAttribute");
        if (customAttribute != null)
        {
            assemblyInfoVersion = (string)customAttribute.ConstructorArguments[0].Value;
            assemblyInfoVersion = ReplaceVersion3rd(assemblyInfoVersion, ver.Revision);
            VerifyStartsWithVersion(assemblyInfoVersion);
            customAttribute.ConstructorArguments[0] = new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion);
        }
        else
        {
            var versionAttribute = GetFileVersionAttribute();
            var constructor = ModuleDefinition.Import(versionAttribute.Methods.First(x => x.IsConstructor));
            customAttribute = new CustomAttribute(constructor);

            assemblyInfoVersion = customAttribute.ConstructorArguments.Count == 1
                ? (string)customAttribute.ConstructorArguments[0].Value
                : assemblyVersion.ToString();

            assemblyInfoVersion = ReplaceVersion3rd(assemblyInfoVersion, ver.Revision);

            customAttribute.ConstructorArguments.Add(new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion));
            customAttributes.Add(customAttribute);
        }
        LogInfo("AssemblyFileVersionAttribute processed.");

        /* AssemblyInformationalVersionAttribute */
        customAttribute = customAttributes.FirstOrDefault(x => x.AttributeType.Name == "AssemblyInformationalVersionAttribute");
        if (customAttribute != null)
        {
            assemblyInfoVersion = (string)customAttribute.ConstructorArguments[0].Value;
            assemblyInfoVersion = formatStringTokenResolver.ReplaceTokens(assemblyInfoVersion, ModuleDefinition, ver);
            VerifyStartsWithVersion(assemblyInfoVersion);
            customAttribute.ConstructorArguments[0] = new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion);
        }
        else
        {
            var versionAttribute = GetInformationalVersionAttribute();
            var constructor = ModuleDefinition.Import(versionAttribute.Methods.First(x => x.IsConstructor));
            customAttribute = new CustomAttribute(constructor);
            if (!ver.HasChanges)
            {
                assemblyInfoVersion = string.Format("{0} {1} Path:'{2}' r{3}",
                    assemblyVersionReplaced,
                    Environment.MachineName,
                    ver.BranchName,
                    ver.Revision);
            }
            else
            {
                assemblyInfoVersion = string.Format("{0} {1} Path:'{2}' r{3} HasChanges",
                    assemblyVersionReplaced,
                    Environment.MachineName,
                    ver.BranchName,
                    ver.Revision);
            }
            customAttribute.ConstructorArguments.Add(new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion));
            customAttributes.Add(customAttribute);
        }

        LogInfo(string.Format("AssemblyInformationalVersionAttribute processed: {0}", assemblyInfoVersion));
    }