Beispiel #1
0
        private static void ConvertScripts(AssemblyType assemblyType, IList <SourceFile> scripts, UnityScript2CSharpConverter converter, CommandLineArguments args, List <SymbolInfo> referencedSymbols, HashSet <CompilerError> compilerErrors)
        {
            IEnumerable <string> references = AssemblyReferencesFor(args, assemblyType);

            Console.WriteLine("Converting '{0}' ({1} scripts)", assemblyType, scripts.Count);

            if (args.Verbose)
            {
                Console.WriteLine("Referenced assemblies:");
                foreach (var r in references)
                {
                    System.Console.WriteLine($"\t{r}");
                }
            }

            Action <string, string, int> handler = (scriptPath, context, unsupportedCount) => HandleConvertedScript(scriptPath, context, args.RemoveOriginalFiles, args.Verbose, unsupportedCount);

            if (args.DryRun)
            {
                handler = (_, __, ___) => {}
            }
            ;

            converter.Convert(scripts, args.Symbols, references, handler);

            referencedSymbols.AddRange(converter.ReferencedPreProcessorSymbols);
            foreach (var error in converter.CompilerErrors)
            {
                compilerErrors.Add(error);
            }

            using (WithConsoleColors.SetTo(ConsoleColor.Yellow, ConsoleColor.Black))
            {
                foreach (var warning in converter.CompilerWarnings)
                {
                    Console.WriteLine("\t{0}", warning);
                }
            }
        }
Beispiel #2
0
        static int Main(string[] args)
        {
            ParserResult <CommandLineArguments> options = null;

            try
            {
                options = Parser.Default.ParseArguments <CommandLineArguments>(args);
                if (!IsValid(options))
                {
                    return(-2);
                }
            }
            catch
            {
                Console.WriteLine();
                Console.WriteLine("Failed to parse command line arguments. See valid command line arguments below:");

                var h = new HelpText {
                    AddDashesToOption = true, AdditionalNewLineAfterOption = true
                };

                h.AddOptions(new CommandLineArguments());
                Console.WriteLine(h.ToString());

                return(-3);
            }

            RedirectConsoleOutput(options.Value.OutputFile);

            if (!ReadResponseFile(options.Value))
            {
                return(-4);
            }

            try
            {
                options.Value.ProjectPath = Path.GetFullPath(options.Value.ProjectPath);

                // We should ignore scripts in assets/WebGLTemplates
                var ignoredPathsRegex = new Regex(string.Format("assets{0}{0}webgltemplates{0}{0}", Path.DirectorySeparatorChar), RegexOptions.Compiled | RegexOptions.IgnoreCase);

                var editorSubFolder        = String.Format("{0}Editor{0}", Path.DirectorySeparatorChar);
                var pluginsEditorSubFolder = String.Format("{0}Plugins{0}Editor{0}", Path.DirectorySeparatorChar);
                var pluginSubFolder        = String.Format("{0}Plugins{0}", Path.DirectorySeparatorChar);

                var allFiles = Directory.GetFiles(Path.Combine(options.Value.ProjectPath, "Assets"), "*.js", SearchOption.AllDirectories).Where(path => !ignoredPathsRegex.IsMatch(path));
                var filter   = new Regex(string.Format(@"{0}|{1}|{2}", editorSubFolder, pluginSubFolder, pluginsEditorSubFolder).Replace("\\", "\\\\"), RegexOptions.Compiled);

                var runtimeScripts = allFiles.Where(scriptPath => !filter.IsMatch(scriptPath)).Select(scriptPath => new SourceFile {
                    FileName = scriptPath, Contents = File.ReadAllText(scriptPath)
                }).ToArray();
                var editorScripts = allFiles.Where(scriptPath => scriptPath.Contains(editorSubFolder) && !scriptPath.Contains(pluginsEditorSubFolder)).Select(scriptPath => new SourceFile {
                    FileName = scriptPath, Contents = File.ReadAllText(scriptPath)
                }).ToArray();
                var pluginScripts = allFiles.Where(scriptPath => scriptPath.Contains(pluginSubFolder) && !scriptPath.Contains(pluginsEditorSubFolder)).Select(scriptPath => new SourceFile {
                    FileName = scriptPath, Contents = File.ReadAllText(scriptPath)
                }).ToArray();
                var pluginEditorScritps = allFiles.Where(scriptPath => scriptPath.Contains(pluginsEditorSubFolder)).Select(scriptPath => new SourceFile {
                    FileName = scriptPath, Contents = File.ReadAllText(scriptPath)
                }).ToArray();

                if (!ValidateAssemblyReferences(options))
                {
                    return(-1);
                }

                if (options.Value.DumpScripts)
                {
                    DumpScripts("Runtime", runtimeScripts);
                    DumpScripts("Editor", editorScripts);
                    DumpScripts("Plugin", pluginScripts);
                    DumpScripts("Plugin/Editor", pluginEditorScritps);
                }

                var converter = new UnityScript2CSharpConverter(options.Value.IgnoreErrors, options.Value.SkipComments, options.Value.ShowOrphanComments);

                var referencedSymbols = new List <SymbolInfo>();
                var errors            = new HashSet <CompilerError>(new CompilerErrorComparer());

                ConvertScripts(AssemblyType.Runtime, runtimeScripts, converter, options.Value, referencedSymbols, errors);
                ConvertScripts(AssemblyType.Editor, editorScripts, converter, options.Value, referencedSymbols, errors);
                ConvertScripts(AssemblyType.RuntimePlugins, pluginScripts, converter, options.Value, referencedSymbols, errors);
                ConvertScripts(AssemblyType.EditorPlugins, pluginEditorScritps, converter, options.Value, referencedSymbols, errors);

                var foundConditionalCompilation = ReportConversionFinished(runtimeScripts.Length + editorScripts.Length + pluginScripts.Length, options.Value, referencedSymbols, errors);
                return(foundConditionalCompilation ? 1 : 0);
            }
            catch (Exception ex)
            {
                using (WithConsoleColors.SetTo(ConsoleColor.DarkRed, ConsoleColor.Black))
                {
                    Console.WriteLine(ex.Message);
                    if (options.Value.Verbose)
                    {
                        Console.WriteLine();
                        Console.WriteLine(ex.StackTrace);
                    }

                    if (!options.Value.IgnoreErrors)
                    {
                        Console.WriteLine("Consider running converter with '-i' option.");
                    }
                }

                return(-5);
            }
        }