/// <summary>
        /// Applies the code dependencies.
        /// </summary>
        /// <param name="visualStudioService">The visual studio service.</param>
        /// <param name="codeConfig">The code config.</param>
        /// <returns>The messages.</returns>
        public IEnumerable<string> ApplyCodeDependencies(
            IVisualStudioService visualStudioService, CodeConfig codeConfig)
        {
            TraceService.WriteLine("CodeConfigService::ApplyCodeDependencies");

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

            //// apply any code dependencies
            foreach (CodeSnippet codeSnippet in codeConfig.CodeDependencies)
            {
                IEnumerable<string> snippetMessages = this.ApplyCodeSnippet(visualStudioService, codeSnippet);

                messages.AddRange(snippetMessages);
            }

            return messages;
        }
        /// <summary>
        /// Gets the name of the bootstrap file.
        /// </summary>
        /// <param name="codeConfig">The code config.</param>
        /// <param name="friendlyName">Name of the friendly.</param>
        /// <returns>The bootstrap file name.</returns>
        public string GetBootstrapFileName(
            CodeConfig codeConfig, 
            string friendlyName)
        {
            TraceService.WriteLine("CodeConfigService::GetBootstrapFileName name=" + friendlyName);

            string bootstrapFileName = friendlyName + "PluginBootstrap.cs";

            //// the bootstrap file name can be overridden in the code config file.
            if (codeConfig != null &&
                string.IsNullOrEmpty(codeConfig.BootstrapFileNameOverride) == false)
            {
                bootstrapFileName = codeConfig.BootstrapFileNameOverride;

                TraceService.WriteLine("bootstrapFile has been overidden filename=" + bootstrapFileName);
            }

            return bootstrapFileName;
        }
        /// <summary>
        /// Gets the nuget command.
        /// </summary>
        /// <param name="codeConfig">The code config.</param>
        /// <returns>The nuget command.</returns>
        public string GetNugetCommand(CodeConfig codeConfig)
        {
            TraceService.WriteLine("CodeConfigService::GetNugetCommand");

            if (codeConfig != null &&
                string.IsNullOrEmpty(codeConfig.NugetPackage) == false)
            {
                return codeConfig.NugetPackage;
            }

            return string.Empty;
        }
        /// <summary>
        /// Processes the config.
        /// </summary>
        /// <param name="visualStudioService">The visual studio service.</param>
        /// <param name="projectService">The project service.</param>
        /// <param name="codeConfig">The code config.</param>
        internal void ProcessConfig(
            IVisualStudioService visualStudioService,
            IProjectService projectService, 
            CodeConfig codeConfig)
        {
            TraceService.WriteLine("ServicesService::ProcessConfig");

            if (codeConfig != null)
            {
                //// do we have any dependent plugins?
                codeConfig.DependentPlugins.ForEach(x => this.AddDependantPlugin(visualStudioService, x));

                string sourceDirectory = this.settingsService.MvvmCrossAssembliesPath;

                //// don't need to add reference if we are going to use nuget.
                if (this.settingsService.UseNugetForServices == false)
                {
                    foreach (string reference in codeConfig.References)
                    {
                        projectService.AddReference(
                            "Lib",
                            projectService.GetProjectPath() + @"\Lib\" + reference,
                            sourceDirectory + @"\" + reference,
                            this.settingsService.IncludeLibFolderInProjects,
                            this.settingsService.CopyAssembliesToLibFolder);
                    }
                }

                //// if we want to add via nuget then generate the command.
                if (this.settingsService.UseNugetForServices ||
                    codeConfig.NugetInstallationMandatory == "Y")
                {
                    ////this.nugetService.UpdateViaNuget(projectService, codeConfig);
                }

                //// apply any code dependencies
                IEnumerable<string> messages = this.codeConfigService.ApplyCodeDependencies(
                    visualStudioService,
                    codeConfig);

                this.Messages.AddRange(messages);
            }
        }
        /// <summary>
        /// Gets the project nuget command if we need one.
        /// </summary>
        /// <param name="codeConfig">The code config.</param>
        /// <param name="projectService">The project service.</param>
        /// <returns>The nuget command if applicable.</returns>
        public string GetProjectNugetCommand(
            CodeConfig codeConfig,
            IProjectService projectService)
        {
            TraceService.WriteLine("CodeConfigService::GetProjectNugetCommand");

            string command = string.Empty;

            string nugetCommand = this.GetNugetCommand(codeConfig);

            if (string.IsNullOrEmpty(nugetCommand) == false)
            {
                command = Settings.NugetInstallPackage.Replace("%s", nugetCommand);

                //// need to add the project to the end of the command!
                command += string.Format(" {0}", projectService.Name);

                TraceService.WriteLine("Command=" + command);
            }

            return command;
        }
        /// <summary>
        /// Processes the code config.
        /// </summary>
        /// <param name="projectService">The project service.</param>
        /// <param name="codeConfig">The code config.</param>
        /// <param name="extensionSource">The extension source.</param>
        /// <param name="extensionDestination">The extension destination.</param>
        public void ProcessCodeConfig(
            IProjectService projectService,
            CodeConfig codeConfig,
            string extensionSource,
            string extensionDestination)
        {
            TraceService.WriteLine("CodeConfigService::ProcessCodeConfig");

            if (codeConfig != null)
            {
                //// process it if we are using local disk copy.
                if (this.UseLocalDiskCopy(codeConfig))
                {
                    string sourceDirectory = this.fileSystem.Path.GetDirectoryName(extensionSource);
                    string destinationDirectory = this.fileSystem.Path.GetDirectoryName(extensionDestination);

                    codeConfig.References.ForEach(
                        x => projectService.AddReference(
                            "Lib",
                            destinationDirectory + @"\" + x,
                            sourceDirectory + @"\" + x,
                            this.settingsService.IncludeLibFolderInProjects,
                            this.settingsService.CopyAssembliesToLibFolder));
                }
            }
        }
        public void TestUseLocalDiskCopyWhenNugetRequested()
        {
            this.mockSettingsService.SetupGet(x => x.UseNugetForPlugins).Returns(true);

            CodeConfig codeConfig = new CodeConfig();

            bool useLocalCopy = this.service.UseLocalDiskCopy(codeConfig);

            Assert.IsTrue(useLocalCopy);
        }
        public void TestProcessConfig()
        {
            Mock<IVisualStudioService> mockVisualStudioService = new Mock<IVisualStudioService>();

            Mock<IProjectService> mockProjectService = new Mock<IProjectService>();

            mockVisualStudioService.SetupGet(x => x.CoreProjectService)
                .Returns(mockProjectService.Object);

            CodeConfig codeConfig = new CodeConfig();

            service.ProcessConfig(
                mockVisualStudioService.Object,
                mockProjectService.Object,
                codeConfig);
        }
        public void TestNugetRequestedAndNotSupportedTrue()
        {
            this.mockSettingsService.SetupGet(x => x.UseNugetForPlugins).Returns(true);

            CodeConfig codeConfig = new CodeConfig();

            bool supported = this.service.NugetRequestedAndNotSupported(codeConfig);

            Assert.IsTrue(supported);
        }
 /// <summary>
 /// Uses the local disk copy.
 /// </summary>
 /// <param name="codeConfig">The code config.</param>
 /// <returns>True or false.</returns>
 public bool UseLocalDiskCopy(CodeConfig codeConfig)
 {
     return this.settingsService.UseNugetForPlugins == false ||
            string.IsNullOrEmpty(this.GetNugetCommand(codeConfig));
 }
        /// <summary>
        /// Adds the core.
        /// </summary>
        /// <param name="projectService">The project service.</param>
        /// <param name="plugin">The plugin.</param>
        /// <param name="codeConfig">The code config.</param>
        /// <param name="addPluginFromLocalDisk">if set to <c>true</c> [add plugin from local disk].</param>
        /// <param name="destination">The destination.</param>
        /// <param name="source">The source.</param>
        /// <returns>True or false.</returns>
        internal bool AddCorePlugin(
            IProjectService projectService,
            Plugin plugin,
            CodeConfig codeConfig,
            bool addPluginFromLocalDisk,
            string destination,
            string source)
        {
            TraceService.WriteLine("PluginService::AddCorePlugin plugin=" + plugin.FriendlyName);

            //// inform the user that we cant install from nuget.
            if (this.codeConfigService.NugetRequestedAndNotSupported(codeConfig))
            {
                this.Messages.Add(
                    plugin.FriendlyName
                    + " plugin does not support being installed via Nuget and has been installed from the local machine.");
            }

            //// don't need to add reference if we are going to use nuget.
            if (addPluginFromLocalDisk)
            {
                //// only do if destination file doesn't exist
                if (this.fileSystem.File.Exists(destination) == false)
                {
                    projectService.AddReference(
                        "Lib",
                        destination,
                        source,
                        this.settingsService.IncludeLibFolderInProjects,
                        this.settingsService.CopyAssembliesToLibFolder);
                }
            }

            return true;
        }
 /// <summary>
 /// Nuget has been requested and is not supported.
 /// </summary>
 /// <param name="codeConfig">The code config.</param>
 /// <returns>True or false.</returns>
 public bool NugetRequestedAndNotSupported(CodeConfig codeConfig)
 {
     return this.settingsService.UseNugetForPlugins &&
            string.IsNullOrEmpty(this.GetNugetCommand(codeConfig));
 }
        /// <summary>
        /// Updates the service via nuget.
        /// </summary>
        /// <param name="projectService">The project service.</param>
        /// <param name="codeConfig">The code config.</param>
        /// <returns>True if updated via nuget.</returns>
        internal bool UpdateServiceViaNuget(
            IProjectService projectService,
            CodeConfig codeConfig)
        {
            TraceService.WriteLine("ServicesService::UpdateViaNuget");

            return this.UpdateViaNuget(projectService, codeConfig);
        }
        /// <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);
                                }
                            }
                        }
                    }
                }
            }
        }
        public void TestProcessCodeConfig()
        {
            SettingsService settingsService = new SettingsService();

            this.mockSettingsService.SetupGet(x => x.ConfigPath).Returns(settingsService.ConfigPath);

            Mock<IProjectService> mockProjectService = new Mock<IProjectService>();
            mockProjectService.SetupGet(x => x.Name).Returns("Adrian.WindowsPhone");

            CodeConfig codeConfig = new CodeConfig
            {
                References = new List<string> { "test" }
            };

            MockPathBase mockPathBase = new MockPathBase();

            this.mockFileSystem.SetupGet(x => x.Path).Returns(mockPathBase);

            ////this.mockSettingsService.SetupGet(x => x.UseNugetForPlugins).Returns(false);

            /*this.service.ProcessCodeConfig(mockProjectService.Object, "SQLite", "source", "destination");

            mockProjectService.Verify(
                x => x.AddReference(
                It.IsAny<string>(),
                It.IsAny<string>(),
                It.IsAny<string>(),
                It.IsAny<bool>(),
                It.IsAny<bool>()));*/
        }
        public void TestUseLocalDiskCopyShouldBeFalse()
        {
            this.mockSettingsService.SetupGet(x => x.UseNugetForPlugins).Returns(true);

            CodeConfig codeConfig = new CodeConfig
                                        {
                                            NugetPackage = "nugetPackage"
                                        };

            bool useLocalCopy = this.service.UseLocalDiskCopy(codeConfig);

            Assert.IsTrue(!useLocalCopy);
        }
        public void TestGetBootstrapFileNameIsOveridden()
        {
            CodeConfig codeConfig = new CodeConfig
            {
                BootstrapFileNameOverride = "OverrideBootstrap.cs"
            };

            string fileName = this.service.GetBootstrapFileName(codeConfig, "friendlyName");

            Assert.IsTrue(fileName == "OverrideBootstrap.cs");
        }
        /// <summary>
        /// Updates the via nuget.
        /// </summary>
        /// <param name="projectService">The project service.</param>
        /// <param name="codeConfig">The code config.</param>
        /// <returns>True if updated via nuget.</returns>
        internal bool UpdateViaNuget(
            IProjectService projectService,
            CodeConfig codeConfig)
        {
            TraceService.WriteLine("BaseCodeService::UpdateViaNuget");

            bool updated = false;

            if (codeConfig != null &&
                string.IsNullOrEmpty(codeConfig.NugetPackage) == false)
            {
                string command = Settings.NugetInstallPackage.Replace("%s", codeConfig.NugetPackage);

                //// need to add the project to the end of the command!
                command += string.Format(" {0}", projectService.Name);

                this.NugetCommands.Add(command);

                updated = true;
            }

            return updated;
        }