Exemplo n.º 1
0
        public void Install()
        {
            foreach (var d in Dependencies)
            {
                d.Install();
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Installing " + Name);
            Console.ResetColor();

            var vpmglobal       = new VpmGlobals(this);
            var assemblies      = VpmConfig.Instance.ReferencedAssemblies;
            var nugetassemblies = new List <Assembly>();

            if (NugetPackages != null && NugetPackages.Count > 0)
            {
                Console.WriteLine("Initializing Nuget for this pack");
                var packdir     = Path.Combine(TempDir, "NugetPackages");
                var packdirinfo = Directory.CreateDirectory(packdir);
                var repo        = VpmConfig.Instance.DefaultNugetRepository;
                var packman     = new PackageManager(repo, packdir);
                packman.PackageInstalled += (sender, args) =>
                {
                    Console.WriteLine("Installed " + args.Package.Id);
                };
                for (int i = 0; i < NugetPackages.Count; i++)
                {
                    var packnode = NugetPackages[i];
                    Console.WriteLine("Installing Nuget Package " + packnode.InnerText);
                    var version = packnode.Attributes?["version"]?.Value;

                    var      packages = repo.FindPackagesById(packnode.InnerText);
                    IPackage package  = null;
                    foreach (var p in packages)
                    {
                        bool versioncheck = p.IsLatestVersion;
                        if (version != null)
                        {
                            versioncheck = SemanticVersion.Parse(version) == p.Version;
                        }

                        if (versioncheck)
                        {
                            package = p;
                            break;
                        }
                    }
                    if (package == null)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("No nuget package found with those conditions");
                        Console.ResetColor();
                    }
                    else
                    {
                        packman.InstallPackage(package, false, true);
                    }
                }
                var dlls = packdirinfo.EnumerateDirectories().Where(info => info.GetDirectories("lib").Length > 0).Select(packinfo =>
                {
                    var libdir     = packinfo.GetDirectories("lib")[0];
                    var netversion = libdir.EnumerateDirectories("net*").Max(libinfo =>
                    {
                        var regexc  = new Regex(@"net(\d*?$)");
                        var matches = regexc.Matches(libinfo.Name);
                        if (matches.Count == 0)
                        {
                            return(0);
                        }
                        return(int.Parse(matches[0].Groups[1].Value));
                    });
                    return(libdir.GetDirectories("net" + netversion)[0].GetFiles("*.dll"));
                });
                foreach (var fileInfos in dlls)
                {
                    foreach (var file in fileInfos)
                    {
                        try
                        {
                            var ass = Assembly.LoadFrom(file.FullName);
                            Console.WriteLine("Loaded assembly " + file.Name);
                            if (!(assemblies.Contains(ass) || nugetassemblies.Contains(ass)))
                            {
                                nugetassemblies.Add(ass);
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
            try
            {
                if (VpmConfig.Instance.Arguments.UseEmbeddedScript)
                {
                    ScriptDebug.SelectedDebugScript(vpmglobal);
                }
                else
                {
                    CSharpScript.EvaluateAsync(InstallScript,
                                               globals: vpmglobal,
                                               options: ScriptOptions.Default.WithReferences(assemblies.Concat(nugetassemblies)));
                }
            }
            catch (Exception e)
            {
                if (e is CompilationErrorException)
                {
                    var ee = (CompilationErrorException)e;
                    Console.WriteLine("Compilation error:");
                    Console.WriteLine(string.Join(Environment.NewLine, ee.Diagnostics));
                }
                else
                {
                    Console.WriteLine("Script error:");
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                }
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey(true);
                VpmUtils.CleanUp();
                Environment.Exit(0);
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("vpm at your service!");
            Console.ResetColor();
            try
            {
                VpmConfig.Instance.Arguments = Args.Parse <VpmArgs>(args);
            }
            catch (ArgException ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.ResetColor();
                Console.WriteLine(ArgUsage.GenerateUsageFromTemplate <VpmArgs>());
                VpmUtils.CleanUp();
                Environment.Exit(0);
            }
            try
            {
                UpdateTimer.Instance.Reset();
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Parsing input Pack");
                Console.ResetColor();
                var vpxml = VpmUtils.ParseAndValidateXmlFile(VpmConfig.Instance.Arguments.VPackFile);

                var namenode    = vpxml.SelectSingleNode("/vpack/meta/name");
                var srcnode     = vpxml.SelectSingleNode("/vpack/meta/source");
                var aliasesnode = vpxml.SelectSingleNode("/vpack/meta/aliases");

                if (namenode == null)
                {
                    throw new Exception("VPack name is not specified");
                }

                var name = namenode.InnerText.Trim();

                var src = "";
                if (srcnode != null)
                {
                    src = srcnode.InnerText.Trim();
                }

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Input pack is " + name);
                Console.ResetColor();

                VpmConfig.Instance.WaitSignal = true;
                VpmConfig.Instance.WinApp.BeginInvoke(() =>
                {
                    var winapp        = VpmConfig.Instance.WinApp;
                    var window        = VpmConfig.Instance.DirWindow = new ChooseDir(VpmConfig.Instance.Arguments.VVVVDir);
                    winapp.MainWindow = window;
                    window.Show();
                });
                VpmUtils.Wait();
                var dirwindow = (ChooseDir)VpmConfig.Instance.DirWindow;
                if (dirwindow.Cancelled)
                {
                    //VpmUtils.CleanUp();
                    Environment.Exit(0);
                }
                VpmConfig.Instance.Arguments.VVVVDir = dirwindow.PathResult;

                if (!Directory.Exists(VpmConfig.Instance.Arguments.VVVVDir))
                {
                    Console.WriteLine("Destination directory doesn't exist. Creating it.");
                    try
                    {
                        Directory.CreateDirectory(VpmConfig.Instance.Arguments.VVVVDir);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Couldn't do that.");
                        throw;
                    }
                }

                List <string> aliaslist = null;
                if (aliasesnode != null)
                {
                    var antext = aliasesnode.InnerText;
                    aliaslist = antext.Split(',').ToList();
                    for (int j = 0; j < aliaslist.Count; j++)
                    {
                        aliaslist[j] = aliaslist[j].Trim();
                    }
                }
                string matchalias;
                if (VpmUtils.IsPackExisting(name, aliaslist, out matchalias))
                {
                    Console.WriteLine("Input pack seems to be already existing as " + matchalias);
                    if (VpmUtils.PromptYayOrNay("Do you want to overwrite?"))
                    {
                        var packdir = Path.Combine(VpmConfig.Instance.Arguments.VVVVDir, "packs", matchalias);
                        VpmUtils.DeleteDirectory(packdir, true);
                    }
                    else
                    {
                        VpmUtils.CleanUp();
                        Environment.Exit(0);
                    }
                }
                var vpack = new VPack(name, src, aliaslist, vpxml);

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Initialization complete.");
                Console.ResetColor();

                if (!VpmConfig.Instance.Arguments.Quiet)
                {
                    if (!VpmUtils.PromptYayOrNay(
                            "Vpm does not ask individual licenses anymore. " +
                            "It is the user's responsibility to know and fully comply with the licenses " +
                            "of the currently installed pack and all of its dependencies.\n" +
                            "Do you agree?"))
                    {
                        VpmUtils.CleanUp();
                        Environment.Exit(0);
                    }
                }

                vpack.Install();
                VpmUtils.CleanUp();

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("All good in theory.");
                Console.WriteLine("Enjoy!");
                Thread.Sleep(5000);
            }
            catch (Exception e)
            {
                Console.WriteLine("Something went wrong:");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(e.Message);
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine(e.StackTrace);
                VpmUtils.CleanUp();
                Console.ResetColor();
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey(true);
                Environment.Exit(0);
            }
        }