/// <summary>
        /// Builds the plugin asynchronous.
        /// </summary>
        /// <param name="msBuild">The ms build.</param>
        /// <returns></returns>
        protected Task BuildPluginAsync(string msBuild)
        {
            var task = new Task(() =>
            {
                string projectFile = Plugin.CombinePaths(PluginPath, Plugin.ProjectFile);
                if (File.Exists(projectFile))
                {
                    //
                    // Initialize a new release builder to process this import operation.
                    //
                    var projectBuilder = new Builders.ProjectBuilder();
                    projectBuilder.StatusTextChanged += ReleaseBuilder_StatusTextChanged;
                    projectBuilder.ConsoleOutput     += ProjectBuilder_ConsoleOutput;

                    if (!projectBuilder.BuildProject(Plugin.CombinePaths(PluginPath, Plugin.ProjectFile), msBuild))
                    {
                        throw new Exception("See build log for details");
                    }
                }

                Dispatcher.Invoke(() => { txtStatus.Text = "Packaging..."; });

                var pluginBuilder         = new Builders.PluginBuilder(Plugin, PluginPath);
                pluginBuilder.LogMessage += ProjectBuilder_ConsoleOutput;
                var stream = pluginBuilder.Build();

                Dispatcher.Invoke(() =>
                {
                    //
                    // Get the filename the user wants to save the plugin into.
                    //
                    var saveFileDialog = new SaveFileDialog
                    {
                        DereferenceLinks = false,
                        FileName         = Plugin.Name.Replace(" ", "") + ".plugin",
                        DefaultExt       = "plugin",
                        Filter           = "Plugin Files (*.plugin)|*.plugin|All Files (*.*)|*.*"
                    };

                    if (saveFileDialog.ShowDialog() == false)
                    {
                        Dispatcher.Invoke(() =>
                        {
                            txtStatus.Text = "Cancelled";
                            UpdateState();
                        });

                        return;
                    }

                    using (var writer = File.Open(saveFileDialog.FileName, FileMode.Create))
                    {
                        stream.CopyTo(writer);
                    }

                    txtStatus.Text = "Package Built";
                    UpdateState();
                });
            });

            task.ContinueWith((t) =>
            {
                if (t.IsFaulted)
                {
                    Dispatcher.Invoke(() =>
                    {
                        txtStatus.Text = "Build Failed";
                        MessageBox.Show(t.Exception.InnerException.Message, "Build Failed", MessageBoxButton.OK);
                        UpdateState();
                    });
                }
            });

            task.Start();

            return(task);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Installs the plugin specified by the plugin spec file.
        /// </summary>
        /// <param name="pluginFile">The plugin spec file.</param>
        /// <exception cref="Exception"></exception>
        public void InstallPlugin(string pluginFile, bool verbose)
        {
            var rockWeb    = Path.Combine(Path.Combine(Support.GetInstancesPath(), Name), "RockWeb");
            var pluginPath = Path.GetDirectoryName(pluginFile);

            if (!Directory.Exists(rockWeb))
            {
                throw new Exception(string.Format("Cannot install plugin '{0}'; instance '{1}' does not exist.", pluginFile, Name));
            }

            var pluginJson = File.ReadAllText(pluginFile);
            var plugin     = JsonConvert.DeserializeObject <Shared.PluginFormat.Plugin>(pluginJson);

            plugin.ConfigureDefaults();

            var logger = new EventHandler <string>((sender, message) =>
            {
                if (verbose)
                {
                    if (message.EndsWith("\n"))
                    {
                        message = message.Substring(0, message.Length - 1);
                    }
                    if (message.EndsWith("\r"))
                    {
                        message = message.Substring(0, message.Length - 1);
                    }

                    var bootstrap = ( Bootstrapper )((Jint.Runtime.Interop.ObjectWrapper)Engine.GetValue("__Bootstrap").AsObject()).Target;
                    bootstrap.Log(message);
                }
            });

            //
            // Initialize a new release builder to process this import operation.
            //
            var projectBuilder = new Builders.ProjectBuilder();

            projectBuilder.ConsoleOutput += logger;

            if (!projectBuilder.BuildProject(plugin.CombinePaths(pluginPath, plugin.ProjectFile), VisualStudioInstall.GetDefaultInstall().GetMsBuild()))
            {
                throw new Exception("Build Plugin Failed");
            }

            var pluginBuilder = new Builders.PluginBuilder(plugin, pluginPath);

            pluginBuilder.LogMessage += logger;

            var stream = pluginBuilder.Build();

            using (var zf = new ZipFile(stream))
            {
                foreach (ZipEntry entry in zf)
                {
                    if (!entry.Name.StartsWith("content/"))
                    {
                        continue;
                    }

                    if (entry.IsFile)
                    {
                        string fullpath  = Path.Combine(rockWeb, entry.Name.Replace("content/", ""));
                        string directory = Path.GetDirectoryName(fullpath);

                        if (!Directory.Exists(directory))
                        {
                            Directory.CreateDirectory(directory);
                        }

                        using (FileStream streamWriter = File.Create(fullpath))
                        {
                            using (var zipStream = zf.GetInputStream(entry))
                            {
                                zipStream.CopyTo(streamWriter);
                            }
                        }
                    }
                }

                var sqlInstallEntry = zf.GetEntry("install/run.sql");
                if (sqlInstallEntry != null)
                {
                    string sqlScript;

                    using (var zipStream = zf.GetInputStream(sqlInstallEntry))
                    {
                        using (StreamReader reader = new StreamReader(zipStream, Encoding.UTF8))
                        {
                            sqlScript = reader.ReadToEnd();
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(sqlScript))
                    {
                        using (var connection = Views.InstancesView.DefaultInstancesView.GetSqlConnection())
                        {
                            var command = connection.CreateCommand();
                            command.CommandText = sqlScript;
                            command.ExecuteNonQuery();
                        }
                    }
                }
            }
        }