private static string CreateBackgroundScript(IAppInfo appInfo, BoundsSpecification bounds)
        {
            var windoInfoJson = new StringBuilder();

            if (!string.IsNullOrEmpty(appInfo.Launch.Id))
            {
                windoInfoJson.AppendFormat(", 'id': '{0}'", appInfo.Launch.Id);
            }

            if (!appInfo.Launch.AutoSaveLocation)
            {
                windoInfoJson.Append(", 'autoSaveLocation': false");
            }

            var boundsJson = bounds != null ? (", 'outerBounds' : " + JsonConvert.SerializeObject(bounds)) : string.Empty;
            var background = string.Format(@"
                paragon.app.window.create('{0}', {{
                        'frame': {{
                            'type': 'notSpecified'
                        }}
                        {1}
                        {2}
                    }});
                ",
                                           appInfo.Launch.WebUrl,
                                           windoInfoJson,
                                           boundsJson);

            return(background);
        }
        public ApplicationPackage ToPackagedApplicationPackage()
        {
            var manifest = Manifest;

            if (manifest == null)
            {
                throw new Exception("No manifest in package");
            }

            var app = manifest.App;

            if (app == null)
            {
                throw new Exception("Missing application info in the manifest");
            }

            var launch = app.Launch;

            if (launch == null)
            {
                throw new Exception("Missing application launch information");
            }

            if (manifest.Type == ApplicationType.Hosted)
            {
                Uri result = null;
                if (Uri.TryCreate(app.Launch.WebUrl, UriKind.RelativeOrAbsolute, out result))
                {
                    if (!result.IsAbsoluteUri)
                    {
                        throw new Exception("Missing absolute uri in the manifest");
                    }
                }
                else
                {
                    throw new Exception("Invalid app launch web url in the manifest");
                }
            }

            var bounds = new BoundsSpecification
            {
                Width  = launch.Width >= 0 ? launch.Width : 1000,
                Height = launch.Height >= 0 ? launch.Height : 750
            };

            if (launch.Left >= 0)
            {
                bounds.Left = launch.Left;
            }
            if (launch.Top >= 0)
            {
                bounds.Top = launch.Top;
            }
            if (launch.MaxWidth >= 0)
            {
                bounds.MaxWidth = launch.MaxWidth;
            }
            if (launch.MaxHeight >= 0)
            {
                bounds.MaxHeight = launch.MaxHeight;
            }
            if (launch.MinWidth >= 0)
            {
                bounds.MinWidth = launch.MinWidth;
            }
            if (launch.MinHeight >= 0)
            {
                bounds.MinHeight = launch.MinHeight;
            }

            var stream       = new MemoryStream();
            var newPackage   = Package.Open(stream, FileMode.Create);
            var partUri      = PackUriHelper.CreatePartUri(new Uri("background.js", UriKind.Relative));
            var bgScriptPart = newPackage.CreatePart(partUri, "text/javascript");

            var partStream = bgScriptPart.GetStream();

            using (var sr = new StreamWriter(partStream))
            {
                sr.WriteLine(CreateBackgroundScript(app, bounds));
                sr.Flush();
                partStream.Flush();
            }
            // Create icons in package
            if (manifest.Icons != null)
            {
                CreateIconPart(newPackage, manifest.Icons.Icon16);
                CreateIconPart(newPackage, manifest.Icons.Icon128);
            }
            else
            {
                // Set up the favicon as the icon for the application
                var uri  = new Uri(app.Launch.WebUrl);
                var path = string.IsNullOrEmpty(uri.Query) ? uri.PathAndQuery : uri.PathAndQuery.Substring(0, uri.PathAndQuery.IndexOf(uri.Query));
                path          += path.EndsWith("/") ? "favicon.ico" : "/favicon.ico";
                manifest.Icons = new IconInfo()
                {
                    Icon128 = string.Format("{0}://{1}{2}{3}", uri.Scheme, uri.Host, uri.IsDefaultPort ? string.Empty : (":" + uri.Port.ToString()), path)
                };
            }

            newPackage.Flush();
            newPackage.Close();
            stream.Flush();

            newPackage     = Package.Open(stream, FileMode.Open);
            app.Launch     = null;
            app.Background = new BackgroundInfo
            {
                Scripts = new[] { "background.js" }
            };
            return(new ApplicationPackage(newPackage, manifest));
        }