예제 #1
0
        //Remove a package from the packages directory.
        public void RemovePackage(string[] args)
        {
            //Check the argument is there.
            if (args.Length < 1)
            {
                Error.FatalNoContext("No packages given to remove.");
            }

            //Deserialize package file.
            SharpiePackages packages = JsonConvert.DeserializeObject <SharpiePackages>(File.ReadAllText(PackagesFile));

            //For each argument, process the package.
            foreach (var pkg in args)
            {
                //Does the package exist?
                if (!packages.PackageExists(pkg))
                {
                    Error.WarningNoContext("A package with the name '" + pkg + "' does not exist in the master list, so has been skipped.");
                    continue;
                }

                //Package does exist, get it and check if it's installed.
                SharpiePackage pkgObj = packages.GetPackage(pkg);
                if (!pkgObj.Installed)
                {
                    Error.WarningNoContext("The package '" + pkg + "' was not installed, so has not been removed. Skipping.");
                    continue;
                }

                //Delete the package directory, mark as uninstalled.
                string pkgDir = CPFilePath.GetPlatformFilePath(new string[] { PackagesDirectory, pkg });
                try
                {
                    Directory.Delete(pkgDir, true);
                } catch (Exception e)
                {
                    Error.WarningNoContext("Failed to delete package directory for the package '" + pkg + "' (" + e.Message + "), skipping.");
                    continue;
                }

                //Mark as uninstalled, serialize back.
                pkgObj.Installed = false;
                packages.SetPackage(pkg, pkgObj);
                File.WriteAllText(PackagesFile, JsonConvert.SerializeObject(packages));

                //Confirm success.
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Successfully removed package '" + pkg + "'.");
                Console.ForegroundColor = ConsoleColor.White;
            }
        }
예제 #2
0
        //Add a package to the packages directory.
        public void AddPackage(string[] args)
        {
            //Check the argument is there.
            if (args.Length < 1)
            {
                Error.FatalNoContext("No packages given to install.");
            }

            //Deserialize package file.
            SharpiePackages packages = JsonConvert.DeserializeObject <SharpiePackages>(File.ReadAllText(PackagesFile));

            //For each argument, process the package.
            foreach (var pkg in args)
            {
                //Check if this package exists in the master list.
                if (!packages.PackageExists(pkg))
                {
                    Error.WarningNoContext("No package exists in the master list with name '" + pkg + "' to install, it has been skipped.");
                    continue;
                }

                //It does, so attempt to do a HTTP grab from the link to get the file.
                SharpiePackage pkgObj = packages.GetPackage(pkg);
                if (pkgObj.Installed == true)
                {
                    Error.WarningNoContext("The package '" + pkg + "' is already installed, so it has been skipped.");
                    continue;
                }

                string pkgZipFile = CPFilePath.GetPlatformFilePath(new string[] { PackagesDirectory, pkg + ".zip" });
                using (var client = new WebClient())
                {
                    try
                    {
                        client.DownloadFile(pkgObj.Link, pkgZipFile);
                    }
                    catch (Exception e)
                    {
                        Error.WarningNoContext("An unknown error occured when downloading the given package '" + pkg + "' (" + e.Message + "), package skipped.");
                        continue;
                    }
                }

                //Unzip the file into the packages directory. (/packages/somepkgname)
                try
                {
                    ZipFile.ExtractToDirectory(pkgZipFile, CPFilePath.GetPlatformFilePath(new string[] { PackagesDirectory, pkg }));
                } catch (Exception e)
                {
                    Error.WarningNoContext("An unknown error occured when extracting the given package '" + pkg + "' (" + e.Message + "), package skipped.");
                    continue;
                }

                //Deleting the zip file.
                try
                {
                    File.Delete(pkgZipFile);
                }
                catch (Exception e)
                {
                    Error.FatalNoContext("Failed to delete temporary file '" + pkgZipFile + "', given error: " + e.Message);
                    return;
                }

                //Mark as installed in sharpie packages.
                pkgObj.Installed             = true;
                pkgObj.CurrentPackageVersion = pkgObj.PackageVersion;

                //Save to object, serialize again.
                packages.SetPackage(pkg, pkgObj);
                File.WriteAllText(PackagesFile, JsonConvert.SerializeObject(packages));

                //Notify of successful installation.
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Successfully installed package '" + pkg + "'.");
                Console.ForegroundColor = ConsoleColor.White;
            }
        }