예제 #1
0
        /// <summary>
        /// return true if it added an assembly
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="asm"></param>
        /// <param name="path"></param>
        /// <param name="reportFile"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        private bool AddAssembly(CodeDomProvider provider, IScriptAssembly asm, string path, bool getsubdirs, out string error)
        {
            error = string.Empty;
            string[] files;
            if (getsubdirs)
            {
                files = GetFiles(path, "*." + provider.FileExtension);
            }
            else
            {
                files = GetFilesNoSub(path, "*." + provider.FileExtension);
            }

            if (files.Length == 0)
            {
                return(false);
            }
            CompilerResults results = Compile(provider, files);

            if (results.Errors.Count > 0)
            {
                ReportErrors(path, provider.FileExtension + "errors.log", results, out error);
                return(false);
            }
            asm.AddAssembly(results.CompiledAssembly);
            return(true);
        }
예제 #2
0
        /// <summary>
        /// Loads all script from a directory and optionally it's sub directorys
        /// </summary>
        /// <param name="scriptAssemblyType">Needs to inherit IScriptAssembly</param>
        /// <param name="getsubdirs">if true it will search sub directories</param>
        /// <param name="path">Path to scripts</param>
        /// <param name="error">returns the error if LoadScripts returns false</param>
        /// <returns>Returns true if no errors occured.</returns>
        public override bool LoadScripts(Type scriptAssemblyType, bool getsubdirs, string path, out string error)
        {
            error = string.Empty;

            if (scriptAssemblyType.GetInterface(typeof(IScriptAssembly).ToString()) == null)
            {
                error = scriptAssemblyType.ToString() + " does not implemented " + typeof(IScriptAssembly).ToString();
                return(false);
            }

            DirectoryInfo dir;

            try
            {
                dir = new DirectoryInfo(path);
            }
            catch (ArgumentException e)
            {
                error = e.Message + " " + path;
                return(false);
            }
            if (dir.Exists == false)
            {
                return(true);
            }
            //string shortpath = path;
            string shortpath = dir.FullName;

            path = dir.FullName;
            if (getsubdirs)
            {
                if (!CheckPath(path, out error))
                {
                    return(false);
                }
            }
            else if (ScriptAssemblies.Contains(path))
            {
                error = "There's already a script assembly on that path.";
                return(false);
            }

            IScriptAssembly asm        = (IScriptAssembly)Activator.CreateInstance(scriptAssemblyType);
            int             assemblies = 0;

            string[] assemblyfiles = GetFilesNoSub(shortpath, "*.dll");
            foreach (string afile in assemblyfiles)
            {
                try {
                    Assembly sassembly = Assembly.LoadFile(afile);
                    asm.AddAssembly(sassembly);
                    assemblies++;
                    Console.WriteLine("SCRIPT: " + scriptAssemblyType.Name + " loaded the script assembly: " + afile);
                } catch (Exception e) {
                    error = e.ToString();
                    return(false);
                }
            }

            //#region Old ScriptCode

#if SUPPORT_CSHARP
            if (!AddAssembly(new Microsoft.CSharp.CSharpCodeProvider(), asm, shortpath, getsubdirs, out error))
            {
                if (error != string.Empty)
                {
                    return(false);
                }
            }
            else
            {
                assemblies++;
            }
#endif
#if SUPPORT_VB
            if (!AddAssembly(new Microsoft.VisualBasic.VBCodeProvider(), asm, shortpath, getsubdirs, out error))
            {
                if (error != string.Empty)
                {
                    return(false);
                }
            }
            else
            {
                assemblies++;
            }
#endif
#if SUPPORT_MCPP
            if (!AddAssembly(new Microsoft.MCpp.MCppCodeProvider(), asm, shortpath, getsubdirs, out error))
            {
                if (error != string.Empty)
                {
                    return(false);
                }
            }
            else
            {
                assemblies++;
            }
#endif
#if SUPPORT_VJSHARP
            if (!AddAssembly(new Microsoft.VJSharp.VJSharpCodeProvider(), asm, shortpath, getsubdirs, out error))
            {
                if (error != string.Empty)
                {
                    return(false);
                }
            }
            else
            {
                assemblies++;
            }
#endif

            //#endregion

            if (assemblies > 0)
            {
                if (asm.Load(out error) == false)
                {
                    return(false);
                }
                ScriptAssemblies[path] = asm;
            }
            return(true);
        }