Example #1
0
    public void Replace_version()
    {
        DoWithCurrentRepo(repo =>
        {
            var result = resolver.ReplaceTokens("%version%", moduleDefinition, repo);

            Assert.AreEqual("1.0.0.0", result);
        });
    }
Example #2
0
    public void Execute()
    {
        SetSearchPath();

        var config = new Configuration(Config);

        LogInfo("Starting search for git repository in " + (config.UseProject ? "ProjectDir" : "SolutionDir"));


        var customAttributes = ModuleDefinition.Assembly.CustomAttributes;

        var gitDir = Repository.Discover(config.UseProject ? ProjectDirectoryPath : SolutionDirectoryPath);

        if (gitDir == null)
        {
            LogWarning("No .git directory found.");
            return;
        }
        LogInfo("Found git repository in " + gitDir);

        dotGitDirExists = true;

        using (var repo = GetRepo(gitDir))
        {
            var branch = repo.Head;

            if (branch.Tip == null)
            {
                LogWarning("No Tip found. Has repo been initialized?");
                return;
            }

            assemblyVersion = ModuleDefinition.Assembly.Name.Version;

            var customAttribute = customAttributes.FirstOrDefault(x => x.AttributeType.Name == "AssemblyInformationalVersionAttribute");
            if (customAttribute != null)
            {
                assemblyInfoVersion = (string)customAttribute.ConstructorArguments[0].Value;
                assemblyInfoVersion = formatStringTokenResolver.ReplaceTokens(assemblyInfoVersion, ModuleDefinition, repo, config.ChangeString);
                VerifyStartsWithVersion(assemblyInfoVersion);
                customAttribute.ConstructorArguments[0] = new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion);
            }
            else
            {
                var versionAttribute = GetVersionAttribute();
                var constructor      = ModuleDefinition.Import(versionAttribute.Methods.First(x => x.IsConstructor));
                customAttribute = new CustomAttribute(constructor);

                assemblyInfoVersion = string.Format("{0} Head:'{1}' Sha:{2}{3}", assemblyVersion, repo.Head.Name, branch.Tip.Sha, repo.IsClean() ? "" : " " + config.ChangeString);

                customAttribute.ConstructorArguments.Add(new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, assemblyInfoVersion));
                customAttributes.Add(customAttribute);
            }
        }
    }
Example #3
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));
    }
 private string CreateAssemblyInfoByReplacingTokens(Repository repo, Configuration config)
 {
     return(_formatStringTokenResolver.ReplaceTokens(_assemblyInfoVersion, ModuleDefinition, repo, config.ChangeString));
 }