/// <summary>
        /// Applies the code snippet.
        /// </summary>
        /// <param name="visualStudioService">The visual studio service.</param>
        /// <param name="codeSnippet">The code snippet.</param>
        /// <returns>The messages.</returns>
        internal IEnumerable <string> ApplyCodeSnippet(
            IVisualStudioService visualStudioService,
            CodeSnippet codeSnippet)
        {
            TraceService.WriteLine("CodeConfigService::ApplyCodeSnippet");

            List <string> messages = new List <string>();

            //// find the project
            IProjectService projectService = visualStudioService.GetProjectServiceBySuffix(codeSnippet.Project);

            //// find the class
            IProjectItemService projectItemService = projectService?.GetProjectItem(codeSnippet.Class + ".cs");

            //// find the method.
            CodeFunction codeFunction = projectItemService?.GetFirstClass().GetFunction(codeSnippet.Method);

            string code = codeFunction?.GetCode();

            if (code?.Contains(codeSnippet.Code.Trim()) == false)
            {
                codeFunction.InsertCode(codeSnippet.Code, true);

                string message = string.Format(
                    "Code added to project {0} class {1} method {2}.",
                    projectService.Name,
                    codeSnippet.Class,
                    codeSnippet.Method);

                messages.Add(message);
            }

            return(messages);
        }
Пример #2
0
        /// <summary>
        /// Gets the nuget command strings.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="visualStudioService">The visual studio service.</param>
        /// <param name="settingsService">The settings service.</param>
        /// <param name="usePreRelease">if set to <c>true</c> [use pre release].</param>
        /// <returns>The Nuget Command strings.</returns>
        internal static string GetNugetCommandStrings(
            this Plugin instance,
            IVisualStudioService visualStudioService,
            ISettingsService settingsService,
            bool usePreRelease)
        {
            string commands = string.Empty;

            foreach (string platform in instance.Platforms)
            {
                IProjectService projectService = visualStudioService.GetProjectServiceBySuffix(platform);

                if (projectService != null)
                {
                    foreach (NugetCommand nugetCommand in instance.NugetCommands)
                    {
                        if (IsCommandRequired(nugetCommand, platform))
                        {
                            string pluginNugetCommand = nugetCommand.Command;

                            //// check to see if we are going to use local nuget

                            if (settingsService.UseLocalNuget &&
                                settingsService.LocalNugetName != string.Empty)
                            {
                                pluginNugetCommand = pluginNugetCommand.Replace(
                                    "-ProjectName",
                                    " -Source " + settingsService.LocalNugetName + " -ProjectName");
                            }

                            if (usePreRelease)
                            {
                                pluginNugetCommand += Settings.NugetIncludePreRelease;
                            }

                            if (instance.OverwriteFiles)
                            {
                                commands += Settings.NugetInstallPackageOverwriteFiles.Replace("%s", pluginNugetCommand) + " " + projectService.Name + Environment.NewLine;
                            }
                            else
                            {
                                commands += Settings.NugetInstallPackage.Replace("%s", pluginNugetCommand) + " " + projectService.Name + Environment.NewLine;
                            }
                        }
                    }
                }
            }

            return(commands);
        }
        /// <summary>
        /// Gets the nuget command strings.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="visualStudioService">The visual studio service.</param>
        /// <param name="settingsService">The settings service.</param>
        /// <param name="usePreRelease">if set to <c>true</c> [use pre release].</param>
        /// <returns>The Nuget Command strings.</returns>
        internal static string GetNugetCommandStrings(
            this Plugin instance,
            IVisualStudioService visualStudioService,
            ISettingsService settingsService,
            bool usePreRelease)
        {
            string commands = string.Empty;

            foreach (string platform in instance.Platforms)
            {
                IProjectService projectService = visualStudioService.GetProjectServiceBySuffix(platform);

                if (projectService != null)
                {
                    foreach (NugetCommand nugetCommand in instance.NugetCommands)
                    {
                        if (IsCommandRequired(nugetCommand, platform))
                        {
                            string pluginNugetCommand = nugetCommand.Command;

                            //// check to see if we are going to use local nuget

                            if (settingsService.UseLocalNuget &&
                                settingsService.LocalNugetName != string.Empty)
                            {
                                pluginNugetCommand = pluginNugetCommand.Replace(
                                    "-ProjectName",
                                    " -Source " + settingsService.LocalNugetName + " -ProjectName");
                            }

                            if (usePreRelease)
                            {
                                pluginNugetCommand += Settings.NugetIncludePreRelease;
                            }

                            if (instance.OverwriteFiles)
                            {
                                commands += Settings.NugetInstallPackageOverwriteFiles.Replace("%s", pluginNugetCommand) + " " + projectService.Name + Environment.NewLine;
                            }
                            else
                            {
                                commands += Settings.NugetInstallPackage.Replace("%s", pluginNugetCommand) + " " + projectService.Name + Environment.NewLine;
                            }
                        }
                    }
                }
            }

            return commands;
        }
        /// <summary>
        /// Gets the nuget command messages.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="visualStudioService">The visual studio service.</param>
        /// <returns></returns>
        internal static string GetNugetCommandMessages(
            this Plugin instance,
            IVisualStudioService visualStudioService)
        {
            string commands = string.Empty;

            foreach (string platform in instance.Platforms)
            {
                IProjectService projectService = visualStudioService.GetProjectServiceBySuffix(platform);

                if (projectService != null)
                {
                    foreach (string nugetCommand in instance.NugetCommands)
                    {
                        commands += nugetCommand + " nuget package added to " + projectService.Name + " project." + Environment.NewLine;
                    }
                }
            }

            return commands;
        }
        /// <summary>
        /// Gets the nuget command strings.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="visualStudioService">The visual studio service.</param>
        /// <param name="usePreRelease">if set to <c>true</c> [use pre release].</param>
        /// <returns></returns>
        internal static string GetNugetCommandStrings(
            this Plugin instance,
            IVisualStudioService visualStudioService,
            bool usePreRelease)
        {
            string commands = string.Empty;

            foreach (string platform in instance.Platforms)
            {
                IProjectService projectService = visualStudioService.GetProjectServiceBySuffix(platform);

                if (projectService != null)
                {
                    foreach (string nugetCommand in instance.NugetCommands)
                    {
                        string pluginNugetCommand = nugetCommand;

                        if (usePreRelease)
                        {
                            pluginNugetCommand += Settings.NugetIncludePreRelease;
                        }

                        if (instance.OverwriteFiles)
                        {
                            commands += Settings.NugetInstallPackageOverwriteFiles .Replace("%s", pluginNugetCommand) + " " + projectService.Name + Environment.NewLine;
                        }

                        else
                        {
                            commands += Settings.NugetInstallPackage.Replace("%s", pluginNugetCommand) + " " + projectService.Name + Environment.NewLine;
                        }
                    }
                }
            }

            return commands;
        }
        /// <summary>
        /// Applies the code snippet.
        /// </summary>
        /// <param name="visualStudioService">The visual studio service.</param>
        /// <param name="codeSnippet">The code snippet.</param>
        /// <returns>The messages.</returns>
        internal IEnumerable<string> ApplyCodeSnippet(
            IVisualStudioService visualStudioService, 
            CodeSnippet codeSnippet)
        {
            TraceService.WriteLine("CodeConfigService::ApplyCodeSnippet");

            List<string> messages = new List<string>();

            //// find the project
            IProjectService projectService = visualStudioService.GetProjectServiceBySuffix(codeSnippet.Project);

            //// find the class
            IProjectItemService projectItemService = projectService?.GetProjectItem(codeSnippet.Class + ".cs");

            //// find the method.
            CodeFunction codeFunction = projectItemService?.GetFirstClass().GetFunction(codeSnippet.Method);

            string code = codeFunction?.GetCode();

            if (code?.Contains(codeSnippet.Code.Trim()) == false)
            {
                codeFunction.InsertCode(codeSnippet.Code, true);

                string message = string.Format(
                    "Code added to project {0} class {1} method {2}.",
                    projectService.Name,
                    codeSnippet.Class,
                    codeSnippet.Method);

                messages.Add(message);
            }

            return messages;
        }
        /// <summary>
        /// Gets the nuget commands.
        /// </summary>
        /// <param name="visualStudioService">The visual studio service.</param>
        /// <param name="templates">The templates.</param>
        /// <param name="verboseOutput">if set to <c>true</c> [verbose output].</param>
        /// <param name="debug">if set to <c>true</c> [debug].</param>
        /// <param name="usePreRelease">if set to <c>true</c> [use pre release].</param>
        /// <returns>The nuget commands.</returns>
        public string GetNugetCommands(
            IVisualStudioService visualStudioService,
            IEnumerable<ProjectTemplateInfo> templates,
            bool verboseOutput,
            bool debug,
            bool usePreRelease)
        {
            TraceService.WriteLine("NugetService::ExecuteNugetCommands");

            string nugetCommandsString = string.Empty;

            string verboseOption = this.GetVerboseOption(verboseOutput);

            string debugOption = this.GetDebugOption(debug);

            string preReleaseOption = this.GetPreReleaseOption(usePreRelease);

            foreach (ProjectTemplateInfo projectTemplateInfo in templates
                .Where(x => x.NugetCommands != null))
            {
                //// we need to work out if the project was successfully added to the solution
                //// in cases where the project is not supported (SDK not installed) it will
                //// not have been added (and therefore no nuget commands to run)

                IProjectService projectService = visualStudioService.GetProjectServiceBySuffix(projectTemplateInfo.ProjectSuffix);

                if (projectService != null)
                {
                    foreach (string nugetCommand in projectTemplateInfo.NugetCommands)
                    {
                        nugetCommandsString += string.Format(
                            "{0} {1} {2} {3} {4}",
                            nugetCommand,
                            verboseOption,
                            debugOption,
                            preReleaseOption,
                            Environment.NewLine);
                    }
                }
            }

            TraceService.WriteLine("commands=" + nugetCommandsString);

            return nugetCommandsString;
        }
        /// <summary>
        /// Gets the nuget commands.
        /// </summary>
        /// <param name="visualStudioService">The visual studio service.</param>
        /// <param name="templates">The templates.</param>
        /// <param name="verboseOutput">if set to <c>true</c> [verbose output].</param>
        /// <param name="debug">if set to <c>true</c> [debug].</param>
        /// <returns>The nuget commands.</returns>
        public string GetNugetCommands(
            IVisualStudioService visualStudioService,
            IEnumerable<ProjectTemplateInfo> templates,
            bool verboseOutput,
            bool debug)
        {
            TraceService.WriteLine("NugetService::ExecuteNugetCommands");

            string nugetCommands = string.Empty;

            string verboseOption = string.Empty;

            if (verboseOutput)
            {
                verboseOption = "-verbose";
            }

            string debugOption = string.Empty;

            if (debug)
            {
                debugOption = "-debug";
            }

            foreach (ProjectTemplateInfo projectTemplateInfo in templates)
            {
                if (projectTemplateInfo.NugetCommands != null)
                {
                    //// we need to work out if the project was sucessfully added to the solution
                    //// in cases where the project is not supported (SDK not installed) it will
                    //// not have been added (and therefore no nuget commands to run)

                    IProjectService projectService = visualStudioService.GetProjectServiceBySuffix(projectTemplateInfo.ProjectSuffix);

                    if (projectService != null)
                    {
                        foreach (string nugetCommand in projectTemplateInfo.NugetCommands)
                        {
                            nugetCommands += string.Format(
                                "{0} {1} {2} {3}",
                                nugetCommand,
                                verboseOption,
                                debugOption,
                                Environment.NewLine);
                        }
                    }
                }
            }

            return nugetCommands;
        }
        /// <summary>
        /// Applies the code dependencies.
        /// </summary>
        /// <param name="visualStudioService">The visual studio service.</param>
        /// <param name="codeConfig">The code config.</param>
        internal void ApplyCodeDependencies(
            IVisualStudioService visualStudioService,
            CodeConfig codeConfig)
        {
            TraceService.WriteLine("ServicesService::ApplyCodeDependencies");

            //// apply any code dependencies
            if (codeConfig.CodeDependencies != null)
            {
                foreach (CodeSnippet codeSnippet in codeConfig.CodeDependencies)
                {
                    //// find the project
                    IProjectService projectService = visualStudioService.GetProjectServiceBySuffix(codeSnippet.Project);

                    if (projectService != null)
                    {
                        //// find the class
                        IProjectItemService projectItemService = projectService.GetProjectItem(codeSnippet.Class + ".cs");

                        if (projectItemService != null)
                        {
                            //// find the method.
                            CodeFunction codeFunction = projectItemService.GetFirstClass().GetFunction(codeSnippet.Method);

                            if (codeFunction != null)
                            {
                                string code  = codeFunction.GetCode();

                                if (code.Contains(codeSnippet.Code.Trim()) == false)
                                {
                                    codeFunction.InsertCode(codeSnippet.Code, true);

                                    string message = string.Format(
                                        "Code added to project {0} class {1} method {2}.",
                                        projectService.Name,
                                        codeSnippet.Class,
                                        codeSnippet.Method);

                                    this.Messages.Add(message);
                                }
                            }
                        }
                    }
                }
            }
        }