Example #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;
            }
        }
Example #2
0
        //Update a given Algo package.
        public void UpdatePackage(string[] args)
        {
            //Check the argument is there.
            if (args.Length < 1)
            {
                Error.FatalNoContext("No packages given to update.");
            }

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

            //For each argument, process the package.
            if (args[0] == "*")
            {
                //If the argument is '*', it means all installed packages.
                args = packages.Packages.Where(x => x.Installed == true).Select(x => x.PackageName).ToArray();
            }

            foreach (var pkg in args)
            {
                //Does the package exist?
                if (!packages.PackageExists(pkg))
                {
                    Error.WarningNoContext("No package with the name '" + pkg + "' exists in the master list, skipping.");
                    continue;
                }

                //Is it installed?
                SharpiePackage pkgObj = packages.GetPackage(pkg);
                if (!pkgObj.Installed)
                {
                    Error.WarningNoContext("The package '" + pkg + "' is not installed, so cannot update. Skipping.");
                    continue;
                }

                //Is the current version less than the version?
                if (pkgObj.CurrentPackageVersion >= pkgObj.PackageVersion)
                {
                    Console.WriteLine("No update required for package '" + pkg + "'.");
                    continue;
                }

                //It needs updating. Attempt to grab from the source.
                Console.WriteLine("Package '" + pkg + "' requires an update, reinstalling...");
                RemovePackage(new string[] { pkg });
                AddPackage(new string[] { pkg });
                Console.WriteLine("Successfully updated package '" + pkg + "' to the latest version (" + pkgObj.PackageVersion + ").");
            }
        }
Example #3
0
        /// <summary>
        /// Adds a source to the Sharpie master list.
        /// </summary>
        public void AddSource(string[] args)
        {
            //Check the argument length.
            if (args.Length < 1)
            {
                Error.FatalNoContext("No source link(s) supplied to add.");
            }

            //Deserialize source list and package list.
            SharpieSources  sources  = JsonConvert.DeserializeObject <SharpieSources>(File.ReadAllText(SourcesFile));
            SharpiePackages packages = JsonConvert.DeserializeObject <SharpiePackages>(File.ReadAllText(PackagesFile));

            //Has the warning been read already? If not, read the warning.
            if (!sources.SourceWarningRead)
            {
                DisplayWarning(); sources.SourceWarningRead = true;
            }

            //Loop the sources, and add them.
            foreach (var source in args)
            {
                //Try and download the source.
                string srcString = "";
                using (var client = new WebClient())
                {
                    try
                    {
                        srcString = client.DownloadString(source);
                    }
                    catch (Exception e)
                    {
                        Error.WarningNoContext("An unknown error occured when downloading the source '" + source + "' (" + e.Message + "), source skipped.");
                        continue;
                    }
                }

                //Attempt to parse the source into a source object.
                Tuple <SharpieSource, List <SharpiePackage> > parsed = SharpieSourceParser.Parse(srcString, source);

                //Check if a source with this name already exists.
                if (sources.SourceExists(parsed.Item1.SourceName))
                {
                    Error.WarningNoContext("A source with the name '" + parsed.Item1.SourceName + "' is already installed, so skipping.");
                    continue;
                }

                //No, so add the packages and source.
                Console.WriteLine("Attempting to add source packages for source '" + parsed.Item1.SourceName + "'...");
                foreach (var pkg in parsed.Item2)
                {
                    if (packages.PackageExists(pkg.PackageName))
                    {
                        Error.WarningNoContext("A package with the name '" + pkg.PackageName + "' already exists, so skipping.");
                        continue;
                    }

                    packages.Packages.Add(pkg);
                    Console.WriteLine("Added package '" + pkg.PackageName + "'.");
                }

                sources.Sources.Add(parsed.Item1);

                //Serializing and saving.
                File.WriteAllText(SourcesFile, JsonConvert.SerializeObject(sources));
                File.WriteAllText(PackagesFile, JsonConvert.SerializeObject(packages));
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Successfully added source '" + parsed.Item1.SourceName + "'.");
                Console.ForegroundColor = ConsoleColor.White;
            }
        }
Example #4
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;
            }
        }