Exemple #1
0
        internal static string PerlArchivePath(StrawberryPerl perl)
        {
            string path;

            try
            {
                if (!Directory.Exists(perl.ArchivePath))
                {
                    Directory.CreateDirectory(perl.ArchivePath);
                }

                return(perl.ArchivePath + @"\" + perl.File);
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine("Error, do not have permissions to create directory: " + perl.ArchivePath);
            }

            Console.WriteLine("Creating temporary directory instead");
            do
            {
                path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            } while (Directory.Exists(path));

            Directory.CreateDirectory(path);

            return(path + @"\" + perl.File);
        }
Exemple #2
0
        private void Extract(StrawberryPerl perl, string archivePath)
        {
            if (File.Exists(archivePath))
            {
                Console.WriteLine("Extracting " + archivePath);
                try
                {
                    if (Debug)
                    {
                        Console.WriteLine("\nExtracting {0} to {1}", archivePath, perl.InstallPath);
                    }

                    using (ZipFile zip = ZipFile.Read(archivePath))
                    {
                        foreach (ZipEntry e in zip)
                        {
                            e.Extract(perl.InstallPath);
                        }
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("\nFailed to extract {0} to {1}", archivePath, perl.InstallPath);
                }
            }
        }
Exemple #3
0
        internal void Exec(StrawberryPerl perl, string command, string sysPath)
        {
            Console.WriteLine("Perl-" + perl.Name + "\n==============");

            Process          process   = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();

            startInfo.WindowStyle = ProcessWindowStyle.Hidden;

            List <String> newPath;

            newPath = perl.Paths;
            newPath.Add(sysPath);

            System.Environment.SetEnvironmentVariable("PATH", String.Join(";", newPath));

            startInfo.FileName  = "cmd.exe";
            startInfo.Arguments = "/c " + perl.PerlPath + @"\" + command;
            process.StartInfo   = startInfo;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError  = true;
            process.StartInfo.UseShellExecute        = false;
            process.Start();

            Console.WriteLine(process.StandardOutput.ReadToEnd());
            Console.WriteLine(process.StandardError.ReadToEnd());
            process.WaitForExit();
        }
Exemple #4
0
        public bool Clone(string sourcePerlName, string destPerlName)
        {
            if (!CheckName(destPerlName))
            {
                return(false);
            }

            StrawberryPerl sourcePerl    = PerlResolveVersion(sourcePerlName);
            string         sourcePerlDir = sourcePerl.InstallPath;
            string         destPerlDir   = this.rootPath + destPerlName;
            DirectoryInfo  src           = new DirectoryInfo(sourcePerlDir);

            if (!src.Exists)
            {
                throw new DirectoryNotFoundException(
                          "Source directory does not exist or could not be found: "
                          + sourcePerlDir
                          );
            }
            try
            {
                if (!Directory.Exists(destPerlDir))
                {
                    Directory.CreateDirectory(destPerlDir);
                }

                foreach (string dirPath in Directory.GetDirectories(sourcePerlDir, "*",
                                                                    SearchOption.AllDirectories))
                {
                    Directory.CreateDirectory(dirPath.Replace(sourcePerlDir, destPerlDir));
                }

                foreach (string newPath in Directory.GetFiles(sourcePerlDir, "*.*",
                                                              SearchOption.AllDirectories))
                {
                    File.Copy(newPath, newPath.Replace(sourcePerlDir, destPerlDir), true);
                }

                if (!Directory.Exists(destPerlDir))
                {
                    Console.WriteLine("\nfailed to clone {0} to {1}", sourcePerlDir, destPerlDir);
                    Environment.Exit(0);
                }

                PerlRegisterCustomInstall(destPerlName, sourcePerl);

                Console.WriteLine("\nSuccessfully installed custom perl '{0}'", destPerlName);
                return(true);
            }
            catch (System.IO.IOException err)
            {
                Console.WriteLine("\nClone failed due to disk I/O error... ensure the disk isn't full\n");
                if (Debug)
                {
                    Console.WriteLine(err);
                }
                return(false);
            }
        }
Exemple #5
0
        internal void PathAddPerl(StrawberryPerl perl)
        {
            string        path    = PathGet();
            List <string> newPath = perl.Paths;

            newPath.Add(path);
            PathSet(newPath);
        }
Exemple #6
0
        public string Install(string version)
        {
            StrawberryPerl perl         = PerlResolveVersion(version);
            string         archive_path = Fetch(perl);

            Extract(perl, archive_path);
            Available();
            return(perl.Name);
        }
Exemple #7
0
 internal static bool PerlIsInstalled(StrawberryPerl perl)
 {
     if (Directory.Exists(perl.InstallPath) &&
         File.Exists(perl.PerlPath + @"\perl.exe"))
     {
         return(true);
     }
     return(false);
 }
Exemple #8
0
        private string Fetch(StrawberryPerl perl)
        {
            WebClient webClient   = new WebClient();
            string    archivePath = PerlArchivePath(perl);

            if (!File.Exists(archivePath))
            {
                Console.WriteLine("Downloading " + perl.Url + " to " + archivePath);
                webClient.DownloadFile(perl.Url, archivePath);
            }

            Console.WriteLine("Confirming checksum ...");
            using (var cryptoProvider = new SHA1CryptoServiceProvider())
            {
                using (var stream = File.OpenRead(archivePath))
                {
                    string hash = BitConverter.ToString(cryptoProvider.ComputeHash(stream)).Replace("-", "").ToLower();

                    if (perl.Sha1Checksum != hash)
                    {
                        Console.WriteLine("Error checksum of downloaded archive \n"
                                          + archivePath
                                          + "\ndoes not match expected output\nexpected: "
                                          + perl.Sha1Checksum
                                          + "\n     got: " + hash);
                        stream.Dispose();
                        Console.Write("Whould you like berrybrew to delete the corrupted download file? y/n [n]");
                        if (Console.ReadLine() == "y")
                        {
                            string retval = FileRemove(archivePath);
                            if (retval == "True")
                            {
                                Console.WriteLine("Deleted! Try to install it again!");
                            }
                            else
                            {
                                Console.WriteLine("Unable to delete " + archivePath);
                            }
                        }
                        Environment.Exit(0);
                    }
                }
            }
            return(archivePath);
        }
Exemple #9
0
        internal void PerlRegisterCustomInstall(string perlName, StrawberryPerl perlBase = new StrawberryPerl())
        {
            Dictionary <string, object> data = new Dictionary <string, object>();

            data["name"]   = perlName;
            data["custom"] = perlBase.Custom;
            data["file"]   = perlBase.File;
            data["url"]    = perlBase.Url;
            data["ver"]    = perlBase.Version;
            data["csum"]   = perlBase.Sha1Checksum;

            List <Dictionary <string, object> > perlList = new List <Dictionary <string, object> >();

            perlList.Add(data);
            JsonWrite("perls_custom", perlList);

            this.bypassOrphanCheck = true;
        }
Exemple #10
0
        private void Extract(StrawberryPerl perl, string archivePath)
        {
            ZipFile zf = null;

            try
            {
                FileStream fs = File.OpenRead(archivePath);
                zf = new ZipFile(fs);

                foreach (ZipEntry zipEntry in zf)
                {
                    if (!zipEntry.IsFile)
                    {
                        continue;
                    }

                    String entryFileName = zipEntry.Name;
                    byte[] buffer        = new byte[4096]; // 4K is optimum
                    Stream zipStream     = zf.GetInputStream(zipEntry);

                    String fullZipToPath = Path.Combine(perl.InstallPath, entryFileName);
                    string directoryName = Path.GetDirectoryName(fullZipToPath);
                    if (directoryName.Length > 0)
                    {
                        Directory.CreateDirectory(directoryName);
                    }

                    using (FileStream streamWriter = File.Create(fullZipToPath))
                    {
                        ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(zipStream, streamWriter, buffer);
                    }
                }
            }
            finally
            {
                if (zf != null)
                {
                    zf.IsStreamOwner = true;
                    zf.Close();
                }
            }
        }
Exemple #11
0
        public void Switch(string switchToVersion)
        {
            try
            {
                StrawberryPerl perl = PerlResolveVersion(switchToVersion);

                if (!PerlIsInstalled(perl))
                {
                    Console.WriteLine("Perl version " + perl.Name + " is not installed. Run the command:\n\n\tberrybrew install " + perl.Name);
                    Environment.Exit(0);
                }

                PathRemovePerl();
                PathAddPerl(perl);

                Console.WriteLine("Switched to " + switchToVersion + ", start a new terminal to use it.");
            }
            catch (ArgumentException)
            {
                Message.Print("perl_unknown_version");
                Environment.Exit(0);
            }
        }
Exemple #12
0
        public void Available()
        {
            Message.Print("available_header");

            StrawberryPerl currentPerl = PerlInUse();
            List <int>     nameLengths = new List <int>();

            foreach (string perlName in Perls.Keys)
            {
                nameLengths.Add(perlName.Length);
            }

            int maxNameLength = nameLengths.Max();

            foreach (StrawberryPerl perl in Perls.Values)
            {
                string perlNameToPrint = perl.Name + new String(' ', (maxNameLength - perl.Name.Length) + 2);
                Console.Write("\t" + perlNameToPrint);

                if (perl.Custom)
                {
                    Console.Write(" [custom]");
                }
                if (PerlIsInstalled(perl))
                {
                    Console.Write(" [installed]");
                }
                if (perl.Name == currentPerl.Name)
                {
                    Console.Write(" *");
                }

                Console.Write("\n");
            }
            Message.Print("available_footer");
        }
Exemple #13
0
        internal StrawberryPerl PerlInUse()
        {
            string         path        = PathGet();
            StrawberryPerl currentPerl = new StrawberryPerl();

            if (path != null)
            {
                string[] paths = path.Split(';');
                foreach (StrawberryPerl perl in Perls.Values)
                {
                    for (int i = 0; i < paths.Length; i++)
                    {
                        if (paths[i] == perl.PerlPath ||
                            paths[i] == perl.CPath ||
                            paths[i] == perl.PerlSitePath)
                        {
                            currentPerl = perl;
                            break;
                        }
                    }
                }
            }
            return(currentPerl);
        }
Exemple #14
0
        public void PerlRemove(string perlVersionToRemove)
        {
            try
            {
                StrawberryPerl perl        = PerlResolveVersion(perlVersionToRemove);
                StrawberryPerl currentPerl = PerlInUse();

                if (perl.Name == currentPerl.Name)
                {
                    Console.WriteLine("Removing Perl " + perlVersionToRemove + " from PATH");
                    PathRemovePerl();
                }

                if (Directory.Exists(perl.InstallPath))
                {
                    try
                    {
                        FilesystemResetAttributes(perl.InstallPath);
                        Directory.Delete(perl.InstallPath, true);
                        Console.WriteLine("Successfully removed Strawberry Perl " + perlVersionToRemove);
                    }
                    catch (System.IO.IOException err)
                    {
                        Console.WriteLine("Unable to completely remove Strawberry Perl " + perlVersionToRemove + " some files may remain");

                        if (Debug)
                        {
                            Console.WriteLine(err);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Strawberry Perl " + perlVersionToRemove + " not found (are you sure it's installed?");
                    Environment.Exit(0);
                }

                if (perl.Custom)
                {
                    dynamic customPerlList = JsonParse("perls_custom", true);
                    customPerlList = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(customPerlList);

                    List <Dictionary <string, object> > updatedPerls = new List <Dictionary <string, object> >();

                    foreach (Dictionary <string, object> perlStruct in customPerlList)
                    {
                        if (!perlVersionToRemove.Equals(perlStruct["name"].ToString()))
                        {
                            updatedPerls.Add(perlStruct);
                        }
                    }
                    JsonWrite("perls_custom", updatedPerls, true);
                }
            }
            catch (ArgumentException err)
            {
                Message.Print("perl_unknown_version");

                if (Debug)
                {
                    Console.WriteLine(err);
                }

                Environment.Exit(0);
            }
            catch (UnauthorizedAccessException err)
            {
                Console.WriteLine("Unable to remove Strawberry Perl " + perlVersionToRemove + " permission was denied by System");

                if (Debug)
                {
                    Console.WriteLine(err);
                }
            }
        }