Пример #1
0
        private static int Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("UpdateProject v0.2 [www.mosa-project.org]");
            Console.WriteLine("Copyright 2010. New BSD License.");
            Console.WriteLine("Written by Philipp Garcia ([email protected])");
            Console.WriteLine();

            if (args.Length < 1) {
                Console.WriteLine("Usage: UpdateProject <options>");
                Console.Error.WriteLine("ERROR: Missing arguments");
                return -1;
            }

            try {
                Options options = new Options(args);

                foreach (string file in options.Files)
                    Transform.Process(options, file);

                if (options.UpdateProjectFiles)
                    foreach (string project in options.Projects)
                        Add.Process(project);

            }
            catch (Exception e) {
                Console.Error.WriteLine("Error: " + e);
                return -1;
            }

            return 0;
        }
Пример #2
0
        /// <summary>
        /// Processes the specified root.
        /// </summary>
        /// <param name="file">The filename.</param>
        /// <param name="root">The root.</param>
        internal static void Process(Options options, string source)
        {
            string filename = Path.GetFileName(source);

            string originalFile = source.Insert(source.Length - 3, ".Original");
            string mosaFile = source.Insert(source.Length - 3, ".Mosa");
            string monoFile = source.Insert(source.Length - 3, ".Internal");

            bool same = CompareFile.Compare(Path.Combine(options.Source, source), Path.Combine(options.Destination, originalFile));

            //Console.WriteLine("# " + filename);

            if (!File.Exists(Path.Combine(options.Source, source)))
            {
                Console.WriteLine("0> " + filename);

                if (options.UpdateOnChange)
                {
                    File.Delete(Path.Combine(options.Destination, source));
                    File.Delete(Path.Combine(options.Destination, originalFile));
                    File.Delete(Path.Combine(options.Destination, mosaFile));
                    File.Delete(Path.Combine(options.Destination, monoFile));
                }

                return;
            }

            string[] lines = File.ReadAllLines(Path.Combine(options.Source, source));

            ClassNode rootNode = new ClassNode();
            List<ClassNode> classNodes = new List<ClassNode>();
            List<MethodNode> methodNodes = new List<MethodNode>();

            List<int> namespaces = new List<int>();
            List<int> usings = new List<int>();

            ClassNode currentNode = rootNode;
            bool incomment = false;

            // Analyze File
            for (int linenbr = 0; linenbr < lines.Length; linenbr++)
            {
                string trim = GetLine(lines, linenbr, ref incomment).Replace('\t', ' ');

                if (incomment)
                    continue;

                trim = StripWithInDoubleQuotes(trim);

                int skip = 0;

                if (string.IsNullOrEmpty(trim))
                    continue;
                else if (trim.StartsWith("#"))
                    continue;
                else if ((trim.StartsWith("extern ") || (trim.Contains(" extern "))) && !trim.Contains(" alias "))
                {
                    int start = GetPreviousBlockEnd(lines, linenbr);
                    skip = GetNumberOfMethodDeclarationLines(lines, linenbr, false);
                    MethodNode node = new MethodNode(currentNode, start, linenbr + skip, linenbr);
                    methodNodes.Add(node);
                    currentNode.Methods.Add(node);
                }
                else if (trim.StartsWith("using ") && trim.Contains(";"))
                {
                    usings.Add(linenbr);

                }
                else if (trim.StartsWith("namespace "))
                {
                    namespaces.Add(linenbr);
                }
                else if (trim.Contains(" class ") || (trim.StartsWith("class ")) ||
                         trim.Contains(" struct ") || (trim.StartsWith("struct ")))
                {
                    // Search backwards for the start of the class definition (might not be on the same line as class keyword)
                    int start = GetPreviousBlockEnd(lines, linenbr);

                    string className = GetClassName(lines, start, linenbr);

                    // Attempt to handle #else class definitions
                    if (className == currentNode.Name)
                    {
                        currentNode.OtherDeclare.Add(linenbr);
                        continue;
                    }

                    // Find the last line of the class
                    int end = GetEndOfScope(lines, linenbr);

                    // Go up to parent
                    while (linenbr > currentNode.End)
                        currentNode = currentNode.Parent;

                    // Child
                    ClassNode child = new ClassNode(currentNode, start, end, linenbr);
                    classNodes.Add(child);
                    currentNode.Children.Add(child);
                    currentNode = child;
                    child.Name = className;
                }

                linenbr = linenbr + skip;

                // Go up to parent
                while (linenbr >= currentNode.End)
                    currentNode = currentNode.Parent;
            }

            // Mark all partial nodes
            foreach (ClassNode node in classNodes)
                if (node.Methods.Count != 0)
                {
                    node.Partial = true;
                    ClassNode upNode = node;
                    do
                    {
                        upNode.Parent.Partial = true;
                        upNode = upNode.Parent;
                    } while (upNode != upNode.Parent);
                }

            Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine(options.Destination, source)));

            if (methodNodes.Count != 0)
            {
                if (options.CreateOriginalFile && !same)
                {
                    Console.WriteLine("1> " + originalFile);
                    File.Copy(Path.Combine(options.Source, source), Path.Combine(options.Destination, originalFile), true);
                }

                ExpandUsing(lines, usings);

                if (options.CreateMosaFile)
                {
                    if (!File.Exists(Path.Combine(options.Destination, mosaFile)) || (options.UpdateOnChange && !same))
                    {
                        Console.WriteLine("2> " + mosaFile);
                        CreatePartialFileForMosa(lines, rootNode, usings, namespaces, Path.Combine(options.Destination, mosaFile));
                    }
                }

                if (options.CreateMonoFile)
                {
                    if (!File.Exists(Path.Combine(options.Destination, monoFile)) || (options.UpdateOnChange && !same))
                    {
                        Console.WriteLine("3> " + monoFile);
                        CreatePartialFileForMono(lines, rootNode, usings, namespaces, Path.Combine(options.Destination, monoFile));
                    }
                }

                if (options.UpdateOnChange && !same)
                {
                    Console.WriteLine("4> " + source);
                    CreateModifiedFile(lines, classNodes, methodNodes, Path.Combine(options.Destination, source));
                }
            }
            else
            {
                same = CompareFile.Compare(Path.Combine(options.Source, source), Path.Combine(options.Destination, source));

                if (options.UpdateOnChange && !same)
                {
                    Console.WriteLine("5> " + source);
                    File.Copy(Path.Combine(options.Source, source), Path.Combine(options.Destination, source), true);
                    File.Delete(Path.Combine(options.Destination, originalFile));
                    File.Delete(Path.Combine(options.Destination, mosaFile));
                    File.Delete(Path.Combine(options.Destination, monoFile));
                }
            }
        }