Exemplo n.º 1
0
        /// <summary>
        /// Check Required Properties
        /// </summary>
        /// <param name="obj">Object to check</param>
        /// <param name="cmd">Command</param>
        /// <param name="error">Variable for capture the error fail</param>
        /// <returns>Return true if OK, false if not</returns>
        public static bool CheckRequiredProperties(object obj, CommandLayer cmd, out string error)
        {
            error = null;
            Type fileInfoType = typeof(FileInfo);
            Type dirInfoType  = typeof(DirectoryInfo);

            foreach (PropertyInfo pi in ReflectionHelper.GetProperties(obj, true, true, true))
            {
                ConfigurableProperty c = pi.GetCustomAttribute <ConfigurableProperty>();
                if (c == null)
                {
                    continue;
                }

                object val = pi.GetValue(obj, null);

                if (val == null)
                {
                    if (c.Optional)
                    {
                        continue;
                    }

                    error = Lang.Get("Require_Set_Property", pi.Name);
                    return(false);
                }
                else
                {
                    if (pi.PropertyType == fileInfoType)
                    {
                        RequireExistsAttribute c2 = pi.GetCustomAttribute <RequireExistsAttribute>();
                        if (c2 != null && !c2.IsValid(val))
                        {
                            error = Lang.Get("File_Defined_Not_Exists", pi.Name);
                            return(false);
                        }
                    }
                    else
                    {
                        if (pi.PropertyType == dirInfoType)
                        {
                            // Check directory
                            DirectoryInfo di = (DirectoryInfo)val;
                            di.Refresh();
                            if (!di.Exists)
                            {
                                if (cmd == null)
                                {
                                    // Por si acaso
                                    error = Lang.Get("Folder_Required", di.FullName);
                                    return(false);
                                }

                                cmd.WriteLine(Lang.Get("Folder_Required_Ask", di.FullName));
                                if (!(bool)ConvertHelper.ConvertTo(cmd.ReadLine(null, null), typeof(bool)))
                                {
                                    error = Lang.Get("Folder_Required", di.FullName);
                                    return(false);
                                }

                                try { di.Create(); }
                                catch (Exception e) { cmd.WriteError(e.ToString()); }
                            }
                        }
                    }
                }
            }

            return(error == null);
        }
Exemplo n.º 2
0
        public bool CheckModule()
        {
            if (!CheckModule(true, EModuleType.Module))
            {
                return(false);
            }

            Interlocked.Exchange(ref _LastCheck, 0);

            try
            {
                if (((Module)_Current).IsCheckIntrusive())
                {
                    _IO.WriteInfo(Lang.Get("Can_Be_Intrusive"));
                    string line = _IO.ReadLine(null, null);
                    if (!(bool)ConvertHelper.ConvertTo(line, typeof(bool)))
                    {
                        return(false);
                    }
                }

                Module m = (Module)_Current.Clone();
                m.Prepare(_Current);

                Thread th = _IO.CancelableThread = new Thread(new ParameterizedThreadStart(checkModule));
                th.Name         = "CHECK " + _Current.FullPath;
                th.IsBackground = true;
                th.Start(m);
                th.Join();

                //if (th.ThreadState == System.Threading.ThreadState.Aborted)
                //{
                //    // Check abort
                //    _IO.WriteError(Lang.Get("Aborting"));
                //}

                return((ECheck)_LastCheck == ECheck.Ok);
            }
            catch (Exception e)
            {
                _Current.WriteError(e.Message);
            }
            finally
            {
                if (_IO.CancelableThread != null)
                {
                    _IO.CancelableThread = null;
                    switch ((ECheck)_LastCheck)
                    {
                    case ECheck.CantCheck: _Current.WriteInfo(Lang.Get("Check_CantCheck")); break;

                    case ECheck.Error: _Current.WriteInfo(Lang.Get("Check_Result"), Lang.Get("Error"), ConsoleColor.Red); break;

                    case ECheck.NotSure: _Current.WriteInfo(Lang.Get("Check_NotSure")); break;

                    case ECheck.Ok: _Current.WriteInfo(Lang.Get("Check_Result"), Lang.Get("Ok"), ConsoleColor.Green); break;
                    }
                }
            }
            return(false);
        }