Пример #1
0
        private async Task ExecuteSelectedCommand()
        {
            if (ExecutingCommand)
            {
                return;
            }

            ExecutingCommand = true;

            try
            {
                // build up the command<response<lastobject>>
                var responseType = SelectedResponseType.MakeGenericType(SelectedLastObjectType);
                var genericType  = SelectedBaseCommandType.MakeGenericType(responseType);

                if (!_lastAuth.Authenticated)
                {
                    await _lastAuth.GetSessionTokenAsync("tehrikkit", "#facedusk17a");
                }

                var instance = Activator.CreateInstance(genericType, _lastAuth);

                var parameters = CommandParameters
                                 .Where(pair => !string.IsNullOrWhiteSpace(pair.Key) && !string.IsNullOrWhiteSpace(pair.Value))
                                 .ToDictionary(pair => pair.Key, pair => pair.Value);

                var methodProperty = genericType.GetProperty("Method", BindingFlags.Public | BindingFlags.Instance);
                methodProperty.SetValue(instance, CommandMethodName);

                if (SelectedResponseType == typeof(PageResponse <>))
                {
                    var pageProperty = genericType.GetProperty("Page", BindingFlags.Public | BindingFlags.Instance);
                    pageProperty.SetValue(instance, int.Parse(CommandPageNumber));

                    var countProperty = genericType.GetProperty("Count", BindingFlags.Public | BindingFlags.Instance);
                    countProperty.SetValue(instance, int.Parse(CommandItemCount));
                }

                var parametersProperty = genericType.GetProperty("Parameters",
                                                                 BindingFlags.Public | BindingFlags.Instance);
                parametersProperty.SetValue(instance, parameters);

                // execute
                var executeMethod = genericType.GetMethods().First(m => m.Name == "ExecuteAsync");
                await(dynamic) executeMethod.Invoke(instance, null);

                // cast so we can get the Json response
                var dummyCommand = (IDummyCommand)instance;
                var jo           = dummyCommand.Response;

                var formattedJson = jo.ToString(Formatting.Indented);

                // writeout to file
                var filename = string.Format("syro-{0}-{1}.json", jo.Properties().First().Name,
                                             DateTime.Now.ToString("yyMMdd-HHmmss"));
                var tempDirPath = Path.GetFullPath(SolutionDir + "tmp/");

                if (!Directory.Exists(tempDirPath))
                {
                    Directory.CreateDirectory(tempDirPath);
                }
                var path = Path.GetFullPath(tempDirPath + filename);

                // write to output directory and launch
                using (var fs = new FileStream(path, FileMode.Create))
                {
                    using (var sw = new StreamWriter(fs))
                    {
                        sw.Write(formattedJson);
                    }
                }
                Process.Start(path);

                CommandResult = formattedJson;
            }
            finally
            {
                ExecutingCommand = false;
            }
        }