Ejemplo n.º 1
0
        public static string Fetch(Configuration.Library library)
        {
            string targetPath;

            log.Info("Fetching library " + library.Name + " from " + library.Uri);

            using (var client = new WebClient())
            {
                var zipPath = Path.Combine(Core.BaseDirectory, $"deps/{library.Name}.zip");

                if (!File.Exists(zipPath))
                {
                    client.DownloadFile(library.Uri, zipPath);
                }

                targetPath = $"Scripts/{library.Name}";

                if (Directory.Exists(targetPath))
                {
                    Directory.Delete(targetPath, recursive: true);
                }

                ZipFile.ExtractToDirectory(zipPath, targetPath);
            }

            return(targetPath);
        }
Ejemplo n.º 2
0
        public Library(Configuration.Library libConfig, Assembly assembly)
        {
            Name     = libConfig.Name;
            Assembly = assembly;

            Assembly.GetTypes();

            Types = Assembly.GetTypes().Where(type => !libConfig.GetIgnoreType(type)).ToArray();

            TypesByName     = new TypeTable(Types.Length);
            TypesByFullName = new TypeTable(Types.Length);

            foreach (var type in Types)
            {
                TypesByName.Add(type.Name, type);
                TypesByFullName.Add(type.FullName, type);

                if (type.IsDefined(typeof(TypeAliasAttribute), false))
                {
                    var attrs = type.GetCustomAttributes(typeof(TypeAliasAttribute), false);

                    if (attrs.Length > 0 && attrs[0] != null)
                    {
                        var attr = attrs[0] as TypeAliasAttribute;

                        foreach (var alias in attr.Aliases)
                        {
                            TypesByFullName.Add(alias, type);
                        }
                    }
                }
            }

            TypeCache = new TypeCache(Types, TypesByName, TypesByFullName);
        }
Ejemplo n.º 3
0
        private static void GetScripts(Configuration.Library libConfig, Hashtable list, string path, string type)
        {
            foreach (string dir in Directory.GetDirectories(path))
            {
                string baseName = Path.GetFileName(dir).ToLower();

                for (int i = 0; i < m_IgnoreNames.Length; i++)
                {
                    if (baseName == m_IgnoreNames[i])
                    {
                        continue;
                    }
                }

                GetScripts(libConfig, list, dir, type);
            }

            foreach (string filename in Directory.GetFiles(path, type))
            {
                // Pass relative filename only.
                if (libConfig == null || !libConfig.GetIgnoreSource(filename))
                {
                    list[filename] = File.GetLastWriteTime(filename);
                }
            }
        }
Ejemplo n.º 4
0
        private static Hashtable GetScripts(Configuration.Library libConfig, string type)
        {
            Hashtable list = new Hashtable();

            GetScripts(libConfig, list, libConfig.SourcePath.FullName, type);

            return(list);
        }
Ejemplo n.º 5
0
        private static Dictionary <string, DateTime> GetScripts(Configuration.Library libConfig, string type)
        {
            var sourceCodeFileProvider = new SourceCodeFileProvider(libConfig, type);

            var files = sourceCodeFileProvider.ProvideSources();

            return(files);
        }
Ejemplo n.º 6
0
        private static Hashtable GetScripts(Configuration.Library libConfig, IEnumerable overlays, string type)
        {
            Hashtable files = GetScripts(libConfig, type);

            if (overlays != null)
            {
                foreach (Configuration.Library overlay in overlays)
                {
                    Hashtable files2 = GetScripts(overlay, type);

                    Overlay(libConfig.SourcePath.FullName, files, overlay.SourcePath.FullName, files2);
                }
            }

            return(files);
        }
Ejemplo n.º 7
0
        public Library(Configuration.Library libConfig, Assembly assembly)
        {
            m_Name     = libConfig.Name;
            m_Assembly = assembly;

            m_Assembly.GetTypes();

            List <Type> typeList = new List <Type>();

            foreach (Type type in m_Assembly.GetTypes())
            {
                if (libConfig == null || !libConfig.GetIgnoreType(type))
                {
                    typeList.Add(type);
                }
            }

            m_Types = typeList.ToArray();

            m_TypesByName     = new TypeTable(m_Types.Length);
            m_TypesByFullName = new TypeTable(m_Types.Length);

            foreach (Type type in m_Types)
            {
                m_TypesByName.Add(type.Name, type);
                m_TypesByFullName.Add(type.FullName, type);

                if (type.IsDefined(typeof(TypeAliasAttribute), false))
                {
                    object[] attrs = type.GetCustomAttributes(typeof(TypeAliasAttribute), false);

                    if (attrs != null && attrs.Length > 0 && attrs[0] != null)
                    {
                        TypeAliasAttribute attr = attrs[0] as TypeAliasAttribute;

                        foreach (string alias in attr.Aliases)
                        {
                            m_TypesByFullName.Add(alias, type);
                        }
                    }
                }
            }

            m_TypeCache = new TypeCache(m_Types, m_TypesByName, m_TypesByFullName);
        }
Ejemplo n.º 8
0
        private static ArrayList SortLibrariesByDepends()
        {
            ArrayList libs  = new ArrayList(Environment.Config.Libraries);
            Hashtable queue = new Hashtable();
            ArrayList dst   = new ArrayList();

            // Handle distro first, for most compatibility.
            Configuration.Library libConfig = Environment.Config.GetLibrary("distro");

            if (libConfig != null)
            {
                EnqueueLibrary(dst, libs, queue, libConfig);
            }

            while (libs.Count > 0)
            {
                EnqueueLibrary(dst, libs, queue, (Configuration.Library)libs[0]);
            }

            return(dst);
        }
Ejemplo n.º 9
0
 public SourceCodeFileProvider(Configuration.Library libConfig, string type)
 {
     m_LibraryConfig = libConfig;
     m_Type          = type;
 }
Ejemplo n.º 10
0
        public static bool Compile(bool debug)
        {
            if (m_AdditionalReferences != null)
            {
                throw new ApplicationException("already compiled");
            }

            m_AdditionalReferences = new List <string>();

            m_Libraries = new List <Library>();
            m_Libraries.Add(new Library(Environment.Config.GetLibrary("core"), Environment.Assembly));

            // Prepare overlays.
            foreach (Configuration.Library libConfig in Environment.Config.Libraries)
            {
                if (libConfig.Overlays == null || !libConfig.Exists ||
                    libConfig.Name == "core")
                {
                    continue;
                }

                if (libConfig.SourcePath == null)
                {
                    Console.WriteLine("Error: Can't overlay the binary library {0}", libConfig.Name);
                    throw new ApplicationException();
                }

                foreach (string name in libConfig.Overlays)
                {
                    Configuration.Library overlay = Environment.Config.GetLibrary(name);

                    if (overlay == null || !overlay.Exists)
                    {
                        Console.WriteLine("Error: Can't overlay {0} with {1}, because it does not exist", libConfig.Name, name);
                        throw new ApplicationException();
                    }

                    if (overlay.SourcePath == null)
                    {
                        Console.WriteLine("Error: Can't overlay {0} with {1}, because it is binary only", libConfig.Name, overlay.Name);
                        throw new ApplicationException();
                    }

                    overlay.Disabled = true;
                }
            }

            foreach (Configuration.Library libConfig in Environment.Config.Libraries)
            {
                if (libConfig.Overlays != null && libConfig.Exists && libConfig.Name != "core" && libConfig.Disabled)
                {
                    Console.WriteLine("Error: Can't overlay library {0} which is already used as overlay for another library", libConfig.Name);
                    throw new ApplicationException();
                }
            }

            // Collect Config.Library objects, sort them and compile.
            ArrayList libConfigs = SortLibrariesByDepends();

            foreach (Configuration.Library libConfig in libConfigs)
            {
                bool result = Compile(libConfig, debug);

                if (!result)
                {
                    return(false);
                }
            }

            // Delete unused cache directories.
            DirectoryInfo cacheDir = new DirectoryInfo(Environment.Config.CacheDirectory);

            foreach (DirectoryInfo sub in cacheDir.GetDirectories())
            {
                string libName = sub.Name.ToLower();

                if (GetLibrary(libName) == null)
                {
                    sub.Delete(true);
                }
            }

            return(true);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Enqueue a library for compilation, resolving all dependencies first.
        /// </summary>
        /// <param name="dst">This array will receive the libraries in the correct order.</param>
        /// <param name="libs">Source libraries.</param>
        /// <param name="queue">Somewhat like a stack of libraries currently waiting.</param>
        /// <param name="libConfig">The library to be added.</param>
        private static void EnqueueLibrary(ArrayList dst, ArrayList libs, Hashtable queue, Configuration.Library libConfig)
        {
            string[] depends = libConfig.Depends;

            if (libConfig.Name == "core" || libConfig.Disabled)
            {
                libs.Remove(libConfig);
                return;
            }

            if (!libConfig.Exists)
            {
                libs.Remove(libConfig);
                Console.WriteLine("Warning: library {0} does not exist", libConfig.Name);
                return;
            }

            /* first resolve dependencies */
            if (depends != null)
            {
                queue[libConfig.Name] = 1;

                foreach (string depend in depends)
                {
                    /* if the depended library is already in the
                     * queue, there is a circular dependency */
                    if (queue.ContainsKey(depend))
                    {
                        Console.WriteLine("Error: Circular library dependency {0} on {1}",
                                          libConfig.Name, depend);
                        throw new ApplicationException();
                    }

                    Configuration.Library next = Environment.Config.GetLibrary(depend);
                    if (next == null || !next.Exists)
                    {
                        Console.WriteLine("Error: Unresolved library dependency: {0} depends on {1}, which does not exist",
                                          libConfig.Name, depend);
                        throw new ApplicationException();
                    }

                    if (next.Disabled)
                    {
                        Console.WriteLine("Error: Unresolved library dependency: {0} depends on {1}, which is disabled",
                                          libConfig.Name, depend);
                        throw new ApplicationException();
                    }

                    if (!dst.Contains(next))
                    {
                        EnqueueLibrary(dst, libs, queue, next);
                    }
                }

                queue.Remove(libConfig.Name);
            }

            /* then add it to 'dst' */
            dst.Add(libConfig);
            libs.Remove(libConfig);
        }
Ejemplo n.º 12
0
        private static bool Compile(Configuration.Library libConfig, bool debug)
        {
            // Check if there is source code for this library.
            if (libConfig.SourcePath == null)
            {
                if (libConfig.BinaryPath == null)
                {
                    Console.WriteLine("Warning: library {0} does not exist", libConfig.Name);
                    return(true);
                }
                else if (!libConfig.BinaryPath.Exists)
                {
                    Console.WriteLine("Warning: library {0} does not exist: {1}", libConfig.Name, libConfig.BinaryPath);
                    return(false);
                }

                Console.WriteLine("Libraries: Loading library {0}", libConfig.Name);

                m_Libraries.Add(new Library(libConfig, Assembly.LoadFrom(libConfig.BinaryPath.FullName)));
                m_AdditionalReferences.Add(libConfig.BinaryPath.FullName);

                return(true);
            }
            else if (!libConfig.SourcePath.Exists)
            {
                Console.WriteLine("Warning: library {0} does not exist", libConfig.Name);
                return(true);
            }

            DirectoryInfo cache = new DirectoryInfo(Environment.Config.CacheDirectory).CreateSubdirectory(libConfig.Name);

            if (!cache.Exists)
            {
                Console.WriteLine("Scripts: Failed to create directory {0}", cache.FullName);
                return(false);
            }

            List <Configuration.Library> overlays = null;

            if (libConfig.Overlays != null)
            {
                overlays = new List <Configuration.Library>();

                foreach (string name in libConfig.Overlays)
                {
                    overlays.Add(Environment.Config.GetLibrary(name));
                }
            }

            string    csFile = Path.Combine(cache.FullName, libConfig.Name + ".dll");
            Hashtable files  = GetScripts(libConfig, overlays, "*.cs");

            if (files.Count > 0)
            {
                string stampFile = Path.Combine(cache.FullName, libConfig.Name + ".stm");

                if (File.Exists(csFile) && CheckStamps(files, stampFile))
                {
                    m_Libraries.Add(new Library(libConfig, Assembly.LoadFrom(csFile)));
                    m_AdditionalReferences.Add(csFile);
                    Console.WriteLine("Libraries: Loaded binary library {0}", libConfig.Name);
                }
                else
                {
                    ArrayList sorted = new ArrayList(files.Keys);
                    sorted.Sort();

                    CompilerResults results = CompileCSScripts(sorted, csFile, libConfig, debug);

                    if (results != null)
                    {
                        if (results.Errors.HasErrors)
                        {
                            return(false);
                        }

                        m_Libraries.Add(new Library(libConfig, results.CompiledAssembly));
                        WriteStampFile(stampFile, files);
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 13
0
        private static CompilerResults CompileCSScripts(ICollection fileColl,
                                                        string assemblyFile,
                                                        Configuration.Library libConfig,
                                                        bool debug)
        {
            CSharpCodeProvider provider = new CSharpCodeProvider();

#pragma warning disable 618
            ICodeCompiler compiler = provider.CreateCompiler();
#pragma warning restore 618

            string[] files;

            Console.WriteLine("Scripts: Compiling library {0}, {1} C# sources",
                              libConfig.Name, fileColl.Count);

            string tempFile = compiler.GetType().FullName == "Mono.CSharp.CSharpCodeCompiler"
                                ? Path.GetTempFileName() : null;
            if (tempFile == String.Empty)
            {
                tempFile = null;
            }
            if (tempFile == null)
            {
                files = new string[fileColl.Count];
                fileColl.CopyTo(files, 0);
            }
            else
            {
                /* to prevent an "argument list too long" error, we
                 * write a list of file names to a temporary file
                 * and add them with @filename */
                StreamWriter w = new StreamWriter(tempFile, false);
                foreach (string file in fileColl)
                {
                    w.Write("\"" + file + "\" ");
                }
                w.Close();

                files = new string[0];
            }

            CompilerParameters parms = new CompilerParameters(GetReferenceAssemblies(), assemblyFile, debug);
            if (tempFile != null)
            {
                parms.CompilerOptions += "@" + tempFile;
            }

            if (libConfig.WarningLevel >= 0)
            {
                parms.WarningLevel = libConfig.WarningLevel;
            }

            CompilerResults results = null;
            try
            {
                results = compiler.CompileAssemblyFromFileBatch(parms, files);
            }
            catch (System.ComponentModel.Win32Exception e)
            {
                /* from WinError.h:
                 * #define ERROR_FILE_NOT_FOUND 2L
                 * #define ERROR_PATH_NOT_FOUND 3L
                 */
                if (e.NativeErrorCode == 2 || e.NativeErrorCode == 3)
                {
                    Console.WriteLine("Fatal: Could not find the compiler - are you sure MCS is installed?");
                    Console.WriteLine("       (on Debian, try: apt-get install mono-mcs)");
                    System.Environment.Exit(2);
                }
                else
                {
                    throw e;
                }
            }

            if (tempFile != null)
            {
                File.Delete(tempFile);
            }

            m_AdditionalReferences.Add(assemblyFile);

            Display(results);

            return(results);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Enqueue a library for compilation, resolving all dependencies first.
        /// </summary>
        /// <param name="dst">This array will receive the libraries in the correct order.</param>
        /// <param name="libs">Source libraries.</param>
        /// <param name="queue">Somewhat like a stack of libraries currently waiting.</param>
        /// <param name="libConfig">The library to be added.</param>
        private static void EnqueueLibrary(List <Configuration.Library> dst, List <Configuration.Library> libs,
                                           ISet <string> queue, Configuration.Library libConfig)
        {
            var depends = libConfig.Depends;

            if (libConfig.Name == "Core" || libConfig.Disabled)
            {
                libs.Remove(libConfig);
                return;
            }

            if (libConfig.IsRemote && (!libConfig.Exists || Core.ForceUpdateDeps))
            {
                var srcPath = Dependencies.Fetch(libConfig);
                libConfig.SourcePath = new DirectoryInfo(srcPath);
            }

            if (!libConfig.Exists)
            {
                libs.Remove(libConfig);
                log.Warning("Library {0} does not exist", libConfig.Name);
                return;
            }

            // First resolve dependencies.
            if (depends != null)
            {
                queue.Add(libConfig.Name);

                foreach (var depend in depends)
                {
                    // If the depended library is already in the queue, there is a circular dependency.
                    if (queue.Contains(depend))
                    {
                        log.Error("Circular library dependency {0} on {1}", libConfig.Name, depend);
                        throw new ApplicationException();
                    }

                    var next = Core.LibraryConfig.GetLibrary(depend);
                    if (next == null || !next.Exists)
                    {
                        log.Error("Unresolved library dependency: {0} depends on {1}, which does not exist", libConfig.Name, depend);
                        throw new ApplicationException();
                    }

                    if (next.Disabled)
                    {
                        log.Error("Unresolved library dependency: {0} depends on {1}, which is disabled", libConfig.Name, depend);
                        throw new ApplicationException();
                    }

                    if (!dst.Contains(next))
                    {
                        EnqueueLibrary(dst, libs, queue, next);
                    }
                }

                queue.Remove(libConfig.Name);
            }

            // Then add it to 'dst'.
            dst.Add(libConfig);
            libs.Remove(libConfig);
        }
Ejemplo n.º 15
0
        private static bool Compile(Configuration.Library libConfig, bool debug)
        {
            // Check if there is source code for this library.
            if (libConfig.SourcePath == null)
            {
                if (libConfig.BinaryPath == null)
                {
                    log.Warning("Library {0} does not exist", libConfig.Name);
                    return(true);
                }
                if (!libConfig.BinaryPath.Exists)
                {
                    log.Warning("Library {0} does not exist: {1}", libConfig.Name, libConfig.BinaryPath);
                    return(false);
                }

                log.Info("Loading library {0}", libConfig.Name);

                m_Libraries.Add(new Library(libConfig, Assembly.LoadFrom(libConfig.BinaryPath.FullName)));
                m_AdditionalReferences.Add(libConfig.BinaryPath.FullName);

                return(true);
            }
            if (!libConfig.SourcePath.Exists)
            {
                log.Warning("Library {0} does not exist", libConfig.Name);
                return(true);
            }

            var cache = new DirectoryInfo(Core.Config.CacheDirectory).CreateSubdirectory(libConfig.Name);

            if (!cache.Exists)
            {
                log.Error("Failed to create directory {0}", cache.FullName);
                return(false);
            }

            var csFile = Path.Combine(cache.FullName, libConfig.Name + ".dll");
            var files  = GetScripts(libConfig, "*.cs");

            if (files.Count > 0)
            {
                var stampFile = Path.Combine(cache.FullName, libConfig.Name + ".stm");

                if (File.Exists(csFile) && CheckStamps(files, stampFile))
                {
                    m_Libraries.Add(new Library(libConfig, Assembly.LoadFrom(csFile)));
                    m_AdditionalReferences.Add(csFile);
                    log.Info("Loaded binary library {0}", libConfig.Name);
                }
                else
                {
                    var sorted = new List <string>(files.Keys);
                    sorted.Sort();

                    var results = CompileCSScripts(sorted, csFile, libConfig, debug);

                    if (results != null)
                    {
                        if (results.Errors.HasErrors)
                        {
                            return(false);
                        }

                        m_Libraries.Add(new Library(libConfig, results.CompiledAssembly));
                        WriteStampFile(stampFile, files);
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Enqueue a library for compilation, resolving all dependencies first.
        /// </summary>
        /// <param name="dst">This array will receive the libraries in the correct order.</param>
        /// <param name="libs">Source libraries.</param>
        /// <param name="queue">Somewhat like a stack of libraries currently waiting.</param>
        /// <param name="libConfig">The library to be added.</param>
        private static void EnqueueLibrary(List <Configuration.Library> dst, List <Configuration.Library> libs, ISet <string> queue, Configuration.Library libConfig)
        {
            var depends = libConfig.Depends;

            if (libConfig.Name == "core" || libConfig.Disabled)
            {
                libs.Remove(libConfig);
                return;
            }

            if (!libConfig.Exists)
            {
                libs.Remove(libConfig);
                Console.WriteLine("Warning: library {0} does not exist", libConfig.Name);
                return;
            }

            // First resolve dependencies.
            if (depends != null)
            {
                queue.Add(libConfig.Name);

                foreach (string depend in depends)
                {
                    // If the depended library is already in the queue, there is a circular dependency.
                    if (queue.Contains(depend))
                    {
                        Console.WriteLine("Error: Circular library dependency {0} on {1}", libConfig.Name, depend);
                        throw new ApplicationException();
                    }

                    var next = Environment.Config.GetLibrary(depend);
                    if (next == null || !next.Exists)
                    {
                        Console.WriteLine("Error: Unresolved library dependency: {0} depends on {1}, which does not exist", libConfig.Name, depend);
                        throw new ApplicationException();
                    }

                    if (next.Disabled)
                    {
                        Console.WriteLine("Error: Unresolved library dependency: {0} depends on {1}, which is disabled", libConfig.Name, depend);
                        throw new ApplicationException();
                    }

                    if (!dst.Contains(next))
                    {
                        EnqueueLibrary(dst, libs, queue, next);
                    }
                }

                queue.Remove(libConfig.Name);
            }

            // Then add it to 'dst'.
            dst.Add(libConfig);
            libs.Remove(libConfig);
        }