Пример #1
0
 /// <summary>
 /// Constructs a instance of local Closure Compiler
 /// </summary>
 /// <param name="javaVmPath">Path to Java Virtual Machine</param>
 /// <param name="closureCompilerAppPath">Path to Google Closure Compiler Application</param>
 /// <param name="tempDirectoryPath">Absolute path to directory that contains temporary files</param>
 /// <param name="commonExternsDependencies">List of common JS externs dependencies</param>
 /// <param name="options">Compilation options</param>
 public ClosureLocalJsCompiler(string javaVmPath, string closureCompilerAppPath,
                               string tempDirectoryPath, DependencyCollection commonExternsDependencies,
                               LocalJsCompilationOptions options)
     : base(commonExternsDependencies)
 {
     _javaVmPath                        = javaVmPath;
     _closureCompilerAppPath            = closureCompilerAppPath;
     _tempDirectoryPath                 = tempDirectoryPath;
     _commonExternsTempDirectoryPath    = string.Empty;
     _commonExternsTempDirectoryCreated = false;
     _commonExternsTempFilePaths        = new List <string>();
     _options       = options ?? new LocalJsCompilationOptions();
     _optionsString = ConvertCompilationOptionsToArgs(_options);
 }
Пример #2
0
        /// <summary>
        /// Converts a compilation options to command line arguments
        /// </summary>
        /// <param name="options">Compilation options</param>
        /// <returns>Command line arguments</returns>
        private static string ConvertCompilationOptionsToArgs(LocalJsCompilationOptions options)
        {
            var           stringBuilderPool = StringBuilderPool.Shared;
            StringBuilder argsBuilder       = stringBuilderPool.Rent();

            if (options.AcceptConstKeyword)
            {
                argsBuilder.Append("--accept_const_keyword ");
            }

            if (options.AllowEs6Output)
            {
                argsBuilder.Append("--allow_es6_out ");
            }

            if (options.AngularPass)
            {
                argsBuilder.Append("--angular_pass ");
            }

            if (!string.IsNullOrWhiteSpace(options.Charset))
            {
                argsBuilder.AppendFormat("--charset {0} ", options.Charset);
            }

            argsBuilder.AppendFormat("--compilation_level {0} ", ConvertCompilationLevelEnumValueToCode(options.CompilationLevel));

            IDictionary <string, string> defs = ParseDefinitions(options.DefinitionList);

            if (defs.Count > 0)
            {
                foreach (KeyValuePair <string, string> def in defs)
                {
                    string variableName  = def.Key;
                    string variableValue = def.Value;

                    argsBuilder.Append("--define ");
                    argsBuilder.Append(variableName);
                    if (variableValue != null)
                    {
                        argsBuilder.AppendFormat("={0}", variableValue);
                    }
                    argsBuilder.Append(" ");
                }
            }

            string[] errors = Utils.ConvertToStringCollection(options.ErrorList, ',',
                                                              trimItemValues: true, removeEmptyItems: true);
            if (errors.Length > 0)
            {
                foreach (string warningClassName in errors)
                {
                    argsBuilder.AppendFormat("--jscomp_error {0} ", warningClassName);
                }
            }

            if (options.ExportLocalPropertyDefinitions)
            {
                argsBuilder.Append("--export_local_property_definitions ");
            }

            string[] extraAnnotationNames = Utils.ConvertToStringCollection(options.ExtraAnnotationNameList, ',',
                                                                            trimItemValues: true, removeEmptyItems: true);
            if (extraAnnotationNames.Length > 0)
            {
                foreach (string extraAnnotationName in extraAnnotationNames)
                {
                    argsBuilder.AppendFormat("--extra_annotation_name {0} ", extraAnnotationName);
                }
            }

            if (options.GenerateExports)
            {
                argsBuilder.Append("--generate_exports ");
            }

            ExperimentalLanguageSpec languageInput = options.LanguageInput;

            if (languageInput != ExperimentalLanguageSpec.None)
            {
                argsBuilder.AppendFormat("--language_in {0} ", ConvertLanguageSpecEnumValueToCode(languageInput));
            }

            ExperimentalLanguageSpec languageOutput = (options.LanguageOutput != ExperimentalLanguageSpec.None) ?
                                                      options.LanguageOutput : languageInput;

            if (languageOutput != ExperimentalLanguageSpec.None)
            {
                argsBuilder.AppendFormat("--language_out {0} ", ConvertLanguageSpecEnumValueToCode(languageOutput));
            }

            if (options.PrettyPrint)
            {
                argsBuilder.Append("--formatting PRETTY_PRINT ");
            }

            if (options.ProcessClosurePrimitives)
            {
                argsBuilder.Append("--process_closure_primitives ");
            }

            if (options.ProcessJqueryPrimitives)
            {
                argsBuilder.Append("--process_jquery_primitives ");
            }

            if (options.SingleQuotes)
            {
                argsBuilder.Append("--formatting SINGLE_QUOTES ");
            }

            if (options.ThirdParty)
            {
                argsBuilder.Append("--third_party ");
            }

            if (options.TranspileOnly)
            {
                argsBuilder.Append("--transpile_only ");
            }

            string[] turnOffWarningClasses = Utils.ConvertToStringCollection(options.TurnOffWarningClassList, ',',
                                                                             trimItemValues: true, removeEmptyItems: true);
            if (turnOffWarningClasses.Length > 0)
            {
                foreach (string warningClassName in turnOffWarningClasses)
                {
                    argsBuilder.AppendFormat("--jscomp_off {0} ", warningClassName);
                }
            }

            if (options.UseOnlyCustomExterns)
            {
                argsBuilder.Append("--use_only_custom_externs ");
            }

            if (options.UseTypesForOptimization)
            {
                argsBuilder.Append("--use_types_for_optimization ");
            }

            string[] warnings = Utils.ConvertToStringCollection(options.WarningList, ',',
                                                                trimItemValues: true, removeEmptyItems: true);
            if (warnings.Length > 0)
            {
                foreach (string warningClassName in warnings)
                {
                    argsBuilder.AppendFormat("--jscomp_warning {0} ", warningClassName);
                }
            }

            int severity = options.Severity;

            if (severity >= 0 && severity <= 3)
            {
                argsBuilder.Append("--warning_level ");

                switch (severity)
                {
                case 0:
                case 1:
                    argsBuilder.Append("QUIET ");
                    break;

                case 2:
                    argsBuilder.Append("DEFAULT ");
                    break;

                case 3:
                    argsBuilder.Append("VERBOSE ");
                    break;
                }
            }

            string args = argsBuilder.ToString();

            stringBuilderPool.Return(argsBuilder);

            return(args);
        }