Пример #1
0
        public MainWindow()
        {
            InitializeComponent();

            _connections = new BindingList <GeneratorConfig.Connection>();
            _tables      = new BindingList <string>();

            this.ConnectionsCombo.DataContext = _connections;
            this.DataContext = this;

            var configFilePath = GetConfigurationFilePath();

            config                     = JsonConfigHelper.LoadConfig <GeneratorConfig>(configFilePath);
            config.Connections         = config.Connections ?? new List <GeneratorConfig.Connection>();
            config.RemoveForeignFields = config.RemoveForeignFields ?? new List <string>();

            foreach (var connection in config.Connections)
            {
                _connections.Add(connection);
            }

            if (!config.WebProjectFile.IsEmptyOrNull())
            {
                var webConfig = Path.Combine(Path.GetDirectoryName(Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, config.WebProjectFile))), "web.config");
                AddConnectionsFromAppConfig(webConfig);
            }
        }
Пример #2
0
        /// <summary>
        /// Adds translations from JSON files at specified path. File names in this directory should be in format
        /// {anyprefix}.{languageID}.json where {languageID} is a language code like 'en', 'en-GB' etc.
        /// </summary>
        /// <param name="path">Path containing JSON files</param>
        public static void AddFromFilesInFolder(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (!Directory.Exists(path))
            {
                return;
            }

            var files = Directory.GetFiles(path, "*.json");

            Array.Sort(files);

            foreach (var file in files)
            {
                var texts  = JsonConfigHelper.LoadConfig <Dictionary <string, JToken> >(file);
                var langID = Path.GetFileNameWithoutExtension(Path.GetFileName(file));

                var idx = langID.LastIndexOf(".");
                if (idx >= 0)
                {
                    langID = langID.Substring(idx + 1);
                }

                if (langID.ToLowerInvariant() == "invariant")
                {
                    langID = "";
                }

                AddFromNestedDictionary(texts, "", langID);
            }
        }
Пример #3
0
        public static GeneratorConfig Load()
        {
            var configFilePath = GetConfigurationFilePath();
            var config         = JsonConfigHelper.LoadConfig <GeneratorConfig>(configFilePath);

            config.Connections         = config.Connections ?? new List <GeneratorConfig.Connection>();
            config.RemoveForeignFields = config.RemoveForeignFields ?? new List <string>();
            return(config);
        }
Пример #4
0
        public ListResponse <TranslationItem> List(TranslationListRequest request)
        {
            var result = new ListResponse <TranslationItem>();

            var availableKeys    = GetAllAvailableLocalTextKeys();
            var targetLanguageID = request.TargetLanguageID.TrimToNull();

            var customTranslations = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            var textsFilePath = GetUserTextsFilePath(targetLanguageID);

            if (File.Exists(textsFilePath))
            {
                var json = JsonConfigHelper.LoadConfig <Dictionary <string, JToken> >(textsFilePath);
                JsonLocalTextRegistration.ProcessNestedDictionary(json, "", customTranslations);
                foreach (var key in customTranslations.Keys)
                {
                    availableKeys.Add(key);
                }
            }

            var sorted = new string[availableKeys.Count];

            availableKeys.CopyTo(sorted);
            Array.Sort(sorted);

            var registry = Dependency.Resolve <ILocalTextRegistry>();

            targetLanguageID = targetLanguageID ?? "";
            var sourceLanguageID = request.SourceLanguageID.TrimToEmpty();

            result.Entities = new List <TranslationItem>();

            Func <string, string> effective = delegate(string key)
            {
                if (key.StartsWith("Navigation."))
                {
                    key = key.Substring("Navigation.".Length);
                    return(key.Split(new char[] { '/' }).Last());
                }
                else if (key.StartsWith("Forms.") && key.Contains(".Categories."))
                {
                    return(key.Split(new char[] { '.' }).Last().TrimToNull());
                }

                return(key);
            };

            foreach (var key in sorted)
            {
                string customText;
                if (!customTranslations.TryGetValue(key, out customText))
                {
                    customText = null;
                }

                result.Entities.Add(new TranslationItem
                {
                    Key        = key,
                    SourceText = registry.TryGet(sourceLanguageID, key) ?? effective(key),
                    TargetText = registry.TryGet(targetLanguageID, key) ?? effective(key),
                    CustomText = customText
                });
            }

            return(result);
        }
Пример #5
0
        public ScriptBundleManager()
        {
            var settings = Config.Get <ScriptBundlingSettings>();

            var bundles = JsonConfigHelper.LoadConfig <Dictionary <string, string[]> >(
                HostingEnvironment.MapPath("~/Scripts/site/ScriptBundles.json"));

            bundleIncludes = BundleUtils.ExpandBundleIncludes(bundles, "dynamic://Bundle.", "script");

            if (bundles.Count == 0 ||
                settings.Enabled != true)
            {
                return;
            }

            bundleKeyBySourceUrl  = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            bundleKeysBySourceUrl = new Dictionary <string, HashSet <string> >(StringComparer.OrdinalIgnoreCase);
            bundleByKey           = new Dictionary <string, ConcatenatedScript>(StringComparer.OrdinalIgnoreCase);

            bool minimize   = settings.Minimize == true;
            var  noMinimize = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            if (settings.NoMinimize != null)
            {
                noMinimize.AddRange(settings.NoMinimize);
            }

            foreach (var pair in bundles)
            {
                var sourceFiles = pair.Value;
                if (sourceFiles == null ||
                    sourceFiles.Length == 0)
                {
                    continue;
                }

                var bundleKey   = pair.Key;
                var bundleName  = "Bundle." + bundleKey;
                var bundleParts = new List <Func <string> >();
                var scriptNames = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

                Action <string> registerInBundle = delegate(string sourceFile)
                {
                    if (bundleKey.IndexOf('/') < 0 && !bundleKeyBySourceUrl.ContainsKey(sourceFile))
                    {
                        bundleKeyBySourceUrl[sourceFile] = bundleKey;
                    }

                    HashSet <string> bundleKeys;
                    if (!bundleKeysBySourceUrl.TryGetValue(sourceFile, out bundleKeys))
                    {
                        bundleKeys = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                        bundleKeysBySourceUrl[sourceFile] = new HashSet <string>();
                    }

                    bundleKeys.Add(bundleKey);
                };

                foreach (var sourceFile in sourceFiles)
                {
                    if (sourceFile.IsNullOrEmpty())
                    {
                        continue;
                    }

                    if (sourceFile.StartsWith("dynamic://", StringComparison.OrdinalIgnoreCase))
                    {
                        registerInBundle(sourceFile);

                        var scriptName = sourceFile.Substring(10);
                        scriptNames.Add(scriptName);
                        bundleParts.Add(() =>
                        {
                            if (recursionCheck != null)
                            {
                                if (recursionCheck.Contains(scriptName) || recursionCheck.Count > 100)
                                {
                                    return(String.Format(errorLines,
                                                         String.Format("Caught infinite recursion with dynamic scripts '{0}'!",
                                                                       String.Join(", ", recursionCheck))));
                                }
                            }
                            else
                            {
                                recursionCheck = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                            }

                            recursionCheck.Add(scriptName);
                            try
                            {
                                var code = DynamicScriptManager.GetScriptText(scriptName);
                                if (code == null)
                                {
                                    return(String.Format(errorLines,
                                                         String.Format("Dynamic script with name '{0}' is not found!", scriptName)));
                                }

                                if (minimize &&
                                    !scriptName.StartsWith("Bundle.", StringComparison.OrdinalIgnoreCase))
                                {
                                    try
                                    {
                                        var result = NUglify.Uglify.Js(code);
                                        if (!result.HasErrors)
                                        {
                                            code = result.Code;
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        ex.Log();
                                    }
                                }

                                return(code);
                            }
                            finally
                            {
                                recursionCheck.Remove(scriptName);
                            }
                        });

                        continue;
                    }

                    string sourceUrl = BundleUtils.ExpandVersionVariable(sourceFile);
                    sourceUrl = VirtualPathUtility.ToAbsolute(sourceUrl);

                    if (sourceUrl.IsNullOrEmpty())
                    {
                        continue;
                    }

                    registerInBundle(sourceUrl);

                    bundleParts.Add(() =>
                    {
                        var sourcePath = HostingEnvironment.MapPath(sourceUrl);
                        if (!File.Exists(sourcePath))
                        {
                            return(String.Format(errorLines, String.Format("File {0} is not found!", sourcePath)));
                        }

                        if (minimize && !noMinimize.Contains(sourceFile))
                        {
                            if (settings.UseMinJS == true)
                            {
                                var minPath = Path.ChangeExtension(sourcePath, ".min.js");
                                if (File.Exists(minPath))
                                {
                                    sourcePath = minPath;
                                    using (StreamReader sr = new StreamReader(File.OpenRead(sourcePath)))
                                        return(sr.ReadToEnd());
                                }
                            }

                            string code;
                            using (StreamReader sr = new StreamReader(File.OpenRead(sourcePath)))
                                code = sr.ReadToEnd();

                            try
                            {
                                var result = NUglify.Uglify.Js(code);
                                if (result.HasErrors)
                                {
                                    return(code);
                                }
                                return(result.Code);
                            }
                            catch (Exception ex)
                            {
                                ex.Log();
                                return(code);
                            }
                        }

                        using (StreamReader sr = new StreamReader(File.OpenRead(sourcePath)))
                            return(sr.ReadToEnd());
                    });
                }

                var bundle = new ConcatenatedScript(bundleParts, checkRights: () =>
                {
                    foreach (var scriptName in scriptNames)
                    {
                        if (recursionCheck != null)
                        {
                            if (recursionCheck.Contains(scriptName) || recursionCheck.Count > 100)
                            {
                                throw new InvalidOperationException(String.Format(
                                                                        "Caught infinite recursion with dynamic scripts '{0}'!",
                                                                        String.Join(", ", recursionCheck)));
                            }
                        }
                        else
                        {
                            recursionCheck = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                        }

                        recursionCheck.Add(scriptName);
                        try
                        {
                            DynamicScriptManager.CheckScriptRights(scriptName);
                        }
                        finally
                        {
                            recursionCheck.Remove(scriptName);
                        }
                    }
                });

                DynamicScriptManager.Register(bundleName, bundle);
                bundleByKey[bundleKey] = bundle;
            }

            isEnabled = true;
        }