Пример #1
0
        public static RamlData GetRamlData(string ramlSource, string targetNamespace)
        {
            try
            {
                var ramlInfo = RamlInfoService.GetRamlInfo(ramlSource);

                if (ramlInfo.HasErrors)
                {
                    MessageBox.Show(ramlInfo.ErrorMessage);
                    return(null);
                }

                var model    = new WebApiGeneratorService(ramlInfo.RamlDocument).BuildModel();
                var filename = Path.GetFileName(ramlSource);
                if (string.IsNullOrWhiteSpace(filename))
                {
                    filename = "source.raml";
                }

                return(new RamlData(model, ramlInfo.RamlContents, filename));
            }
            catch (Exception ex)
            {
                ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource,
                                     VisualStudioAutomationHelper.GetExceptionInfo(ex));
                var errorMessage = ex.Message;
                if (ex.InnerException != null)
                {
                    errorMessage += " - " + ex.InnerException.Message;
                }

                MessageBox.Show(errorMessage);
                return(null);
            }
        }
Пример #2
0
        private async Task GetRamlFromURL()
        {
            //StartProgress();
            //DoEvents();

            try
            {
                var url    = RamlOriginalSource;
                var result = includesManager.Manage(url, Path.GetTempPath(), Path.GetTempPath());

                var raml   = result.ModifiedContents;
                var parser = new RamlParser();

                var ramlDocument = await parser.LoadRamlAsync(raml, Path.GetTempPath());

                var filename = SetFilename(url);

                var path = Path.Combine(Path.GetTempPath(), filename);
                File.WriteAllText(path, raml);
                RamlTempFilePath   = path;
                RamlOriginalSource = url;

                SetPreview(ramlDocument);

                btnOk.IsEnabled = true;
                //StopProgress();
            }
            catch (UriFormatException uex)
            {
                ShowErrorAndDisableOk(uex.Message);
            }
            catch (HttpRequestException rex)
            {
                ShowErrorAndDisableOk(GetFriendlyMessage(rex));
                ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource,
                                     VisualStudioAutomationHelper.GetExceptionInfo(rex));
            }
            catch (Exception ex)
            {
                ShowErrorAndDisableOk(ex.Message);
                ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource,
                                     VisualStudioAutomationHelper.GetExceptionInfo(ex));
            }
        }
Пример #3
0
        public async Task FromFile()
        {
            //TODO: check
            try
            {
                txtFileName.Text = Path.GetFileName(RamlTempFilePath);

                SetDefaultClientRootClassName();

                var result   = includesManager.Manage(RamlTempFilePath, Path.GetTempPath(), Path.GetTempPath());
                var parser   = new RamlParser();
                var document = await parser.LoadRamlAsync(result.ModifiedContents, Path.GetTempPath());

                SetPreview(document);
            }
            catch (Exception ex)
            {
                ShowErrorStopProgressAndDisableOk("Error while parsing raml file. " + ex.Message);
                ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource,
                                     VisualStudioAutomationHelper.GetExceptionInfo(ex));
            }
        }
Пример #4
0
        public static RamlInfo GetRamlInfo(string ramlSource)
        {
            var info = new RamlInfo();

            if (ramlSource.StartsWith("http"))
            {
                Uri uri;
                if (!Uri.TryCreate(ramlSource, UriKind.Absolute, out uri))
                {
                    info.ErrorMessage = "Invalid Url specified: " + uri.AbsoluteUri;
                    ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource, info.ErrorMessage);
                    return(info);
                }

                var absolutePath = uri.AbsoluteUri;
                if (absolutePath.Contains("/"))
                {
                    absolutePath = absolutePath.Substring(0, absolutePath.LastIndexOf("/", StringComparison.InvariantCulture) + 1);
                }

                info.AbsolutePath = absolutePath;

                try
                {
                    info.RamlContents = Downloader.GetContents(uri);
                }
                catch (HttpRequestException rex)
                {
                    var errorMessage = rex.Message;
                    if (rex.InnerException != null)
                    {
                        errorMessage += " - " + rex.InnerException.Message;
                    }

                    if (errorMessage.Contains("404"))
                    {
                        errorMessage = "Url not found, could not load the specified url: " + uri.AbsoluteUri;
                    }

                    info.ErrorMessage = errorMessage;

                    ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource,
                                         VisualStudioAutomationHelper.GetExceptionInfo(rex));

                    return(info);
                }
                catch (Exception ex)
                {
                    var errorMessage = ex.Message;
                    if (ex.InnerException != null)
                    {
                        errorMessage += " - " + ex.InnerException.Message;
                    }

                    info.ErrorMessage = "Error when trying to load specified url " + uri.AbsoluteUri + ". " + errorMessage;

                    ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource,
                                         VisualStudioAutomationHelper.GetExceptionInfo(ex));

                    return(info);
                }
            }
            else
            {
                if (!File.Exists(ramlSource))
                {
                    info.ErrorMessage = "Error. File " + ramlSource + " does not exist.";
                    return(info);
                }

                info.AbsolutePath = Path.GetDirectoryName(ramlSource) + "\\";

                try
                {
                    info.RamlContents = File.ReadAllText(ramlSource);
                }
                catch (Exception ex)
                {
                    var errorMessage = ex.Message;
                    if (ex.InnerException != null)
                    {
                        errorMessage += " - " + ex.InnerException.Message;
                    }

                    info.ErrorMessage = "Error when trying to read file " + ramlSource + ". " + errorMessage;

                    ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource,
                                         VisualStudioAutomationHelper.GetExceptionInfo(ex));

                    return(info);
                }
            }

            var task = new RamlParser().LoadRamlAsync(info.RamlContents, ramlSource);

            task.WaitWithPumping();
            info.RamlDocument = task.Result;

            return(info);
        }
Пример #5
0
        private async void btnChooseFile_Click(object sender, RoutedEventArgs e)
        {
            FileDialog fd = new OpenFileDialog();

            fd.DefaultExt = ".raml;*.rml";
            fd.Filter     = "RAML files |*.raml;*.rml";

            var opened = fd.ShowDialog();

            progressBar.Visibility = Visibility.Visible;

            if (opened != true)
            {
                progressBar.Visibility = Visibility.Hidden;
                return;
            }
            DoEvents();

            RamlTempFilePath   = fd.FileName;
            RamlOriginalSource = fd.FileName;

            addressText.Text = "File: " + Path.GetFileName(fd.FileName);
            txtFileName.Text = Path.GetFileName(fd.FileName);

            try
            {
                var parser   = new RamlParser();
                var document = await parser.LoadAsync(RamlTempFilePath);

                SetPreview(document);
            }
            catch (Exception ex)
            {
                ShowErrorStopProgressAndDisableOk("Error while parsing raml file. " + ex.Message);
                ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource, VisualStudioAutomationHelper.GetExceptionInfo(ex));
            }
        }
Пример #6
0
        private async void GoButton_Click(object sender, RoutedEventArgs e)
        {
            StartProgress();
            DoEvents();

            try
            {
                var includesManager = new RamlIncludesManager();
                var url             = addressText.Text;
                var result          = includesManager.Manage(url, Path.GetTempPath());

                var raml   = result.ModifiedContents;
                var parser = new RamlParser();

                var ramlDocument = await parser.LoadRamlAsync(raml, Path.GetTempPath());

                var filename = Path.GetFileName(url);

                if (string.IsNullOrEmpty(filename))
                {
                    filename = "reference.raml";
                }

                if (!filename.ToLowerInvariant().EndsWith(RamlFileExtension))
                {
                    filename += RamlFileExtension;
                }

                txtFileName.Text = NetNamingMapper.RemoveIndalidChars(Path.GetFileNameWithoutExtension(filename)) + RamlFileExtension;

                var path = Path.Combine(Path.GetTempPath(), filename);
                File.WriteAllText(path, raml);
                RamlTempFilePath   = path;
                RamlOriginalSource = url;

                SetPreview(ramlDocument);

                btnOk.IsEnabled = true;
                StopProgress();
            }
            catch (UriFormatException uex)
            {
                ShowErrorAndStopProgress(uex.Message);
            }
            catch (HttpRequestException rex)
            {
                ShowErrorAndStopProgress(GetFriendlyMessage(rex));
                ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource, VisualStudioAutomationHelper.GetExceptionInfo(rex));
            }
            catch (Exception ex)
            {
                ShowErrorAndStopProgress(ex.Message);
                ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource, VisualStudioAutomationHelper.GetExceptionInfo(ex));
            }
        }
Пример #7
0
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            StartProgress();
            DoEvents();

            if (!txtFileName.Text.ToLowerInvariant().EndsWith(RamlFileExtension))
            {
                ShowErrorStopProgressAndDisableOk("Error: the file must have the .raml extension.");
                DialogResult = false;
                return;
            }

            if (!IsContractUseCase && !File.Exists(RamlTempFilePath))
            {
                ShowErrorStopProgressAndDisableOk("Error: the specified file does not exist.");
                DialogResult = false;
                return;
            }

            var path = Path.GetDirectoryName(GetType().Assembly.Location) + Path.DirectorySeparatorChar;

            try
            {
                ResourcesLabel.Text = "Processing. Please wait..." + Environment.NewLine + Environment.NewLine;

                // Execute action (add RAML Reference, Scaffold Web Api, etc)
                var parameters = new RamlChooserActionParams(RamlOriginalSource, RamlTempFilePath, txtTitle.Text, path,
                                                             txtFileName.Text, txtNamespace.Text, chkDoNotScaffold.IsChecked);
                action(parameters);

                ResourcesLabel.Text += "Succeeded";
                StopProgress();
                btnOk.IsEnabled = true;
                DialogResult    = true;
                Close();
            }
            catch (Exception ex)
            {
                ShowErrorStopProgressAndDisableOk("Error: " + ex.Message);

                ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource, VisualStudioAutomationHelper.GetExceptionInfo(ex));
            }
        }
Пример #8
0
        private void SetPreview(RamlDocument document)
        {
            Dispatcher.Invoke(() =>
            {
                try
                {
                    SelectExistingRamlOption();
                    ResourcesLabel.Text = GetResourcesPreview(document);
                    StopProgress();
                    btnOk.IsEnabled = true;
                    SetNamespace(RamlTempFilePath);

                    if (NetNamingMapper.HasIndalidChars(txtFileName.Text))
                    {
                        ShowErrorStopProgressAndDisableOk("The specied file name has invalid chars");
                        txtFileName.Focus();
                    }
                }
                catch (Exception ex)
                {
                    ShowErrorStopProgressAndDisableOk("Error while parsing raml file. " + ex.Message);
                    ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource, VisualStudioAutomationHelper.GetExceptionInfo(ex));
                }
            });
        }