Exemplo n.º 1
0
        private static int Main(string[] args)
        {
            if (args == null || args.Length == 0)
            {
                // No arguments
                return(1);
            }

            var version = args.LastOrDefault(p => !string.IsNullOrWhiteSpace(p));

            if (version == null)
            {
                // No version
                return(1);
            }

            var release = args.Any(p => p == "Release");

            var paths = args.Where(p => !string.IsNullOrWhiteSpace(p) && p != version && p != "Release").Distinct().ToArray();

            var documents = new Dictionary <string, XDocument>(paths.Length);

            (var success, var majorVersion, var minorVersion, var buildVersion, var revisionVersion, var versionSuffix) = VersionElementsHelper.GetVersionElementsFromString(version);

            if (!success)
            {
                // Version string not good
                return(2);
            }

            IDirectory directory = new Directory();

            if (paths.Length == 0)
            {
                paths = new[] { directory.GetCurrentDirectory() };
            }

            IFile file = new File();
            IPath path = new Path();

            var service = new FileParserService(file, path, directory);

            var result = service.ProcessPaths(paths, majorVersion, minorVersion, buildVersion, revisionVersion ?? 0, versionSuffix, release);

            if (!result)
            {
                // Specified path - failure
                return(4);
            }

            // Success
            return(0);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Processes the file.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="newMajorVersion">The new major version.</param>
        /// <param name="newMinorVersion">The new minor version.</param>
        /// <param name="newBuildVersion">The new build version.</param>
        /// <param name="newRevisionVersion">The new revision version.</param>
        /// <param name="newVersionSuffix">The new version suffix.</param>
        /// <param name="noRevision">If set to <c>true</c>, there is no revision in the final version.</param>
        /// <returns><c>true</c> if the document has been processed correctly, or <c>false</c> if it doesn't exist, or there is nothing to process.</returns>
        public bool ProcessFile(string fileName, int newMajorVersion, int newMinorVersion, int newBuildVersion, int newRevisionVersion = 0, string newVersionSuffix = null, bool noRevision = false)
        {
            if (!this.file.Exists(fileName))
            {
                return(false);
            }

            XDocument document;

            try
            {
                using (global::System.IO.Stream s = this.file.OpenRead(fileName))
                {
                    document = XDocument.Load(s, LoadOptions.PreserveWhitespace);
                }
            }
            catch
            {
                return(false);
            }

            XElement root = document.Root;

            if (root == null)
            {
                return(false);
            }

            XElement missingContainer = root.Elements().Where(p => p.Name.LocalName.CurrentCultureEquals("metadata")).FirstOrDefault();

            if (missingContainer == null)
            {
                missingContainer = new XElement("metadata");
                root.Add(missingContainer);
            }

            (var releaseVersion, var packageVersion, var fileVersion, var assemblyVersion)
                = VersionElementsHelper.VersionStrings(newMajorVersion, newMinorVersion, newBuildVersion, newRevisionVersion, newVersionSuffix, noRevision);

            IEnumerable <XElement> xVersions = missingContainer.Elements().Where(p => p.Name.LocalName.CurrentCultureEquals("version"));

            EnsureCorrectOnlyOneVersion(
                xVersions,
                packageVersion,
                "version");

            void EnsureCorrectOnlyOneVersion(IEnumerable <XElement> xElements, string correctVersion, string versionName)
            {
                XElement xElement = xElements.FirstOrDefault();

                if (xElement == null)
                {
                    xElement = new XElement(versionName);

                    missingContainer.Add(xElement);
                }

                xElement.SetValue(correctVersion);

                xElements.Where(p => p != xElement).ForEach(p => p.Remove());
            }

            using (global::System.IO.Stream s = this.file.Create(fileName))
            {
                document.Save(s);
            }

            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Processes the file.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="newMajorVersion">The new major version.</param>
        /// <param name="newMinorVersion">The new minor version.</param>
        /// <param name="newBuildVersion">The new build version.</param>
        /// <param name="newRevisionVersion">The new revision version.</param>
        /// <param name="newVersionSuffix">The new version suffix.</param>
        /// <param name="noRevision">If set to <c>true</c>, there is no revision in the final version.</param>
        /// <returns><c>true</c> if the document has been processed correctly, or <c>false</c> if it doesn't exist, or there is nothing to process.</returns>
        public bool ProcessFile(string fileName, int newMajorVersion, int newMinorVersion, int newBuildVersion, int newRevisionVersion = 0, string newVersionSuffix = null, bool noRevision = false)
        {
            if (!this.file.Exists(fileName))
            {
                return(false);
            }

            XDocument document;

            try
            {
                using (global::System.IO.Stream s = this.file.OpenRead(fileName))
                {
                    document = XDocument.Load(s, LoadOptions.PreserveWhitespace);
                }
            }
            catch
            {
                return(false);
            }

            XElement root = document.Root;

            if (root == null)
            {
                return(false);
            }

            XElement missingContainer = null;

            (var releaseVersion, var packageVersion, var fileVersion, var assemblyVersion)
                = VersionElementsHelper.VersionStrings(newMajorVersion, newMinorVersion, newBuildVersion, newRevisionVersion, newVersionSuffix, noRevision);

            var isCore = root.Attribute("Sdk")?.Value?.InvariantCultureEqualsInsensitive("Microsoft.NET.Sdk") ?? false;

            if (isCore)
            {
                IEnumerable <XElement> xVersions = root.Descendants("PropertyGroup").Descendants("Version");
                EnsureCorrectOnlyOneVersion(
                    missingContainer,
                    root,
                    xVersions,
                    packageVersion,
                    "Version");

                IEnumerable <XElement> xFileVersions = root.Descendants("PropertyGroup").Descendants("FileVersion");
                EnsureCorrectOnlyOneVersion(
                    missingContainer,
                    root,
                    xFileVersions,
                    fileVersion,
                    "FileVersion");

                IEnumerable <XElement> xAssemblyVersions = root.Descendants("PropertyGroup").Descendants("AssemblyVersion");
                EnsureCorrectOnlyOneVersion(
                    missingContainer,
                    root,
                    xAssemblyVersions,
                    assemblyVersion,
                    "AssemblyVersion");

                void EnsureCorrectOnlyOneVersion(XElement missingElementContainerBase, in XElement rootElementContainer, in IEnumerable <XElement> xElements, in string correctVersion, in string versionName)
                {
                    XElement xElement = xElements.FirstOrDefault();

                    if (xElement == null)
                    {
                        EnsureMissingContainerExists(missingElementContainerBase, rootElementContainer);

                        xElement = new XElement(versionName);

                        missingElementContainerBase.Add(xElement);

                        void EnsureMissingContainerExists(XElement missingElementContainer, in XElement rootContainer)
                        {
                            if (missingElementContainer != null)
                            {
                                return;
                            }

                            missingElementContainer = new XElement("PropertyGroup");

                            rootContainer.Add(missingElementContainer);
                        }
                    }

                    xElement.SetValue(correctVersion);

                    xElements.Where(p => p != xElement).ForEach(p => p.Remove());
                }

                using (global::System.IO.Stream s = this.file.Create(fileName))
                {
                    document.Save(s);
                }

                return(true);
            }
            else
            {
                var folderPath = this.path.GetDirectoryName(fileName);

                var foundAssemblyAttributes = false;

                this.directory
                .EnumerateFilesRecursively(folderPath, "*.cs")
#if NETSTANDARD1_0
                .ForEach(filePath =>
#else
                .ParallelForEach(filePath =>
#endif
                {
                    var lines = new List <string>();
                    var found = false;
                    using (global::System.IO.StreamReader handle = this.file.OpenText(filePath))
                    {
                        while (!handle.EndOfStream)
                        {
                            var line = handle.ReadLine();

                            if (!string.IsNullOrWhiteSpace(line))
                            {
                                Match versionMatch = AssemblyVersionRegex.Match(line);
                                if (versionMatch.Success)
                                {
                                    found = true;

                                    lines.Add($"[assembly: global::System.Reflection.AssemblyVersion(\"{assemblyVersion}\")]");
                                }
                                else
                                {
                                    Match assemblyVersionMatch = AssemblyFileVersionRegex.Match(line);
                                    if (assemblyVersionMatch.Success)
                                    {
                                        found = true;

                                        lines.Add($"[assembly: global::System.Reflection.AssemblyFileVersion(\"{fileVersion}\")]");
                                    }
                                    else
                                    {
                                        lines.Add(line);
                                    }
                                }
                            }
                            else
                            {
                                lines.Add(string.Empty);
                            }
                        }
                    }

                    if (found)
                    {
                        var finalText = string.Join(Environment.NewLine, lines);
                        using (global::System.IO.StreamWriter writeHandle = this.file.CreateText(filePath))
                        {
                            writeHandle.Write(finalText);
                        }

                        foundAssemblyAttributes = true;
                    }

                    lines.Clear();
                });

                return(foundAssemblyAttributes);
            }
        }