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));
            }
        }
示例#2
0
        public static RamlData GetRamlData(string ramlSource, string targetNamespace)
        {
            var logger = new Logger();

            try
            {
                var ramlInfo = RamlInfoService.GetRamlInfo(ramlSource);

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


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

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

                MessageBox.Show(errorMessage);
                return(null);
            }
        }
        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);
        }
示例#4
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));
            }
        }
示例#5
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));
            }
        }
示例#6
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));
            }
        }
示例#7
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));
                }
            });
        }
 private string GetNamespace(string fileName)
 {
     return(VisualStudioAutomationHelper.GetDefaultNamespace(ServiceProvider) + "." +
            NetNamingMapper.GetObjectName(Path.GetFileNameWithoutExtension(fileName)));
 }