Esempio n. 1
0
        public static void Run(InlineTextBlock label, Action action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var spinner = new CustomSpinner(label);

            spinner.Display();
            try {
                action();
                spinner.Close();
            }
            catch (Exception e) {
                spinner.DoneText = null;
                string error = e.Message;
                if (error.StartsWith("Error:"))
                {
                    error = error.Substring(7);
                }
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.Write("[ERROR]");
                Console.ResetColor();
                Console.Write($" {error}");

                spinner.Close();
                Environment.Exit(1);
            }
        }
Esempio n. 2
0
        internal static void Create()
        {
            using var spinner = new CustomSpinner("Creating index");

            var cacheDir = new DirectoryInfo(Program.CachePath);

            SqliteConnection.CreateFile(Program.DbPath);

            using var conn = new SqliteConnection("Data Source=" + Program.DbPath + ";");
            conn.Open();

            using var command = new SqliteCommand(conn);
            using SqliteTransaction transaction = conn.BeginTransaction();

            command.CommandText =
                "CREATE TABLE pages (name VARCHAR(100), platform VARCHAR(10), lang VARCHAR(7), local INTEGER)";
            command.ExecuteNonQuery();

            command.CommandText = "CREATE TABLE config (parameter VARCHAR(20), value VARCHAR(100))";
            command.ExecuteNonQuery();

            command.CommandText = "INSERT INTO config (parameter, value) VALUES(@parameter, @value)";
            command.Parameters.AddWithValue("@parameter", "last-update");
            command.Parameters.AddWithValue("@value",
                                            DateTime.UtcNow.Date.ToString(CultureInfo.InvariantCulture));
            command.ExecuteNonQuery();
            command.Transaction = transaction;
            command.CommandType = CommandType.Text;

            // Create indexes
            command.CommandText = "CREATE INDEX names_index ON pages (name, platform, lang, local)";
            command.ExecuteNonQuery();
            command.CommandText = "CREATE INDEX lang_platform_index ON pages (lang, platform, name, local)";
            command.ExecuteNonQuery();
            command.CommandText = "CREATE INDEX platform_index ON pages (platform)";
            command.ExecuteNonQuery();
            command.CommandText = "CREATE INDEX lang_index ON pages (lang)";
            command.ExecuteNonQuery();

            // Add pages
            command.CommandText =
                "INSERT INTO pages (name, platform, lang, local) VALUES(@name, @platform, @lang, @local)";
            List <string> preferredLanguages = Locale.GetEnvLanguages();

            foreach (DirectoryInfo dir in cacheDir.EnumerateDirectories("*pages*"))
            {
                string lang    = Locale.DefaultLanguage;
                bool   isLocal = true;
                if (dir.Name.Contains("."))
                {
                    lang = dir.Name.Split('.')[1];
                }

                if (lang != Locale.DefaultLanguage &&
                    preferredLanguages.All(x => lang.Substring(0, 2) != x.Substring(0, 2)))
                {
                    isLocal = false;
                }

                foreach (DirectoryInfo osDir in dir.EnumerateDirectories())
                {
                    foreach (FileInfo file in osDir.EnumerateFiles("*.md", SearchOption.AllDirectories))
                    {
                        command.Parameters.AddWithValue("@name",
                                                        Path.GetFileNameWithoutExtension(file.Name));
                        command.Parameters.AddWithValue("@platform", osDir.Name);
                        command.Parameters.AddWithValue("@lang", lang);
                        command.Parameters.AddWithValue("@local", isLocal);
                        command.ExecuteNonQuery();
                    }
                }
            }

            transaction.Commit();
            spinner.Close();
        }