private static void AddPackage(string folder, string packageName, string version = null, bool restore = false)
        {
            var packagesFolder = Path.Combine(folder, "packages");

            if (!Directory.Exists(packagesFolder))
            {
                Directory.CreateDirectory(packagesFolder);
            }

            bool hasVersion = !string.IsNullOrWhiteSpace(version);

            string name      = packageName + (hasVersion ? "." + version : "");
            string localFile = Path.Combine(packagesFolder, name + ".nupkg");

            if (File.Exists(localFile))
            {
                File.Delete(localFile);
            }

            var msg = $"Installing {packageName} ";

            if (!ConsoleSpinner.DumbTerm)
            {
                if (msg.Length <= 29)
                {
                    msg = msg.PadRight(29);
                }
            }

            Console.Write(msg);
            if (Directory.Exists(Path.Combine(packagesFolder, name)))
            {
                Warn("skipped (already exists)");

                return;
            }

            try
            {
                string packageFolder        = null;
                bool   exists               = false;
                bool   success              = false;
                var    repos                = GetRepos();
                PackageRequestResult result = null;
                using (var spinner = new ConsoleSpinner())
                {
                    spinner.Start();
                    foreach (var repo in repos)
                    {
                        result = RequestPackage(repo, packageName, version, localFile, packagesFolder);
                        if (result.Success)
                        {
                            exists        = result.Exists;
                            packageFolder = result.Folder;
                            success       = true;
                            break;
                        }
                    }
                }

                if (!success)
                {
                    if (result != null && result.Exception != null)
                    {
                        Error("failed.");
                        throw result.Exception;
                    }
                    else
                    {
                        Error("not found.");
                    }
                }
                else if (exists)
                {
                    Warn("skipped (already exists)");
                }
                else
                {
                    Info("done.");

                    if (!restore)
                    {
                        RemovePackage(folder, packageName);
                    }

                    Directory.CreateDirectory(packageFolder);
                    ZipFile.ExtractToDirectory(localFile, packageFolder);
                    File.Move(localFile, Path.Combine(packageFolder, Path.GetFileName(packageFolder) + ".nupkg"));

                    PackageInfo info = null;

                    if (!restore)
                    {
                        info = ReadPackageInfo(packageName, packageFolder);
                        AddPackageToConfig(folder, info.Id, info.Version);
                    }

                    CleanPackageAfterDownload(packageFolder);

                    if (info != null && !restore)
                    {
                        DownloadDependencies(folder, info);
                    }
                }
            }
            catch (Exception e)
            {
                var exceptionChain = "";
                var currException  = e;

                while (currException != null)
                {
                    exceptionChain += "- " + currException.Message + nl;
                    currException   = currException.InnerException;
                }
                Error("Error: " + nl + exceptionChain);
#if DEBUG
                throw e;
#endif
            }
        }
Exemple #2
0
        private static int Main(string[] args)
        {
            var currentDir = Environment.CurrentDirectory;
            var msg        = "";

            CoreFolder = GetCoreFolder(currentDir);
            var resolver = new AssemblyResolver(CoreFolder);

            AppDomain.CurrentDomain.AssemblyResolve += resolver.CurrentDomain_AssemblyResolve;

            TranslatorAssembly = GetTranslatorAssembly(CoreFolder);
            ContractAssembly   = GetContractAssembly(CoreFolder);
            EnablePrerelease   = false;

            if (!EnsureMinimalCompilerVersion())
            {
                return(1);
            }

            if (args.Length == 0)
            {
                ShowHelp();

                return(1);
            }

            bool    skip          = false;
            bool    run           = false;
            dynamic bridgeOptions = null;

            /*try
             * {
             *  bridgeOptions = GetBridgeOptionsFromCommandLine(currentDir, args, ref skip, ref run);
             * }
             * catch(Exception)
             * {
             *  Error("Invalid command line");
             *  return 1;
             * }*/
            bridgeOptions = GetBridgeOptionsFromCommandLine(currentDir, args, ref skip, ref run);

            if (bridgeOptions == null)
            {
                ShowHelp();

                return(1);
            }

            if (skip)
            {
                return(0);
            }

            if (bridgeOptions.Rebuild && File.Exists(bridgeOptions.Lib))
            {
                File.Delete(bridgeOptions.Lib);
            }

            if (bridgeOptions.BridgeLocation == null)
            {
                bridgeOptions.BridgeLocation = GetBridgeLocation(currentDir);
            }

            msg = $"Building {Path.GetFileName(currentDir)}";

            if (msg.Length >= 28)
            {
                msg += "  ";
            }
            else
            {
                msg = msg.PadRight(29);
            }

            Info(msg, false);

            using (var spinner = new ConsoleSpinner())
            {
                spinner.Start();
                var     logger    = CreateLogger();
                dynamic processor = CreateTranslatorProcessor(bridgeOptions, logger);

                processor.PreProcess();

                try
                {
                    processor.Process();
                    var outputPath = processor.PostProcess();

                    if (run)
                    {
                        var htmlFile = Path.Combine(outputPath, "index.html");

                        if (File.Exists(htmlFile))
                        {
                            System.Diagnostics.Process.Start(htmlFile);
                        }
                    }
                }
                catch (Exception ex)
                {
                    spinner.Stop();

                    var exceptionType = ex.GetType();

                    if (GetEmitterException().IsAssignableFrom(exceptionType))
                    {
                        dynamic dex = ex;
                        Error(string.Format("Bridge.NET Compiler error: {2} ({3}, {4}) {0} {1}", ex.Message, ex.StackTrace, dex.FileName, dex.StartLine, dex.StartColumn));

                        return(1);
                    }

                    var ee = processor.Translator != null?processor.Translator.CreateExceptionFromLastNode() : null;

                    if (ee != null)
                    {
                        Error(string.Format("Bridge.NET Compiler error: {2} ({3}, {4}) {0} {1}", ex.Message, ex.StackTrace, ee.FileName, ee.StartLine, ee.StartColumn));
                    }
                    else
                    {
                        // Iteractively print inner exceptions
                        var ine  = ex;
                        var elvl = 0;

                        while (ine != null)
                        {
                            Error(string.Format("Bridge.NET Compiler error: exception level: {0} - {1}\nStack trace:\n{2}", elvl++, ine.Message, ine.StackTrace));
                            ine = ine.InnerException;
                        }
                    }


                    return(1);
                }
            }

            Info("done.");

            return(0);
        }
        private static void InstallTemplate(string path)
        {
            var rootPath      = Path.GetDirectoryName(typeof(Program).Assembly.Location);
            var templatesPath = Path.Combine(rootPath, Constants.TemplatesFolder);

            if (File.Exists(path))
            {
                try
                {
                    ZipFile.ExtractToDirectory(path, templatesPath);
                }
                catch (Exception e)
                {
                    Error($"The error during template's archive extraction: {e.Message}");

                    return;
                }
            }
            else
            {
                var isFile = true;

                try
                {
                    var uri = new Uri(path);
                    isFile = uri.IsFile;
                }
                catch (Exception)
                {
                }

                if (isFile)
                {
                    Error("Template file doesn't exist");

                    return;
                }

                WriteLine("Downloading template ", false);

                using (var spinner = new ConsoleSpinner())
                {
                    spinner.Start();

                    try
                    {
                        var localFile = Path.GetTempFileName();

                        WebClient client = new WebClient();
                        client.DownloadFile(path, localFile);
                        client.Dispose();
                        ZipFile.ExtractToDirectory(localFile, templatesPath);
                        File.Delete(localFile);
                        WriteLine("done.");

                        return;
                    }
                    catch (Exception e)
                    {
                        Error($"The download error: {e.Message}");

                        return;
                    }
                }
            }

            WriteLine($"Template has been installed");
        }